From f3e7b1ff29b06d067d4929096a6a11fc6b85129e Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Sun, 30 May 2021 18:40:44 +0900 Subject: [PATCH 1/7] fix: array one of --- aspida.config.js | 5 ++++ samples/array-one-of.yml | 44 ++++++++++++++++++++++++++++ samples/array-one-of/$api.ts | 33 +++++++++++++++++++++ samples/array-one-of/@types/index.ts | 14 +++++++++ samples/array-one-of/user/$api.ts | 31 ++++++++++++++++++++ samples/array-one-of/user/index.ts | 13 ++++++++ src/buildV3.ts | 2 +- 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 samples/array-one-of.yml create mode 100644 samples/array-one-of/$api.ts create mode 100644 samples/array-one-of/@types/index.ts create mode 100644 samples/array-one-of/user/$api.ts create mode 100644 samples/array-one-of/user/index.ts diff --git a/aspida.config.js b/aspida.config.js index cd9665b5..b9f7fcdb 100644 --- a/aspida.config.js +++ b/aspida.config.js @@ -33,5 +33,10 @@ module.exports = [ input: 'samples/nullable-object', outputEachDir: true, openapi: { inputFile: 'samples/nullable-object.yml' } + }, + { + input: 'samples/array-one-of', + outputEachDir: true, + openapi: { inputFile: 'samples/array-one-of.yml' } } ] diff --git a/samples/array-one-of.yml b/samples/array-one-of.yml new file mode 100644 index 00000000..99699f45 --- /dev/null +++ b/samples/array-one-of.yml @@ -0,0 +1,44 @@ +openapi: 3.0.0 +info: + version: "1.0.0" + title: "Sample" +paths: + /user: + get: + responses: + "200": + description: "sample" + content: + application/json: + schema: + type: object + properties: + user: + nullable: true + anyOf: + - $ref: "#/components/schemas/User" +components: + schemas: + User: + type: object + properties: + id: + type: string + roles: + type: array + items: + oneOf: + - $ref: "#/components/schemas/RoleA" + - $ref: "#/components/schemas/RoleB" + RoleA: + type: object + properties: + name: + type: string + RoleB: + type: object + properties: + name: + type: string + authority: + type: string diff --git a/samples/array-one-of/$api.ts b/samples/array-one-of/$api.ts new file mode 100644 index 00000000..a7a7f008 --- /dev/null +++ b/samples/array-one-of/$api.ts @@ -0,0 +1,33 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './user' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/user' + const GET = 'GET' + + return { + user: { + /** + * @returns sample + */ + get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json(), + /** + * @returns sample + */ + $get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json().then(r => r.body), + $path: () => `${prefix}${PATH0}` + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/array-one-of/@types/index.ts b/samples/array-one-of/@types/index.ts new file mode 100644 index 00000000..ce3215ce --- /dev/null +++ b/samples/array-one-of/@types/index.ts @@ -0,0 +1,14 @@ +/* eslint-disable */ +export type User = { + id: string + roles: (RoleA | RoleB)[] +} + +export type RoleA = { + name: string +} + +export type RoleB = { + name: string + authority: string +} diff --git a/samples/array-one-of/user/$api.ts b/samples/array-one-of/user/$api.ts new file mode 100644 index 00000000..7a848ee5 --- /dev/null +++ b/samples/array-one-of/user/$api.ts @@ -0,0 +1,31 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from '.' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/user' + const GET = 'GET' + + return { + /** + * @returns sample + */ + get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json(), + /** + * @returns sample + */ + $get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json().then(r => r.body), + $path: () => `${prefix}${PATH0}` + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/array-one-of/user/index.ts b/samples/array-one-of/user/index.ts new file mode 100644 index 00000000..64edb78d --- /dev/null +++ b/samples/array-one-of/user/index.ts @@ -0,0 +1,13 @@ +/* eslint-disable */ +import type * as Types from '../@types' + +export type Methods = { + get: { + status: 200 + + /** sample */ + resBody: { + user: Partial | null + } + } +} diff --git a/src/buildV3.ts b/src/buildV3.ts index 22023e74..a701a0f8 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -370,7 +370,7 @@ export default (openapi: OpenAPIV3.Document) => { ] .map(p => `\n${description2Doc(p.description, '')}export type ${p.name} = ${p.text}\n`) .join('') - .replace(/ Types\./g, ' ') + .replace(/(\W)Types\./g, '$1') : null return { From d3d8c9d29e54f8585b6d2931d890841701be1fe0 Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Sun, 30 May 2021 19:55:20 +0900 Subject: [PATCH 2/7] feat: allow at mark in path --- aspida.config.js | 5 ++++ samples/path-at-mark.yml | 26 ++++++++++++++++++++ samples/path-at-mark/$api.ts | 35 +++++++++++++++++++++++++++ samples/path-at-mark/@types/index.ts | 4 +++ samples/path-at-mark/user/$api.ts | 33 +++++++++++++++++++++++++ samples/path-at-mark/user/me/$api.ts | 31 ++++++++++++++++++++++++ samples/path-at-mark/user/me/index.ts | 13 ++++++++++ src/buildTemplate.ts | 7 ++++-- src/buildV3.ts | 26 +++++++++++--------- src/builderUtils/getDirName.ts | 12 +++++++-- src/getConfig.ts | 5 +++- 11 files changed, 181 insertions(+), 16 deletions(-) create mode 100644 samples/path-at-mark.yml create mode 100644 samples/path-at-mark/$api.ts create mode 100644 samples/path-at-mark/@types/index.ts create mode 100644 samples/path-at-mark/user/$api.ts create mode 100644 samples/path-at-mark/user/me/$api.ts create mode 100644 samples/path-at-mark/user/me/index.ts diff --git a/aspida.config.js b/aspida.config.js index cd9665b5..6d7ae83b 100644 --- a/aspida.config.js +++ b/aspida.config.js @@ -33,5 +33,10 @@ module.exports = [ input: 'samples/nullable-object', outputEachDir: true, openapi: { inputFile: 'samples/nullable-object.yml' } + }, + { + input: 'samples/path-at-mark', + outputEachDir: true, + openapi: { inputFile: 'samples/path-at-mark.yml', replaceLeadingAtMark: '' } } ] diff --git a/samples/path-at-mark.yml b/samples/path-at-mark.yml new file mode 100644 index 00000000..b8d58843 --- /dev/null +++ b/samples/path-at-mark.yml @@ -0,0 +1,26 @@ +openapi: 3.0.0 +info: + version: "1.0.0" + title: "Sample" +paths: + /user/@me: + get: + responses: + "200": + description: "sample" + content: + application/json: + schema: + type: object + properties: + user: + nullable: true + anyOf: + - $ref: "#/components/schemas/User" +components: + schemas: + User: + type: object + properties: + id: + type: string diff --git a/samples/path-at-mark/$api.ts b/samples/path-at-mark/$api.ts new file mode 100644 index 00000000..1bbc8b07 --- /dev/null +++ b/samples/path-at-mark/$api.ts @@ -0,0 +1,35 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './user/me' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/user/me' + const GET = 'GET' + + return { + user: { + me: { + /** + * @returns sample + */ + get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json(), + /** + * @returns sample + */ + $get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json().then(r => r.body), + $path: () => `${prefix}${PATH0}` + } + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/path-at-mark/@types/index.ts b/samples/path-at-mark/@types/index.ts new file mode 100644 index 00000000..6d7e029b --- /dev/null +++ b/samples/path-at-mark/@types/index.ts @@ -0,0 +1,4 @@ +/* eslint-disable */ +export type User = { + id: string +} diff --git a/samples/path-at-mark/user/$api.ts b/samples/path-at-mark/user/$api.ts new file mode 100644 index 00000000..56e33a83 --- /dev/null +++ b/samples/path-at-mark/user/$api.ts @@ -0,0 +1,33 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './me' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/user/me' + const GET = 'GET' + + return { + me: { + /** + * @returns sample + */ + get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json(), + /** + * @returns sample + */ + $get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json().then(r => r.body), + $path: () => `${prefix}${PATH0}` + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/path-at-mark/user/me/$api.ts b/samples/path-at-mark/user/me/$api.ts new file mode 100644 index 00000000..6edd1b96 --- /dev/null +++ b/samples/path-at-mark/user/me/$api.ts @@ -0,0 +1,31 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from '.' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/user/me' + const GET = 'GET' + + return { + /** + * @returns sample + */ + get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json(), + /** + * @returns sample + */ + $get: (option?: { config?: T }) => + fetch(prefix, PATH0, GET, option).json().then(r => r.body), + $path: () => `${prefix}${PATH0}` + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/path-at-mark/user/me/index.ts b/samples/path-at-mark/user/me/index.ts new file mode 100644 index 00000000..f967fe62 --- /dev/null +++ b/samples/path-at-mark/user/me/index.ts @@ -0,0 +1,13 @@ +/* eslint-disable */ +import type * as Types from '../../@types' + +export type Methods = { + get: { + status: 200 + + /** sample */ + resBody: { + user: Partial | null + } + } +} diff --git a/src/buildTemplate.ts b/src/buildTemplate.ts index a9138b30..2ff94b4b 100644 --- a/src/buildTemplate.ts +++ b/src/buildTemplate.ts @@ -6,11 +6,14 @@ import { Config } from './getConfig' const isV3 = (openapi: OpenAPI.Document): openapi is OpenAPIV3.Document => 'openapi' in openapi -export default async ({ input, isYaml }: Config) => { +export default async ({ input, isYaml, replaceLeadingAtMark }: Config) => { const openapi = await parse(input, { parse: { json: !isYaml } }) const docs = isV3(openapi) ? openapi : await require('swagger2openapi').convertObj(openapi, { direct: true }) - return buildV3(await resolveExternalRefs(docs, typeof input === 'string' ? input : '')) + return buildV3( + await resolveExternalRefs(docs, typeof input === 'string' ? input : ''), + replaceLeadingAtMark + ) } diff --git a/src/buildV3.ts b/src/buildV3.ts index 22023e74..45eaa02b 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -25,7 +25,7 @@ const getParamsList = ( params?: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[] ) => params?.map(p => (isRefObject(p) ? resolveParamsRef(openapi, p.$ref) : p)) || [] -export default (openapi: OpenAPIV3.Document) => { +export default (openapi: OpenAPIV3.Document, replaceLeadingAtMark: string) => { const files: { file: string[]; methods: string }[] = [] const schemas = schemas2Props(openapi.components?.schemas, openapi) || [] const parameters = parameters2Props(openapi.components?.parameters, openapi) || [] @@ -44,16 +44,20 @@ export default (openapi: OpenAPIV3.Document) => { .split('/') .slice(1) .map(p => - getDirName(p, [ - ...getParamsList(openapi, openapi.paths[path]!.parameters), - ...methodProps.reduce( - (prev, c) => [ - ...prev, - ...getParamsList(openapi, openapi.paths[path]![c]?.parameters) - ], - [] as OpenAPIV3.ParameterObject[] - ) - ]) + getDirName( + p, + [ + ...getParamsList(openapi, openapi.paths[path]!.parameters), + ...methodProps.reduce( + (prev, c) => [ + ...prev, + ...getParamsList(openapi, openapi.paths[path]![c]?.parameters) + ], + [] as OpenAPIV3.ParameterObject[] + ) + ], + replaceLeadingAtMark + ) ), 'index' ] diff --git a/src/builderUtils/getDirName.ts b/src/builderUtils/getDirName.ts index ece60184..79efec80 100644 --- a/src/builderUtils/getDirName.ts +++ b/src/builderUtils/getDirName.ts @@ -1,9 +1,17 @@ import { OpenAPIV3 } from 'openapi-types' import { getPropertyName, schema2value } from './converters' -export default (text: string, params: OpenAPIV3.ParameterObject[]) => { +export default ( + text: string, + params: OpenAPIV3.ParameterObject[], + replaceLeadingAtMark: string +) => { if (text === '*') return '_any' - if (!/^{/.test(text)) return text + if (!/^{/.test(text)) { + if (replaceLeadingAtMark !== '@' && text.startsWith('@')) + return text.replace('@', replaceLeadingAtMark) + return text + } const valName = text.slice(1, -1) const schemaVal = schema2value(params.find(p => p.in === 'path' && p.name === valName)?.schema) diff --git a/src/getConfig.ts b/src/getConfig.ts index 167cfac4..040ddbef 100644 --- a/src/getConfig.ts +++ b/src/getConfig.ts @@ -5,12 +5,14 @@ export type Config = Pick { trailingSlash: config.trailingSlash, outputEachDir: config.outputEachDir, outputMode: config.outputMode, - isYaml: openapi.yaml ?? !openapi.inputFile.endsWith('.json') + isYaml: openapi.yaml ?? !openapi.inputFile.endsWith('.json'), + replaceLeadingAtMark: openapi.replaceLeadingAtMark ?? '@' } } From fc0e6108431e87120a227183d15d2db21e72e578 Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Sun, 30 May 2021 21:49:22 +0900 Subject: [PATCH 3/7] feat: Adds support for externally setting default values for required fields --- aspida.config.js | 5 + samples/default-required.yml | 147 ++++++++++++++++++ samples/default-required/$api.ts | 136 ++++++++++++++++ samples/default-required/@types/index.ts | 10 ++ samples/default-required/dummy/$api.ts | 61 ++++++++ .../dummy/_id@number/content/index.ts | 8 + .../dummy/_id@number/query/index.ts | 11 ++ .../dummy/_id@number/simple/index.ts | 6 + samples/default-required/file/$api.ts | 35 +++++ .../file/_id@number/upload/index.ts | 13 ++ samples/default-required/user/$api.ts | 67 ++++++++ .../user/_id@number/abc/index.ts | 10 ++ .../default-required/user/_id@number/index.ts | 14 ++ .../user/_id@number/xyz/index.ts | 10 ++ src/buildTemplate.ts | 7 +- src/buildV3.ts | 75 +++++---- src/builderUtils/converters.ts | 21 +-- src/builderUtils/getDirName.ts | 7 +- src/builderUtils/parameters2Props.ts | 10 +- src/builderUtils/schemas2Props.ts | 8 +- src/getConfig.ts | 30 +++- 21 files changed, 638 insertions(+), 53 deletions(-) create mode 100644 samples/default-required.yml create mode 100644 samples/default-required/$api.ts create mode 100644 samples/default-required/@types/index.ts create mode 100644 samples/default-required/dummy/$api.ts create mode 100644 samples/default-required/dummy/_id@number/content/index.ts create mode 100644 samples/default-required/dummy/_id@number/query/index.ts create mode 100644 samples/default-required/dummy/_id@number/simple/index.ts create mode 100644 samples/default-required/file/$api.ts create mode 100644 samples/default-required/file/_id@number/upload/index.ts create mode 100644 samples/default-required/user/$api.ts create mode 100644 samples/default-required/user/_id@number/abc/index.ts create mode 100644 samples/default-required/user/_id@number/index.ts create mode 100644 samples/default-required/user/_id@number/xyz/index.ts diff --git a/aspida.config.js b/aspida.config.js index cd9665b5..acd2a4d6 100644 --- a/aspida.config.js +++ b/aspida.config.js @@ -33,5 +33,10 @@ module.exports = [ input: 'samples/nullable-object', outputEachDir: true, openapi: { inputFile: 'samples/nullable-object.yml' } + }, + { + input: 'samples/default-required', + outputEachDir: true, + openapi: { inputFile: 'samples/default-required.yml', requiredConfig: { schema: false } } } ] diff --git a/samples/default-required.yml b/samples/default-required.yml new file mode 100644 index 00000000..0427e179 --- /dev/null +++ b/samples/default-required.yml @@ -0,0 +1,147 @@ +openapi: "3.0.0" +info: + title: "simple" + version: "1.0" +paths: + /file/{id}/upload: + post: + parameters: + - in: path + name: id + schema: + type: integer + required: true + description: ID + - in: query + name: path + schema: + type: string + requestBody: + content: + application/octet-stream: + schema: + type: string + format: binary + responses: + "204": + description: No Content + /user/{id}: + parameters: + - in: path + name: id + schema: + type: integer + required: true + description: The user ID + get: + responses: + 202: + description: "accept" + patch: + summary: Updates an existing user with the specified ID + responses: + 202: + description: "accept" + delete: + summary: Deletes the user with the specified ID + responses: + 202: + description: "accept" + /user/{id}/abc: + get: + parameters: + - in: path + name: id + schema: + type: integer + required: true + description: The user ID + - in: query + name: q + schema: + type: string + responses: + 202: + description: "accept" + /user/{id}/xyz: + parameters: + - in: path + name: id + schema: + type: integer + required: true + description: The user ID + get: + responses: + 202: + description: "accept" + put: + responses: + 202: + description: "accept" + /dummy/{id}/simple: + put: + summary: "simple" + parameters: + - name: "id" + in: "path" + description: "id" + required: true + schema: + type: "integer" + responses: + 202: + description: "accept" + /dummy/{id}/query: + put: + summary: "query" + parameters: + - name: "id" + in: "path" + description: "id" + required: true + schema: + type: "integer" + - name: "q" + in: "query" + description: "query" + schema: + type: "string" + responses: + 202: + description: "accept" + /dummy/{id}/content: + put: + summary: "ng" + parameters: + - name: "id" + in: "path" + description: "id" + required: true + schema: + type: "integer" + responses: + 202: + description: "accept" + content: + application/json: + schema: + type: "string" +components: + schemas: + Customer: + description: Customer + properties: + id: + description: ID + type: integer + name: + description: NAME + type: string + pet: + description: "Type of pet 1:dog 2:cat 3:other" + type: number + enum: + - 1 + - 2 + - 3 diff --git a/samples/default-required/$api.ts b/samples/default-required/$api.ts new file mode 100644 index 00000000..9eda81fa --- /dev/null +++ b/samples/default-required/$api.ts @@ -0,0 +1,136 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders, dataToURLString } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './dummy/_id@number/content' +// prettier-ignore +import { Methods as Methods1 } from './dummy/_id@number/query' +// prettier-ignore +import { Methods as Methods2 } from './dummy/_id@number/simple' +// prettier-ignore +import { Methods as Methods3 } from './file/_id@number/upload' +// prettier-ignore +import { Methods as Methods4 } from './user/_id@number' +// prettier-ignore +import { Methods as Methods5 } from './user/_id@number/abc' +// prettier-ignore +import { Methods as Methods6 } from './user/_id@number/xyz' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/dummy' + const PATH1 = '/content' + const PATH2 = '/query' + const PATH3 = '/simple' + const PATH4 = '/file' + const PATH5 = '/upload' + const PATH6 = '/user' + const PATH7 = '/abc' + const PATH8 = '/xyz' + const GET = 'GET' + const POST = 'POST' + const PUT = 'PUT' + const DELETE = 'DELETE' + const PATCH = 'PATCH' + + return { + dummy: { + _id: (val1: number) => { + const prefix1 = `${PATH0}/${val1}` + + return { + content: { + /** + * @returns accept + */ + put: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH1}`, PUT, option).text(), + /** + * @returns accept + */ + $put: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH1}`, PUT, option).text().then(r => r.body), + $path: () => `${prefix}${prefix1}${PATH1}` + }, + query: { + put: (option: { query: Methods1['put']['query'], config?: T }) => + fetch(prefix, `${prefix1}${PATH2}`, PUT, option).send(), + $put: (option: { query: Methods1['put']['query'], config?: T }) => + fetch(prefix, `${prefix1}${PATH2}`, PUT, option).send().then(r => r.body), + $path: (option?: { method: 'put'; query: Methods1['put']['query'] }) => + `${prefix}${prefix1}${PATH2}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` + }, + simple: { + put: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH3}`, PUT, option).send(), + $put: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH3}`, PUT, option).send().then(r => r.body), + $path: () => `${prefix}${prefix1}${PATH3}` + } + } + } + }, + file: { + _id: (val1: number) => { + const prefix1 = `${PATH4}/${val1}` + + return { + upload: { + post: (option: { body: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) => + fetch(prefix, `${prefix1}${PATH5}`, POST, option).send(), + $post: (option: { body: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) => + fetch(prefix, `${prefix1}${PATH5}`, POST, option).send().then(r => r.body), + $path: (option?: { method: 'post'; query: Methods3['post']['query'] }) => + `${prefix}${prefix1}${PATH5}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` + } + } + } + }, + user: { + _id: (val1: number) => { + const prefix1 = `${PATH6}/${val1}` + + return { + abc: { + get: (option: { query: Methods5['get']['query'], config?: T }) => + fetch(prefix, `${prefix1}${PATH7}`, GET, option).send(), + $get: (option: { query: Methods5['get']['query'], config?: T }) => + fetch(prefix, `${prefix1}${PATH7}`, GET, option).send().then(r => r.body), + $path: (option?: { method?: 'get'; query: Methods5['get']['query'] }) => + `${prefix}${prefix1}${PATH7}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` + }, + xyz: { + get: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH8}`, GET, option).send(), + $get: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH8}`, GET, option).send().then(r => r.body), + put: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH8}`, PUT, option).send(), + $put: (option?: { config?: T }) => + fetch(prefix, `${prefix1}${PATH8}`, PUT, option).send().then(r => r.body), + $path: () => `${prefix}${prefix1}${PATH8}` + }, + get: (option?: { config?: T }) => + fetch(prefix, prefix1, GET, option).send(), + $get: (option?: { config?: T }) => + fetch(prefix, prefix1, GET, option).send().then(r => r.body), + patch: (option?: { config?: T }) => + fetch(prefix, prefix1, PATCH, option).send(), + $patch: (option?: { config?: T }) => + fetch(prefix, prefix1, PATCH, option).send().then(r => r.body), + delete: (option?: { config?: T }) => + fetch(prefix, prefix1, DELETE, option).send(), + $delete: (option?: { config?: T }) => + fetch(prefix, prefix1, DELETE, option).send().then(r => r.body), + $path: () => `${prefix}${prefix1}` + } + } + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/default-required/@types/index.ts b/samples/default-required/@types/index.ts new file mode 100644 index 00000000..b0fd9f54 --- /dev/null +++ b/samples/default-required/@types/index.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ +/** Customer */ +export type Customer = { + /** ID */ + id?: number + /** NAME */ + name?: string + /** Type of pet 1:dog 2:cat 3:other */ + pet?: 1 | 2 | 3 +} diff --git a/samples/default-required/dummy/$api.ts b/samples/default-required/dummy/$api.ts new file mode 100644 index 00000000..e938f01f --- /dev/null +++ b/samples/default-required/dummy/$api.ts @@ -0,0 +1,61 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders, dataToURLString } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './_id@number/content' +// prettier-ignore +import { Methods as Methods1 } from './_id@number/query' +// prettier-ignore +import { Methods as Methods2 } from './_id@number/simple' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/dummy' + const PATH1 = '/content' + const PATH2 = '/query' + const PATH3 = '/simple' + const PUT = 'PUT' + + return { + _id: (val0: number) => { + const prefix0 = `${PATH0}/${val0}` + + return { + content: { + /** + * @returns accept + */ + put: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH1}`, PUT, option).text(), + /** + * @returns accept + */ + $put: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH1}`, PUT, option).text().then(r => r.body), + $path: () => `${prefix}${prefix0}${PATH1}` + }, + query: { + put: (option: { query: Methods1['put']['query'], config?: T }) => + fetch(prefix, `${prefix0}${PATH2}`, PUT, option).send(), + $put: (option: { query: Methods1['put']['query'], config?: T }) => + fetch(prefix, `${prefix0}${PATH2}`, PUT, option).send().then(r => r.body), + $path: (option?: { method: 'put'; query: Methods1['put']['query'] }) => + `${prefix}${prefix0}${PATH2}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` + }, + simple: { + put: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH3}`, PUT, option).send(), + $put: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH3}`, PUT, option).send().then(r => r.body), + $path: () => `${prefix}${prefix0}${PATH3}` + } + } + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/default-required/dummy/_id@number/content/index.ts b/samples/default-required/dummy/_id@number/content/index.ts new file mode 100644 index 00000000..2871b9ff --- /dev/null +++ b/samples/default-required/dummy/_id@number/content/index.ts @@ -0,0 +1,8 @@ +/* eslint-disable */ +export type Methods = { + put: { + status: 202 + /** accept */ + resBody: string + } +} diff --git a/samples/default-required/dummy/_id@number/query/index.ts b/samples/default-required/dummy/_id@number/query/index.ts new file mode 100644 index 00000000..9175cb9f --- /dev/null +++ b/samples/default-required/dummy/_id@number/query/index.ts @@ -0,0 +1,11 @@ +/* eslint-disable */ +export type Methods = { + put: { + query: { + /** query */ + q: string + } + + status: 202 + } +} diff --git a/samples/default-required/dummy/_id@number/simple/index.ts b/samples/default-required/dummy/_id@number/simple/index.ts new file mode 100644 index 00000000..db3b6a14 --- /dev/null +++ b/samples/default-required/dummy/_id@number/simple/index.ts @@ -0,0 +1,6 @@ +/* eslint-disable */ +export type Methods = { + put: { + status: 202 + } +} diff --git a/samples/default-required/file/$api.ts b/samples/default-required/file/$api.ts new file mode 100644 index 00000000..aa348632 --- /dev/null +++ b/samples/default-required/file/$api.ts @@ -0,0 +1,35 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders, dataToURLString } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './_id@number/upload' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/file' + const PATH1 = '/upload' + const POST = 'POST' + + return { + _id: (val0: number) => { + const prefix0 = `${PATH0}/${val0}` + + return { + upload: { + post: (option: { body: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) => + fetch(prefix, `${prefix0}${PATH1}`, POST, option).send(), + $post: (option: { body: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) => + fetch(prefix, `${prefix0}${PATH1}`, POST, option).send().then(r => r.body), + $path: (option?: { method: 'post'; query: Methods0['post']['query'] }) => + `${prefix}${prefix0}${PATH1}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` + } + } + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/default-required/file/_id@number/upload/index.ts b/samples/default-required/file/_id@number/upload/index.ts new file mode 100644 index 00000000..de5890d2 --- /dev/null +++ b/samples/default-required/file/_id@number/upload/index.ts @@ -0,0 +1,13 @@ +/* eslint-disable */ +import type { ReadStream } from 'fs' + +export type Methods = { + post: { + query: { + path: string + } + + status: 204 + reqBody: File | ReadStream + } +} diff --git a/samples/default-required/user/$api.ts b/samples/default-required/user/$api.ts new file mode 100644 index 00000000..5c2ac55e --- /dev/null +++ b/samples/default-required/user/$api.ts @@ -0,0 +1,67 @@ +/* eslint-disable */ +// prettier-ignore +import { AspidaClient, BasicHeaders, dataToURLString } from 'aspida' +// prettier-ignore +import { Methods as Methods0 } from './_id@number' +// prettier-ignore +import { Methods as Methods1 } from './_id@number/abc' +// prettier-ignore +import { Methods as Methods2 } from './_id@number/xyz' + +// prettier-ignore +const api = ({ baseURL, fetch }: AspidaClient) => { + const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '') + const PATH0 = '/user' + const PATH1 = '/abc' + const PATH2 = '/xyz' + const GET = 'GET' + const PUT = 'PUT' + const DELETE = 'DELETE' + const PATCH = 'PATCH' + + return { + _id: (val0: number) => { + const prefix0 = `${PATH0}/${val0}` + + return { + abc: { + get: (option: { query: Methods1['get']['query'], config?: T }) => + fetch(prefix, `${prefix0}${PATH1}`, GET, option).send(), + $get: (option: { query: Methods1['get']['query'], config?: T }) => + fetch(prefix, `${prefix0}${PATH1}`, GET, option).send().then(r => r.body), + $path: (option?: { method?: 'get'; query: Methods1['get']['query'] }) => + `${prefix}${prefix0}${PATH1}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` + }, + xyz: { + get: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH2}`, GET, option).send(), + $get: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH2}`, GET, option).send().then(r => r.body), + put: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH2}`, PUT, option).send(), + $put: (option?: { config?: T }) => + fetch(prefix, `${prefix0}${PATH2}`, PUT, option).send().then(r => r.body), + $path: () => `${prefix}${prefix0}${PATH2}` + }, + get: (option?: { config?: T }) => + fetch(prefix, prefix0, GET, option).send(), + $get: (option?: { config?: T }) => + fetch(prefix, prefix0, GET, option).send().then(r => r.body), + patch: (option?: { config?: T }) => + fetch(prefix, prefix0, PATCH, option).send(), + $patch: (option?: { config?: T }) => + fetch(prefix, prefix0, PATCH, option).send().then(r => r.body), + delete: (option?: { config?: T }) => + fetch(prefix, prefix0, DELETE, option).send(), + $delete: (option?: { config?: T }) => + fetch(prefix, prefix0, DELETE, option).send().then(r => r.body), + $path: () => `${prefix}${prefix0}` + } + } + } +} + +// prettier-ignore +export type ApiInstance = ReturnType +// prettier-ignore +export default api diff --git a/samples/default-required/user/_id@number/abc/index.ts b/samples/default-required/user/_id@number/abc/index.ts new file mode 100644 index 00000000..e974c178 --- /dev/null +++ b/samples/default-required/user/_id@number/abc/index.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ +export type Methods = { + get: { + query: { + q: string + } + + status: 202 + } +} diff --git a/samples/default-required/user/_id@number/index.ts b/samples/default-required/user/_id@number/index.ts new file mode 100644 index 00000000..4d9e0b7a --- /dev/null +++ b/samples/default-required/user/_id@number/index.ts @@ -0,0 +1,14 @@ +/* eslint-disable */ +export type Methods = { + get: { + status: 202 + } + + patch: { + status: 202 + } + + delete: { + status: 202 + } +} diff --git a/samples/default-required/user/_id@number/xyz/index.ts b/samples/default-required/user/_id@number/xyz/index.ts new file mode 100644 index 00000000..d4f55062 --- /dev/null +++ b/samples/default-required/user/_id@number/xyz/index.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ +export type Methods = { + get: { + status: 202 + } + + put: { + status: 202 + } +} diff --git a/src/buildTemplate.ts b/src/buildTemplate.ts index a9138b30..7b136338 100644 --- a/src/buildTemplate.ts +++ b/src/buildTemplate.ts @@ -6,11 +6,14 @@ import { Config } from './getConfig' const isV3 = (openapi: OpenAPI.Document): openapi is OpenAPIV3.Document => 'openapi' in openapi -export default async ({ input, isYaml }: Config) => { +export default async ({ input, isYaml, requiredConfig }: Config) => { const openapi = await parse(input, { parse: { json: !isYaml } }) const docs = isV3(openapi) ? openapi : await require('swagger2openapi').convertObj(openapi, { direct: true }) - return buildV3(await resolveExternalRefs(docs, typeof input === 'string' ? input : '')) + return buildV3( + await resolveExternalRefs(docs, typeof input === 'string' ? input : ''), + requiredConfig + ) } diff --git a/src/buildV3.ts b/src/buildV3.ts index 22023e74..c67a07ba 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -17,6 +17,7 @@ import { resolveParamsRef, resolveResRef, resolveReqRef } from './builderUtils/r import getDirName from './builderUtils/getDirName' import schemas2Props from './builderUtils/schemas2Props' import parameters2Props from './builderUtils/parameters2Props' +import { RequiredConfig } from './getConfig' const methodNames = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'] as const @@ -25,10 +26,11 @@ const getParamsList = ( params?: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[] ) => params?.map(p => (isRefObject(p) ? resolveParamsRef(openapi, p.$ref) : p)) || [] -export default (openapi: OpenAPIV3.Document) => { +export default (openapi: OpenAPIV3.Document, requiredConfig: RequiredConfig) => { const files: { file: string[]; methods: string }[] = [] - const schemas = schemas2Props(openapi.components?.schemas, openapi) || [] - const parameters = parameters2Props(openapi.components?.parameters, openapi) || [] + const schemas = schemas2Props(openapi.components?.schemas, openapi, requiredConfig.schema) || [] + const parameters = + parameters2Props(openapi.components?.parameters, openapi, requiredConfig.parameter) || [] files.push( ...Object.keys(openapi.paths) @@ -44,16 +46,20 @@ export default (openapi: OpenAPIV3.Document) => { .split('/') .slice(1) .map(p => - getDirName(p, [ - ...getParamsList(openapi, openapi.paths[path]!.parameters), - ...methodProps.reduce( - (prev, c) => [ - ...prev, - ...getParamsList(openapi, openapi.paths[path]![c]?.parameters) - ], - [] as OpenAPIV3.ParameterObject[] - ) - ]) + getDirName( + p, + [ + ...getParamsList(openapi, openapi.paths[path]!.parameters), + ...methodProps.reduce( + (prev, c) => [ + ...prev, + ...getParamsList(openapi, openapi.paths[path]![c]?.parameters) + ], + [] as OpenAPIV3.ParameterObject[] + ) + ], + requiredConfig.parameter + ) ), 'index' ] @@ -71,7 +77,7 @@ export default (openapi: OpenAPIV3.Document) => { const reqHeaders: Prop[] = [] const refQuery: PropValue[] = [] const query: Prop[] = [] - let queryRequired = true + let queryRequired = requiredConfig.query ;[...(openapi.paths[path]!.parameters || []), ...(target.parameters || [])].forEach( p => { @@ -91,18 +97,18 @@ export default (openapi: OpenAPIV3.Document) => { break case 'query': refQuery.push(val) - queryRequired = ref.required ?? true + queryRequired = ref.required ?? requiredConfig.query break default: break } } else { - const value = schema2value(p.schema) + const value = schema2value(p.schema, requiredConfig.query) if (!value) return const prop = { name: getPropertyName(p.name), - required: p.required ?? true, + required: p.required ?? requiredConfig.query, description: p.description ?? null, values: [value] } @@ -113,7 +119,7 @@ export default (openapi: OpenAPIV3.Document) => { break case 'query': query.push(prop) - queryRequired = p.required ?? true + queryRequired = p.required ?? requiredConfig.query break default: break @@ -172,7 +178,7 @@ export default (openapi: OpenAPIV3.Document) => { if (code) { params.push({ name: 'status', - required: true, + required: requiredConfig.status, description: null, values: [ { @@ -192,11 +198,11 @@ export default (openapi: OpenAPIV3.Document) => { Object.entries(ref.content).find(([key]) => key.startsWith('application/'))?.[1] if (content?.schema) { - const val = schema2value(content.schema, true) + const val = schema2value(content.schema, requiredConfig.resBody, true) val && params.push({ name: 'resBody', - required: true, + required: requiredConfig.resBody, description: ref.description, values: [val] }) @@ -205,7 +211,7 @@ export default (openapi: OpenAPIV3.Document) => { if (ref.headers) { params.push({ name: 'resHeaders', - required: true, + required: requiredConfig.resHeader, description: null, values: [ { @@ -223,14 +229,14 @@ export default (openapi: OpenAPIV3.Document) => { description: null, value: $ref2Type(headerData.$ref) } - : schema2value(headerData.schema) + : schema2value(headerData.schema, requiredConfig.resHeader) return ( val && { name: getPropertyName(header), required: isRefObject(headerData) ? true - : headerData.required ?? true, + : headerData.required ?? requiredConfig.resHeader, description: isRefObject(headerData) ? null : headerData.description, @@ -249,7 +255,7 @@ export default (openapi: OpenAPIV3.Document) => { if (target.requestBody) { let reqFormat = '' let reqBody: PropValue | null = null - let required = true + let required = requiredConfig.reqBody let description: string | null = null if (isRefObject(target.requestBody)) { @@ -267,21 +273,25 @@ export default (openapi: OpenAPIV3.Document) => { description: null, value: $ref2Type(target.requestBody.$ref) } - required = ref.required ?? true + required = ref.required ?? requiredConfig.reqBody description = ref.description ?? null } else { - required = target.requestBody.required ?? true + required = target.requestBody.required ?? requiredConfig.reqBody description = target.requestBody.description ?? null if (target.requestBody.content['multipart/form-data']?.schema) { reqFormat = 'FormData' - reqBody = schema2value(target.requestBody.content['multipart/form-data'].schema) + reqBody = schema2value( + target.requestBody.content['multipart/form-data'].schema, + requiredConfig.reqBody + ) } else if ( target.requestBody.content['application/x-www-form-urlencoded']?.schema ) { reqFormat = 'URLSearchParams' reqBody = schema2value( - target.requestBody.content['application/x-www-form-urlencoded'].schema + target.requestBody.content['application/x-www-form-urlencoded'].schema, + requiredConfig.reqBody ) } else { const content = @@ -290,14 +300,15 @@ export default (openapi: OpenAPIV3.Document) => { key.startsWith('application/') )?.[1] - if (content?.schema) reqBody = schema2value(content.schema) + if (content?.schema) + reqBody = schema2value(content.schema, requiredConfig.reqBody) } } if (reqFormat) { params.push({ name: 'reqFormat', - required: true, + required: requiredConfig.reqFormat, description: null, values: [ { @@ -323,7 +334,7 @@ export default (openapi: OpenAPIV3.Document) => { return { name: method, - required: true, + required: requiredConfig.method, description: target.description ?? null, values: [ { isArray: false, isEnum: false, nullable: false, description: null, value: params } diff --git a/src/builderUtils/converters.ts b/src/builderUtils/converters.ts index 5c55003b..ec1929c5 100644 --- a/src/builderUtils/converters.ts +++ b/src/builderUtils/converters.ts @@ -40,14 +40,14 @@ export const isObjectSchema = ( export const getPropertyName = (name: string) => /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(name) ? name : `'${name}'` -const of2Values = (obj: OpenAPIV3.SchemaObject): PropValue[] | null => { +const of2Values = (obj: OpenAPIV3.SchemaObject, required: boolean): PropValue[] | null => { const values = (obj.oneOf || obj.allOf || obj.anyOf || []) - .map(p => schema2value(p)) + .map(p => schema2value(p, required)) .filter(v => v) as PropValue[] return values.length ? values : null } -const object2value = (obj: OpenAPIV3.NonArraySchemaObject): Prop[] => { +const object2value = (obj: OpenAPIV3.NonArraySchemaObject, required: boolean): Prop[] => { const properties = obj.properties ?? {} const value = Object.keys(properties) @@ -56,12 +56,12 @@ const object2value = (obj: OpenAPIV3.NonArraySchemaObject): Prop[] => { return isRefObject(target) || !target.deprecated }) .map(name => { - const val = schema2value(properties[name]) + const val = schema2value(properties[name], required) if (!val) return null return { name: getPropertyName(name), - required: obj.required?.includes(name) ?? true, + required: obj.required?.includes(name) ?? required, description: val.description, values: [val] } @@ -79,12 +79,12 @@ const object2value = (obj: OpenAPIV3.NonArraySchemaObject): Prop[] => { description: null, value: 'any' } - : schema2value(additionalProps) + : schema2value(additionalProps, required) if (val) value.push({ name: '[key: string]', - required: true, + required, description: val.description, values: [val] }) @@ -97,6 +97,7 @@ export const BINARY_TYPE = 'File | ReadStream' export const schema2value = ( schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined, + required: boolean, isResponse?: true ): PropValue | null => { if (!schema) return null @@ -116,15 +117,15 @@ export const schema2value = ( if (schema.oneOf || schema.allOf || schema.anyOf) { hasOf = schema.oneOf ? 'oneOf' : schema.allOf ? 'allOf' : 'anyOf' - value = of2Values(schema) + value = of2Values(schema, required) } else if (schema.enum) { isEnum = true value = schema.type === 'string' ? schema.enum.map(e => `'${e}'`) : schema.enum } else if (isArraySchema(schema)) { isArray = true - value = schema2value(schema.items) + value = schema2value(schema.items, required) } else if (schema.properties || schema.additionalProperties) { - value = object2value(schema) + value = object2value(schema, required) } else if (schema.format === 'binary') { value = isResponse ? 'Blob' : BINARY_TYPE } else if (schema.type !== 'object') { diff --git a/src/builderUtils/getDirName.ts b/src/builderUtils/getDirName.ts index ece60184..170a27a8 100644 --- a/src/builderUtils/getDirName.ts +++ b/src/builderUtils/getDirName.ts @@ -1,12 +1,15 @@ import { OpenAPIV3 } from 'openapi-types' import { getPropertyName, schema2value } from './converters' -export default (text: string, params: OpenAPIV3.ParameterObject[]) => { +export default (text: string, params: OpenAPIV3.ParameterObject[], required: boolean) => { if (text === '*') return '_any' if (!/^{/.test(text)) return text const valName = text.slice(1, -1) - const schemaVal = schema2value(params.find(p => p.in === 'path' && p.name === valName)?.schema) + const schemaVal = schema2value( + params.find(p => p.in === 'path' && p.name === valName)?.schema, + required + ) return `_${getPropertyName(valName)}${ schemaVal && typeof schemaVal.value === 'string' ? `@${schemaVal.value}` : '' diff --git a/src/builderUtils/parameters2Props.ts b/src/builderUtils/parameters2Props.ts index 5b0bbba6..6dcfbe6f 100644 --- a/src/builderUtils/parameters2Props.ts +++ b/src/builderUtils/parameters2Props.ts @@ -5,7 +5,11 @@ import { resolveParamsRef } from './resolvers' export type Parameter = { name: string; prop: string | Prop } -export default (params: OpenAPIV3.ComponentsObject['parameters'], openapi: OpenAPIV3.Document) => +export default ( + params: OpenAPIV3.ComponentsObject['parameters'], + openapi: OpenAPIV3.Document, + required: boolean +) => params && Object.keys(params) .filter(defKey => { @@ -19,12 +23,12 @@ export default (params: OpenAPIV3.ComponentsObject['parameters'], openapi: OpenA if (isRefObject(target)) { prop = $ref2Type(target.$ref) } else { - const value = schema2value(target.schema) + const value = schema2value(target.schema, required) if (!value) return null prop = { name: getPropertyName(target.name), - required: target.required ?? true, + required: target.required ?? required, description: target.description ?? null, values: [value] } diff --git a/src/builderUtils/schemas2Props.ts b/src/builderUtils/schemas2Props.ts index 7abc834e..e8006536 100644 --- a/src/builderUtils/schemas2Props.ts +++ b/src/builderUtils/schemas2Props.ts @@ -5,7 +5,11 @@ import { resolveSchemasRef } from './resolvers' export type Schema = { name: string; value: PropValue } -export default (schemas: OpenAPIV3.ComponentsObject['schemas'], openapi: OpenAPIV3.Document) => +export default ( + schemas: OpenAPIV3.ComponentsObject['schemas'], + openapi: OpenAPIV3.Document, + required: boolean +) => schemas && Object.keys(schemas) .filter(defKey => { @@ -13,7 +17,7 @@ export default (schemas: OpenAPIV3.ComponentsObject['schemas'], openapi: OpenAPI return !(isRefObject(target) ? resolveSchemasRef(openapi, target.$ref) : target).deprecated }) .map(defKey => { - const value = schema2value(schemas[defKey]) + const value = schema2value(schemas[defKey], required) return value ? { name: defKey2defName(defKey), value } : null }) .filter((v): v is Schema => !!v) diff --git a/src/getConfig.ts b/src/getConfig.ts index 167cfac4..1120c79e 100644 --- a/src/getConfig.ts +++ b/src/getConfig.ts @@ -1,29 +1,55 @@ import { OpenAPI } from 'openapi-types' import { AspidaConfig, getConfigs } from 'aspida/dist/commands' +export type RequiredConfig = { + schema: boolean + parameter: boolean + query: boolean + status: boolean + resBody: boolean + resHeader: boolean + reqBody: boolean + reqFormat: boolean + method: boolean +} + export type Config = Pick & { input: string | OpenAPI.Document output: string isYaml: boolean + requiredConfig: RequiredConfig } export type ConfigFile = AspidaConfig & { openapi?: { inputFile: string yaml?: boolean + requiredConfig?: Partial } } +const defaultRequired = { + schema: true, + parameter: true, + query: true, + status: true, + resBody: true, + resHeader: true, + reqBody: true, + reqFormat: true, + method: true +} + const createConfig = (config: ConfigFile): Config => { const openapi = config.openapi! - return { input: openapi.inputFile, output: config.input, trailingSlash: config.trailingSlash, outputEachDir: config.outputEachDir, outputMode: config.outputMode, - isYaml: openapi.yaml ?? !openapi.inputFile.endsWith('.json') + isYaml: openapi.yaml ?? !openapi.inputFile.endsWith('.json'), + requiredConfig: { ...defaultRequired, ...openapi.requiredConfig } } } From 228c3e7e0260c88d41270e2992fd1103f915036b Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Wed, 2 Jun 2021 16:45:11 +0900 Subject: [PATCH 4/7] post install --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 35e5ec4e..176a3e62 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "lint": "eslint --ext .js,.ts .", "lint:fix": "npm run lint -- --fix", "test": "jest", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "postinstall": "npm run build" }, "homepage": "/~https://github.com/aspida/openapi2aspida#readme", "repository": { From 8562bee2c934a922f10157d6cbc87953f7665ac8 Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Thu, 3 Jun 2021 10:04:18 +0900 Subject: [PATCH 5/7] comment out codecov and release action --- .github/workflows/nodejs.yml | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 363a7316..d0bfb040 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -30,30 +30,30 @@ jobs: if: matrix.os != 'windows-latest' - run: yarn typecheck - run: yarn test --coverage - - run: npx codecov - if: github.ref == 'refs/heads/master' && matrix.os == 'ubuntu-latest' && matrix.node-version == 14 - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + # - run: npx codecov + # if: github.ref == 'refs/heads/master' && matrix.os == 'ubuntu-latest' && matrix.node-version == 14 + # env: + # CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - release: - runs-on: ubuntu-latest - needs: test - if: contains(github.ref, 'tags/v') - steps: - - uses: actions/checkout@v2 - - name: Use Node.js - uses: actions/setup-node@v1 - with: - node-version: 12 - registry-url: "https://registry.npmjs.org" - - uses: actions/cache@v2 - id: yarn-cache - with: - path: "node_modules" - key: ${{ runner.os }}-node-v12-yarn-${{ hashFiles('yarn.lock') }} - - run: yarn --frozen-lockfile - if: steps.yarn-cache.outputs.cache-hit != 'true' - - run: yarn build - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + # release: + # runs-on: ubuntu-latest + # needs: test + # if: contains(github.ref, 'tags/v') + # steps: + # - uses: actions/checkout@v2 + # - name: Use Node.js + # uses: actions/setup-node@v1 + # with: + # node-version: 12 + # registry-url: "https://registry.npmjs.org" + # - uses: actions/cache@v2 + # id: yarn-cache + # with: + # path: "node_modules" + # key: ${{ runner.os }}-node-v12-yarn-${{ hashFiles('yarn.lock') }} + # - run: yarn --frozen-lockfile + # if: steps.yarn-cache.outputs.cache-hit != 'true' + # - run: yarn build + # - run: npm publish + # env: + # NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} From 3f6145d8223d5d2bbf36f883acb1ff276e90ece7 Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Thu, 3 Jun 2021 17:05:19 +0900 Subject: [PATCH 6/7] adds dist --- .gitignore | 2 +- dist/buildTemplate.d.ts | 13 + dist/buildTemplate.d.ts.map | 1 + dist/buildTemplate.js | 72 ++++ dist/buildTemplate.js.map | 1 + dist/buildV3.d.ts | 13 + dist/buildV3.d.ts.map | 1 + dist/buildV3.js | 344 ++++++++++++++++++++ dist/buildV3.js.map | 1 + dist/builderUtils/converters.d.ts | 14 + dist/builderUtils/converters.d.ts.map | 1 + dist/builderUtils/converters.js | 144 ++++++++ dist/builderUtils/converters.js.map | 1 + dist/builderUtils/getDirName.d.ts | 4 + dist/builderUtils/getDirName.d.ts.map | 1 + dist/builderUtils/getDirName.js | 17 + dist/builderUtils/getDirName.js.map | 1 + dist/builderUtils/parameters2Props.d.ts | 9 + dist/builderUtils/parameters2Props.d.ts.map | 1 + dist/builderUtils/parameters2Props.js | 34 ++ dist/builderUtils/parameters2Props.js.map | 1 + dist/builderUtils/props2String.d.ts | 18 + dist/builderUtils/props2String.d.ts.map | 1 + dist/builderUtils/props2String.js | 45 +++ dist/builderUtils/props2String.js.map | 1 + dist/builderUtils/resolvers.d.ts | 6 + dist/builderUtils/resolvers.d.ts.map | 1 + dist/builderUtils/resolvers.js | 27 ++ dist/builderUtils/resolvers.js.map | 1 + dist/builderUtils/schemas2Props.d.ts | 9 + dist/builderUtils/schemas2Props.d.ts.map | 1 + dist/builderUtils/schemas2Props.js | 18 + dist/builderUtils/schemas2Props.js.map | 1 + dist/cli.d.ts | 2 + dist/cli.d.ts.map | 1 + dist/cli.js | 21 ++ dist/cli.js.map | 1 + dist/getConfig.d.ts | 32 ++ dist/getConfig.d.ts.map | 1 + dist/getConfig.js | 45 +++ dist/getConfig.js.map | 1 + dist/index.d.ts | 4 + dist/index.d.ts.map | 1 + dist/index.js | 78 +++++ dist/index.js.map | 1 + dist/resolveExternalRefs.d.ts | 4 + dist/resolveExternalRefs.d.ts.map | 1 + dist/resolveExternalRefs.js | 209 ++++++++++++ dist/resolveExternalRefs.js.map | 1 + dist/writeRouteFile.d.ts | 11 + dist/writeRouteFile.d.ts.map | 1 + dist/writeRouteFile.js | 26 ++ dist/writeRouteFile.js.map | 1 + package.json | 3 +- 54 files changed, 1247 insertions(+), 3 deletions(-) create mode 100644 dist/buildTemplate.d.ts create mode 100644 dist/buildTemplate.d.ts.map create mode 100644 dist/buildTemplate.js create mode 100644 dist/buildTemplate.js.map create mode 100644 dist/buildV3.d.ts create mode 100644 dist/buildV3.d.ts.map create mode 100644 dist/buildV3.js create mode 100644 dist/buildV3.js.map create mode 100644 dist/builderUtils/converters.d.ts create mode 100644 dist/builderUtils/converters.d.ts.map create mode 100644 dist/builderUtils/converters.js create mode 100644 dist/builderUtils/converters.js.map create mode 100644 dist/builderUtils/getDirName.d.ts create mode 100644 dist/builderUtils/getDirName.d.ts.map create mode 100644 dist/builderUtils/getDirName.js create mode 100644 dist/builderUtils/getDirName.js.map create mode 100644 dist/builderUtils/parameters2Props.d.ts create mode 100644 dist/builderUtils/parameters2Props.d.ts.map create mode 100644 dist/builderUtils/parameters2Props.js create mode 100644 dist/builderUtils/parameters2Props.js.map create mode 100644 dist/builderUtils/props2String.d.ts create mode 100644 dist/builderUtils/props2String.d.ts.map create mode 100644 dist/builderUtils/props2String.js create mode 100644 dist/builderUtils/props2String.js.map create mode 100644 dist/builderUtils/resolvers.d.ts create mode 100644 dist/builderUtils/resolvers.d.ts.map create mode 100644 dist/builderUtils/resolvers.js create mode 100644 dist/builderUtils/resolvers.js.map create mode 100644 dist/builderUtils/schemas2Props.d.ts create mode 100644 dist/builderUtils/schemas2Props.d.ts.map create mode 100644 dist/builderUtils/schemas2Props.js create mode 100644 dist/builderUtils/schemas2Props.js.map create mode 100644 dist/cli.d.ts create mode 100644 dist/cli.d.ts.map create mode 100644 dist/cli.js create mode 100644 dist/cli.js.map create mode 100644 dist/getConfig.d.ts create mode 100644 dist/getConfig.d.ts.map create mode 100644 dist/getConfig.js create mode 100644 dist/getConfig.js.map create mode 100644 dist/index.d.ts create mode 100644 dist/index.d.ts.map create mode 100644 dist/index.js create mode 100644 dist/index.js.map create mode 100644 dist/resolveExternalRefs.d.ts create mode 100644 dist/resolveExternalRefs.d.ts.map create mode 100644 dist/resolveExternalRefs.js create mode 100644 dist/resolveExternalRefs.js.map create mode 100644 dist/writeRouteFile.d.ts create mode 100644 dist/writeRouteFile.d.ts.map create mode 100644 dist/writeRouteFile.js create mode 100644 dist/writeRouteFile.js.map diff --git a/.gitignore b/.gitignore index 0d53d201..0b056415 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules -dist +#dist external_refs_sample diff --git a/dist/buildTemplate.d.ts b/dist/buildTemplate.d.ts new file mode 100644 index 00000000..66b57a3d --- /dev/null +++ b/dist/buildTemplate.d.ts @@ -0,0 +1,13 @@ +import { OpenAPIV3 } from 'openapi-types'; +import { Config } from './getConfig'; +declare const _default: ({ input, isYaml, requiredConfig, replaceLeadingAtMark }: Config) => Promise<{ + openapi: OpenAPIV3.Document<{}>; + baseURL: string; + types: string | null; + files: { + file: string[]; + methods: string; + }[]; +}>; +export default _default; +//# sourceMappingURL=buildTemplate.d.ts.map \ No newline at end of file diff --git a/dist/buildTemplate.d.ts.map b/dist/buildTemplate.d.ts.map new file mode 100644 index 00000000..2f2743de --- /dev/null +++ b/dist/buildTemplate.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"buildTemplate.d.ts","sourceRoot":"","sources":["../src/buildTemplate.ts"],"names":[],"mappings":"AACA,OAAO,EAAW,SAAS,EAAE,MAAM,eAAe,CAAA;AAGlD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;kFAI2C,MAAM;;;;;;;;;AAArF,wBAWC"} \ No newline at end of file diff --git a/dist/buildTemplate.js b/dist/buildTemplate.js new file mode 100644 index 00000000..c94de1cb --- /dev/null +++ b/dist/buildTemplate.js @@ -0,0 +1,72 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var swagger_parser_1 = require("swagger-parser"); +var buildV3_1 = __importDefault(require("./buildV3")); +var resolveExternalRefs_1 = __importDefault(require("./resolveExternalRefs")); +var isV3 = function (openapi) { return 'openapi' in openapi; }; +exports.default = (function (_a) { + var input = _a.input, isYaml = _a.isYaml, requiredConfig = _a.requiredConfig, replaceLeadingAtMark = _a.replaceLeadingAtMark; + return __awaiter(void 0, void 0, void 0, function () { + var openapi, docs, _b, _c; + return __generator(this, function (_d) { + switch (_d.label) { + case 0: return [4 /*yield*/, swagger_parser_1.parse(input, { parse: { json: !isYaml } })]; + case 1: + openapi = _d.sent(); + if (!isV3(openapi)) return [3 /*break*/, 2]; + _b = openapi; + return [3 /*break*/, 4]; + case 2: return [4 /*yield*/, require('swagger2openapi').convertObj(openapi, { direct: true })]; + case 3: + _b = _d.sent(); + _d.label = 4; + case 4: + docs = _b; + _c = buildV3_1.default; + return [4 /*yield*/, resolveExternalRefs_1.default(docs, typeof input === 'string' ? input : '')]; + case 5: return [2 /*return*/, _c.apply(void 0, [_d.sent(), requiredConfig, + replaceLeadingAtMark])]; + } + }); + }); +}); +//# sourceMappingURL=buildTemplate.js.map \ No newline at end of file diff --git a/dist/buildTemplate.js.map b/dist/buildTemplate.js.map new file mode 100644 index 00000000..24bdd444 --- /dev/null +++ b/dist/buildTemplate.js.map @@ -0,0 +1 @@ +{"version":3,"file":"buildTemplate.js","sourceRoot":"","sources":["../src/buildTemplate.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAsC;AAEtC,sDAA+B;AAC/B,8EAAuD;AAGvD,IAAM,IAAI,GAAG,UAAC,OAAyB,IAAoC,OAAA,SAAS,IAAI,OAAO,EAApB,CAAoB,CAAA;AAE/F,mBAAe,UAAO,EAA+D;QAA7D,KAAK,WAAA,EAAE,MAAM,YAAA,EAAE,cAAc,oBAAA,EAAE,oBAAoB,0BAAA;;;;;wBACzD,qBAAM,sBAAK,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,EAAA;;oBAA1D,OAAO,GAAG,SAAgD;yBACnD,IAAI,CAAC,OAAO,CAAC,EAAb,wBAAa;oBACtB,KAAA,OAAO,CAAA;;wBACP,qBAAM,OAAO,CAAC,iBAAiB,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAA;;oBAAtE,KAAA,SAAsE,CAAA;;;oBAFpE,IAAI,KAEgE;oBAEnE,KAAA,iBAAO,CAAA;oBACZ,qBAAM,6BAAmB,CAAC,IAAI,EAAE,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAA;wBADzE,sBAAO,kBACL,SAAuE,EACvE,cAAc;wBACd,oBAAoB,EACrB,EAAA;;;;CACF,EAAA"} \ No newline at end of file diff --git a/dist/buildV3.d.ts b/dist/buildV3.d.ts new file mode 100644 index 00000000..a70743a7 --- /dev/null +++ b/dist/buildV3.d.ts @@ -0,0 +1,13 @@ +import { OpenAPIV3 } from 'openapi-types'; +import { RequiredConfig } from './getConfig'; +declare const _default: (openapi: OpenAPIV3.Document, requiredConfig: RequiredConfig, replaceLeadingAtMark: string) => { + openapi: OpenAPIV3.Document<{}>; + baseURL: string; + types: string | null; + files: { + file: string[]; + methods: string; + }[]; +}; +export default _default; +//# sourceMappingURL=buildV3.d.ts.map \ No newline at end of file diff --git a/dist/buildV3.d.ts.map b/dist/buildV3.d.ts.map new file mode 100644 index 00000000..259a9b39 --- /dev/null +++ b/dist/buildV3.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"buildV3.d.ts","sourceRoot":"","sources":["../src/buildV3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAmBzC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;kCAUjC,kBAAkB,kBACX,cAAc,wBACR,MAAM;;;;;cAEP,MAAM,EAAE;iBAAW,MAAM;;;AALhD,wBAqXC"} \ No newline at end of file diff --git a/dist/buildV3.js b/dist/buildV3.js new file mode 100644 index 00000000..a1ee28f2 --- /dev/null +++ b/dist/buildV3.js @@ -0,0 +1,344 @@ +"use strict"; +var __read = (this && this.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __spreadArray = (this && this.__spreadArray) || function (to, from) { + for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) + to[j] = from[i]; + return to; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var converters_1 = require("./builderUtils/converters"); +var props2String_1 = require("./builderUtils/props2String"); +var resolvers_1 = require("./builderUtils/resolvers"); +var getDirName_1 = __importDefault(require("./builderUtils/getDirName")); +var schemas2Props_1 = __importDefault(require("./builderUtils/schemas2Props")); +var parameters2Props_1 = __importDefault(require("./builderUtils/parameters2Props")); +var methodNames = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch']; +var getParamsList = function (openapi, params) { return (params === null || params === void 0 ? void 0 : params.map(function (p) { return (converters_1.isRefObject(p) ? resolvers_1.resolveParamsRef(openapi, p.$ref) : p); })) || []; }; +exports.default = (function (openapi, requiredConfig, replaceLeadingAtMark) { + var _a, _b, _c, _d; + var files = []; + var schemas = schemas2Props_1.default((_a = openapi.components) === null || _a === void 0 ? void 0 : _a.schemas, openapi, requiredConfig.schema) || []; + var parameters = parameters2Props_1.default((_b = openapi.components) === null || _b === void 0 ? void 0 : _b.parameters, openapi, requiredConfig.parameter) || []; + files.push.apply(files, __spreadArray([], __read(Object.keys(openapi.paths) + .map(function (path) { + var methodProps = Object.keys(openapi.paths[path]).filter(function (method) { + return methodNames.includes(method); + }); + var file = __spreadArray(__spreadArray([], __read(path + .replace(/\/$/, '') + .split('/') + .slice(1) + .map(function (p) { + return getDirName_1.default(p, __spreadArray(__spreadArray([], __read(getParamsList(openapi, openapi.paths[path].parameters))), __read(methodProps.reduce(function (prev, c) { + var _a; + return __spreadArray(__spreadArray([], __read(prev)), __read(getParamsList(openapi, (_a = openapi.paths[path][c]) === null || _a === void 0 ? void 0 : _a.parameters))); + }, []))), requiredConfig.parameter, replaceLeadingAtMark); + }))), [ + 'index' + ]); + var methods = methodProps + .map(function (method) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; + var target = openapi.paths[path][method]; + if (target.deprecated) + return null; + var params = []; + if (target.parameters || openapi.paths[path].parameters) { + var reqRefHeaders_1 = []; + var reqHeaders_1 = []; + var refQuery_1 = []; + var query_1 = []; + var queryRequired_1 = requiredConfig.query; + __spreadArray(__spreadArray([], __read((openapi.paths[path].parameters || []))), __read((target.parameters || []))).forEach(function (p) { + var _a, _b, _c, _d, _e; + if (converters_1.isRefObject(p)) { + var ref = resolvers_1.resolveParamsRef(openapi, p.$ref); + var val = { + isArray: false, + isEnum: false, + nullable: false, + description: (_a = ref.description) !== null && _a !== void 0 ? _a : null, + value: converters_1.$ref2Type(p.$ref) + }; + switch (ref.in) { + case 'header': + reqRefHeaders_1.push(val); + break; + case 'query': + refQuery_1.push(val); + queryRequired_1 = (_b = ref.required) !== null && _b !== void 0 ? _b : requiredConfig.query; + break; + default: + break; + } + } + else { + var value = converters_1.schema2value(p.schema, requiredConfig.query); + if (!value) + return; + var prop = { + name: converters_1.getPropertyName(p.name), + required: (_c = p.required) !== null && _c !== void 0 ? _c : requiredConfig.query, + description: (_d = p.description) !== null && _d !== void 0 ? _d : null, + values: [value] + }; + switch (p.in) { + case 'header': + reqHeaders_1.push(prop); + break; + case 'query': + query_1.push(prop); + queryRequired_1 = (_e = p.required) !== null && _e !== void 0 ? _e : requiredConfig.query; + break; + default: + break; + } + } + }); + if (reqHeaders_1.length || reqRefHeaders_1.length) { + params.push({ + name: 'reqHeaders', + required: false, + description: null, + values: __spreadArray(__spreadArray([], __read(reqRefHeaders_1)), __read((reqHeaders_1.length + ? [ + { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: reqHeaders_1 + } + ] + : []))) + }); + } + if (refQuery_1.length || query_1.length) { + params.push({ + name: 'query', + required: queryRequired_1, + description: null, + values: __spreadArray(__spreadArray([], __read(refQuery_1)), __read((query_1.length + ? [ + { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: query_1 + } + ] + : []))) + }); + } + } + if (target.responses) { + var code = Object.keys(target.responses).find(function (code) { return code.startsWith('20'); }); + if (code) { + params.push({ + name: 'status', + required: requiredConfig.status, + description: null, + values: [ + { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: code + } + ] + }); + var res = target.responses[code]; + var ref_1 = converters_1.isRefObject(res) ? resolvers_1.resolveResRef(openapi, res.$ref) : res; + var content = ref_1.content && + ((_a = Object.entries(ref_1.content).find(function (_a) { + var _b = __read(_a, 1), key = _b[0]; + return key.startsWith('application/'); + })) === null || _a === void 0 ? void 0 : _a[1]); + if (content === null || content === void 0 ? void 0 : content.schema) { + var val = converters_1.schema2value(content.schema, requiredConfig.resBody, true); + val && + params.push({ + name: 'resBody', + required: requiredConfig.resBody, + description: ref_1.description, + values: [val] + }); + } + if (ref_1.headers) { + params.push({ + name: 'resHeaders', + required: requiredConfig.resHeader, + description: null, + values: [ + { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: Object.keys(ref_1.headers) + .map(function (header) { + var _a; + var headerData = ref_1.headers[header]; + var val = converters_1.isRefObject(headerData) + ? { + isArray: false, + isEnum: false, + description: null, + value: converters_1.$ref2Type(headerData.$ref) + } + : converters_1.schema2value(headerData.schema, requiredConfig.resHeader); + return (val && { + name: converters_1.getPropertyName(header), + required: converters_1.isRefObject(headerData) + ? true + : (_a = headerData.required) !== null && _a !== void 0 ? _a : requiredConfig.resHeader, + description: converters_1.isRefObject(headerData) + ? null + : headerData.description, + values: [val] + }); + }) + .filter(function (v) { return !!v; }) + } + ] + }); + } + } + } + if (target.requestBody) { + var reqFormat = ''; + var reqBody = null; + var required = requiredConfig.reqBody; + var description = null; + if (converters_1.isRefObject(target.requestBody)) { + var ref = resolvers_1.resolveReqRef(openapi, target.requestBody.$ref); + if ((_b = ref.content['multipart/form-data']) === null || _b === void 0 ? void 0 : _b.schema) { + reqFormat = 'FormData'; + } + else if ((_c = ref.content['application/x-www-form-urlencoded']) === null || _c === void 0 ? void 0 : _c.schema) { + reqFormat = 'URLSearchParams'; + } + reqBody = { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: converters_1.$ref2Type(target.requestBody.$ref) + }; + required = (_d = ref.required) !== null && _d !== void 0 ? _d : requiredConfig.reqBody; + description = (_e = ref.description) !== null && _e !== void 0 ? _e : null; + } + else { + required = (_f = target.requestBody.required) !== null && _f !== void 0 ? _f : requiredConfig.reqBody; + description = (_g = target.requestBody.description) !== null && _g !== void 0 ? _g : null; + if ((_h = target.requestBody.content['multipart/form-data']) === null || _h === void 0 ? void 0 : _h.schema) { + reqFormat = 'FormData'; + reqBody = converters_1.schema2value(target.requestBody.content['multipart/form-data'].schema, requiredConfig.reqBody); + } + else if ((_j = target.requestBody.content['application/x-www-form-urlencoded']) === null || _j === void 0 ? void 0 : _j.schema) { + reqFormat = 'URLSearchParams'; + reqBody = converters_1.schema2value(target.requestBody.content['application/x-www-form-urlencoded'].schema, requiredConfig.reqBody); + } + else { + var content = target.requestBody.content && + ((_k = Object.entries(target.requestBody.content).find(function (_a) { + var _b = __read(_a, 1), key = _b[0]; + return key.startsWith('application/'); + })) === null || _k === void 0 ? void 0 : _k[1]); + if (content === null || content === void 0 ? void 0 : content.schema) + reqBody = converters_1.schema2value(content.schema, requiredConfig.reqBody); + } + } + if (reqFormat) { + params.push({ + name: 'reqFormat', + required: requiredConfig.reqFormat, + description: null, + values: [ + { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: reqFormat + } + ] + }); + } + if (reqBody) { + params.push({ + name: 'reqBody', + required: required, + description: description, + values: [reqBody] + }); + } + } + return { + name: method, + required: requiredConfig.method, + description: (_l = target.description) !== null && _l !== void 0 ? _l : null, + values: [ + { isArray: false, isEnum: false, nullable: false, description: null, value: params } + ] + }; + }) + .filter(function (method) { return !!method; }); + if (methods.length) { + var methodsText = props2String_1.props2String(methods, ''); + var hasBinary = methodsText.includes(converters_1.BINARY_TYPE); + var hasTypes = /( |<)Types\./.test(methodsText); + return { + file: file, + methods: "/* eslint-disable */\n" + (hasBinary ? "import type { ReadStream } from 'fs'\n" : '') + (hasBinary && !hasTypes ? '\n' : '') + (hasTypes + ? "import type * as Types from '" + file.map(function () { return ''; }).join('../') + "@types'\n\n" + : '') + "export type Methods = " + methodsText + "\n" + }; + } + else { + return { file: file, methods: '' }; + } + }) + .filter(function (file) { return file.methods; })))); + var typesText = parameters.length + schemas.length + ? __spreadArray(__spreadArray([], __read(parameters.map(function (p) { return ({ + name: p.name, + description: null, + text: typeof p.prop === 'string' ? p.prop : props2String_1.props2String([p.prop], '') + }); }))), __read(schemas.map(function (s) { return ({ + name: s.name, + description: s.value.description, + text: props2String_1.value2String(s.value, '').replace(/\n {2}/g, '\n') + }); }))).map(function (p) { return "\n" + props2String_1.description2Doc(p.description, '') + "export type " + p.name + " = " + p.text + "\n"; }) + .join('') + .replace(/(\W)Types\./g, '$1') + : null; + return { + openapi: openapi, + baseURL: ((_d = (_c = openapi.servers) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.url) || '', + types: typesText && + "/* eslint-disable */" + (typesText.includes(converters_1.BINARY_TYPE) ? "\nimport type { ReadStream } from 'fs'\n" : '') + typesText, + files: files + }; +}); +//# sourceMappingURL=buildV3.js.map \ No newline at end of file diff --git a/dist/buildV3.js.map b/dist/buildV3.js.map new file mode 100644 index 00000000..f6e5ea02 --- /dev/null +++ b/dist/buildV3.js.map @@ -0,0 +1 @@ +{"version":3,"file":"buildV3.js","sourceRoot":"","sources":["../src/buildV3.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,wDAMkC;AAClC,4DAMoC;AACpC,sDAAyF;AACzF,yEAAkD;AAClD,+EAAwD;AACxD,qFAA8D;AAG9D,IAAM,WAAW,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAU,CAAA;AAEzF,IAAM,aAAa,GAAG,UACpB,OAA2B,EAC3B,MAAkE,IAC/D,OAAA,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,wBAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,4BAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAxD,CAAwD,CAAC,KAAI,EAAE,EAAhF,CAAgF,CAAA;AAErF,mBAAe,UACb,OAA2B,EAC3B,cAA8B,EAC9B,oBAA4B;;IAE5B,IAAM,KAAK,GAA0C,EAAE,CAAA;IACvD,IAAM,O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AW,EAAE,CAAC,CAAC,KAAK,CAAC,WAAW;YAChC,IAAI,EAAE,2BAAY,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC;SACzD,CAAC,EAJkB,CAIlB,CAAC,GAEF,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,OAAK,8BAAe,CAAC,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,oBAAe,CAAC,CAAC,IAAI,WAAM,CAAC,CAAC,IAAI,OAAI,EAA5E,CAA4E,CAAC;aACtF,IAAI,CAAC,EAAE,CAAC;aACR,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC;QAClC,CAAC,CAAC,IAAI,CAAA;IAEV,OAAO;QACL,OAAO,SAAA;QACP,OAAO,EAAE,CAAA,MAAA,MAAA,OAAO,CAAC,OAAO,0CAAG,CAAC,CAAC,0CAAE,GAAG,KAAI,EAAE;QACxC,KAAK,EACH,SAAS;YACT,0BACE,SAAS,CAAC,QAAQ,CAAC,wBAAW,CAAC,CAAC,CAAC,CAAC,0CAA0C,CAAC,CAAC,CAAC,EAAE,IAChF,SAAW;QAChB,KAAK,OAAA;KACN,CAAA;AACH,CAAC,EAAA"} \ No newline at end of file diff --git a/dist/builderUtils/converters.d.ts b/dist/builderUtils/converters.d.ts new file mode 100644 index 00000000..67f13935 --- /dev/null +++ b/dist/builderUtils/converters.d.ts @@ -0,0 +1,14 @@ +import { OpenAPIV3 } from 'openapi-types'; +import { PropValue } from './props2String'; +export declare const defKey2defName: (key: string) => string; +export declare const $ref2TypeName: (ref: string) => { + typeName: string; + propName: string | null; +}; +export declare const $ref2Type: (ref: string) => string; +export declare const isRefObject: (params: OpenAPIV3.ReferenceObject | OpenAPIV3.ResponseObject | OpenAPIV3.RequestBodyObject | OpenAPIV3.HeaderObject | OpenAPIV3.ParameterObject | OpenAPIV3.SchemaObject) => params is OpenAPIV3.ReferenceObject; +export declare const isObjectSchema: (schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject) => schema is OpenAPIV3.NonArraySchemaObject; +export declare const getPropertyName: (name: string) => string; +export declare const BINARY_TYPE = "File | ReadStream"; +export declare const schema2value: (schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined, required: boolean, isResponse?: true | undefined) => PropValue | null; +//# sourceMappingURL=converters.d.ts.map \ No newline at end of file diff --git a/dist/builderUtils/converters.d.ts.map b/dist/builderUtils/converters.d.ts.map new file mode 100644 index 00000000..8b1adaae --- /dev/null +++ b/dist/builderUtils/converters.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"converters.d.ts","sourceRoot":"","sources":["../../src/builderUtils/converters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAQ,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAEhD,eAAO,MAAM,cAAc,QAAS,MAAM,WAGJ,CAAA;AAEtC,eAAO,MAAM,aAAa,QAAS,MAAM;;;CAGxC,CAAA;AAGD,eAAO,MAAM,SAAS,QAAS,MAAM,WAMpC,CAAA;AAED,eAAO,MAAM,WAAW,WAElB,UAAU,eAAe,GACzB,UAAU,cAAc,GACxB,UAAU,iBAAiB,GAC3B,UAAU,YAAY,GACtB,UAAU,eAAe,GACzB,UAAU,YAAY,wCAC8B,CAAA;AAK1D,eAAO,MAAM,cAAc,WACjB,UAAU,eAAe,GAAG,UAAU,YAAY,6CACkC,CAAA;AAE9F,eAAO,MAAM,eAAe,SAAU,MAAM,WACkB,CAAA;AAuD9D,eAAO,MAAM,WAAW,sBAAsB,CAAA;AAE9C,eAAO,MAAM,YAAY,WACf,UAAU,eAAe,GAAG,UAAU,YAAY,GAAG,SAAS,YAC5D,OAAO,oCAEhB,SAAS,GAAG,IAyCd,CAAA"} \ No newline at end of file diff --git a/dist/builderUtils/converters.js b/dist/builderUtils/converters.js new file mode 100644 index 00000000..de10e1ab --- /dev/null +++ b/dist/builderUtils/converters.js @@ -0,0 +1,144 @@ +"use strict"; +var __read = (this && this.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.schema2value = exports.BINARY_TYPE = exports.getPropertyName = exports.isObjectSchema = exports.isRefObject = exports.$ref2Type = exports.$ref2TypeName = exports.defKey2defName = void 0; +var defKey2defName = function (key) { + return "" + key[0].replace(/^([^a-zA-Z$_])$/, '$$$1').toUpperCase() + key + .slice(1) + .replace(/[^a-zA-Z0-9$_]/g, '_'); +}; +exports.defKey2defName = defKey2defName; +var $ref2TypeName = function (ref) { + var _a = __read(ref.split('/'), 6), typeName = _a[3], propName = _a[5]; + return { typeName: typeName, propName: propName || null }; +}; +exports.$ref2TypeName = $ref2TypeName; +// $ref2Type: replace /Array$/ for Swagger 2.0 +var $ref2Type = function (ref) { + var _a = exports.$ref2TypeName(ref), typeName = _a.typeName, propName = _a.propName; + return ("Types." + exports.defKey2defName(typeName) + (propName ? "['" + propName + "']" : '')).replace(/Array$/, '[]'); +}; +exports.$ref2Type = $ref2Type; +var isRefObject = function (params) { return '$ref' in params; }; +exports.isRefObject = isRefObject; +var isArraySchema = function (schema) { + return schema.type === 'array'; +}; +var isObjectSchema = function (schema) { return !exports.isRefObject(schema) && schema.type !== 'array'; }; +exports.isObjectSchema = isObjectSchema; +var getPropertyName = function (name) { + return /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(name) ? name : "'" + name + "'"; +}; +exports.getPropertyName = getPropertyName; +var of2Values = function (obj, required) { + var values = (obj.oneOf || obj.allOf || obj.anyOf || []) + .map(function (p) { return exports.schema2value(p, required); }) + .filter(function (v) { return v; }); + return values.length ? values : null; +}; +var object2value = function (obj, required) { + var _a; + var properties = (_a = obj.properties) !== null && _a !== void 0 ? _a : {}; + var value = Object.keys(properties) + .filter(function (name) { + var target = properties[name]; + return exports.isRefObject(target) || !target.deprecated; + }) + .map(function (name) { + var _a, _b; + var val = exports.schema2value(properties[name], required); + if (!val) + return null; + return { + name: exports.getPropertyName(name), + required: (_b = (_a = obj.required) === null || _a === void 0 ? void 0 : _a.includes(name)) !== null && _b !== void 0 ? _b : required, + description: val.description, + values: [val] + }; + }) + .filter(function (v) { return v; }); + var additionalProps = obj.additionalProperties; + if (additionalProps) { + var val = additionalProps === true + ? { + isArray: false, + isEnum: false, + nullable: false, + description: null, + value: 'any' + } + : exports.schema2value(additionalProps, required); + if (val) + value.push({ + name: '[key: string]', + required: required, + description: val.description, + values: [val] + }); + } + return value; +}; +exports.BINARY_TYPE = 'File | ReadStream'; +var schema2value = function (schema, required, isResponse) { + var _a, _b; + if (!schema) + return null; + var isArray = false; + var isEnum = false; + var nullable = false; + var hasOf; + var value = null; + var description = null; + if (exports.isRefObject(schema)) { + value = exports.$ref2Type(schema.$ref); + } + else { + nullable = !!schema.nullable; + description = (_a = schema.description) !== null && _a !== void 0 ? _a : null; + if (schema.oneOf || schema.allOf || schema.anyOf) { + hasOf = schema.oneOf ? 'oneOf' : schema.allOf ? 'allOf' : 'anyOf'; + value = of2Values(schema, required); + } + else if (schema.enum) { + isEnum = true; + value = schema.type === 'string' ? schema.enum.map(function (e) { return "'" + e + "'"; }) : schema.enum; + } + else if (isArraySchema(schema)) { + isArray = true; + value = exports.schema2value(schema.items, required); + } + else if (schema.properties || schema.additionalProperties) { + value = object2value(schema, required); + } + else if (schema.format === 'binary') { + value = isResponse ? 'Blob' : exports.BINARY_TYPE; + } + else if (schema.type !== 'object') { + value = { + integer: 'number', + number: 'number', + null: 'null', + string: 'string', + boolean: 'boolean' + }[(_b = schema.type) !== null && _b !== void 0 ? _b : 'string']; + } + } + return value ? { isArray: isArray, isEnum: isEnum, nullable: nullable, hasOf: hasOf, value: value, description: description } : null; +}; +exports.schema2value = schema2value; +//# sourceMappingURL=converters.js.map \ No newline at end of file diff --git a/dist/builderUtils/converters.js.map b/dist/builderUtils/converters.js.map new file mode 100644 index 00000000..726b5472 --- /dev/null +++ b/dist/builderUtils/converters.js.map @@ -0,0 +1 @@ +{"version":3,"file":"converters.js","sourceRoot":"","sources":["../../src/builderUtils/converters.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAGO,IAAM,cAAc,GAAG,UAAC,GAAW;IACxC,OAAA,KAAG,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG;SAC7D,KAAK,CAAC,CAAC,CAAC;SACR,OAAO,CAAC,iBAAiB,EAAE,GAAG,CAAG;AAFpC,CAEoC,CAAA;AAHzB,QAAA,cAAc,kBAGW;AAE/B,IAAM,aAAa,GAAG,UAAC,GAAW;IACjC,IAAA,KAAA,OAA+B,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAA,EAAtC,QAAQ,QAAA,EAAI,QAAQ,QAAkB,CAAA;IACnD,OAAO,EAAE,QAAQ,UAAA,EAAE,QAAQ,EAAE,QAAQ,IAAI,IAAI,EAAE,CAAA;AACjD,CAAC,CAAA;AAHY,QAAA,aAAa,iBAGzB;AAED,8CAA8C;AACvC,IAAM,SAAS,GAAG,UAAC,GAAW;IAC7B,IAAA,KAAyB,qBAAa,CAAC,GAAG,CAAC,EAAzC,QAAQ,cAAA,EAAE,QAAQ,cAAuB,CAAA;IACjD,OAAO,CAAA,WAAS,sBAAc,CAAC,QAAQ,CAAC,IAAG,QAAQ,CAAC,CAAC,CAAC,OAAK,QAAQ,OAAI,CAAC,CAAC,CAAC,EAAE,CAAE,CAAA,CAAC,OAAO,CACpF,QAAQ,EACR,IAAI,CACL,CAAA;AACH,CAAC,CAAA;AANY,QAAA,SAAS,aAMrB;AAEM,IAAM,WAAW,GAAG,UAC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AE,QAAQ,CAAC,CAAA;SAC7C;aAAM,IAAI,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,oBAAoB,EAAE;YAC3D,KAAK,GAAG,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;SACvC;aAAM,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,EAAE;YACrC,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,mBAAW,CAAA;SAC1C;aAAM,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE;YACnC,KAAK,GAAG;gBACN,OAAO,EAAE,QAAQ;gBACjB,MAAM,EAAE,QAAQ;gBAChB,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,QAAQ;gBAChB,OAAO,EAAE,SAAS;aACnB,CAAC,MAAA,MAAM,CAAC,IAAI,mCAAI,QAAQ,CAAC,CAAA;SAC3B;KACF;IAED,OAAO,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,SAAA,EAAE,MAAM,QAAA,EAAE,QAAQ,UAAA,EAAE,KAAK,OAAA,EAAE,KAAK,OAAA,EAAE,WAAW,aAAA,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AAChF,CAAC,CAAA;AA7CY,QAAA,YAAY,gBA6CxB"} \ No newline at end of file diff --git a/dist/builderUtils/getDirName.d.ts b/dist/builderUtils/getDirName.d.ts new file mode 100644 index 00000000..4d25cd0a --- /dev/null +++ b/dist/builderUtils/getDirName.d.ts @@ -0,0 +1,4 @@ +import { OpenAPIV3 } from 'openapi-types'; +declare const _default: (text: string, params: OpenAPIV3.ParameterObject[], required: boolean, replaceLeadingAtMark: string) => string; +export default _default; +//# sourceMappingURL=getDirName.d.ts.map \ No newline at end of file diff --git a/dist/builderUtils/getDirName.d.ts.map b/dist/builderUtils/getDirName.d.ts.map new file mode 100644 index 00000000..d1508063 --- /dev/null +++ b/dist/builderUtils/getDirName.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"getDirName.d.ts","sourceRoot":"","sources":["../../src/builderUtils/getDirName.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;+BAIjC,MAAM,UACJ,UAAU,eAAe,EAAE,YACzB,OAAO,wBACK,MAAM;AAJ9B,wBAsBC"} \ No newline at end of file diff --git a/dist/builderUtils/getDirName.js b/dist/builderUtils/getDirName.js new file mode 100644 index 00000000..36df5471 --- /dev/null +++ b/dist/builderUtils/getDirName.js @@ -0,0 +1,17 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var converters_1 = require("./converters"); +exports.default = (function (text, params, required, replaceLeadingAtMark) { + var _a; + if (text === '*') + return '_any'; + if (!/^{/.test(text)) { + if (replaceLeadingAtMark !== '@' && text.startsWith('@')) + return text.replace('@', replaceLeadingAtMark); + return text; + } + var valName = text.slice(1, -1); + var schemaVal = converters_1.schema2value((_a = params.find(function (p) { return p.in === 'path' && p.name === valName; })) === null || _a === void 0 ? void 0 : _a.schema, required); + return "_" + converters_1.getPropertyName(valName) + (schemaVal && typeof schemaVal.value === 'string' ? "@" + schemaVal.value : ''); +}); +//# sourceMappingURL=getDirName.js.map \ No newline at end of file diff --git a/dist/builderUtils/getDirName.js.map b/dist/builderUtils/getDirName.js.map new file mode 100644 index 00000000..73e43d12 --- /dev/null +++ b/dist/builderUtils/getDirName.js.map @@ -0,0 +1 @@ +{"version":3,"file":"getDirName.js","sourceRoot":"","sources":["../../src/builderUtils/getDirName.ts"],"names":[],"mappings":";;AACA,2CAA4D;AAE5D,mBAAe,UACb,IAAY,EACZ,MAAmC,EACnC,QAAiB,EACjB,oBAA4B;;IAE5B,IAAI,IAAI,KAAK,GAAG;QAAE,OAAO,MAAM,CAAA;IAC/B,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QACpB,IAAI,oBAAoB,KAAK,GAAG,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;YACtD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,oBAAoB,CAAC,CAAA;QAChD,OAAO,IAAI,CAAA;KACZ;IAED,IAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IACjC,IAAM,SAAS,GAAG,yBAAY,CAC5B,MAAA,MAAM,CAAC,IAAI,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,EAAE,KAAK,MAAM,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,EAArC,CAAqC,CAAC,0CAAE,MAAM,EAC/D,QAAQ,CACT,CAAA;IAED,OAAO,MAAI,4BAAe,CAAC,OAAO,CAAC,IACjC,SAAS,IAAI,OAAO,SAAS,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAI,SAAS,CAAC,KAAO,CAAC,CAAC,CAAC,EAAE,CAC7E,CAAA;AACJ,CAAC,EAAA"} \ No newline at end of file diff --git a/dist/builderUtils/parameters2Props.d.ts b/dist/builderUtils/parameters2Props.d.ts new file mode 100644 index 00000000..2704f80c --- /dev/null +++ b/dist/builderUtils/parameters2Props.d.ts @@ -0,0 +1,9 @@ +import { OpenAPIV3 } from 'openapi-types'; +import { Prop } from './props2String'; +export declare type Parameter = { + name: string; + prop: string | Prop; +}; +declare const _default: (params: OpenAPIV3.ComponentsObject['parameters'], openapi: OpenAPIV3.Document, required: boolean) => Parameter[] | undefined; +export default _default; +//# sourceMappingURL=parameters2Props.d.ts.map \ No newline at end of file diff --git a/dist/builderUtils/parameters2Props.d.ts.map b/dist/builderUtils/parameters2Props.d.ts.map new file mode 100644 index 00000000..4658771a --- /dev/null +++ b/dist/builderUtils/parameters2Props.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"parameters2Props.d.ts","sourceRoot":"","sources":["../../src/builderUtils/parameters2Props.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAEzC,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAA;AAGrC,oBAAY,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAA;iCAGnD,UAAU,gBAAgB,CAAC,YAAY,CAAC,WACvC,kBAAkB,YACjB,OAAO;AAHnB,wBA+BuC"} \ No newline at end of file diff --git a/dist/builderUtils/parameters2Props.js b/dist/builderUtils/parameters2Props.js new file mode 100644 index 00000000..d558902d --- /dev/null +++ b/dist/builderUtils/parameters2Props.js @@ -0,0 +1,34 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var converters_1 = require("./converters"); +var resolvers_1 = require("./resolvers"); +exports.default = (function (params, openapi, required) { + return params && + Object.keys(params) + .filter(function (defKey) { + var target = params[defKey]; + return !(converters_1.isRefObject(target) ? resolvers_1.resolveParamsRef(openapi, target.$ref) : target).deprecated; + }) + .map(function (defKey) { + var _a, _b; + var target = params[defKey]; + var prop; + if (converters_1.isRefObject(target)) { + prop = converters_1.$ref2Type(target.$ref); + } + else { + var value = converters_1.schema2value(target.schema, required); + if (!value) + return null; + prop = { + name: converters_1.getPropertyName(target.name), + required: (_a = target.required) !== null && _a !== void 0 ? _a : required, + description: (_b = target.description) !== null && _b !== void 0 ? _b : null, + values: [value] + }; + } + return { name: converters_1.defKey2defName(defKey), prop: prop }; + }) + .filter(function (v) { return !!v; }); +}); +//# sourceMappingURL=parameters2Props.js.map \ No newline at end of file diff --git a/dist/builderUtils/parameters2Props.js.map b/dist/builderUtils/parameters2Props.js.map new file mode 100644 index 00000000..d0f9acd3 --- /dev/null +++ b/dist/builderUtils/parameters2Props.js.map @@ -0,0 +1 @@ +{"version":3,"file":"parameters2Props.js","sourceRoot":"","sources":["../../src/builderUtils/parameters2Props.ts"],"names":[],"mappings":";;AACA,2CAAoG;AAEpG,yCAA8C;AAI9C,mBAAe,UACb,MAAgD,EAChD,OAA2B,EAC3B,QAAiB;IAEjB,OAAA,MAAM;QACN,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;aAChB,MAAM,CAAC,UAAA,MAAM;YACZ,IAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;YAC7B,OAAO,CAAC,CAAC,wBAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,4BAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,UAAU,CAAA;QAC5F,CAAC,CAAC;aACD,GAAG,CAAC,UAAA,MAAM;;YACT,IAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;YAC7B,IAAI,IAAuB,CAAA;YAE3B,IAAI,wBAAW,CAAC,MAAM,CAAC,EAAE;gBACvB,IAAI,GAAG,sBAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;aAC9B;iBAAM;gBACL,IAAM,KAAK,GAAG,yBAAY,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;gBACnD,IAAI,CAAC,KAAK;oBAAE,OAAO,IAAI,CAAA;gBAEvB,IAAI,GAAG;oBACL,IAAI,EAAE,4BAAe,CAAC,MAAM,CAAC,IAAI,CAAC;oBAClC,QAAQ,EAAE,MAAA,MAAM,CAAC,QAAQ,mCAAI,QAAQ;oBACrC,WAAW,EAAE,MAAA,MAAM,CAAC,WAAW,mCAAI,IAAI;oBACvC,MAAM,EAAE,CAAC,KAAK,CAAC;iBAChB,CAAA;aACF;YAED,OAAO,EAAE,IAAI,EAAE,2BAAc,CAAC,MAAM,CAAC,EAAE,IAAI,MAAA,EAAE,CAAA;QAC/C,CAAC,CAAC;aACD,MAAM,CAAC,UAAC,CAAC,IAAqB,OAAA,CAAC,CAAC,CAAC,EAAH,CAAG,CAAC;AA1BrC,CA0BqC,EAAA"} \ No newline at end of file diff --git a/dist/builderUtils/props2String.d.ts b/dist/builderUtils/props2String.d.ts new file mode 100644 index 00000000..f9160f3e --- /dev/null +++ b/dist/builderUtils/props2String.d.ts @@ -0,0 +1,18 @@ +export declare type PropValue = { + isArray: boolean; + isEnum: boolean; + nullable: boolean; + hasOf?: 'oneOf' | 'allOf' | 'anyOf'; + description: string | null; + value: Prop[] | string | string[] | PropValue | PropValue[]; +}; +export declare type Prop = { + name: string; + required: boolean; + values: PropValue[]; + description: string | null; +}; +export declare const value2String: (v: PropValue, indent: string) => string; +export declare const description2Doc: (desc: string | null, indent: string) => string; +export declare const props2String: (props: Prop[], indent: string) => string; +//# sourceMappingURL=props2String.d.ts.map \ No newline at end of file diff --git a/dist/builderUtils/props2String.d.ts.map b/dist/builderUtils/props2String.d.ts.map new file mode 100644 index 00000000..edcefd0d --- /dev/null +++ b/dist/builderUtils/props2String.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"props2String.d.ts","sourceRoot":"","sources":["../../src/builderUtils/props2String.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG;IACtB,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,OAAO,CAAA;IACf,QAAQ,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,CAAA;IACnC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IAE1B,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,GAAG,SAAS,EAAE,CAAA;CAC5D,CAAA;AAED,oBAAY,IAAI,GAAG;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,SAAS,EAAE,CAAA;IACnB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAC3B,CAAA;AAOD,eAAO,MAAM,YAAY,MAAO,SAAS,UAAU,MAAM,KAAG,MAW1B,CAAA;AASlC,eAAO,MAAM,eAAe,SAAU,MAAM,GAAG,IAAI,UAAU,MAAM,WAOlE,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,IAAI,EAAE,UAAU,MAAM,WAY/B,CAAA"} \ No newline at end of file diff --git a/dist/builderUtils/props2String.js b/dist/builderUtils/props2String.js new file mode 100644 index 00000000..2b4ee49b --- /dev/null +++ b/dist/builderUtils/props2String.js @@ -0,0 +1,45 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.props2String = exports.description2Doc = exports.value2String = void 0; +var array2String = function (val, indent) { + var hasMulti = (val.isEnum || val.hasOf) && Array.isArray(val.value) && val.value.length; + return "" + (hasMulti ? '(' : '') + exports.value2String(val, indent) + (hasMulti ? ')' : '') + "[]"; +}; +var value2String = function (v, indent) { + return "" + (v.hasOf + ? values2String(v.value, v.hasOf, indent) + : v.isArray + ? array2String(v.value, indent) + : v.isEnum + ? v.value.join(' | ') + : Array.isArray(v.value) + ? exports.props2String(v.value, " " + indent) + : v.value) + (v.nullable ? ' | null' : ''); +}; +exports.value2String = value2String; +var values2String = function (values, hasOf, indent) { + return "" + (hasOf === 'anyOf' ? 'Partial<' : '') + values + .map(function (a) { return exports.value2String(a, indent); }) + .join(hasOf === 'oneOf' ? ' | ' : ' & ') + (hasOf === 'anyOf' ? '>' : ''); +}; +var isMultiLine = function (values) { return values.find(function (v) { return !v.isEnum && Array.isArray(v.value); }); }; +var description2Doc = function (desc, indent) { + if (!desc) + return ''; + var rows = desc.trim().split('\n'); + return rows.length === 1 + ? indent + "/** " + rows[0] + " */\n" + : indent + "/**\n" + indent + " * " + rows.join("\n" + indent + " * ") + "\n" + indent + " */\n"; +}; +exports.description2Doc = description2Doc; +var props2String = function (props, indent) { + return "{\n" + props + .map(function (p, i) { + return exports.description2Doc(p.description, " " + indent) + " " + indent + p.name + (p.required ? '' : '?') + ": " + values2String(p.values, undefined, indent) + (props.length - 1 === i || isMultiLine(p.values) || isMultiLine(props[i + 1].values) + ? '\n' + : ''); + }) + .join('\n') + indent + "}"; +}; +exports.props2String = props2String; +//# sourceMappingURL=props2String.js.map \ No newline at end of file diff --git a/dist/builderUtils/props2String.js.map b/dist/builderUtils/props2String.js.map new file mode 100644 index 00000000..c1b44bcf --- /dev/null +++ b/dist/builderUtils/props2String.js.map @@ -0,0 +1 @@ +{"version":3,"file":"props2String.js","sourceRoot":"","sources":["../../src/builderUtils/props2String.ts"],"names":[],"mappings":";;;AAiBA,IAAM,YAAY,GAAG,UAAC,GAAc,EAAE,MAAc;IAClD,IAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,MAAM,CAAA;IAC1F,OAAO,MAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAG,oBAAY,CAAC,GAAG,EAAE,MAAM,CAAC,IAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,QAAI,CAAA;AACrF,CAAC,CAAA;AAEM,IAAM,YAAY,GAAG,UAAC,CAAY,EAAE,MAAc;IACvD,OAAA,MACE,CAAC,CAAC,KAAK;QACL,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,KAAoB,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC;QACxD,CAAC,CAAC,CAAC,CAAC,OAAO;YACX,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAkB,EAAE,MAAM,CAAC;YAC5C,CAAC,CAAC,CAAC,CAAC,MAAM;gBACV,CAAC,CAAE,CAAC,CAAC,KAAkB,CAAC,IAAI,CAAC,KAAK,CAAC;gBACnC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;oBACxB,CAAC,CAAC,oBAAY,CAAC,CAAC,CAAC,KAAe,EAAE,OAAK,MAAQ,CAAC;oBAChD,CAAC,CAAC,CAAC,CAAC,KAAK,KACV,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAA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,QAAM,KAAK;SACR,GAAG,CACF,UAAC,CAAC,EAAE,CAAC;QACH,OAAG,uBAAe,CAAC,CAAC,CAAC,WAAW,EAAE,OAAK,MAAQ,CAAC,UAAK,MAAM,GAAG,CAAC,CAAC,IAAI,IAClE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,WAClB,aAAa,CAAC,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,IAC7C,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;YACjF,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,EAAE,CACN;IANF,CAME,CACL;SACA,IAAI,CAAC,IAAI,CAAC,GAAG,MAAM,MAAG;AAXzB,CAWyB,CAAA;AAZd,QAAA,YAAY,gBAYE"} \ No newline at end of file diff --git a/dist/builderUtils/resolvers.d.ts b/dist/builderUtils/resolvers.d.ts new file mode 100644 index 00000000..f9c2a9d8 --- /dev/null +++ b/dist/builderUtils/resolvers.d.ts @@ -0,0 +1,6 @@ +import { OpenAPIV3 } from 'openapi-types'; +export declare const resolveParamsRef: (openapi: OpenAPIV3.Document, ref: string) => OpenAPIV3.ParameterObject; +export declare const resolveSchemasRef: (openapi: OpenAPIV3.Document, ref: string) => OpenAPIV3.SchemaObject; +export declare const resolveResRef: (openapi: OpenAPIV3.Document, ref: string) => OpenAPIV3.ResponseObject; +export declare const resolveReqRef: (openapi: OpenAPIV3.Document, ref: string) => OpenAPIV3.RequestBodyObject; +//# sourceMappingURL=resolvers.d.ts.map \ No newline at end of file diff --git a/dist/builderUtils/resolvers.d.ts.map b/dist/builderUtils/resolvers.d.ts.map new file mode 100644 index 00000000..dbbc43b4 --- /dev/null +++ b/dist/builderUtils/resolvers.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../src/builderUtils/resolvers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAGzC,eAAO,MAAM,gBAAgB,YAClB,kBAAkB,OACtB,MAAM,KACV,UAAU,eAGZ,CAAA;AAED,eAAO,MAAM,iBAAiB,YACnB,kBAAkB,OACtB,MAAM,KACV,UAAU,YAKZ,CAAA;AAED,eAAO,MAAM,aAAa,YACf,kBAAkB,OACtB,MAAM,KACV,UAAU,cAGZ,CAAA;AAED,eAAO,MAAM,aAAa,YACf,kBAAkB,OACtB,MAAM,KACV,UAAU,iBAGZ,CAAA"} \ No newline at end of file diff --git a/dist/builderUtils/resolvers.js b/dist/builderUtils/resolvers.js new file mode 100644 index 00000000..236cdafc --- /dev/null +++ b/dist/builderUtils/resolvers.js @@ -0,0 +1,27 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.resolveReqRef = exports.resolveResRef = exports.resolveSchemasRef = exports.resolveParamsRef = void 0; +var converters_1 = require("./converters"); +var resolveParamsRef = function (openapi, ref) { + var target = openapi.components.parameters[converters_1.$ref2TypeName(ref).typeName]; + return converters_1.isRefObject(target) ? exports.resolveParamsRef(openapi, target.$ref) : target; +}; +exports.resolveParamsRef = resolveParamsRef; +var resolveSchemasRef = function (openapi, ref) { + var _a = converters_1.$ref2TypeName(ref), typeName = _a.typeName, propName = _a.propName; + var target = openapi.components.schemas[typeName]; + target = !converters_1.isRefObject(target) && propName ? target.properties[propName] : target; + return converters_1.isRefObject(target) ? exports.resolveSchemasRef(openapi, target.$ref) : target; +}; +exports.resolveSchemasRef = resolveSchemasRef; +var resolveResRef = function (openapi, ref) { + var target = openapi.components.responses[converters_1.$ref2TypeName(ref).typeName]; + return converters_1.isRefObject(target) ? exports.resolveResRef(openapi, target.$ref) : target; +}; +exports.resolveResRef = resolveResRef; +var resolveReqRef = function (openapi, ref) { + var target = openapi.components.requestBodies[converters_1.$ref2TypeName(ref).typeName]; + return converters_1.isRefObject(target) ? exports.resolveReqRef(openapi, target.$ref) : target; +}; +exports.resolveReqRef = resolveReqRef; +//# sourceMappingURL=resolvers.js.map \ No newline at end of file diff --git a/dist/builderUtils/resolvers.js.map b/dist/builderUtils/resolvers.js.map new file mode 100644 index 00000000..c1220955 --- /dev/null +++ b/dist/builderUtils/resolvers.js.map @@ -0,0 +1 @@ +{"version":3,"file":"resolvers.js","sourceRoot":"","sources":["../../src/builderUtils/resolvers.ts"],"names":[],"mappings":";;;AACA,2CAAyD;AAElD,IAAM,gBAAgB,GAAG,UAC9B,OAA2B,EAC3B,GAAW;IAEX,IAAM,MAAM,GAAG,OAAO,CAAC,UAAW,CAAC,UAAW,CAAC,0BAAa,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC3E,OAAO,wBAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,wBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;AAC9E,CAAC,CAAA;AANY,QAAA,gBAAgB,oBAM5B;AAEM,IAAM,iBAAiB,GAAG,UAC/B,OAA2B,EAC3B,GAAW;IAEL,IAAA,KAAyB,0BAAa,CAAC,GAAG,CAAC,EAAzC,QAAQ,cAAA,EAAE,QAAQ,cAAuB,CAAA;IACjD,IAAI,MAAM,GAAG,OAAO,CAAC,UAAW,CAAC,OAAQ,CAAC,QAAQ,CAAC,CAAA;IACnD,MAAM,GAAG,CAAC,wBAAW,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,UAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;IACjF,OAAO,wBAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,yBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;AAC/E,CAAC,CAAA;AARY,QAAA,iBAAiB,qBAQ7B;AAEM,IAAM,aAAa,GAAG,UAC3B,OAA2B,EAC3B,GAAW;IAEX,IAAM,MAAM,GAAG,OAAO,CAAC,UAAW,CAAC,SAAU,CAAC,0BAAa,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC1E,OAAO,wBAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,qBAAa,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;AAC3E,CAAC,CAAA;AANY,QAAA,aAAa,iBAMzB;AAEM,IAAM,aAAa,GAAG,UAC3B,OAA2B,EAC3B,GAAW;IAEX,IAAM,MAAM,GAAG,OAAO,CAAC,UAAW,CAAC,aAAc,CAAC,0BAAa,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC9E,OAAO,wBAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,qBAAa,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;AAC3E,CAAC,CAAA;AANY,QAAA,aAAa,iBAMzB"} \ No newline at end of file diff --git a/dist/builderUtils/schemas2Props.d.ts b/dist/builderUtils/schemas2Props.d.ts new file mode 100644 index 00000000..21ae93e2 --- /dev/null +++ b/dist/builderUtils/schemas2Props.d.ts @@ -0,0 +1,9 @@ +import { OpenAPIV3 } from 'openapi-types'; +import { PropValue } from './props2String'; +export declare type Schema = { + name: string; + value: PropValue; +}; +declare const _default: (schemas: OpenAPIV3.ComponentsObject['schemas'], openapi: OpenAPIV3.Document, required: boolean) => Schema[] | undefined; +export default _default; +//# sourceMappingURL=schemas2Props.d.ts.map \ No newline at end of file diff --git a/dist/builderUtils/schemas2Props.d.ts.map b/dist/builderUtils/schemas2Props.d.ts.map new file mode 100644 index 00000000..82b29f1e --- /dev/null +++ b/dist/builderUtils/schemas2Props.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"schemas2Props.d.ts","sourceRoot":"","sources":["../../src/builderUtils/schemas2Props.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG1C,oBAAY,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAA;kCAG5C,UAAU,gBAAgB,CAAC,SAAS,CAAC,WACrC,kBAAkB,YACjB,OAAO;AAHnB,wBAeoC"} \ No newline at end of file diff --git a/dist/builderUtils/schemas2Props.js b/dist/builderUtils/schemas2Props.js new file mode 100644 index 00000000..3a5e9e4a --- /dev/null +++ b/dist/builderUtils/schemas2Props.js @@ -0,0 +1,18 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var converters_1 = require("./converters"); +var resolvers_1 = require("./resolvers"); +exports.default = (function (schemas, openapi, required) { + return schemas && + Object.keys(schemas) + .filter(function (defKey) { + var target = schemas[defKey]; + return !(converters_1.isRefObject(target) ? resolvers_1.resolveSchemasRef(openapi, target.$ref) : target).deprecated; + }) + .map(function (defKey) { + var value = converters_1.schema2value(schemas[defKey], required); + return value ? { name: converters_1.defKey2defName(defKey), value: value } : null; + }) + .filter(function (v) { return !!v; }); +}); +//# sourceMappingURL=schemas2Props.js.map \ No newline at end of file diff --git a/dist/builderUtils/schemas2Props.js.map b/dist/builderUtils/schemas2Props.js.map new file mode 100644 index 00000000..e12d7912 --- /dev/null +++ b/dist/builderUtils/schemas2Props.js.map @@ -0,0 +1 @@ +{"version":3,"file":"schemas2Props.js","sourceRoot":"","sources":["../../src/builderUtils/schemas2Props.ts"],"names":[],"mappings":";;AACA,2CAAwE;AAExE,yCAA+C;AAI/C,mBAAe,UACb,OAA8C,EAC9C,OAA2B,EAC3B,QAAiB;IAEjB,OAAA,OAAO;QACP,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;aACjB,MAAM,CAAC,UAAA,MAAM;YACZ,IAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;YAC9B,OAAO,CAAC,CAAC,wBAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,6BAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,UAAU,CAAA;QAC7F,CAAC,CAAC;aACD,GAAG,CAAC,UAAA,MAAM;YACT,IAAM,KAAK,GAAG,yBAAY,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAA;YACrD,OAAO,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,2BAAc,CAAC,MAAM,CAAC,EAAE,KAAK,OAAA,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;QAC/D,CAAC,CAAC;aACD,MAAM,CAAC,UAAC,CAAC,IAAkB,OAAA,CAAC,CAAC,CAAC,EAAH,CAAG,CAAC;AAVlC,CAUkC,EAAA"} \ No newline at end of file diff --git a/dist/cli.d.ts b/dist/cli.d.ts new file mode 100644 index 00000000..794e21cb --- /dev/null +++ b/dist/cli.d.ts @@ -0,0 +1,2 @@ +export declare const run: (args: string[]) => void; +//# sourceMappingURL=cli.d.ts.map \ No newline at end of file diff --git a/dist/cli.d.ts.map b/dist/cli.d.ts.map new file mode 100644 index 00000000..89087be0 --- /dev/null +++ b/dist/cli.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,GAAG,SAAU,MAAM,EAAE,SAWjC,CAAA"} \ No newline at end of file diff --git a/dist/cli.js b/dist/cli.js new file mode 100644 index 00000000..b2888803 --- /dev/null +++ b/dist/cli.js @@ -0,0 +1,21 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.run = void 0; +var minimist_1 = __importDefault(require("minimist")); +var _1 = __importDefault(require(".")); +var run = function (args) { + var argv = minimist_1.default(args, { + string: ['version', 'input', 'config'], + alias: { v: 'version', i: 'input', c: 'config' } + }); + argv.version !== undefined + ? console.log("v" + require('../package.json').version) + : argv.input + ? _1.default({ outputEachDir: true, openapi: { inputFile: argv.input } }) + : _1.default(argv.config); +}; +exports.run = run; +//# sourceMappingURL=cli.js.map \ No newline at end of file diff --git a/dist/cli.js.map b/dist/cli.js.map new file mode 100644 index 00000000..f35c40be --- /dev/null +++ b/dist/cli.js.map @@ -0,0 +1 @@ +{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA+B;AAC/B,uCAAqB;AAEd,IAAM,GAAG,GAAG,UAAC,IAAc;IAChC,IAAM,IAAI,GAAG,kBAAQ,CAAC,IAAI,EAAE;QAC1B,MAAM,EAAE,CAAC,SAAS,EAAE,OAAO,EAAE,QAAQ,CAAC;QACtC,KAAK,EAAE,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE;KACjD,CAAC,CAAA;IAEF,IAAI,CAAC,OAAO,KAAK,SAAS;QACxB,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,MAAI,OAAO,CAAC,iBAAiB,CAAC,CAAC,OAAS,CAAC;QACvD,CAAC,CAAC,IAAI,CAAC,KAAK;YACZ,CAAC,CAAC,UAAK,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;YACpE,CAAC,CAAC,UAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AACxB,CAAC,CAAA;AAXY,QAAA,GAAG,OAWf"} \ No newline at end of file diff --git a/dist/getConfig.d.ts b/dist/getConfig.d.ts new file mode 100644 index 00000000..2fd4ca17 --- /dev/null +++ b/dist/getConfig.d.ts @@ -0,0 +1,32 @@ +import { OpenAPI } from 'openapi-types'; +import { AspidaConfig } from 'aspida/dist/commands'; +export declare type RequiredConfig = { + schema: boolean; + parameter: boolean; + query: boolean; + status: boolean; + resBody: boolean; + resHeader: boolean; + reqBody: boolean; + reqFormat: boolean; + method: boolean; +}; +export declare type Config = Pick & { + input: string | OpenAPI.Document; + output: string; + isYaml: boolean; + requiredConfig: RequiredConfig; + replaceLeadingAtMark: string; +}; +export declare type ConfigFile = AspidaConfig & { + openapi?: { + inputFile: string; + yaml?: boolean; + requiredConfig?: Partial; + replaceLeadingAtMark?: string; + }; +}; +declare type PartialConfig = Partial | Partial[]; +declare const _default: (config?: string | PartialConfig | undefined) => Config[]; +export default _default; +//# sourceMappingURL=getConfig.d.ts.map \ No newline at end of file diff --git a/dist/getConfig.d.ts.map b/dist/getConfig.d.ts.map new file mode 100644 index 00000000..dbbc9c40 --- /dev/null +++ b/dist/getConfig.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"getConfig.d.ts","sourceRoot":"","sources":["../src/getConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,YAAY,EAAc,MAAM,sBAAsB,CAAA;AAE/D,oBAAY,cAAc,GAAG;IAC3B,MAAM,EAAE,OAAO,CAAA;IACf,SAAS,EAAE,OAAO,CAAA;IAClB,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,OAAO,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,SAAS,EAAE,OAAO,CAAA;IAClB,OAAO,EAAE,OAAO,CAAA;IAChB,SAAS,EAAE,OAAO,CAAA;IAClB,MAAM,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,oBAAY,MAAM,GAAG,IAAI,CAAC,YAAY,EAAE,eAAe,GAAG,YAAY,GAAG,eAAe,CAAC,GAAG;IAC1F,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAA;IAChC,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,OAAO,CAAA;IACf,cAAc,EAAE,cAAc,CAAA;IAC9B,oBAAoB,EAAE,MAAM,CAAA;CAC7B,CAAA;AAED,oBAAY,UAAU,GAAG,YAAY,GAAG;IACtC,OAAO,CAAC,EAAE;QACR,SAAS,EAAE,MAAM,CAAA;QACjB,IAAI,CAAC,EAAE,OAAO,CAAA;QACd,cAAc,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;QACxC,oBAAoB,CAAC,EAAE,MAAM,CAAA;KAC9B,CAAA;CACF,CAAA;AA4BD,aAAK,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,EAAE,CAAA;yEAEd,MAAM,EAAE;AAA1D,wBAG8B"} \ No newline at end of file diff --git a/dist/getConfig.js b/dist/getConfig.js new file mode 100644 index 00000000..3706be62 --- /dev/null +++ b/dist/getConfig.js @@ -0,0 +1,45 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var commands_1 = require("aspida/dist/commands"); +var defaultRequired = { + schema: true, + parameter: true, + query: true, + status: true, + resBody: true, + resHeader: true, + reqBody: true, + reqFormat: true, + method: true +}; +var createConfig = function (config) { + var _a, _b; + var openapi = config.openapi; + return { + input: openapi.inputFile, + output: config.input, + trailingSlash: config.trailingSlash, + outputEachDir: config.outputEachDir, + outputMode: config.outputMode, + isYaml: (_a = openapi.yaml) !== null && _a !== void 0 ? _a : !openapi.inputFile.endsWith('.json'), + requiredConfig: __assign(__assign({}, defaultRequired), openapi.requiredConfig), + replaceLeadingAtMark: (_b = openapi.replaceLeadingAtMark) !== null && _b !== void 0 ? _b : '@' + }; +}; +exports.default = (function (config) { + return commands_1.getConfigs(config) + .filter(function (c) { return c.openapi; }) + .map(function (c) { return createConfig(c); }); +}); +//# sourceMappingURL=getConfig.js.map \ No newline at end of file diff --git a/dist/getConfig.js.map b/dist/getConfig.js.map new file mode 100644 index 00000000..76ba54eb --- /dev/null +++ b/dist/getConfig.js.map @@ -0,0 +1 @@ +{"version":3,"file":"getConfig.js","sourceRoot":"","sources":["../src/getConfig.ts"],"names":[],"mappings":";;;;;;;;;;;;;AACA,iDAA+D;AA+B/D,IAAM,eAAe,GAAG;IACtB,MAAM,EAAE,IAAI;IACZ,SAAS,EAAE,IAAI;IACf,KAAK,EAAE,IAAI;IACX,MAAM,EAAE,IAAI;IACZ,OAAO,EAAE,IAAI;IACb,SAAS,EAAE,IAAI;IACf,OAAO,EAAE,IAAI;IACb,SAAS,EAAE,IAAI;IACf,MAAM,EAAE,IAAI;CACb,CAAA;AAED,IAAM,YAAY,GAAG,UAAC,MAAkB;;IACtC,IAAM,OAAO,GAAG,MAAM,CAAC,OAAQ,CAAA;IAC/B,OAAO;QACL,KAAK,EAAE,OAAO,CAAC,SAAS;QACxB,MAAM,EAAE,MAAM,CAAC,KAAK;QACpB,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,MAAM,EAAE,MAAA,OAAO,CAAC,IAAI,mCAAI,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC;QAC5D,cAAc,wBAAO,eAAe,GAAK,OAAO,CAAC,cAAc,CAAE;QACjE,oBAAoB,EAAE,MAAA,OAAO,CAAC,oBAAoB,mCAAI,GAAG;KAC1D,CAAA;AACH,CAAC,CAAA;AAID,mBAAe,UAAC,MAA+B;IAC7C,OAAA,qBAAU,CAAC,MAAM,CAAC;SACf,MAAM,CAAC,UAAC,CAAa,IAAK,OAAA,CAAC,CAAC,OAAO,EAAT,CAAS,CAAC;SACpC,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,YAAY,CAAC,CAAC,CAAC,EAAf,CAAe,CAAC;AAF5B,CAE4B,EAAA"} \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 00000000..3154fd04 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,4 @@ +import getConfig from './getConfig'; +declare const _default: (configs?: Parameters[0]) => Promise[]; +export default _default; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map new file mode 100644 index 00000000..119b0a81 --- /dev/null +++ b/dist/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,aAAa,CAAA;mCAIT,WAAW,gBAAgB,CAAC,CAAC,CAAC,CAAC;AAAzD,wBAwBI"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 00000000..e560c311 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,78 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var fs_1 = __importDefault(require("fs")); +var getConfig_1 = __importDefault(require("./getConfig")); +var buildTemplate_1 = __importDefault(require("./buildTemplate")); +var writeRouteFile_1 = __importDefault(require("./writeRouteFile")); +exports.default = (function (configs) { + return getConfig_1.default(configs).map(function (config) { return __awaiter(void 0, void 0, void 0, function () { + var _a, baseURL, types, files; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!fs_1.default.existsSync(config.output)) { + fs_1.default.mkdirSync(config.output); + } + else if (fs_1.default.readdirSync(config.output).length) { + console.log("fatal: destination path '" + config.output + "' already exists and is not an empty directory."); + return [2 /*return*/]; + } + return [4 /*yield*/, buildTemplate_1.default(config)]; + case 1: + _a = _b.sent(), baseURL = _a.baseURL, types = _a.types, files = _a.files; + writeRouteFile_1.default({ + config: { + input: config.output, + baseURL: baseURL, + outputMode: config.outputMode, + outputEachDir: config.outputEachDir, + trailingSlash: config.trailingSlash + }, + types: types, + files: files + }); + return [2 /*return*/]; + } + }); + }); }); +}); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 00000000..76dd445f --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAmB;AACnB,0DAAmC;AACnC,kEAA2C;AAC3C,oEAA6C;AAE7C,mBAAe,UAAC,OAAyC;IACvD,OAAA,mBAAS,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,UAAM,MAAM;;;;;oBACjC,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;wBACjC,YAAE,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;qBAC5B;yBAAM,IAAI,YAAE,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE;wBAC/C,OAAO,CAAC,GAAG,CACT,8BAA4B,MAAM,CAAC,MAAM,oDAAiD,CAC3F,CAAA;wBACD,sBAAM;qBACP;oBAEiC,qBAAM,uBAAa,CAAC,MAAM,CAAC,EAAA;;oBAAvD,KAA4B,SAA2B,EAArD,OAAO,aAAA,EAAE,KAAK,WAAA,EAAE,KAAK,WAAA;oBAE7B,wBAAc,CAAC;wBACb,MAAM,EAAE;4BACN,KAAK,EAAE,MAAM,CAAC,MAAM;4BACpB,OAAO,SAAA;4BACP,UAAU,EAAE,MAAM,CAAC,UAAU;4BAC7B,aAAa,EAAE,MAAM,CAAC,aAAa;4BACnC,aAAa,EAAE,MAAM,CAAC,aAAa;yBACpC;wBACD,KAAK,OAAA;wBACL,KAAK,OAAA;qBACN,CAAC,CAAA;;;;SACH,CAAC;AAvBF,CAuBE,EAAA"} \ No newline at end of file diff --git a/dist/resolveExternalRefs.d.ts b/dist/resolveExternalRefs.d.ts new file mode 100644 index 00000000..1c93712b --- /dev/null +++ b/dist/resolveExternalRefs.d.ts @@ -0,0 +1,4 @@ +import { OpenAPIV3 } from 'openapi-types'; +declare const _default: (docs: OpenAPIV3.Document, inputDir: string) => Promise; +export default _default; +//# sourceMappingURL=resolveExternalRefs.d.ts.map \ No newline at end of file diff --git a/dist/resolveExternalRefs.d.ts.map b/dist/resolveExternalRefs.d.ts.map new file mode 100644 index 00000000..74cfd88b --- /dev/null +++ b/dist/resolveExternalRefs.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"resolveExternalRefs.d.ts","sourceRoot":"","sources":["../src/resolveExternalRefs.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;+BA4Fb,kBAAkB,YAAY,MAAM,KAAG,QAAQ,kBAAkB,CAAC;AAA9F,wBAyBC"} \ No newline at end of file diff --git a/dist/resolveExternalRefs.js b/dist/resolveExternalRefs.js new file mode 100644 index 00000000..a24559c4 --- /dev/null +++ b/dist/resolveExternalRefs.js @@ -0,0 +1,209 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __read = (this && this.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var https_1 = __importDefault(require("https")); +var http_1 = __importDefault(require("http")); +var fs_1 = __importDefault(require("fs")); +var path_1 = __importDefault(require("path")); +var js_yaml_1 = __importDefault(require("js-yaml")); +var getText = function (url) { + return new Promise(function (resolve) { + ; + (url.startsWith('https') ? https_1.default : http_1.default) + .get(url, function (res) { + var body = ''; + res.setEncoding('utf8'); + res.on('data', function (chunk) { + body += chunk; + }); + res.on('end', function () { + resolve(body); + }); + }) + .on('error', function (e) { + console.log("Could not get: " + url + "\n", e); + }); + }); +}; +var hasExternalRegExp = /"\$ref":"[^#].+?"/g; +var fetchExternalDocs = function (docs, inputDir) { return __awaiter(void 0, void 0, void 0, function () { + var docList, fetchingUrls, fetchDocs; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + docList = []; + fetchingUrls = []; + fetchDocs = function (d, input) { + var _a; + return Promise.all(((_a = JSON.stringify(d).match(hasExternalRegExp)) !== null && _a !== void 0 ? _a : []).map(function (ref) { return __awaiter(void 0, void 0, void 0, function () { + var _a, url, filePath, text, doc; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + _a = __read(ref.match(/"\$ref":"(.+?)[#"]/), 2), url = _a[1]; + if (fetchingUrls.includes(url)) + return [2 /*return*/]; + fetchingUrls.push(url); + filePath = url.startsWith('http') ? url : path_1.default.posix.join(path_1.default.dirname(input), url); + return [4 /*yield*/, (filePath.startsWith('http') + ? getText(filePath) + : fs_1.default.promises.readFile(filePath, 'utf8'))]; + case 1: + text = _b.sent(); + doc = filePath.endsWith('.json') ? JSON.parse(text) : js_yaml_1.default.load(text); + docList[fetchingUrls.indexOf(url)] = { url: url, doc: doc }; + return [4 /*yield*/, fetchDocs(doc, filePath)]; + case 2: + _b.sent(); + return [2 /*return*/]; + } + }); + }); })); + }; + return [4 /*yield*/, fetchDocs(docs, inputDir)]; + case 1: + _a.sent(); + return [2 /*return*/, docList]; + } + }); +}); }; +var getComponentInfo = function (docList, url, prop) { + var data = docList.find(function (d) { return d.url === url; }).doc; + var target = prop ? prop.split('/').reduce(function (prev, current) { return prev[current]; }, data) : data; + if (target.name) + return { type: 'parameters', data: target }; + return { type: 'schemas', data: target }; +}; +var genExternalTypeName = function (docList, url, prop) { + return "External" + docList.findIndex(function (d) { return d.url === url; }) + (prop ? "_" + prop.split('/').pop() : ''); +}; +var resolveExternalDocs = function (docs, inputDir) { return __awaiter(void 0, void 0, void 0, function () { + var externalDocs, componentsInfoList, replacedExternalDocs; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, fetchExternalDocs(docs, inputDir)]; + case 1: + externalDocs = _a.sent(); + componentsInfoList = []; + replacedExternalDocs = externalDocs.map(function (selfDoc) { + var _a; + var docsString = JSON.stringify(selfDoc.doc); + ((_a = docsString.match(/"\$ref":".+?"/g)) !== null && _a !== void 0 ? _a : []).forEach(function (refs) { + var targetText = refs.replace('"$ref":"', '').slice(0, -1); + var _a = __read(targetText.split('#'), 2), urlBase = _a[0], _b = _a[1], propBase = _b === void 0 ? '/' : _b; + var url = urlBase || selfDoc.url; + var prop = propBase.slice(1); + var info = getComponentInfo(externalDocs, url, prop); + var name = genExternalTypeName(externalDocs, url, prop); + docsString = docsString.replace(targetText, "#/components/" + info.type + "/" + name); + componentsInfoList.push({ url: url, prop: prop, name: name }); + }); + return { url: selfDoc.url, doc: JSON.parse(docsString) }; + }); + return [2 /*return*/, { + externalDocs: replacedExternalDocs, + components: componentsInfoList.reduce(function (prev, _a) { + var _b, _c; + var url = _a.url, prop = _a.prop, name = _a.name; + var info = getComponentInfo(replacedExternalDocs, url, prop); + return __assign(__assign({}, prev), (_b = {}, _b[info.type] = __assign(__assign({}, prev[info.type]), (_c = {}, _c[name] = info.data, _c)), _b)); + }, {}) + }]; + } + }); +}); }; +exports.default = (function (docs, inputDir) { return __awaiter(void 0, void 0, void 0, function () { + var _a, externalDocs, components, docsString, resolved; + var _b; + return __generator(this, function (_c) { + switch (_c.label) { + case 0: return [4 /*yield*/, resolveExternalDocs(docs, inputDir)]; + case 1: + _a = _c.sent(), externalDocs = _a.externalDocs, components = _a.components; + docsString = JSON.stringify(docs); + ((_b = docsString.match(hasExternalRegExp)) !== null && _b !== void 0 ? _b : []).forEach(function (refs) { + var _a; + var targetText = refs.replace('"$ref":"', '').slice(0, -1); + var _b = __read(targetText.split('#'), 2), url = _b[0], _c = _b[1], propBase = _c === void 0 ? '/' : _c; + var prop = propBase.slice(1); + var info = getComponentInfo(externalDocs, url, prop); + components[info.type] = components[info.type] || {}; + var name = genExternalTypeName(externalDocs, url, prop); + Object.assign(components[info.type], (_a = {}, _a[name] = info.data, _a)); + docsString = docsString.replace(targetText, "#/components/" + info.type + "/" + name); + }); + resolved = JSON.parse(docsString); + resolved.components = resolved.components || {}; + Object.keys(components).forEach(function (key) { + resolved.components[key] = __assign(__assign({}, resolved.components[key]), components[key]); + }); + return [2 /*return*/, resolved]; + } + }); +}); }); +//# sourceMappingURL=resolveExternalRefs.js.map \ No newline at end of file diff --git a/dist/resolveExternalRefs.js.map b/dist/resolveExternalRefs.js.map new file mode 100644 index 00000000..bcb3ba6d --- /dev/null +++ b/dist/resolveExternalRefs.js.map @@ -0,0 +1 @@ +{"version":3,"file":"resolveExternalRefs.js","sourceRoot":"","sources":["../src/resolveExternalRefs.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAyB;AACzB,8CAAuB;AACvB,0CAAmB;AACnB,8CAAuB;AACvB,oDAA0B;AAG1B,IAAM,OAAO,GAAG,UAAC,GAAW;IAC1B,OAAA,IAAI,OAAO,CAAS,UAAA,OAAO;QACzB,CAAC;QAAA,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,eAAK,CAAC,CAAC,CAAC,cAAI,CAAC;aACtC,GAAG,CAAC,GAAG,EAAE,UAAA,GAAG;YACX,IAAI,IAAI,GAAG,EAAE,CAAA;YACb,GAAG,C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QAAQ,CAAC,UAAW,CAAC,GAAG,CAAC,yBACpB,QAAQ,CAAC,UAAW,CAAC,GAAG,CAAC,GACzB,UAAU,CAAC,GAAG,CAAC,CACnB,CAAA;gBACH,CAAC,CAAC,CAAA;gBAEF,sBAAO,QAAQ,EAAA;;;KAChB,EAAA"} \ No newline at end of file diff --git a/dist/writeRouteFile.d.ts b/dist/writeRouteFile.d.ts new file mode 100644 index 00000000..bb1beb2b --- /dev/null +++ b/dist/writeRouteFile.d.ts @@ -0,0 +1,11 @@ +import { AspidaConfig } from 'aspida/dist/commands'; +declare const _default: ({ config, types, files }: { + config: AspidaConfig; + types: string | null; + files: { + file: string[]; + methods: string; + }[]; +}) => void; +export default _default; +//# sourceMappingURL=writeRouteFile.d.ts.map \ No newline at end of file diff --git a/dist/writeRouteFile.d.ts.map b/dist/writeRouteFile.d.ts.map new file mode 100644 index 00000000..63ae1664 --- /dev/null +++ b/dist/writeRouteFile.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"writeRouteFile.d.ts","sourceRoot":"","sources":["../src/writeRouteFile.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,YAAY,EAAE,MAAM,sBAAsB,CAAA;;YAOhD,YAAY;WACb,MAAM,GAAG,IAAI;WACb;QACL,IAAI,EAAE,MAAM,EAAE,CAAA;QACd,OAAO,EAAE,MAAM,CAAA;KAChB,EAAE;;AAVL,wBA8BC"} \ No newline at end of file diff --git a/dist/writeRouteFile.js b/dist/writeRouteFile.js new file mode 100644 index 00000000..3bbb9f3a --- /dev/null +++ b/dist/writeRouteFile.js @@ -0,0 +1,26 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var fs_1 = __importDefault(require("fs")); +var commands_1 = require("aspida/dist/commands"); +exports.default = (function (_a) { + var config = _a.config, types = _a.types, files = _a.files; + if (types) { + fs_1.default.mkdirSync(config.input + "/@types"); + fs_1.default.writeFileSync(config.input + "/@types/index.ts", types, 'utf8'); + } + files.forEach(function (p) { + var fileName = p.file.pop(); + p.file.forEach(function (_d, i, dirList) { + var dirPath = config.input + "/" + dirList.slice(0, i + 1).join('/'); + if (!fs_1.default.existsSync(dirPath)) { + fs_1.default.mkdirSync(dirPath); + } + }); + fs_1.default.writeFileSync(config.input + "/" + p.file.join('/') + "/" + fileName + ".ts", p.methods, 'utf8'); + }); + commands_1.build(config); +}); +//# sourceMappingURL=writeRouteFile.js.map \ No newline at end of file diff --git a/dist/writeRouteFile.js.map b/dist/writeRouteFile.js.map new file mode 100644 index 00000000..2c8faadf --- /dev/null +++ b/dist/writeRouteFile.js.map @@ -0,0 +1 @@ +{"version":3,"file":"writeRouteFile.js","sourceRoot":"","sources":["../src/writeRouteFile.ts"],"names":[],"mappings":";;;;;AAAA,0CAAmB;AACnB,iDAA0D;AAE1D,mBAAe,UAAC,EAWf;QAVC,MAAM,YAAA,EACN,KAAK,WAAA,EACL,KAAK,WAAA;IASL,IAAI,KAAK,EAAE;QACT,YAAE,CAAC,SAAS,CAAI,MAAM,CAAC,KAAK,YAAS,CAAC,CAAA;QACtC,YAAE,CAAC,aAAa,CAAI,MAAM,CAAC,KAAK,qBAAkB,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;KACnE;IAED,KAAK,CAAC,OAAO,CAAC,UAAA,CAAC;QACb,IAAM,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;QAC7B,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,UAAC,EAAE,EAAE,CAAC,EAAE,OAAO;YAC5B,IAAM,OAAO,GAAM,MAAM,CAAC,KAAK,SAAI,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAG,CAAA;YACtE,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;gBAC3B,YAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;aACtB;QACH,CAAC,CAAC,CAAA;QAEF,YAAE,CAAC,aAAa,CAAI,MAAM,CAAC,KAAK,SAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAI,QAAQ,QAAK,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAC3F,CAAC,CAAC,CAAA;IAEF,gBAAK,CAAC,MAAM,CAAC,CAAA;AACf,CAAC,EAAA"} \ No newline at end of file diff --git a/package.json b/package.json index 176a3e62..35e5ec4e 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,7 @@ "lint": "eslint --ext .js,.ts .", "lint:fix": "npm run lint -- --fix", "test": "jest", - "typecheck": "tsc --noEmit", - "postinstall": "npm run build" + "typecheck": "tsc --noEmit" }, "homepage": "/~https://github.com/aspida/openapi2aspida#readme", "repository": { From 00249b11752a1219b7c31b5ccf92490d587557eb Mon Sep 17 00:00:00 2001 From: toshiaki saeki Date: Thu, 3 Jun 2021 18:19:10 +0900 Subject: [PATCH 7/7] adds additional properties --- samples/default-required.yml | 8 ++++++++ samples/default-required/@types/index.ts | 4 ++++ src/buildV3.ts | 1 + 3 files changed, 13 insertions(+) diff --git a/samples/default-required.yml b/samples/default-required.yml index 0427e179..2b44bfee 100644 --- a/samples/default-required.yml +++ b/samples/default-required.yml @@ -145,3 +145,11 @@ components: - 1 - 2 - 3 + parameters: + type: object + additionalProperties: + oneOf: + - type: string + - type: number + - type: boolean + - type: object diff --git a/samples/default-required/@types/index.ts b/samples/default-required/@types/index.ts index b0fd9f54..e9397f3d 100644 --- a/samples/default-required/@types/index.ts +++ b/samples/default-required/@types/index.ts @@ -7,4 +7,8 @@ export type Customer = { name?: string /** Type of pet 1:dog 2:cat 3:other */ pet?: 1 | 2 | 3 + + parameters?: { + [key: string]: string | number | boolean + } } diff --git a/src/buildV3.ts b/src/buildV3.ts index c67a07ba..551efefd 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -382,6 +382,7 @@ export default (openapi: OpenAPIV3.Document, requiredConfig: RequiredConfig) => .map(p => `\n${description2Doc(p.description, '')}export type ${p.name} = ${p.text}\n`) .join('') .replace(/ Types\./g, ' ') + .replace(/\]\?:/g, ']:') : null return {