|
| 1 | +<p align="center"> |
| 2 | +<a href="https://www.npmjs.com/package/swagger-typescript-api-es" target="_blank" rel="noopener noreferrer"> |
| 3 | +<img src="https://api.iconify.design/devicon-plain:swagger-wordmark.svg?color=%2365fb92" alt="logo" style="width:150px;"/></a> |
| 4 | +</p> |
| 5 | + |
| 6 | +<p align="center"> |
| 7 | + ESM and TypeScript rewrite of Acacode's |
| 8 | + <a href="https://github.com/acacode/swagger-typescript-api">swagger-typescript-api</a> |
| 9 | +</p> |
| 10 | + |
| 11 | +<p align="center"> |
| 12 | + <a href="https://www.npmjs.com/package/swagger-typescript-api-es" target="_blank" rel="noopener noreferrer"><img src="https://badge.fury.io/js/swagger-typescript-api-es.svg" alt="NPM Version" /></a> |
| 13 | + <a href="https://www.npmjs.com/package/swagger-typescript-api-es" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/npm/dt/swagger-typescript-api-es.svg?logo=npm" alt="NPM Downloads" /></a> |
| 14 | + <a href="https://bundlephobia.com/result?p=swagger-typescript-api-es" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/bundlephobia/minzip/swagger-typescript-api-es" alt="Minizip" /></a> |
| 15 | + <a href="https://github.com/hunghg255/swagger-typescript-api-es/graphs/contributors" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/all_contributors-1-orange.svg" alt="Contributors" /></a> |
| 16 | + <a href="https://github.com/hunghg255/swagger-typescript-api-es/blob/main/LICENSE" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/github/license/hunghg255/swagger-typescript-api-es" alt="License" /></a> |
| 17 | +</p> |
| 18 | + |
| 19 | +## API |
| 20 | + |
| 21 | +:::code-group-open |
| 22 | + |
| 23 | +```bash [npm] |
| 24 | +npx swagger-typescript-api-es@latest --help |
| 25 | +``` |
| 26 | + |
| 27 | +```bash [yarn] |
| 28 | +yarn dlx swagger-typescript-api-es@latest --help |
| 29 | +``` |
| 30 | + |
| 31 | +```bash [pnpm] |
| 32 | +pnpx swagger-typescript-api-es@latest --help |
| 33 | +``` |
| 34 | + |
| 35 | +```bash [bun] |
| 36 | +bunx swagger-typescript-api-es@latest --help |
| 37 | +``` |
| 38 | + |
| 39 | +:::code-group-close |
| 40 | + |
| 41 | +## CLI |
| 42 | + |
| 43 | +```bash |
| 44 | +npx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api |
| 45 | +``` |
| 46 | + |
| 47 | +:::code-group-open |
| 48 | + |
| 49 | +```bash [npm] |
| 50 | +npx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api |
| 51 | +``` |
| 52 | + |
| 53 | +```bash [yarn] |
| 54 | +yarn dlx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api |
| 55 | +``` |
| 56 | + |
| 57 | +```bash [pnpm] |
| 58 | +pnpx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api |
| 59 | +``` |
| 60 | + |
| 61 | +```bash [bun] |
| 62 | +bunx swagger-typescript-api-es@latest -u https://petstore.swagger.io/v2/swagger.json -o ./src/api |
| 63 | +``` |
| 64 | + |
| 65 | +:::code-group-close |
| 66 | + |
| 67 | +## Install |
| 68 | + |
| 69 | +:::code-group-open |
| 70 | + |
| 71 | +```bash [npm] |
| 72 | +npm i swagger-typescript-api-es@latest --save-dev |
| 73 | +``` |
| 74 | + |
| 75 | +```bash [yarn] |
| 76 | +yarn dlx swagger-typescript-api-es@latest --save-dev |
| 77 | +``` |
| 78 | + |
| 79 | +```bash [pnpm] |
| 80 | +pnpm i swagger-typescript-api-es@latest --save-dev |
| 81 | +``` |
| 82 | + |
| 83 | +```bash [bun] |
| 84 | +bun i swagger-typescript-api-es@latest --save-dev |
| 85 | +``` |
| 86 | + |
| 87 | +:::code-group-close |
| 88 | + |
| 89 | +- Create a file `swagger-typescript-api.config.config.ts` in the root of the project |
| 90 | + |
| 91 | +```ts twoslash |
| 92 | +import { defaultConfig } from 'swagger-typescript-api-es'; |
| 93 | + |
| 94 | +export default defaultConfig({ |
| 95 | + name: 'api-axios.ts', |
| 96 | + output: './src/apis/axios-gentype', |
| 97 | + url: 'http://localhost:5002/api-json', |
| 98 | + httpClientType: 'axios', |
| 99 | +}); |
| 100 | + |
| 101 | +// Options |
| 102 | +interface IOptions { |
| 103 | + /** |
| 104 | + * name of output typescript api file (default: "Api.ts") |
| 105 | + */ |
| 106 | + name: string; |
| 107 | + /** |
| 108 | + * output path of typescript api file (default: "./") |
| 109 | + */ |
| 110 | + output: string; |
| 111 | + /** |
| 112 | + * path/url to swagger scheme |
| 113 | + */ |
| 114 | + url: string; |
| 115 | + input?: string; |
| 116 | + spec?: { |
| 117 | + swagger?: '2.0' | '3.0'; |
| 118 | + info?: { |
| 119 | + version?: string; |
| 120 | + title?: string; |
| 121 | + }; |
| 122 | + }; |
| 123 | + templates?: string; |
| 124 | + httpClientType?: 'axios' | 'fetch'; |
| 125 | + defaultResponseAsSuccess?: boolean; |
| 126 | + generateClient?: boolean; |
| 127 | + generateRouteTypes?: boolean; |
| 128 | + generateResponses?: boolean; |
| 129 | + toJS?: boolean; |
| 130 | + extractRequestParams?: boolean; |
| 131 | + extractRequestBody?: boolean; |
| 132 | + extractEnums?: boolean; |
| 133 | + unwrapResponseData?: boolean; |
| 134 | + /** |
| 135 | + * By default prettier config is load from your project |
| 136 | + */ |
| 137 | + prettier?: { |
| 138 | + printWidth?: number; |
| 139 | + tabWidth?: number; |
| 140 | + trailingComma?: 'all' | string; |
| 141 | + parser?: 'typescript' | string; |
| 142 | + }; |
| 143 | + singleHttpClient?: boolean; |
| 144 | + cleanOutput?: boolean; |
| 145 | + enumNamesAsValues?: boolean; |
| 146 | + moduleNameFirstTag?: boolean; |
| 147 | + generateUnionEnums?: boolean; |
| 148 | + typePrefix?: string; |
| 149 | + typeSuffix?: string; |
| 150 | + enumKeyPrefix?: string; |
| 151 | + enumKeySuffix?: string; |
| 152 | + addReadonly?: boolean; |
| 153 | + sortTypes?: boolean; |
| 154 | + sortRouters?: boolean; |
| 155 | + extractingOptions?: { |
| 156 | + requestBodySuffix?: string[]; |
| 157 | + requestParamsSuffix?: string[]; |
| 158 | + responseBodySuffix?: string[]; |
| 159 | + responseErrorSuffix?: string[]; |
| 160 | + }; |
| 161 | + /** allow to generate extra files based with this extra templates, see more below */ |
| 162 | + extraTemplates?: []; |
| 163 | + anotherArrayType?: boolean; |
| 164 | + fixInvalidTypeNamePrefix?: string; |
| 165 | + fixInvalidEnumKeyPrefix?: string; |
| 166 | + prettierOptions?: Record<string, any>; |
| 167 | + constants?: Record<string, any>; |
| 168 | + templateInfos?: any; |
| 169 | + codeGenConstructs?: (constructs: any) => Record<string, any>; |
| 170 | + primitiveTypeConstructs?: (constructs: any) => Record<string, any>; |
| 171 | + hooks?: { |
| 172 | + onCreateComponent?: (component: any) => void; |
| 173 | + onCreateRequestParams?: (rawType: any) => void; |
| 174 | + onCreateRoute?: (routeData: any) => void; |
| 175 | + onCreateRouteName?: (routeNameInfo: any, rawRouteInfo: any) => void; |
| 176 | + onFormatRouteName?: (routeInfo: any, templateRouteName: any) => void; |
| 177 | + onFormatTypeName?: (typeName: any, rawTypeName: any, schemaType: any) => void; |
| 178 | + onInit?: (configuration: any) => void; |
| 179 | + onPreParseSchema?: (originalSchema: any, typeName: any, schemaType: any) => void; |
| 180 | + onParseSchema?: (originalSchema: any, parsedSchema: any) => void; |
| 181 | + onPrepareConfig?: (currentConfiguration: any) => void; |
| 182 | + }; |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +- Config (file package.json) |
| 187 | + |
| 188 | +```json |
| 189 | +{ |
| 190 | + ... |
| 191 | + "scripts": { |
| 192 | + ... |
| 193 | + "gen-api-types": "swagger-typescript-api-es" |
| 194 | + }, |
| 195 | + ... |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +## 📝 License |
| 200 | + |
| 201 | +Licensed under the [MIT License](https://github.com/hunghg255/swagger-typescript-api-es/blob/master/LICENSE). |
0 commit comments