Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

current.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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: { strategy: 'default_jwt' },
  15. cors: true,
  16. },
  17. }
  18. /** Validator functions by request method */
  19. const validators = {
  20. get: {
  21. params: params.userName,
  22. },
  23. }
  24. module.exports = {
  25. method: 'get',
  26. path: '/{name}',
  27. options: {
  28. ...pluginConfig.docs.get,
  29. ...pluginConfig.opts,
  30. handler: async function (request, h) {
  31. try {
  32. const auth = {
  33. credentials: request.auth.credentials,
  34. token: request.auth.artifacts.token,
  35. }
  36. // /** Get the data for your endpoint */
  37. // const { User } = request.models()
  38. // const all = await User.query()
  39. const { displayService } = request.services()
  40. const user = displayService.user(auth.credentials, auth.token)
  41. return {
  42. ok: true,
  43. handler: pluginConfig.handlerType,
  44. data: { name: request.params.name },
  45. }
  46. } catch (err) {
  47. return {
  48. ok: false,
  49. handler: pluginConfig.handlerType,
  50. data: { error: err },
  51. }
  52. }
  53. },
  54. validate: validators.get,
  55. response: {
  56. schema: Joi.object({
  57. ok: Joi.bool(),
  58. handler: Joi.string(),
  59. data: validators.get.params,
  60. }).label('user_name_res'),
  61. failAction: 'log',
  62. },
  63. },
  64. }