{id}.js 2.2 KB

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