JSPM

  • Created
  • Published
  • Downloads 481
  • Score
    100M100P100Q91588F
  • 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

Support OpenApi v3

An auto typescript code generator from swagger. Every endpoint create as function and full type base.

For Example: Get method of '/Account/{id}' path will be this code in services.ts

import { getAccount } from "./services";

const response = await getAccount({ id: 1234 });

install

$ yarn add swagger-typescript

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

swagger.config.json

{
  "url": "http://example.com/api/swagger.json",
  "dir": "./test",
  "prettierPath": ".prettierrc",
  "ignore": {
    //Will be ignore from services functions.
    "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 create custom response change responseWrapper function in config. You could do something like this.

export type SwaggerResponse<R> = R & { counter: number };

const counter = 0;

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

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;
}