Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
swagger-axios-codegen
Advanced tools
A swagger client uses axios and typescript
require node > v8.0.0
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
export interface ISwaggerOptions {
serviceNameSuffix?: string
enumNamePrefix?: string
methodNameMode?: 'operationId' | 'path'
outputDir?: string
fileName?: string
remoteUrl?: string
source?: any
useStaticMethod?: boolean | undefined
useCustomerRequestInstance?: boolean | undefined
include?: Array<string | IInclude>
format?: (s: string) => string
/** match with tsconfig */
strictNullChecks?: boolean | undefined
/** definition Class mode */
modelMode?: 'class' | 'interface'
/** use class-transformer to transform the results */
useClassTransformer?: boolean
}
const defaultOptions: ISwaggerOptions = {
serviceNameSuffix: 'Service',
enumNamePrefix: 'Enum',
methodNameMode: 'operationId',
outputDir: './service',
fileName: 'index.ts',
useStaticMethod: true,
useCustomerRequestInstance: false,
include: [],
strictNullChecks: true,
/** definition Class mode ,auto use interface mode to streamlined code*/
modelMode?: 'interface'
useClassTransformer: false
}
const { codegen } = require('swagger-axios-codegen')
codegen({
methodNameMode: 'operationId',
source: require('./swagger.json')
})
const { codegen } = require('swagger-axios-codegen')
codegen({
methodNameMode: 'operationId',
remoteUrl:'You remote Url'
})
codegen({
methodNameMode: 'operationId',
remoteUrl: 'http://localhost:22742/swagger/v1/swagger.json',
outputDir: '.',
useStaticMethod: true
});
before
import { UserService } from './service'
const userService = new UserService()
await userService.GetAll();
after
import { UserService } from './service'
await UserService.GetAll();
import axios from 'axios'
import { serviceOptions } from './service'
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
serviceOptions.axios = instance
import YourLib from '<Your lib>'
import { serviceOptions } from './service'
serviceOptions.axios = YourLib
let include = [
'Products', // tagName
'Estimates',//tagName
{ 'User': ['history'] }
]
codegen({
methodNameMode: 'path',
source: require('../swagger.json'),
outputDir: './swagger/services',
include
})
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
{
"ObjectWithDate": {
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date-time"
}
}
}
}
const { codegen } = require('swagger-axios-codegen')
codegen({
methodNameMode: 'operationId',
source:require('./swagger.json'),
useClassTransformer: true,
})
Resulting class:
export class ObjectWithDate {
@Expose()
@Type(() => Date)
public date: Date;
}
The service method will transform the json response and return an instance of this class
0.9.4
FAQs
A swagger client uses axios and typescript
The npm package swagger-axios-codegen receives a total of 3,190 weekly downloads. As such, swagger-axios-codegen popularity was classified as popular.
We found that swagger-axios-codegen demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.