You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const params = require('../../schemas/params')
  2. const pluginConfig = {
  3. handlerType: 'notifictaion',
  4. docs: {
  5. description: 'subscribe',
  6. notes: 'Subscribe to notifications based on profile_id',
  7. },
  8. opts: {
  9. tags: ['api'],
  10. auth:
  11. process.env.USE_AUTH != 'true'
  12. ? false
  13. : { strategy: 'default_jwt' },
  14. cors: true,
  15. },
  16. }
  17. const validators = {
  18. params: params.profileId,
  19. }
  20. module.exports = {
  21. method: 'GET',
  22. path: '/{profile_id}/subscribe',
  23. options: {
  24. ...pluginConfig.docs,
  25. ...pluginConfig.opts,
  26. handler: async (request, h) => {
  27. const { profile_id } = request.params
  28. /**
  29. * Write the initial stream
  30. * !: this must remain open for notifications to work
  31. */
  32. return request.server.methods.notify(
  33. `${profile_id}.stonk`,
  34. {
  35. profile_id,
  36. name: 'BDGRS',
  37. price: (500 + Math.floor(Math.random() * 100)).toString(),
  38. order: Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL',
  39. type: 'info',
  40. },
  41. h,
  42. true,
  43. )
  44. },
  45. /** Validate based on validators object */
  46. validate: {
  47. ...validators,
  48. failAction: 'log',
  49. },
  50. },
  51. }