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.

current.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict'
  2. const Joi = require('joi')
  3. const params = require('../../schemas/params')
  4. const pluginConfig = {
  5. handlerType: 'user',
  6. docs: {
  7. get: {
  8. description: 'Get user',
  9. notes: 'Returns a user item by the id passed in the path',
  10. },
  11. },
  12. opts: {
  13. tags: ['api'],
  14. auth:
  15. process.env.USE_AUTH != 'true'
  16. ? false
  17. : { strategy: 'default_jwt' },
  18. cors: true,
  19. },
  20. }
  21. /** Validator functions by request method */
  22. const validators = {
  23. get: {
  24. params: params.userName,
  25. },
  26. }
  27. module.exports = {
  28. method: 'get',
  29. path: '/{name}',
  30. options: {
  31. ...pluginConfig.docs.get,
  32. ...pluginConfig.opts,
  33. handler: async function (request, h) {
  34. try {
  35. const auth = {
  36. credentials: request.auth.credentials,
  37. token: request.auth.artifacts.token,
  38. }
  39. // /** Get the data for your endpoint */
  40. // const { User } = request.models()
  41. // const all = await User.query()
  42. const { displayService } = request.services()
  43. const user = displayService.user(auth.credentials, auth.token)
  44. return {
  45. ok: true,
  46. handler: pluginConfig.handlerType,
  47. data: { name: request.params.name },
  48. }
  49. } catch (err) {
  50. return {
  51. ok: false,
  52. handler: pluginConfig.handlerType,
  53. data: { error: err },
  54. }
  55. }
  56. },
  57. validate: validators.get,
  58. response: {
  59. schema: Joi.object({
  60. ok: Joi.bool(),
  61. handler: Joi.string(),
  62. data: validators.get.params,
  63. }).label('user_name_res'),
  64. failAction: 'log',
  65. },
  66. },
  67. }