All Projects → Manweill → Swagger Axios Codegen

Manweill / Swagger Axios Codegen

Licence: mit
swagger client to use axios and typescript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Swagger Axios Codegen

Swagger Node Codegen
An OpenAPI 3.x/Swagger 2 code generator for Node.js
Stars: ✭ 189 (+32.17%)
Mutual labels:  swagger, openapi, codegen
Swagger Vue
Swagger to JS & Vue & Axios Codegen
Stars: ✭ 146 (+2.1%)
Mutual labels:  swagger, axios, codegen
Guardrail
Principled code generation from OpenAPI specifications
Stars: ✭ 396 (+176.92%)
Mutual labels:  swagger, openapi, codegen
Openapi Client Axios
JavaScript client library for consuming OpenAPI-enabled APIs with axios
Stars: ✭ 168 (+17.48%)
Mutual labels:  swagger, openapi, axios
Openapi Codegen
OpenAPI 3.0 CodeGen plus Node.js minus the Java and emojis
Stars: ✭ 224 (+56.64%)
Mutual labels:  swagger, openapi, codegen
Openapi Typescript Codegen
NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification
Stars: ✭ 249 (+74.13%)
Mutual labels:  swagger, openapi, codegen
Nswag
The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
Stars: ✭ 4,825 (+3274.13%)
Mutual labels:  swagger, openapi, codegen
Gemini
Model Driven REST framework to automatically generate CRUD APIs
Stars: ✭ 138 (-3.5%)
Mutual labels:  swagger, openapi
Netcoreblockly
.NET Core API to Blockly - generate from WebAPI, Swagger, OData, GraphQL =>
Stars: ✭ 121 (-15.38%)
Mutual labels:  swagger, openapi
Swaggerassertions
Assert your API requests and responses match with your swagger definition
Stars: ✭ 137 (-4.2%)
Mutual labels:  swagger, openapi
Vs Swagger Viewer
Swagger Viewer lets you preview and validate Swagger 2.0 and OpenAPI files as you type in Visual Studio Code.
Stars: ✭ 126 (-11.89%)
Mutual labels:  swagger, openapi
Terraform Provider Openapi
OpenAPI Terraform Provider that configures itself at runtime with the resources exposed by the service provider (defined in a swagger file)
Stars: ✭ 134 (-6.29%)
Mutual labels:  swagger, openapi
Openapi Core
OpenAPI core
Stars: ✭ 119 (-16.78%)
Mutual labels:  swagger, openapi
Wizard
Wizard是一款开源的文档管理工具,支持Markdown/Swagger/Table类型的文档。
Stars: ✭ 1,733 (+1111.89%)
Mutual labels:  swagger, openapi
L5 Swagger
OpenApi or Swagger integration to Laravel
Stars: ✭ 1,781 (+1145.45%)
Mutual labels:  swagger, openapi
Grpc Gateway
The gRPC-Gateway is a plugin of the Google protocol buffers compiler protoc. It reads protobuf service definitions and generates a reverse-proxy server which translates a RESTful HTTP API into gRPC. This server is generated according to the google.api.http annotations in your service definitions.
Stars: ✭ 12,223 (+8447.55%)
Mutual labels:  swagger, openapi
Webapiclient
An open source project based on the HttpClient. You only need to define the c# interface and modify the related features to invoke the client library of the remote http interface asynchronously.
Stars: ✭ 1,618 (+1031.47%)
Mutual labels:  swagger, openapi
Http Router
🎉 Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)
Stars: ✭ 124 (-13.29%)
Mutual labels:  swagger, openapi
Prance
Resolving Swagger/OpenAPI 2.0 and 3.0 Parser
Stars: ✭ 133 (-6.99%)
Mutual labels:  swagger, openapi
Mentorship Backend
Mentorship System is an application that matches women in tech to mentor each other, on career development, through 1:1 relations during a certain period of time. This is the backend of this system.
Stars: ✭ 132 (-7.69%)
Mutual labels:  swagger, openapi

swagger-axios-codegen

A swagger client uses axios and typescript

GitHub Workflow StatusNpmVersionnpm open issues

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... ⭐️ ⭐️ ⭐️ ⭐️ ⭐️

Example

ChangeLog

Contributing

Get Started

  yarn add swagger-axios-codegen
export interface ISwaggerOptions {
  /** service name suffix eg. 'Service' **/
  serviceNameSuffix?: string
  /** enum prefix eg. 'Enum' **/
  enumNamePrefix?: string
  methodNameMode?: 'operationId' | 'path'
  /** path of the generated file eg. './src/service' **/
  outputDir?: string
  /** generated file name eg. 'index.ts' **/
  fileName?: string
  /** path to remote source file eg. 'https://localhost:8080/api/v1/swagger.json' **/
  remoteUrl?: string
  /** path to local source file eg. './swagger.json' **/
  source?: any
  useStaticMethod?: boolean | undefined
  /** client can pass custom headers to the service methods **/
  useCustomerRequestInstance?: boolean | undefined
  /** filter by service name (first tag) or method name using multimatch (https://github.com/sindresorhus/multimatch) **/
  include?: Array<string | IInclude>
  /** include extra types which are not included during the filtering Eg. ["Foo", "Bar"] **/
  includeTypes?: Array<string>
  /** filter urls by following clauses **/
  urlFilters?: Array<string>
  /** custom function to format the output file (default: prettier.format()) **/
  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,
  // force the specified swagger or openAPI version,
  openApi?: string | undefined,
  // extend file url. It will be inserted in front of the service method
  extendDefinitionFile?: string | undefined
  // mark generic type
  extendGenericType?: string[] | undefined
  // split request service 
  multipleFileMode?: boolean | undefined
}

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
}

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

fliter by multimatch using 'include' setting

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

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:

// API
"paths": {
  "/Products/Get": {
    "post": {
      "tags": [
        "MyApp.FirstModule.Products"
      ],
      "operationId": "Get",
// Codegen config
codegen({
  methodNameMode: 'path',
  source: require('../swagger.json'),
  outputDir: './swagger/services',
  include: ['MyAppFirstModule*'] // Only Products and Customers will be included. As you can see dots are escaped being contract names.
})

use class transformer to transform results

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

use validation model

codegen({
    ...
    modelMode: 'class',
    generateValidationModel: true
});

The option above among with class model mode allows to render the model validation rules. The result of this will be as follows:

export class FooFormVm {
  'name'?: string;
  'description'?: string;
 
  constructor(data: undefined | any = {}) {
    this['name'] = data['name'];
    this['description'] = data['description'];
  }
 
  public static validationModel = {
    name: { required: true, maxLength: 50 },
    description: { maxLength: 250 },
  };
}

So you can use the validation model in your application:

function isRequired(vm: any, fieldName: string): boolean {
  return (vm && vm[fieldName] && vm[fieldName].required === true);
}
function maxLength(vm: any, fieldName: string): number {
  return (vm && vm[fieldName] && vm[fieldName].maxLength ? vm[fieldName].maxLength : 4000);
}

Now you can use the functions

var required = isRequired(FooFormVm.validationModel, 'name');
var maxLength = maxLength(FooFormVm.validationModel, 'description');

At the moment there are only two rules are supported - required and maxLength.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].