JSPM

  • Created
  • Published
  • Downloads 729
  • Score
    100M100P100Q94771F
  • License MIT

A auto generate typescript code from swagger

Package Exports

  • swagger-typescript

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (swagger-typescript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

NPM

install size dependencies

An auto typescript code generator from swagger. Every endpoint create as function and full type base. For Example: Get method of '/Account' path will be this code in services.ts

export const getAccountList = async (
  queryParams: { id: string },
  configOverride?: AxiosRequestConfig,
): Promise<SwaggerResponse<CommissionAmountApiModel>> => {
  // internal code
  return response
};

install

$ yarn add swagger-typescript

Before running, add your config to swagger.config.json

swagger.config.json

{
    "url": "http://example.swagger.json",
    "dir": "./test",
    "prettierPath": ".prettierrc",
    "ignore": {
        "headerParams": [
            "terminalId"
        ]
    }
}

run

node ./node_modules/swagger-typescript/lib/index.js'

config.ts

This file automatically will be create after first run. You could change this file for customization. Don't change other files, if you want another config create Issue or PR.

responseWrapper

for customize responseWrapper your can do something like this

export type SwaggerResponse<R> = R;

async function responseWrapper(
  response: AxiosResponse<any>,
): Promise<SwaggerResponse<any>> {
  return response.data;
}

getBaseConfig

async function getBaseConfig(): Promise<AxiosRequestConfig> {
  return {
    baseURL: "http://your_base_url.com",
    headers: {
      // any headers you want to assign for all request
      "Content-Encoding": "UTF-8",
      Accept: "application/json",
      "Content-Type": "application/json-patch+json",
    },
  };
}

errorCatch

function errorCatch(error: AxiosError): any {
  // any things you want
  throw error;
}