{id}.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const { discover } = require('@joystream/discovery')
  2. const debug = require('debug')('joystream:api:discovery');
  3. const MAX_CACHE_AGE = 30 * 60 * 1000;
  4. const USE_CACHE = true;
  5. module.exports = function(config, runtime)
  6. {
  7. var doc = {
  8. // parameters for all operations in this path
  9. parameters: [
  10. {
  11. name: 'id',
  12. in: 'path',
  13. required: true,
  14. description: 'Actor accouuntId',
  15. schema: {
  16. type: 'string',
  17. },
  18. },
  19. ],
  20. // Resolve Service Information
  21. get: async function(req, res)
  22. {
  23. const id = req.params.id;
  24. let cacheMaxAge = req.query.max_age;
  25. if (cacheMaxAge) {
  26. try {
  27. cacheMaxAge = parseInt(cacheMaxAge);
  28. } catch(err) {
  29. cacheMaxAge = MAX_CACHE_AGE
  30. }
  31. } else {
  32. cacheMaxAge = 0
  33. }
  34. // todo - validate id before querying
  35. try {
  36. debug(`resolving ${id}`);
  37. const info = await discover.discover(id, runtime, USE_CACHE, cacheMaxAge);
  38. if (info == null) {
  39. debug('info not found');
  40. res.status(404).end();
  41. } else {
  42. res.status(200).send(info);
  43. }
  44. } catch (err) {
  45. debug(`${err}`);
  46. res.status(400).end()
  47. }
  48. }
  49. };
  50. // OpenAPI specs
  51. doc.get.apiDoc = {
  52. description: 'Resolve Service Information',
  53. operationId: 'discover',
  54. //tags: ['asset', 'data'],
  55. responses: {
  56. 200: {
  57. description: 'Wrapped JSON Service Information',
  58. content: {
  59. 'application/json': {
  60. schema: {
  61. required: ['serialized'],
  62. properties: {
  63. 'serialized': {
  64. type: 'string'
  65. },
  66. 'signature': {
  67. type: 'string'
  68. }
  69. },
  70. },
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return doc;
  77. };