diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2019-02-24 09:45:27 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2019-02-24 09:45:27 +0900 |
| commit | b679163d01b0152b38db658fe49d8a57c6aad930 (patch) | |
| tree | 9919cc598ed825c6841f47813010dac2d704614d /src/server/api | |
| parent | Update schemas.ts (diff) | |
| download | sharkey-b679163d01b0152b38db658fe49d8a57c6aad930.tar.gz sharkey-b679163d01b0152b38db658fe49d8a57c6aad930.tar.bz2 sharkey-b679163d01b0152b38db658fe49d8a57c6aad930.zip | |
Improve type definitions
Diffstat (limited to 'src/server/api')
| -rw-r--r-- | src/server/api/endpoints.ts | 3 | ||||
| -rw-r--r-- | src/server/api/endpoints/charts/notes.ts | 10 | ||||
| -rw-r--r-- | src/server/api/endpoints/notes/create.ts | 6 | ||||
| -rw-r--r-- | src/server/api/openapi/gen-spec.ts | 29 |
4 files changed, 9 insertions, 39 deletions
diff --git a/src/server/api/endpoints.ts b/src/server/api/endpoints.ts index 2873dd3c1e..7abee95a51 100644 --- a/src/server/api/endpoints.ts +++ b/src/server/api/endpoints.ts @@ -1,6 +1,7 @@ import { Context } from 'cafy'; import * as path from 'path'; import * as glob from 'glob'; +import { Schema } from '../../prelude/schema'; export type Param = { validator: Context<any>; @@ -29,7 +30,7 @@ export interface IEndpointMeta { }; }; - res?: any; + res?: Schema; /** * このエンドポイントにリクエストするのにユーザー情報が必須か否か diff --git a/src/server/api/endpoints/charts/notes.ts b/src/server/api/endpoints/charts/notes.ts index d254bb854c..cc0ca8bef7 100644 --- a/src/server/api/endpoints/charts/notes.ts +++ b/src/server/api/endpoints/charts/notes.ts @@ -1,6 +1,7 @@ import $ from 'cafy'; import define from '../../define'; -import notesChart from '../../../../services/chart/notes'; +import notesChart, { notesLogSchema } from '../../../../services/chart/notes'; +import { convertLog } from '../../../../services/chart'; export const meta = { stability: 'stable', @@ -28,12 +29,7 @@ export const meta = { }, }, - res: { - type: 'array', - items: { - type: 'object', - }, - }, + res: convertLog(notesLogSchema), }; export default define(meta, async (ps) => { diff --git a/src/server/api/endpoints/notes/create.ts b/src/server/api/endpoints/notes/create.ts index a4f262bdad..7df86625d6 100644 --- a/src/server/api/endpoints/notes/create.ts +++ b/src/server/api/endpoints/notes/create.ts @@ -175,12 +175,10 @@ export const meta = { res: { type: 'object', - props: { + properties: { createdNote: { type: 'Note', - desc: { - 'ja-JP': '作成した投稿' - } + description: '作成した投稿' } } }, diff --git a/src/server/api/openapi/gen-spec.ts b/src/server/api/openapi/gen-spec.ts index ad46eb20a4..7487918231 100644 --- a/src/server/api/openapi/gen-spec.ts +++ b/src/server/api/openapi/gen-spec.ts @@ -4,6 +4,7 @@ import config from '../../../config'; import { errors as basicErrors } from './errors'; import { schemas } from './schemas'; import { description } from './description'; +import { convertOpenApiSchema } from '../../../prelude/schema'; export function genOpenapiSpec(lang = 'ja-JP') { const spec = { @@ -104,33 +105,7 @@ export function genOpenapiSpec(lang = 'ja-JP') { const required = endpoint.meta.params ? Object.entries(endpoint.meta.params).filter(([k, v]) => !v.validator.isOptional).map(([k, v]) => k) : []; - const resSchema = endpoint.meta.res ? renderType(endpoint.meta.res) : {}; - - function renderType(x: any) { - const res = {} as any; - - if (['User', 'Note', 'DriveFile'].includes(x.type)) { - res['$ref'] = `#/components/schemas/${x.type}`; - } else if (x.type === 'object') { - res['type'] = 'object'; - if (x.props) { - const props = {} as any; - for (const kv of Object.entries(x.props)) { - props[kv[0]] = renderType(kv[1]); - } - res['properties'] = props; - } - } else if (x.type === 'array') { - res['type'] = 'array'; - if (x.items) { - res['items'] = renderType(x.items); - } - } else { - res['type'] = x.type; - } - - return res; - } + const resSchema = endpoint.meta.res ? convertOpenApiSchema(endpoint.meta.res) : {}; const info = { operationId: endpoint.name, |