JSPM

  • Created
  • Published
  • Downloads 806
  • Score
    100M100P100Q110103F
  • 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/javascript code generator from swagger. Each endpoint will be constructed as a function, full type base.

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

import { getAccount } from "./services";

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

install

$ yarn add swagger-typescript

get start

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

swagger.config.json

{
  "url": "http://example.com/api/swagger.json",
  "dir": "./test"
}

run

yarn swag-ts

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.

getBaseConfig

async function getBaseConfig(): Promise<AxiosRequestConfig> {
  return {
    baseURL: "http://your_base_url.com",
    headers: {
      Authorization: `Bearer ${token}`, // <---- authorization
    },
  };
}

swagger.config.json

For Example:

{
  "url": "http://example.com/api/swagger.json",
  "dir": "./test",
  "prettierPath": ".prettierrc",
  "language": "typescript",
  "ignore": {
    "headerParams": ["terminalId"]
  }
}
[Key] [default] Comment
url Required Address of swagger.json
dir Required Address of output
language typescript export to "javascript" or "typescript"
methodName {method}{path} Supported mixed of "{method}{path}{operationId}". for Example: 'service{method}{path}'
ignore Optional Ignore headers from type for Example: "ignore": { "headerParams": ["terminalId"]}

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

Stories

why-you-should-use-swagger-typescript-for-generate-apis-code