iceshrimp-legacy/packages/backend/src/server/api/openapi/gen-spec.ts

191 lines
4 KiB
TypeScript
Raw Normal View History

import endpoints from '../endpoints.js';
import config from '@/config/index.js';
import { errors as basicErrors } from './errors.js';
import { schemas, convertSchemaToOpenApiSchema } from './schemas.js';
2019-02-23 20:08:08 +01:00
export function genOpenapiSpec(lang = 'ja-JP') {
const spec = {
openapi: '3.0.0',
info: {
version: 'v1',
title: 'Misskey API',
2021-12-09 15:58:30 +01:00
'x-logo': { url: '/static-assets/api-doc.png' },
2019-02-23 20:08:08 +01:00
},
externalDocs: {
description: 'Repository',
2021-12-09 15:58:30 +01:00
url: 'https://github.com/misskey-dev/misskey',
2019-02-23 20:08:08 +01:00
},
servers: [{
2021-12-09 15:58:30 +01:00
url: config.apiUrl,
2019-02-23 20:08:08 +01:00
}],
paths: {} as any,
components: {
schemas: schemas,
securitySchemes: {
ApiKeyAuth: {
type: 'apiKey',
in: 'body',
2021-12-09 15:58:30 +01:00
name: 'i',
},
},
},
2019-02-23 20:08:08 +01:00
};
for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) {
const errors = {} as any;
if (endpoint.meta.errors) {
for (const e of Object.values(endpoint.meta.errors)) {
errors[e.code] = {
value: {
2021-12-09 15:58:30 +01:00
error: e,
},
2019-02-23 20:08:08 +01:00
};
}
}
const resSchema = endpoint.meta.res ? convertSchemaToOpenApiSchema(endpoint.meta.res) : {};
2019-02-23 20:08:08 +01:00
let desc = (endpoint.meta.description ? endpoint.meta.description : 'No description provided.') + '\n\n';
2019-02-25 01:37:22 +01:00
desc += `**Credential required**: *${endpoint.meta.requireCredential ? 'Yes' : 'No'}*`;
if (endpoint.meta.kind) {
const kind = endpoint.meta.kind;
desc += ` / **Permission**: *${kind}*`;
}
2019-02-25 01:37:22 +01:00
const requestType = endpoint.meta.requireFile ? 'multipart/form-data' : 'application/json';
const schema = endpoint.params;
if (endpoint.meta.requireFile) {
schema.properties.file = {
type: 'string',
format: 'binary',
description: 'The file contents.',
};
schema.required.push('file');
}
2019-02-23 20:08:08 +01:00
const info = {
operationId: endpoint.name,
summary: endpoint.name,
2019-02-25 01:37:22 +01:00
description: desc,
2019-02-23 20:08:08 +01:00
externalDocs: {
description: 'Source code',
url: `https://github.com/misskey-dev/misskey/blob/develop/packages/backend/src/server/api/endpoints/${endpoint.name}.ts`,
2019-02-23 20:08:08 +01:00
},
...(endpoint.meta.tags ? {
2021-12-09 15:58:30 +01:00
tags: [endpoint.meta.tags[0]],
2019-02-23 20:08:08 +01:00
} : {}),
...(endpoint.meta.requireCredential ? {
security: [{
2021-12-09 15:58:30 +01:00
ApiKeyAuth: [],
}],
2019-02-23 20:08:08 +01:00
} : {}),
requestBody: {
required: true,
content: {
[requestType]: {
schema,
2021-12-09 15:58:30 +01:00
},
},
2019-02-23 20:08:08 +01:00
},
responses: {
...(endpoint.meta.res ? {
'200': {
description: 'OK (with results)',
content: {
'application/json': {
2021-12-09 15:58:30 +01:00
schema: resSchema,
},
},
},
2019-02-23 20:08:08 +01:00
} : {
'204': {
description: 'OK (without any results)',
2021-12-09 15:58:30 +01:00
},
2019-02-23 20:08:08 +01:00
}),
'400': {
description: 'Client error',
content: {
'application/json': {
schema: {
2021-12-09 15:58:30 +01:00
$ref: '#/components/schemas/Error',
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
examples: { ...errors, ...basicErrors['400'] },
},
},
2019-02-23 20:08:08 +01:00
},
'401': {
description: 'Authentication error',
content: {
'application/json': {
schema: {
2021-12-09 15:58:30 +01:00
$ref: '#/components/schemas/Error',
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
examples: basicErrors['401'],
},
},
2019-02-23 20:08:08 +01:00
},
'403': {
description: 'Forbidden error',
2019-02-23 20:08:08 +01:00
content: {
'application/json': {
schema: {
2021-12-09 15:58:30 +01:00
$ref: '#/components/schemas/Error',
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
examples: basicErrors['403'],
},
},
2019-02-23 20:08:08 +01:00
},
'418': {
description: 'I\'m Ai',
content: {
'application/json': {
schema: {
2021-12-09 15:58:30 +01:00
$ref: '#/components/schemas/Error',
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
examples: basicErrors['418'],
},
},
2019-02-23 20:08:08 +01:00
},
...(endpoint.meta.limit ? {
'429': {
description: 'To many requests',
content: {
'application/json': {
schema: {
2021-12-09 15:58:30 +01:00
$ref: '#/components/schemas/Error',
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
examples: basicErrors['429'],
},
},
},
2019-02-23 20:08:08 +01:00
} : {}),
'500': {
description: 'Internal server error',
content: {
'application/json': {
schema: {
2021-12-09 15:58:30 +01:00
$ref: '#/components/schemas/Error',
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
examples: basicErrors['500'],
},
},
2019-02-23 20:08:08 +01:00
},
2021-12-09 15:58:30 +01:00
},
2019-02-23 20:08:08 +01:00
};
spec.paths['/' + endpoint.name] = {
2021-12-09 15:58:30 +01:00
post: info,
2019-02-23 20:08:08 +01:00
};
}
return spec;
}