Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

get.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict'
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const profileSchema = require('../../schemas/profiles')
  5. const params = require('../../schemas/params')
  6. const pluginConfig = {
  7. handlerType: 'profile',
  8. docs: {
  9. description: 'Returns a single profile with tags',
  10. notes: 'returns from the Profiles Table',
  11. },
  12. }
  13. const responseSchemas = {
  14. profile: profileSchema.single,
  15. error: errorSchema.single,
  16. }
  17. const validators = {
  18. params: params.profileId,
  19. }
  20. module.exports = {
  21. method: 'GET',
  22. path: '/{profile_id}',
  23. options: {
  24. ...pluginConfig.docs,
  25. tags: ['api'],
  26. /** Protect this route with authentication? */
  27. // auth: false,
  28. auth: 'default_jwt',
  29. cors: true,
  30. handler: async function (request, h) {
  31. const { profile_id } = request.params
  32. const { profileService } = request.server.services()
  33. const res = {
  34. ok: true,
  35. handler: pluginConfig.handlerType,
  36. data: null,
  37. }
  38. res.data = await profileService.getProfile(profile_id)
  39. try {
  40. return h.response(res).code(200)
  41. } catch (err) {
  42. return h
  43. .response({
  44. ok: false,
  45. handler: pluginConfig.handlerType,
  46. data: { error: `${err}` },
  47. })
  48. .code(409)
  49. }
  50. },
  51. /** Validate based on validators object */
  52. validate: {
  53. ...validators,
  54. failAction: 'log',
  55. },
  56. /** Validate the server response */
  57. response: {
  58. status: {
  59. 200: apiSchema.single
  60. .append({
  61. data: responseSchemas.profile,
  62. })
  63. .label('profile_single_res'),
  64. 409: apiSchema.single
  65. .append({
  66. data: responseSchemas.error,
  67. })
  68. .label('error_single_res'),
  69. },
  70. },
  71. },
  72. }