From 0463c6bb0f8fd32740ceb61ccce04c662272a618 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 23 Apr 2019 22:35:26 +0900 Subject: Refactor API (#4770) * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update description.ts * wip --- src/misc/schema.ts | 74 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 24 deletions(-) (limited to 'src/misc') diff --git a/src/misc/schema.ts b/src/misc/schema.ts index 7c17953d97..442c95ad98 100644 --- a/src/misc/schema.ts +++ b/src/misc/schema.ts @@ -1,14 +1,46 @@ +export const types = { + boolean: 'boolean' as 'boolean', + string: 'string' as 'string', + number: 'number' as 'number', + array: 'array' as 'array', + object: 'object' as 'object', + any: 'any' as 'any', +}; + +export const bool = { + true: true as true, + false: false as false, +}; + export type Schema = { - type: 'number' | 'string' | 'array' | 'object' | any; - optional?: boolean; + type: 'boolean' | 'number' | 'string' | 'array' | 'object' | 'any'; + nullable: boolean; + optional: boolean; items?: Schema; properties?: Obj; description?: string; + example?: any; + format?: string; + ref?: string; + enum?: string[]; }; +type NonUndefinedPropertyNames = { + [K in keyof T]: T[K]['optional'] extends true ? never : K +}[keyof T]; + +type UndefinedPropertyNames = { + [K in keyof T]: T[K]['optional'] extends true ? K : never +}[keyof T]; + +type OnlyRequired = Pick>; +type OnlyOptional = Pick>; + export type Obj = { [key: string]: Schema }; -export type ObjType = { [P in keyof s]: SchemaType }; +export type ObjType = + { [P in keyof OnlyOptional]?: SchemaType } & + { [P in keyof OnlyRequired]: SchemaType }; // https://qiita.com/hrsh7th@github/items/84e8968c3601009cdcf2 type MyType = { @@ -16,26 +48,20 @@ type MyType = { 1: SchemaType; }[T extends Schema ? 1 : 0]; +type NullOrUndefined

= + p['nullable'] extends true + ? p['optional'] extends true + ? (T | null | undefined) + : (T | null) + : p['optional'] extends true + ? (T | undefined) + : T; + export type SchemaType

= - p['type'] extends 'number' ? number : - p['type'] extends 'string' ? string : - p['type'] extends 'array' ? MyType>[] : - p['type'] extends 'object' ? ObjType> : + p['type'] extends 'number' ? NullOrUndefined : + p['type'] extends 'string' ? NullOrUndefined : + p['type'] extends 'boolean' ? NullOrUndefined : + p['type'] extends 'array' ? NullOrUndefined>[]> : + p['type'] extends 'object' ? NullOrUndefined>> : + p['type'] extends 'any' ? NullOrUndefined : any; - -export function convertOpenApiSchema(schema: Schema) { - const x = JSON.parse(JSON.stringify(schema)); // copy - if (!['string', 'number', 'boolean', 'array', 'object'].includes(x.type)) { - x['$ref'] = `#/components/schemas/${x.type}`; - } - if (x.type === 'array' && x.items) { - x.items = convertOpenApiSchema(x.items); - } - if (x.type === 'object' && x.properties) { - x.required = Object.entries(x.properties).filter(([k, v]: any) => !v.isOptional).map(([k, v]: any) => k); - for (const k of Object.keys(x.properties)) { - x.properties[k] = convertOpenApiSchema(x.properties[k]); - } - } - return x; -} -- cgit v1.2.3-freya