{id}.js 2.0 KB

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