api-schema.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import VERSION from '../version'
  7. import { QueryParamInfo, QueryParamType, QueryDefinition, CommonQueryParamsInfo, QueryList } from './api';
  8. import ServerConfig from '../config';
  9. export const shortcutIconLink = `<link rel='shortcut icon' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURQAAAMIrHrspHr0oH7soILonHrwqH7onILsoHrsoH7soH7woILwpIKgVokoAAAAMdFJOUwAQHzNxWmBHS5XO6jdtAmoAAACZSURBVDjLxZNRCsQgDAVNXmwb9f7nXZEaLRgXloXOhwQdjMYYwpOLw55fBT46KhbOKhmRR2zLcFJQj8UR+HxFgArIF5BKJbEncC6NDEdI5SatBRSDJwGAoiFDONrEJXWYhGMIcRJGCrb1TOtDahfUuQXd10jkFYq0ViIrbUpNcVT6redeC1+b9tH2WLR93Sx2VCzkv/7NjfABxjQHksGB7lAAAAAASUVORK5CYII=' />`
  10. export function getApiSchema() {
  11. return {
  12. openapi: '3.0.0',
  13. info: {
  14. version: VERSION,
  15. title: 'ModelServer',
  16. description: 'The ModelServer is a service for accessing subsets of macromolecular model data.',
  17. },
  18. tags: [
  19. {
  20. name: 'General',
  21. }
  22. ],
  23. paths: getPaths(),
  24. components: {
  25. parameters: {
  26. id: {
  27. name: 'id',
  28. in: 'path',
  29. description: 'Id of the entry (i.e. 1tqn).',
  30. required: true,
  31. schema: {
  32. type: 'string',
  33. },
  34. style: 'simple'
  35. },
  36. }
  37. }
  38. }
  39. }
  40. function getPaths() {
  41. const ret: any = {};
  42. for (const { name, definition } of QueryList) {
  43. ret[`${ServerConfig.appPrefix}/v1/{id}/${name}`] = getQueryInfo(definition);
  44. }
  45. return ret;
  46. }
  47. function getQueryInfo(def: QueryDefinition) {
  48. return {
  49. get: {
  50. tags: ['General'],
  51. summary: def.description,
  52. operationId: def.name,
  53. parameters: [
  54. { $ref: '#/components/parameters/id' },
  55. ...def.restParams.map(getParamInfo),
  56. ...CommonQueryParamsInfo.map(getParamInfo)
  57. ],
  58. responses: {
  59. 200: {
  60. description: def.description,
  61. content: {
  62. 'text/plain': {},
  63. 'application/octet-stream': {},
  64. }
  65. }
  66. }
  67. }
  68. };
  69. }
  70. function getParamInfo(info: QueryParamInfo) {
  71. return {
  72. name: info.name,
  73. in: 'query',
  74. description: info.description,
  75. required: !!info.required,
  76. schema: {
  77. type: info.type === QueryParamType.String ? 'string' : info.type === QueryParamType.Integer ? 'integer' : 'number',
  78. enum: info.supportedValues ? info.supportedValues : void 0,
  79. default: info.defaultValue
  80. },
  81. style: 'simple'
  82. };
  83. }