jormungandr-bite/src/server/api/openapi/schemas.ts

82 lines
3.1 KiB
TypeScript
Raw Normal View History

import { packedUserSchema } from '../../../models/repositories/user';
import { Schema } from '../../../misc/schema';
import { packedNoteSchema } from '../../../models/repositories/note';
import { packedUserListSchema } from '../../../models/repositories/user-list';
import { packedAppSchema } from '../../../models/repositories/app';
import { packedMessagingMessageSchema } from '../../../models/repositories/messaging-message';
import { packedNotificationSchema } from '../../../models/repositories/notification';
import { packedDriveFileSchema } from '../../../models/repositories/drive-file';
import { packedDriveFolderSchema } from '../../../models/repositories/drive-folder';
import { packedFollowingSchema } from '../../../models/repositories/following';
import { packedMutingSchema } from '../../../models/repositories/muting';
import { packedBlockingSchema } from '../../../models/repositories/blocking';
import { packedNoteReactionSchema } from '../../../models/repositories/note-reaction';
2019-04-24 22:25:10 -06:00
import { packedHashtagSchema } from '../../../models/repositories/hashtag';
2019-04-30 13:44:46 -06:00
import { packedPageSchema } from '../../../models/repositories/page';
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-23 12:08:08 -07:00
export const schemas = {
Error: {
type: 'object',
properties: {
error: {
type: 'object',
description: 'An error object.',
properties: {
code: {
type: 'string',
2019-02-23 12:27:09 -07:00
description: 'An error code. Unique within the endpoint.',
2019-02-23 12:08:08 -07:00
},
message: {
type: 'string',
description: 'An error message.',
},
id: {
type: 'string',
format: 'uuid',
description: 'An error ID. This ID is static.',
}
},
required: ['code', 'id', 'message']
},
},
required: ['error']
},
User: convertSchemaToOpenApiSchema(packedUserSchema),
UserList: convertSchemaToOpenApiSchema(packedUserListSchema),
App: convertSchemaToOpenApiSchema(packedAppSchema),
MessagingMessage: convertSchemaToOpenApiSchema(packedMessagingMessageSchema),
Note: convertSchemaToOpenApiSchema(packedNoteSchema),
Notification: convertSchemaToOpenApiSchema(packedNotificationSchema),
DriveFile: convertSchemaToOpenApiSchema(packedDriveFileSchema),
DriveFolder: convertSchemaToOpenApiSchema(packedDriveFolderSchema),
Following: convertSchemaToOpenApiSchema(packedFollowingSchema),
Muting: convertSchemaToOpenApiSchema(packedMutingSchema),
Blocking: convertSchemaToOpenApiSchema(packedBlockingSchema),
NoteReaction: convertSchemaToOpenApiSchema(packedNoteReactionSchema),
2019-04-24 22:25:10 -06:00
Hashtag: convertSchemaToOpenApiSchema(packedHashtagSchema),
2019-04-30 13:44:46 -06:00
Page: convertSchemaToOpenApiSchema(packedPageSchema),
2019-02-23 12:08:08 -07:00
};