Gathering detailed insights and metrics for swagger-axios-codegen
Gathering detailed insights and metrics for swagger-axios-codegen
Gathering detailed insights and metrics for swagger-axios-codegen
Gathering detailed insights and metrics for swagger-axios-codegen
swagger-typescript-api
Generate the API client for Fetch or Axios from an OpenAPI Specification
openapi-typescript-codegen
Library that generates Typescript clients based on the OpenAPI specification.
@rosaan/swagger-axios-codegen
A swagger client uses axios and typescript
@hey-api/client-axios
🚀 Axios client for `@hey-api/openapi-ts` codegen.
npm install swagger-axios-codegen
Typescript
Module System
Node Version
NPM Version
TypeScript (95.69%)
JavaScript (4.31%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
313 Stars
497 Commits
88 Forks
5 Watchers
4 Branches
30 Contributors
Updated on Jul 02, 2025
Latest Version
0.17.4
Package Id
swagger-axios-codegen@0.17.4
Unpacked Size
206.32 kB
Size
43.03 kB
File Count
78
NPM Version
10.8.2
Node Version
18.20.8
Published on
Apr 03, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
A swagger client uses axios and typescript
< v0.16 require node > v10.0.0
>= v0.16 require node >= v16
it will always resolve axios.response.data
or reject axios.error
with Promise
support other similar to axios
library, for example Fly.js, required setting ISwaggerOptions.useCustomerRequestInstance = true
the es6 version is generated by calling typescript
Welcome PRs and commit issue
By the way. you can support this repo via Star star sta st s... ⭐️ ⭐️ ⭐️ ⭐️ ⭐️
yarn add swagger-axios-codegen
1 2export interface ISwaggerOptions { 3 serviceNameSuffix?: string 4 enumNamePrefix?: string 5 methodNameMode?: 'operationId' | 'path' | 'shortOperationId' | ((reqProps: IRequestMethod) => string) 6 classNameMode?: 'parentPath' | 'normal' | ((path: string, method: string, reqProps:IRequestMethod) => string) 7 /** only effect classNameMode='parentPath' */ 8 pathClassNameDefaultName?: string 9 outputDir?: string 10 fileName?: string 11 remoteUrl?: string 12 source?: any 13 useStaticMethod?: boolean | undefined 14 useCustomerRequestInstance?: boolean | undefined 15 include?: Array<string | IInclude> 16 /** include types which are not included during the filtering **/ 17 includeTypes?: Array<string> 18 format?: (s: string) => string 19 /** match with tsconfig */ 20 strictNullChecks?: boolean | undefined 21 /** definition Class mode */ 22 modelMode?: 'class' | 'interface' 23 /** use class-transformer to transform the results */ 24 useClassTransformer?: boolean 25 /** force the specified swagger or openAPI version, */ 26 openApi?: string | undefined 27 /** extend file url. It will be inserted in front of the service method */ 28 extendDefinitionFile?: string | undefined 29 /** mark generic type */ 30 extendGenericType?: string[] | undefined 31 /** generate validation model (class model mode only) */ 32 generateValidationModel?: boolean 33 /** split request service. Can't use with sharedServiceOptions*/ 34 multipleFileMode?: boolean | undefined 35 /** url prefix filter*/ 36 urlFilters?: string[] | null | undefined 37 /** shared service options to multiple service. Can't use with MultipleFileMode */ 38 sharedServiceOptions?: boolean | undefined 39 /** use parameters in header or not*/ 40 useHeaderParameters?: boolean 41} 42 43const defaultOptions: ISwaggerOptions = { 44 serviceNameSuffix: 'Service', 45 enumNamePrefix: 'Enum', 46 methodNameMode: 'operationId', 47 classNameMode: 'normal', 48 PathClassNameDefaultName: 'Global', 49 outputDir: './service', 50 fileName: 'index.ts', 51 useStaticMethod: true, 52 useCustomerRequestInstance: false, 53 include: [], 54 strictNullChecks: true, 55 /** definition Class mode ,auto use interface mode to streamlined code*/ 56 modelMode?: 'interface', 57 useClassTransformer: false 58} 59
1 2const { codegen } = require('swagger-axios-codegen') 3codegen({ 4 methodNameMode: 'operationId', 5 source: require('./swagger.json') 6}) 7 8
1 2const { codegen } = require('swagger-axios-codegen') 3codegen({ 4 methodNameMode: 'operationId', 5 remoteUrl:'You remote Url' 6}) 7 8
1codegen({ 2 methodNameMode: 'operationId', 3 remoteUrl: 'http://localhost:22742/swagger/v1/swagger.json', 4 outputDir: '.', 5 useStaticMethod: true 6}); 7
before
1 2import { UserService } from './service' 3const userService = new UserService() 4await userService.GetAll(); 5
after
1 2import { UserService } from './service' 3 4await UserService.GetAll(); 5
1import axios from 'axios' 2import { serviceOptions } from './service' 3const instance = axios.create({ 4 baseURL: 'https://some-domain.com/api/', 5 timeout: 1000, 6 headers: {'X-Custom-Header': 'foobar'} 7}); 8 9serviceOptions.axios = instance 10
1import YourLib from '<Your lib>' 2import { serviceOptions } from './service' 3 4serviceOptions.axios = YourLib 5
fliter by multimatch using 'include' setting
1codegen({ 2 methodNameMode: 'path', 3 source: require('../swagger.json'), 4 outputDir: './swagger/services', 5 include: [ 6 '*', 7 // 'Products*', 8 '!Products', 9 { 'User': ['*', '!history'] }, 10 ] 11})
If you are using special characters in your service name (which is the first tag) you must assume they have been escaped.
For example the service names are MyApp.FirstModule.Products
, MyApp.FirstModule.Customers
, MyApp.SecondModule.Orders
:
1// API 2"paths": { 3 "/Products/Get": { 4 "post": { 5 "tags": [ 6 "MyApp.FirstModule.Products" 7 ], 8 "operationId": "Get",
1// Codegen config 2codegen({ 3 methodNameMode: 'path', 4 source: require('../swagger.json'), 5 outputDir: './swagger/services', 6 include: ['MyAppFirstModule*'] // Only Products and Customers will be included. As you can see dots are escaped being contract names. 7})
This is helpful if you want to transform dates to real date
objects. Swagger can define string formats for different types. Two if these formats are date
and date-time
If a class-transformer
is enabled and a format is set on a string, the result string will be transformed to a Date
instance
// swagger.json
1{ 2 "ObjectWithDate": { 3 "type": "object", 4 "properties": { 5 "date": { 6 "type": "string", 7 "format": "date-time" 8 } 9 } 10 } 11}
1 2const { codegen } = require('swagger-axios-codegen') 3codegen({ 4 methodNameMode: 'operationId', 5 source:require('./swagger.json'), 6 useClassTransformer: true, 7})
Resulting class:
1export class ObjectWithDate { 2 @Expose() 3 @Type(() => Date) 4 public date: Date; 5}
The service method will transform the json response and return an instance of this class
1codegen({ 2 ... 3 modelMode: 'class', 4 generateValidationModel: true 5});
The option above among with class model mode allows to render the model validation rules. The result of this will be as follows:
1export class FooFormVm { 2 'name'?: string; 3 'description'?: string; 4 5 constructor(data: undefined | any = {}) { 6 this['name'] = data['name']; 7 this['description'] = data['description']; 8 } 9 10 public static validationModel = { 11 name: { required: true, maxLength: 50 }, 12 description: { maxLength: 250 }, 13 }; 14}
So you can use the validation model in your application:
1function isRequired(vm: any, fieldName: string): boolean {
2 return (vm && vm[fieldName] && vm[fieldName].required === true);
3}
4function maxLength(vm: any, fieldName: string): number {
5 return (vm && vm[fieldName] && vm[fieldName].maxLength ? vm[fieldName].maxLength : 4000);
6}
Now you can use the functions
1var required = isRequired(FooFormVm.validationModel, 'name'); 2var maxLength = maxLength(FooFormVm.validationModel, 'description');
At the moment there are only two rules are supported - required
and maxLength
.
see in #53, use package json-schema-ref-parser
Microservice Gateway
1const {codegen} = require('swagger-axios-codegen') 2const axios = require('axios') 3// host 地址 4const host = 'http://your-host-name' 5 6// 7const modules = [ 8 ... 9] 10 11axios.get(`${host}/swagger-resources`).then(async ({data}) => { 12 console.warn('code', host) 13 for (let n of data) { 14 if (modules.includes(n.name)) { 15 try { 16 await codegen({ 17 remoteUrl: `${host}${n.url}`, 18 methodNameMode: 'operationId', 19 modelMode: 'interface', 20 strictNullChecks: false, 21 outputDir: './services', 22 fileName: `${n.name}.ts`, 23 sharedServiceOptions: true, 24 extendDefinitionFile: './customerDefinition.ts', 25 }) 26 } catch (e) { 27 console.log(`${n.name} service error`, e.message) 28 } 29 } 30 } 31}) 32
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
Found 6/29 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More