ESM and TypeScript rewrite of Acacode's swagger-typescript-api
#API
npm
yarn
pnpm
bun
npx swagger-typescript-api-es@latest --help
bash
yarn dlx swagger-typescript-api-es@latest --help
bash
pnpx swagger-typescript-api-es@latest --help
bash
bunx swagger-typescript-api-es@latest --help
bash
#CLI
npx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api
bash
npm
yarn
pnpm
bun
npx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api
bash
yarn dlx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api
bash
pnpx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api
bash
bunx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api
bash
#Install
npm
yarn
pnpm
bun
npm i swagger-typescript-api-es@latest --save-dev
bash
yarn dlx swagger-typescript-api-es@latest --save-dev
bash
pnpm i swagger-typescript-api-es@latest --save-dev
bash
bun i swagger-typescript-api-es@latest --save-dev
bash
- Create a file
swagger-typescript-api.config.tsin the root of the project
import { const defaultConfig: (options: IOptions) => IOptionsdefaultConfig } from 'swagger-typescript-api-es';
export default function defaultConfig(options: IOptions): IOptionsdefaultConfig({
IOptions.name: stringname of output typescript api file (default: "Api.ts")
name: 'api-axios.ts',
IOptions.output: stringoutput path of typescript api file (default: "./")
output: './src/apis/axios-gentype',
IOptions.url: stringpath/url to swagger scheme
url: 'http://localhost:5002/api-json',
IOptions.httpClientType?: "axios" | "fetch" | undefinedhttpClientType: 'axios',
});
ts
- Options
// Options
interface IOptions {
/**
* name of output typescript api file (default: "Api.ts")
*/
IOptions.name: stringname of output typescript api file (default: "Api.ts")
name: string;
/**
* output path of typescript api file (default: "./")
*/
IOptions.output: stringoutput path of typescript api file (default: "./")
output: string;
/**
* path/url to swagger scheme
*/
IOptions.url: stringpath/url to swagger scheme
url: string;
IOptions.input?: string | undefinedinput?: string;
IOptions.spec?: {
swagger?: "2.0" | "3.0" | undefined;
info?: {
version?: string | undefined;
title?: string | undefined;
} | undefined;
} | undefinedspec?: {
swagger?: "2.0" | "3.0" | undefinedswagger?: '2.0' | '3.0';
info?: {
version?: string | undefined;
title?: string | undefined;
} | undefinedinfo?: {
version?: string | undefinedversion?: string;
title?: string | undefinedtitle?: string;
};
};
IOptions.templates?: string | undefinedtemplates?: string;
IOptions.httpClientType?: "axios" | "fetch" | undefinedhttpClientType?: 'axios' | 'fetch';
IOptions.defaultResponseAsSuccess?: boolean | undefineddefaultResponseAsSuccess?: boolean;
IOptions.generateClient?: boolean | undefinedgenerateClient?: boolean;
IOptions.generateRouteTypes?: boolean | undefinedgenerateRouteTypes?: boolean;
IOptions.generateResponses?: boolean | undefinedgenerateResponses?: boolean;
IOptions.toJS?: boolean | undefinedtoJS?: boolean;
IOptions.extractRequestParams?: boolean | undefinedextractRequestParams?: boolean;
IOptions.extractRequestBody?: boolean | undefinedextractRequestBody?: boolean;
IOptions.extractEnums?: boolean | undefinedextractEnums?: boolean;
IOptions.unwrapResponseData?: boolean | undefinedunwrapResponseData?: boolean;
/**
* By default prettier config is load from your project
*/
IOptions.prettier?: {
printWidth?: number | undefined;
tabWidth?: number | undefined;
trailingComma?: string | undefined;
parser?: string | undefined;
} | undefinedBy default prettier config is load from your project
prettier?: {
printWidth?: number | undefinedprintWidth?: number;
tabWidth?: number | undefinedtabWidth?: number;
trailingComma?: string | undefinedtrailingComma?: 'all' | string;
parser?: string | undefinedparser?: 'typescript' | string;
};
IOptions.singleHttpClient?: boolean | undefinedsingleHttpClient?: boolean;
IOptions.cleanOutput?: boolean | undefinedcleanOutput?: boolean;
IOptions.enumNamesAsValues?: boolean | undefinedenumNamesAsValues?: boolean;
IOptions.moduleNameFirstTag?: boolean | undefinedmoduleNameFirstTag?: boolean;
IOptions.generateUnionEnums?: boolean | undefinedgenerateUnionEnums?: boolean;
IOptions.typePrefix?: string | undefinedtypePrefix?: string;
IOptions.typeSuffix?: string | undefinedtypeSuffix?: string;
IOptions.enumKeyPrefix?: string | undefinedenumKeyPrefix?: string;
IOptions.enumKeySuffix?: string | undefinedenumKeySuffix?: string;
IOptions.addReadonly?: boolean | undefinedaddReadonly?: boolean;
IOptions.sortTypes?: boolean | undefinedsortTypes?: boolean;
IOptions.sortRouters?: boolean | undefinedsortRouters?: boolean;
IOptions.extractingOptions?: {
requestBodySuffix?: string[] | undefined;
requestParamsSuffix?: string[] | undefined;
responseBodySuffix?: string[] | undefined;
responseErrorSuffix?: string[] | undefined;
} | undefinedextractingOptions?: {
requestBodySuffix?: string[] | undefinedrequestBodySuffix?: string[];
requestParamsSuffix?: string[] | undefinedrequestParamsSuffix?: string[];
responseBodySuffix?: string[] | undefinedresponseBodySuffix?: string[];
responseErrorSuffix?: string[] | undefinedresponseErrorSuffix?: string[];
};
/** allow to generate extra files based with this extra templates, see more below */
IOptions.extraTemplates?: [] | undefinedallow to generate extra files based with this extra templates, see more below
extraTemplates?: [];
IOptions.anotherArrayType?: boolean | undefinedanotherArrayType?: boolean;
IOptions.fixInvalidTypeNamePrefix?: string | undefinedfixInvalidTypeNamePrefix?: string;
IOptions.fixInvalidEnumKeyPrefix?: string | undefinedfixInvalidEnumKeyPrefix?: string;
IOptions.prettierOptions?: Record<string, any> | undefinedprettierOptions?: type Record<K extends string | number | symbol, T> = { [P in K]: T; }Construct a type with a set of properties K of type T
Record<string, any>;
IOptions.constants?: Record<string, any> | undefinedconstants?: type Record<K extends string | number | symbol, T> = { [P in K]: T; }Construct a type with a set of properties K of type T
Record<string, any>;
IOptions.templateInfos?: anytemplateInfos?: any;
IOptions.codeGenConstructs?: ((constructs: any) => Record<string, any>) | undefinedcodeGenConstructs?: (constructs: anyconstructs: any) => type Record<K extends string | number | symbol, T> = { [P in K]: T; }Construct a type with a set of properties K of type T
Record<string, any>;
IOptions.primitiveTypeConstructs?: ((constructs: any) => Record<string, any>) | undefinedprimitiveTypeConstructs?: (constructs: anyconstructs: any) => type Record<K extends string | number | symbol, T> = { [P in K]: T; }Construct a type with a set of properties K of type T
Record<string, any>;
IOptions.hooks?: {
onCreateComponent?: ((component: any) => void) | undefined;
onCreateRequestParams?: ((rawType: any) => void) | undefined;
onCreateRoute?: ((routeData: any) => void) | undefined;
onCreateRouteName?: ((routeNameInfo: any, rawRouteInfo: any) => void) | undefined;
... 5 more ...;
onPrepareConfig?: ((currentConfiguration: any) => void) | undefined;
} | undefinedhooks?: {
onCreateComponent?: ((component: any) => void) | undefinedonCreateComponent?: (component: anycomponent: any) => void;
onCreateRequestParams?: ((rawType: any) => void) | undefinedonCreateRequestParams?: (rawType: anyrawType: any) => void;
onCreateRoute?: ((routeData: any) => void) | undefinedonCreateRoute?: (routeData: anyrouteData: any) => void;
onCreateRouteName?: ((routeNameInfo: any, rawRouteInfo: any) => void) | undefinedonCreateRouteName?: (routeNameInfo: anyrouteNameInfo: any, rawRouteInfo: anyrawRouteInfo: any) => void;
onFormatRouteName?: ((routeInfo: any, templateRouteName: any) => void) | undefinedonFormatRouteName?: (routeInfo: anyrouteInfo: any, templateRouteName: anytemplateRouteName: any) => void;
onFormatTypeName?: ((typeName: any, rawTypeName: any, schemaType: any) => void) | undefinedonFormatTypeName?: (typeName: anytypeName: any, rawTypeName: anyrawTypeName: any, schemaType: anyschemaType: any) => void;
onInit?: ((configuration: any) => void) | undefinedonInit?: (configuration: anyconfiguration: any) => void;
onPreParseSchema?: ((originalSchema: any, typeName: any, schemaType: any) => void) | undefinedonPreParseSchema?: (originalSchema: anyoriginalSchema: any, typeName: anytypeName: any, schemaType: anyschemaType: any) => void;
onParseSchema?: ((originalSchema: any, parsedSchema: any) => void) | undefinedonParseSchema?: (originalSchema: anyoriginalSchema: any, parsedSchema: anyparsedSchema: any) => void;
onPrepareConfig?: ((currentConfiguration: any) => void) | undefinedonPrepareConfig?: (currentConfiguration: anycurrentConfiguration: any) => void;
};
}
ts
- Config (file package.json)
{
...
"scripts": {
...
"gen-api-types": "swagger-typescript-api-es"
},
...
}
json
#📝 License
Licensed under the MIT License.