Skip to content

pcl-labs/swagger-axios-codegen

 
 

Repository files navigation

swagger-axios-codegen

swagger client to use axios and typescript

NpmVersionnpm

require node > v8.0.0

it will always resolve axios.response.data or reject axios.error with Promise

support other support similar to axios library, for example Fly.js, required setting ISwaggerOptions.useCustomerRequestInstance = true

Get Started

  yarn add swagger-axios-codegen
export interface ISwaggerOptions {
  serviceNameSuffix?: string
  methodNameMode?: 'operationId' | 'path'
  outputDir?: string
  fileName?: string
  remoteUrl?: string
  source?: any
  useStaticMethod?: boolean | undefined
  useCustomerRequestInstance?: boolean | undefined
  include?: Array<string | IInclude>
}

const defaultOptions: ISwaggerOptions = {
  classNameSuffix: 'Service',
  methodNameMode: 'operationId',
  outputDir: './service',
  fileName: 'index.ts',
  useStaticMethod: true, // >= 0.4
  useCustomerRequestInstance: false
}

use local swagger api json

const { codegen } = require('swagger-axios-codegen')
codegen({
  methodNameMode: 'operationId',
  source:require('./swagger.json')
})

use remote swagger api json

const { codegen } = require('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

use other library

import YourLib from '<Your lib>'
import { serviceOptions } from './service'

serviceOptions.axios = YourLib

filter service and method

let include = [
  'Products', // tagName
  'Estimates',//tagName
  { 'User': ['history'] }
]
codegen({
  methodNameMode: 'path',
  source: require('../swagger.json'),
  outputDir: './swagger/services',
  include
})

Welcome PR and commit issue

License

Licensed under the GNU Affero General Public License v3.0.

About

swagger client to use axios and typescript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 97.2%
  • JavaScript 2.8%