From 5f04c1041f7559bc936180da7a0e7ed63859cd1d Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 17 May 2025 10:09:50 +0900 Subject: [PATCH 1/2] refactor: replace `GenerateApiParams` with `GenerateApiConfiguration` config Signed-off-by: Sora Morimoto --- index.ts | 9 +- types/index.ts | 431 +++++++++++++++++++------------------------------ 2 files changed, 170 insertions(+), 270 deletions(-) diff --git a/index.ts b/index.ts index 5a33efc0..cd0097a4 100644 --- a/index.ts +++ b/index.ts @@ -8,7 +8,10 @@ import { TemplatesGenConfig } from "./src/commands/generate-templates/configurat import { CodeGenConfig } from "./src/configuration.js"; import { HTTP_CLIENT } from "./src/constants.js"; import { generateApi, generateTemplates } from "./src/index.js"; -import type { GenerateApiParams, HttpClientType } from "./types/index.js"; +import type { + GenerateApiConfiguration, + HttpClientType, +} from "./types/index.js"; const templateGenBaseConfig = new TemplatesGenConfig({}); @@ -287,7 +290,9 @@ const generateCommand = defineCommand({ }, }, run: async ({ args }) => { - const customConfig = await loadConfig({ + const customConfig = await loadConfig< + Partial + >({ name: "swagger-typescript-api", configFile: args["custom-config"], }); diff --git a/types/index.ts b/types/index.ts index 42736d93..6fbac314 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,234 +1,9 @@ import type { ComponentTypeNameResolver } from "../src/component-type-name-resolver.js"; import type { HTTP_CLIENT } from "../src/constants.js"; -import type { MonoSchemaParser } from "../src/schema-parser/mono-schema-parser.js"; import type { Translator } from "../src/translators/translator.js"; export type HttpClientType = (typeof HTTP_CLIENT)[keyof typeof HTTP_CLIENT]; -interface GenerateApiParamsBase { - /** - * default 'api.ts' - */ - fileName?: string; - - /** - * name of the main exported class - */ - apiClassName?: string; - - /** - * path to folder where will be located the created api module. - * - * may set to `false` to skip writing content to disk. in this case, - * you may access the `files` on the return value. - */ - output?: string | false; - - /** - * path to folder containing templates (default: ./src/templates) - */ - templates?: string; - - /** - * generate all "enum" types as union types (T1 | T2 | TN) (default: false) - */ - generateUnionEnums?: boolean; - - /** - * generate type definitions for API routes (default: false) - */ - generateRouteTypes?: boolean; - - /** - * do not generate an API class - */ - generateClient?: boolean; - /** - * generated http client type - */ - httpClientType?: HttpClientType; - /** - * use "default" response status code as success response too. - * some swagger schemas use "default" response status code as success response type by default. - */ - defaultResponseAsSuccess?: boolean; - - /** - * generate additional information about request responses - * also add typings for bad responses - */ - generateResponses?: boolean; - - /** - * unwrap the data item from the response - */ - unwrapResponseData?: boolean; - - /** - * sort data contracts in alphabetical order - */ - sortTypes?: boolean; - - /** - * sort routes in alphabetical order - */ - sortRoutes?: boolean; - - /** - * generate js api module with declaration file (default: false) - */ - toJS?: boolean; - - /** - * determines which path index should be used for routes separation - */ - moduleNameIndex?: number; - /** - * users operation's first tag for route separation - */ - moduleNameFirstTag?: boolean; - /** - * generate separated files for http client, data contracts, and routes (default: false) - */ - modular?: boolean; - /** - * extract request params to data contract (Also combine path params and query params into one object) - */ - extractRequestParams?: boolean; - /** - * extract request body type to data contract - */ - extractRequestBody?: boolean; - /** - * extract response body type to data contract - */ - extractResponseBody?: boolean; - /** - * extract response error type to data contract - */ - extractResponseError?: boolean; - /** - * Output only errors to console (default: false) - */ - silent?: boolean; - /** - * default type for empty response schema (default: "void") - */ - defaultResponseType?: string; - /** - * Ability to send HttpClient instance to Api constructor - */ - singleHttpClient?: boolean; - cleanOutput?: boolean; - enumNamesAsValues?: boolean; - - hooks?: Partial; - /** - * extra templates - */ - extraTemplates?: { name: string; path: string }[]; - - /** - * fix up small errors in the swagger source definition - */ - patch?: boolean; - /** - * authorization token - */ - authorizationToken?: string; - /** - * generate readonly properties (default: false) - */ - addReadonly?: boolean; - - primitiveTypeConstructs?: ( - struct: PrimitiveTypeStruct, - ) => Partial; - - codeGenConstructs?: (struct: CodeGenConstruct) => Partial; - - /** extract all enums from nested types\interfaces to `enum` construction */ - extractEnums?: boolean; - - /** prefix string value needed to fix invalid type names (default: 'Type') */ - fixInvalidTypeNamePrefix?: string; - - /** prefix string value needed to fix invalid enum keys (default: 'Value') */ - fixInvalidEnumKeyPrefix?: string; - - /** prefix string value for enum keys */ - enumKeyPrefix?: string; - - /** suffix string value for enum keys */ - enumKeySuffix?: string; - - /** prefix string value for type names */ - typePrefix?: string; - - /** suffix string value for type names */ - typeSuffix?: string; - - /** extra configuration for extracting type names operations */ - extractingOptions?: ExtractingOptions; - - /** configuration for fetching swagger schema requests */ - requestOptions?: Partial; - - /** ts compiler configuration object (for --to-js option) */ - compilerTsConfig?: Record; - - /** extract all enums from inline interface\\type content to typescript enum construction */ - extractResponses?: boolean; - - /** generate array types as Array (by default Type[]) */ - anotherArrayType?: boolean; - - /** - * custom ts->* translator - * do not use constructor args, it can break functionality of this property, just send class reference - * - * @example - * ```ts - * import { Translator } from "swagger-typescript-api/src/translators/translator"; - * - * class MyTranslator extends Translator { - * - * translate({ fileName, fileExtension, fileContent }) { - * this.codeFormatter.format() - * this.config. - * - * return [ - * { - * fileName, - * fileExtension, - * fileContent, - * } - * ] - * } - * } - * ``` - */ - customTranslator?: new () => Translator; - /** fallback name for enum key resolver */ - enumKeyResolverName?: string; - /** fallback name for type name resolver */ - typeNameResolverName?: string; - /** fallback name for specific arg name resolver */ - specificArgNameResolverName?: string; - schemaParsers?: { - complexOneOf?: MonoSchemaParser; - complexAllOf?: MonoSchemaParser; - complexAnyOf?: MonoSchemaParser; - complexNot?: MonoSchemaParser; - enum?: MonoSchemaParser; - object?: MonoSchemaParser; - complex?: MonoSchemaParser; - primitive?: MonoSchemaParser; - discriminator?: MonoSchemaParser; - array?: MonoSchemaParser; - }; -} - type CodeGenConstruct = { Keyword: { Number: string; @@ -271,48 +46,6 @@ type CodeGenConstruct = { TypeWithGeneric: (content: unknown) => string; }; -type PrimitiveTypeStructValue = - | string - | (( - schema: Record, - parser: import("../src/schema-parser/schema-parser.js").SchemaParser, - ) => string); - -type PrimitiveTypeStruct = Record< - "integer" | "number" | "boolean" | "object" | "file" | "string" | "array", - | string - | ({ $default: PrimitiveTypeStructValue } & Record< - string, - PrimitiveTypeStructValue - >) ->; - -interface GenerateApiParamsFromPath extends GenerateApiParamsBase { - /** - * path to swagger schema - */ - input: string; -} - -interface GenerateApiParamsFromUrl extends GenerateApiParamsBase { - /** - * url to swagger schema - */ - url: string; -} - -interface GenerateApiParamsFromSpecLiteral extends GenerateApiParamsBase { - /** - * swagger schema JSON - */ - spec: import("swagger-schema-official").Spec; -} - -export type GenerateApiParams = - | GenerateApiParamsFromPath - | GenerateApiParamsFromUrl - | GenerateApiParamsFromSpecLiteral; - type BuildRouteParam = { /** {bar} */ $match: string; @@ -659,10 +392,28 @@ export interface GenerateApiConfiguration { hasDescription: boolean; }; config: { + /** + * path to swagger schema + */ input: string; + /** + * path to folder where will be located the created api module. + * + * may set to `false` to skip writing content to disk. in this case, + * you may access the `files` on the return value. + */ output: string | false; + /** + * url to swagger schema + */ url: string; + /** + * swagger schema JSON + */ spec: unknown; + /** + * default 'api.ts' + */ fileName: string; templatePaths: { /** `templates/base` */ @@ -676,52 +427,182 @@ export interface GenerateApiConfiguration { /** custom path to templates (`--templates`) */ custom: string | null; }; + /** + * authorization token + */ authorizationToken?: string; + /** + * generate additional information about request responses + * also add typings for bad responses + */ generateResponses: boolean; + /** + * use "default" response status code as success response too. + * some swagger schemas use "default" response status code as success response type by default. + */ defaultResponseAsSuccess: boolean; + /** + * generate type definitions for API routes (default: false) + */ generateRouteTypes: boolean; + /** + * do not generate an API class + */ generateClient: boolean; + /** + * generate all "enum" types as union types (T1 | T2 | TN) (default: false) + */ generateUnionEnums: boolean; swaggerSchema: object; originalSchema: object; componentsMap: Record; convertedFromSwagger2: boolean; + /** + * determines which path index should be used for routes separation + */ moduleNameIndex: number; + /** + * users operation's first tag for route separation + */ moduleNameFirstTag: boolean; + /** + * extra templates + */ extraTemplates: { name: string; path: string }[]; + /** + * extract request params to data contract (Also combine path params and query params into one object) + */ extractRequestParams: boolean; + /** + * unwrap the data item from the response + */ unwrapResponseData: boolean; + /** + * sort data contracts in alphabetical order + */ sortTypes: boolean; + /** + * sort routes in alphabetical order + */ sortRoutes: boolean; + /** + * Ability to send HttpClient instance to Api constructor + */ singleHttpClient: boolean; + /** + * generate separated files for http client, data contracts, and routes + */ + modular?: boolean; + /** + * prefix string value for type names + */ typePrefix: string; + /** + * suffix string value for type names + */ typeSuffix: string; + /** + * prefix string value for enum keys + */ enumKeyPrefix: string; + /** + * suffix string value for enum keys + */ enumKeySuffix: string; + /** + * fix up small errors in the swagger source definition + */ patch: boolean; cleanOutput: boolean; debug: boolean; + /** + * generate array types as Array (by default Type[]) + */ anotherArrayType: boolean; + /** + * extract request body type to data contract + */ extractRequestBody: boolean; + /** + * generated http client type + */ httpClientType: "axios" | "fetch"; + /** + * generate readonly properties (default: false) + */ addReadonly: boolean; + /** + * extract response body type to data contract + */ extractResponseBody: boolean; + /** + * extract response error type to data contract + */ extractResponseError: boolean; + /** + * extract all enums from nested types\interfaces to `enum` construction + */ extractEnums: boolean; + /** + * extract all enums from inline interface\\type content to typescript enum construction + */ extractResponses: boolean; + /** + * prefix string value needed to fix invalid type names (default: 'Type') + */ fixInvalidTypeNamePrefix: string; + /** + * prefix string value needed to fix invalid enum keys (default: 'Value') + */ fixInvalidEnumKeyPrefix: string; + /** + * default type for empty response schema (default: "void") + */ defaultResponseType: string; + /** + * generate js api module with declaration file (default: false) + */ toJS: boolean; disableThrowOnError: boolean; + /** + * Output only errors to console (default: false) + */ silent: boolean; hooks: Partial; enumNamesAsValues: boolean; version: string; + /** + * ts compiler configuration object (for --to-js option) + */ compilerTsConfig: Record; enumKeyResolverName: string; typeNameResolverName: string; specificArgNameResolverName: string; + /** + * custom ts->* translator + * do not use constructor args, it can break functionality of this property, just send class reference + * + * @example + * ```ts + * import { Translator } from "swagger-typescript-api/src/translators/translator"; + * + * class MyTranslator extends Translator { + * + * translate({ fileName, fileExtension, fileContent }) { + * this.codeFormatter.format() + * this.config. + * + * return [ + * { + * fileName, + * fileExtension, + * fileContent, + * } + * ] + * } + * } + * ``` + */ customTranslator?: new () => Translator; internalTemplateOptions: { addUtilRequiredKeysType: boolean; @@ -746,9 +627,23 @@ export interface GenerateApiConfiguration { objectFieldJsDoc: string; }; routeNameDuplicatesMap: Map; + /** + * name of the main exported class + */ apiClassName: string; + /** + * configuration for fetching swagger schema requests + */ requestOptions?: RequestInit; extractingOptions: ExtractingOptions; + /** + * path to folder containing templates (default: ./src/templates) + */ + templates?: string; + /** + * TypeScript code generation customization + */ + Ts?: Partial; }; modelTypes: ModelType[]; hasFormDataRoutes: boolean; @@ -830,7 +725,7 @@ export interface GenerateApiOutput { } export declare function generateApi( - params: GenerateApiParams, + config: Partial, ): Promise; export interface GenerateTemplatesParams { From 2cca2d641c8bf409432170744c517d820533b614 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 17 May 2025 10:20:39 +0900 Subject: [PATCH 2/2] _ Signed-off-by: Sora Morimoto --- types/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/index.ts b/types/index.ts index 6fbac314..7dc96824 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,3 +1,4 @@ +import type { Spec } from "swagger-schema-official"; import type { ComponentTypeNameResolver } from "../src/component-type-name-resolver.js"; import type { HTTP_CLIENT } from "../src/constants.js"; import type { Translator } from "../src/translators/translator.js"; @@ -410,7 +411,7 @@ export interface GenerateApiConfiguration { /** * swagger schema JSON */ - spec: unknown; + spec: Spec; /** * default 'api.ts' */ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy