misskey/packages/backend/src/server/api/openapi/schemas.ts

57 lines
1.3 KiB
TypeScript
Raw Normal View History

import { refs, Schema } from '@/misc/schema';
export function convertSchemaToOpenApiSchema(schema: Schema) {
const res: any = schema;
if (schema.type === 'object' && schema.properties) {
res.required = Object.entries(schema.properties).filter(([k, v]) => !v.optional).map(([k]) => k);
for (const k of Object.keys(schema.properties)) {
res.properties[k] = convertSchemaToOpenApiSchema(schema.properties[k]);
}
}
if (schema.type === 'array' && schema.items) {
res.items = convertSchemaToOpenApiSchema(schema.items);
}
if (schema.ref) {
res.$ref = `#/components/schemas/${schema.ref}`;
}
return res;
}
2019-02-24 04:08:08 +09:00
export const schemas = {
Error: {
type: 'object',
properties: {
error: {
type: 'object',
description: 'An error object.',
properties: {
code: {
type: 'string',
2019-02-24 04:27:09 +09:00
description: 'An error code. Unique within the endpoint.',
2019-02-24 04:08:08 +09:00
},
message: {
type: 'string',
description: 'An error message.',
},
id: {
type: 'string',
format: 'uuid',
description: 'An error ID. This ID is static.',
2021-12-09 23:58:30 +09:00
},
2019-02-24 04:08:08 +09:00
},
2021-12-09 23:58:30 +09:00
required: ['code', 'id', 'message'],
2019-02-24 04:08:08 +09:00
},
},
2021-12-09 23:58:30 +09:00
required: ['error'],
2019-02-24 04:08:08 +09:00
},
...Object.fromEntries(
Object.entries(refs).map(([key, schema]) => [key, convertSchemaToOpenApiSchema(schema)])
),
2019-02-24 04:08:08 +09:00
};