Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hunghg255/swagger-typescript-api-es

TypeScript API generator via Swagger scheme
https://github.com/hunghg255/swagger-typescript-api-es

api axios definition fetch generate-code rest-api swagger types typescript

Last synced: about 1 month ago
JSON representation

TypeScript API generator via Swagger scheme

Awesome Lists containing this project

README

        



logo


ESM and TypeScript rewrite of Acacode's
swagger-typescript-api


NPM Version
NPM Downloads
Minizip
Contributors
License

## API

```bash
npx swagger-typescript-api-es@latest --help
```

## CLI

```bash
npx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api
```

## Install

```bash
npm i swagger-typescript-api-es@latest --save-dev
```

- Create a file `swagger-typescript-api.config.ts` in the root of the project

```ts
import { defaultConfig } from 'swagger-typescript-api-es';

export default defaultConfig({
name: 'api-axios.ts',
output: './src/apis/axios-gentype',
url: 'http://localhost:5002/api-json',
httpClientType: 'axios',
});
```

- Options

```ts
// Options
interface IOptions {
/**
* name of output typescript api file (default: "Api.ts")
*/
name: string;
/**
* output path of typescript api file (default: "./")
*/
output: string;
/**
* path/url to swagger scheme
*/
url: string;
input?: string;
spec?: {
swagger?: '2.0' | '3.0';
info?: {
version?: string;
title?: string;
};
};
templates?: string;
httpClientType?: 'axios' | 'fetch';
defaultResponseAsSuccess?: boolean;
generateClient?: boolean;
generateRouteTypes?: boolean;
generateResponses?: boolean;
toJS?: boolean;
extractRequestParams?: boolean;
extractRequestBody?: boolean;
extractEnums?: boolean;
unwrapResponseData?: boolean;
/**
* By default prettier config is load from your project
*/
prettier?: {
printWidth?: number;
tabWidth?: number;
trailingComma?: 'all' | string;
parser?: 'typescript' | string;
};
singleHttpClient?: boolean;
cleanOutput?: boolean;
enumNamesAsValues?: boolean;
moduleNameFirstTag?: boolean;
generateUnionEnums?: boolean;
typePrefix?: string;
typeSuffix?: string;
enumKeyPrefix?: string;
enumKeySuffix?: string;
addReadonly?: boolean;
sortTypes?: boolean;
sortRouters?: boolean;
extractingOptions?: {
requestBodySuffix?: string[];
requestParamsSuffix?: string[];
responseBodySuffix?: string[];
responseErrorSuffix?: string[];
};
/** allow to generate extra files based with this extra templates, see more below */
extraTemplates?: [];
anotherArrayType?: boolean;
fixInvalidTypeNamePrefix?: string;
fixInvalidEnumKeyPrefix?: string;
prettierOptions?: Record;
constants?: Record;
templateInfos?: any;
codeGenConstructs?: (constructs: any) => Record;
primitiveTypeConstructs?: (constructs: any) => Record;
hooks?: {
onCreateComponent?: (component: any) => void;
onCreateRequestParams?: (rawType: any) => void;
onCreateRoute?: (routeData: any) => void;
onCreateRouteName?: (routeNameInfo: any, rawRouteInfo: any) => void;
onFormatRouteName?: (routeInfo: any, templateRouteName: any) => void;
onFormatTypeName?: (typeName: any, rawTypeName: any, schemaType: any) => void;
onInit?: (configuration: any) => void;
onPreParseSchema?: (originalSchema: any, typeName: any, schemaType: any) => void;
onParseSchema?: (originalSchema: any, parsedSchema: any) => void;
onPrepareConfig?: (currentConfiguration: any) => void;
};
}
```

- Config (file package.json)

```json
{
...
"scripts": {
...
"gen-api-types": "swagger-typescript-api-es"
},
...
}
```

## 📝 License

Licensed under the [MIT License](https://github.com/hunghg255/swagger-typescript-api-es/blob/master/LICENSE).