@odit/swagger-axios-codegen
A swagger client using axios and typescript
it will always resolve axios.response.data
or reject axios.error
with Promise
Get Started
pnpm i -D @odit/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>
includeTypes?: Array<string>
urlFilters?: Array<string>
format?: (s: string) => string
strictNullChecks?: boolean | undefined
modelMode?: 'class' | 'interface'
useClassTransformer?: boolean,
openApi?: string | undefined,
extendDefinitionFile?: string | undefined
extendGenericType?: string[] | undefined
multipleFileMode?: boolean | undefined
sharedServiceOptions?: boolean | undefined
}
const defaultOptions: ISwaggerOptions = {
serviceNameSuffix: 'Service',
enumNamePrefix: 'Enum',
methodNameMode: 'operationId',
outputDir: './service',
fileName: 'index.ts',
useStaticMethod: true,
useCustomerRequestInstance: false,
include: [],
strictNullChecks: true,
modelMode?: 'interface',
useClassTransformer: false
}
use local swagger api json
const { codegen } = require('@odit/swagger-axios-codegen')
codegen({
methodNameMode: 'operationId',
source: require('./swagger.json')
})
use remote swagger api json
const { codegen } = require('@odit/swagger-axios-codegen')
codegen({
methodNameMode: 'operationId',
remoteUrl:'You remote Url'
})
use static method
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();
use custom axios.instance
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