From 664acb2d0e5c0fdeb63b6a5fa1cccbca55b88e74 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 24 Feb 2019 17:48:54 +0900 Subject: [PATCH] Refactor --- src/server/api/openapi/gen-spec.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/server/api/openapi/gen-spec.ts b/src/server/api/openapi/gen-spec.ts index 78569f1245..c8b1c21de4 100644 --- a/src/server/api/openapi/gen-spec.ts +++ b/src/server/api/openapi/gen-spec.ts @@ -41,24 +41,22 @@ export function genOpenapiSpec(lang = 'ja-JP') { } }; - function genProps(props: { [key: string]: Context & { desc: any, default: any }; }) { + function genProps(props: { [key: string]: Context; }) { const properties = {} as any; - const kvs = Object.entries(props); - - for (const kv of kvs) { - properties[kv[0]] = genProp(kv[1], kv[1].desc, kv[1].default); + for (const [k, v] of Object.entries(props)) { + properties[k] = genProp(v); } return properties; } - function genProp(param: Context, desc?: string, _default?: any): any { + function genProp(param: Context): any { const required = param.name === 'Object' ? (param as any).props ? Object.entries((param as any).props).filter(([k, v]: any) => !v.isOptional).map(([k, v]) => k) : [] : []; return { - description: desc, - default: _default, - ...(_default ? { default: _default } : {}), + description: (param.data || {}).desc, + default: (param.data || {}).default, + ...((param.data || {}).default ? { default: (param.data || {}).default } : {}), type: param.name === 'ID' ? 'string' : param.name.toLowerCase(), ...(param.name === 'ID' ? { example: 'xxxxxxxxxxxxxxxxxxxxxxxx', format: 'id' } : {}), nullable: param.isNullable, @@ -97,8 +95,9 @@ export function genOpenapiSpec(lang = 'ja-JP') { if (endpoint.meta.params) { for (const [k, v] of Object.entries(endpoint.meta.params)) { - if (v.desc) (v.validator as any).desc = v.desc[lang]; - if (v.default) (v.validator as any).default = v.default; + if (v.validator.data == null) v.validator.data = {}; + if (v.desc) v.validator.data.desc = v.desc[lang]; + if (v.default) v.validator.data.default = v.default; porops[k] = v.validator; } }