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.ts
in the root of the project
import { const defaultConfig: (options: IOptions) => IOptions
defaultConfig } from 'swagger-typescript-api-es';
export default function defaultConfig(options: IOptions): IOptions
defaultConfig({
IOptions.name: string
name of output typescript api file (default: "Api.ts")
name: 'api-axios.ts',
IOptions.output: string
output path of typescript api file (default: "./")
output: './src/apis/axios-gentype',
IOptions.url: string
path/url to swagger scheme
url: 'http://localhost:5002/api-json',
IOptions.httpClientType?: "axios" | "fetch" | undefined
httpClientType: 'axios',
});
ts
- Options
// Options
interface IOptions {
/**
* name of output typescript api file (default: "Api.ts")
*/
IOptions.name: string
name of output typescript api file (default: "Api.ts")
name: string;
/**
* output path of typescript api file (default: "./")
*/
IOptions.output: string
output path of typescript api file (default: "./")
output: string;
/**
* path/url to swagger scheme
*/
IOptions.url: string
path/url to swagger scheme
url: string;
IOptions.input?: string | undefined
input?: string;
IOptions.spec?: {
swagger?: "2.0" | "3.0" | undefined;
info?: {
version?: string | undefined;
title?: string | undefined;
} | undefined;
} | undefined
spec?: {
swagger?: "2.0" | "3.0" | undefined
swagger?: '2.0' | '3.0';
info?: {
version?: string | undefined;
title?: string | undefined;
} | undefined
info?: {
version?: string | undefined
version?: string;
title?: string | undefined
title?: string;
};
};
IOptions.templates?: string | undefined
templates?: string;
IOptions.httpClientType?: "axios" | "fetch" | undefined
httpClientType?: 'axios' | 'fetch';
IOptions.defaultResponseAsSuccess?: boolean | undefined
defaultResponseAsSuccess?: boolean;
IOptions.generateClient?: boolean | undefined
generateClient?: boolean;
IOptions.generateRouteTypes?: boolean | undefined
generateRouteTypes?: boolean;
IOptions.generateResponses?: boolean | undefined
generateResponses?: boolean;
IOptions.toJS?: boolean | undefined
toJS?: boolean;
IOptions.extractRequestParams?: boolean | undefined
extractRequestParams?: boolean;
IOptions.extractRequestBody?: boolean | undefined
extractRequestBody?: boolean;
IOptions.extractEnums?: boolean | undefined
extractEnums?: boolean;
IOptions.unwrapResponseData?: boolean | undefined
unwrapResponseData?: boolean;
/**
* By default prettier config is load from your project
*/
IOptions.prettier?: {
printWidth?: number | undefined;
tabWidth?: number | undefined;
trailingComma?: string | undefined;
parser?: string | undefined;
} | undefined
By default prettier config is load from your project
prettier?: {
printWidth?: number | undefined
printWidth?: number;
tabWidth?: number | undefined
tabWidth?: number;
trailingComma?: string | undefined
trailingComma?: 'all' | string;
parser?: string | undefined
parser?: 'typescript' | string;
};
IOptions.singleHttpClient?: boolean | undefined
singleHttpClient?: boolean;
IOptions.cleanOutput?: boolean | undefined
cleanOutput?: boolean;
IOptions.enumNamesAsValues?: boolean | undefined
enumNamesAsValues?: boolean;
IOptions.moduleNameFirstTag?: boolean | undefined
moduleNameFirstTag?: boolean;
IOptions.generateUnionEnums?: boolean | undefined
generateUnionEnums?: boolean;
IOptions.typePrefix?: string | undefined
typePrefix?: string;
IOptions.typeSuffix?: string | undefined
typeSuffix?: string;
IOptions.enumKeyPrefix?: string | undefined
enumKeyPrefix?: string;
IOptions.enumKeySuffix?: string | undefined
enumKeySuffix?: string;
IOptions.addReadonly?: boolean | undefined
addReadonly?: boolean;
IOptions.sortTypes?: boolean | undefined
sortTypes?: boolean;
IOptions.sortRouters?: boolean | undefined
sortRouters?: boolean;
IOptions.extractingOptions?: {
requestBodySuffix?: string[] | undefined;
requestParamsSuffix?: string[] | undefined;
responseBodySuffix?: string[] | undefined;
responseErrorSuffix?: string[] | undefined;
} | undefined
extractingOptions?: {
requestBodySuffix?: string[] | undefined
requestBodySuffix?: string[];
requestParamsSuffix?: string[] | undefined
requestParamsSuffix?: string[];
responseBodySuffix?: string[] | undefined
responseBodySuffix?: string[];
responseErrorSuffix?: string[] | undefined
responseErrorSuffix?: string[];
};
/** allow to generate extra files based with this extra templates, see more below */
IOptions.extraTemplates?: [] | undefined
allow to generate extra files based with this extra templates, see more below
extraTemplates?: [];
IOptions.anotherArrayType?: boolean | undefined
anotherArrayType?: boolean;
IOptions.fixInvalidTypeNamePrefix?: string | undefined
fixInvalidTypeNamePrefix?: string;
IOptions.fixInvalidEnumKeyPrefix?: string | undefined
fixInvalidEnumKeyPrefix?: string;
IOptions.prettierOptions?: Record<string, any> | undefined
prettierOptions?: 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> | undefined
constants?: 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?: any
templateInfos?: any;
IOptions.codeGenConstructs?: ((constructs: any) => Record<string, any>) | undefined
codeGenConstructs?: (constructs: any
constructs: 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>) | undefined
primitiveTypeConstructs?: (constructs: any
constructs: 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;
} | undefined
hooks?: {
onCreateComponent?: ((component: any) => void) | undefined
onCreateComponent?: (component: any
component: any) => void;
onCreateRequestParams?: ((rawType: any) => void) | undefined
onCreateRequestParams?: (rawType: any
rawType: any) => void;
onCreateRoute?: ((routeData: any) => void) | undefined
onCreateRoute?: (routeData: any
routeData: any) => void;
onCreateRouteName?: ((routeNameInfo: any, rawRouteInfo: any) => void) | undefined
onCreateRouteName?: (routeNameInfo: any
routeNameInfo: any, rawRouteInfo: any
rawRouteInfo: any) => void;
onFormatRouteName?: ((routeInfo: any, templateRouteName: any) => void) | undefined
onFormatRouteName?: (routeInfo: any
routeInfo: any, templateRouteName: any
templateRouteName: any) => void;
onFormatTypeName?: ((typeName: any, rawTypeName: any, schemaType: any) => void) | undefined
onFormatTypeName?: (typeName: any
typeName: any, rawTypeName: any
rawTypeName: any, schemaType: any
schemaType: any) => void;
onInit?: ((configuration: any) => void) | undefined
onInit?: (configuration: any
configuration: any) => void;
onPreParseSchema?: ((originalSchema: any, typeName: any, schemaType: any) => void) | undefined
onPreParseSchema?: (originalSchema: any
originalSchema: any, typeName: any
typeName: any, schemaType: any
schemaType: any) => void;
onParseSchema?: ((originalSchema: any, parsedSchema: any) => void) | undefined
onParseSchema?: (originalSchema: any
originalSchema: any, parsedSchema: any
parsedSchema: any) => void;
onPrepareConfig?: ((currentConfiguration: any) => void) | undefined
onPrepareConfig?: (currentConfiguration: any
currentConfiguration: any) => void;
};
}
ts
- Config (file package.json)
{
...
"scripts": {
...
"gen-api-types": "swagger-typescript-api-es"
},
...
}
json
#📝 License
Licensed under the MIT License.