Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

swagger-typescript

hosseinmd3.8kMIT6.9.0TypeScript support: included

An auto ts/js code generate from swagger/openApi

swagger, typescript, nswag, openApi 3, swagger 2, code generator, hossein mohammadi

readme

NPM

install size dependencies

:star::star::star: If you would like to contribute, please refer to To do list and a list of open tasks.:star::star::star:

Swagger-Typescript: Generate ts/js code from swagger/openApi JSON

Support OpenApi v3, swagger v2 and postman collection

An auto typescript/javascript/kotlin code generator from APIs doc. Each endpoint will be created as a function, full type base. Supported

  • Generating a function for every apis
  • Generating all types, interfaces and enums which used in apis
  • React hooks.
  • SignalR hub.
  • Generating mock data.

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 prettier -D && yarn add axios

get start

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

swagger.config.json

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

run

yarn swag-ts

config.ts

more

baseConfig

const baseConfig: AxiosRequestConfig = {
  baseURL: "", // <--- Add your base url
  //other static configs
};

Now you can use APIs, So for advanced config read below.

swagger.config.json

For Example:

{
  "$schema": "https://raw.githubusercontent.com/hosseinmd/swagger-typescript/master/schema/v6.json",
  "url": "http://example.com/api/swagger.json",
  "dir": "./services",
  "prettierPath": ".prettierrc",
  "language": "typescript"
}
[Key] [default] Comment
url Required swagger or postman collection Address. can be online or local (json/yaml) (specific branch)
dir Required Address of output
language typescript export to "javascript", "typescript" or "kotlin"
methodName {method}{path} Supported mixed of "{method}{path}{operationId}". for Example: 'service{method}{path}'
prefix Optional prefix value will be removed from method name For example your endpoints is like "/api/v2/users", If you don't want add "/api/v2" to method name, add it to prefix
ignore Optional Ignore headers from type for Example: "ignore": { "headerParams": ["terminalId"]}
mock false For generate response mocks
keepJson false This code will keep previous JSON for updating partially. change it to true then generate service for creating your first json file then you can update a tag for example $ yarn swag-ts User will update your user APIs which have User tag
reactHooks false For generate react hooks of all APIs (using react-query under the hood)
useQuery [] List of apis which is get but developed with post methods (Is useful for rest apis) for Example: ["postTicketsGetall"] (Needed to enable reactHooks)
useInfiniteQuery [] List of apis which is get and could be handle infinity (Needed to enable reactHooks) parameter should be one of page, pageNo or pageNumber
local false update swagger with local swagger.json located in your dir folder. add it to your config file or run it with cli $ yarn swag-ts --local
kotlinPackage Required (Only kotlin) package name of source dir
generateEnumAsType false
includes [] A list of regex patterns that specify which APIs to include based on matching method names
excludes [] A list of regex patterns that specify which APIs to exclude based on matching method names
  • enum ReferralStatus {Successed="Successed","Error"="Error"}
  • type ReferralStatus="Successed" | "Error"; // generateEnumAsType = true

CLI Options

[Key] [default] Comment
local false update swagger with local swagger.json located in your dir folder. add it to your config file or run it with cli $ yarn swag-ts --local
branch Current Branch to generate swagger for develop run yarn swag-ts --branch=develop or your branch name should be develop or a branch which created from develop
config "./swagger.config.json" A path for config file location
- yarn swag-ts --config=./config path is related for "swagger.config.json" file in config folder
- yarn swag-ts --config=./config/myswagger.json you could change config file name
- yarn swag-ts --config=/user/hosseinmd/desktop/config/swagger.config.json you could add absolute path

Config

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

  • getAxiosInstance

    getAxiosInstance used for create an instance of axios request you can customize that for what you needed

  • baseConfig

    baseConfig used for get static configs and headers. if you need some dynamic configs like add authentication to headers use requestConfig.headers.authorization into of axiosInstance.interceptors.request.use function.

Run by node

const { generate } = require("swagger-typescript");

generate(config);
// or
generate(); // will be use ./swagger.config.json

Conflict

In some situation teams have parallel backend development which cause conflict when updating swagger for solving this we have partially update, you can update your service just for a few tags and keep other old services codes.

For Doing this you need to add this to your swagger.config.json

"keepJson": true,

This code will keep previous JSON for updating partially.

Run $ yarn swag-ts with your base backend, for example develop branch

Others need to pull this changes

Now you can update Tag1 and Tag2 $ yarn swag-ts Tag1 Tag2.

Multiple Gateway

if you have multiple gateway in your project you could handle it by add array of config in swagger.config.json

[
  {
    "$schema": "https://raw.githubusercontent.com/hosseinmd/swagger-typescript/master/schema/v6.json",
    "url": "http://example1.com/api/swagger.json",
    "dir": "./service1",
    "prettierPath": ".prettierrc",
    "language": "typescript"
  },
  {
    "$schema": "https://raw.githubusercontent.com/hosseinmd/swagger-typescript/master/schema/v6.json",
    "url": "http://example2.com/api/swagger.json",
    "dir": "./service2",
    "prettierPath": ".prettierrc",
    "language": "typescript"
  }
]

Specific branch

if you are managing project by multiple branch like gitflow, you need to update swagger based on you working branch or parent branch (for example your parent is develop if you create a branch from develop).

For Example:

{
  "url": [
    {
      "branch": "master",
      "url": "http:/example.com/api/swagger.json"
    },
    {
      "branch": "develop",
      "url": "http://stage.example.com/api/swagger.json"
    }
  ]
}

to generate swagger for develop run yarn swag-ts --branch=develop or your branch name should be develop or a branch which created from develop

Stories

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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

6.9.0 (2024-11-30)

Features

  • add --config to cli options (b95fd80)
  • add includes and excludes config options (#59) (7842bcd)

Bug Fixes

  • replace postman-ke-openapi instead of postman-to-openapi (52a90e9)

6.8.0 (2024-05-24)

Features

  • add --config to cli options (e16cf46)

6.7.0 (2024-03-06)

Features

  • move to new react-query (4138d9c)

6.6.1 (2023-12-23)

Bug Fixes

  • able to put custom formdata as body to support nodejs (fd6e2c1)

6.6.0 (2023-10-24)

Features

6.5.1 (2023-09-08)

Bug Fixes

6.5.0 (2023-07-21)

Features

Bug Fixes

6.4.0 (2022-11-19)

Features

Bug Fixes

6.3.5 (2022-10-06)

Bug Fixes

6.3.4 (2022-10-05)

Bug Fixes

  • add ts-nocheck to auto generated files (38973b0)

6.3.3 (2022-10-05)

Bug Fixes

  • change default response (d258005)
  • nullable and required together (20a311a)

6.3.2 (2022-10-02)

Bug Fixes

6.3.1 (2022-08-14)

Bug Fixes

  • application/x-www-form-urlencoded (16f3c2b)

6.3.0 (2022-08-12)

Features

Bug Fixes

  • await generateService in main function generate (#34) (75ec836)

6.2.7 (2022-08-02)

Bug Fixes

6.2.5 (2022-08-02)

Bug Fixes

  • export SwaggerTypescriptMutationDefaultParams (ec3277c)

6.2.4 (2022-07-25)

Bug Fixes

  • nullable query param swagger 2 (f0128a2)

6.2.3 (2022-07-25)

Bug Fixes

6.2.2 (2022-07-05)

Bug Fixes

6.2.1 (2022-06-22)

Bug Fixes

6.2.0 (2022-06-22)

Features

  • handle specifiic url for branchs (0fdffa5)

6.1.0 (2022-06-19)

Features

  • add isRequestError to isRequestError (3fa6222)

Bug Fixes

6.0.9 (2022-05-23)

Bug Fixes

6.0.8 (2022-05-17)

Bug Fixes

  • nullable properties of openApi 3 (f77e155)

6.0.5 (2022-02-08)

Bug Fixes

6.0.4 (2022-02-08)

Bug Fixes

  • configoverride default value (9a9e38f)

6.0.2 (2022-01-31)

Bug Fixes

6.0.1 (2022-01-15)

Bug Fixes

  • format output files (ecdcacc)
  • remove useless infinity code when infinty doesn't enabled (f4575d3)

6.0.0 (2021-12-26)

⚠ BREAKING CHANGES

  • desable generate auto useInfiniteQuery

Features

  • config to useInfiniteQuery (d6ed3c4)

5.9.1 (2021-12-26)

Bug Fixes

5.9.0 (2021-12-26)

Features

Bug Fixes

5.8.0 (2021-12-13)

Features

  • add generateEnumAsType to generate type insstead of enum (429385b)

Bug Fixes

5.7.0 (2021-12-06)

Features

  • add generateEnumAsType to generate type insstead of enum (429385b)

5.6.6 (2021-12-04)

Bug Fixes

5.6.5 (2021-11-16)

Bug Fixes

  • optional _extraVariables (cf04c84)

5.6.4 (2021-11-15)

Bug Fixes

5.6.0 (2021-10-27)

Features

5.5.0 (2021-10-20)

Features

5.4.0 (2021-10-20)

Features

5.3.3 (2021-10-16)

Bug Fixes

5.3.1 (2021-10-11)

Bug Fixes

5.3.0 (2021-10-11)

Features

  • add extra variables to mutation (e8589e6)

5.2.0 (2021-10-11)

Features

Bug Fixes

5.1.1 (2021-10-10)

Bug Fixes

  • add paginationFlattenData (5ca185c)

5.1.0 (2021-10-09)

Features

Bug Fixes

5.0.0 (2021-10-01)

⚠ BREAKING CHANGES

  • should change imports from services to types.ts

Features

  • generate useMutation for mutable apis (600ef47)
  • move types to types.ts (04e79f0)

Bug Fixes

4.12.3 (2021-09-28)

Bug Fixes

4.12.1 (2021-09-26)

Bug Fixes

4.12.0 (2021-09-26)

Features

Bug Fixes

  • nullable and required (b9480c3)
  • requestError to new typescript mode (675509c)

4.11.7 (2021-09-18)

Bug Fixes

  • support openapi 3 required and nullable (09d6c1e)

4.11.6 (2021-09-15)

Bug Fixes

  • fix prettier format for json files (#25) (148a509)

4.11.5 (2021-08-06)

Bug Fixes

  • handle application/x-www-form-urlencoded (e5a46d8)
  • remove useless async (dbb9cc4)

4.11.0 (2021-06-19)

Features

  • add partial Update sswagger based on tag (8011266)

4.10.2 (2021-06-12)

Bug Fixes

  • remove useless char from models name (e3ec1e1)

4.10.1 (2021-06-02)

Bug Fixes

4.10.0 (2021-05-25)

Features

  • add key to services function (4e0820b)

4.9.3 (2021-05-22)

Bug Fixes

4.9.2 (2021-05-18)

Bug Fixes

4.9.1 (2021-05-16)

Bug Fixes

4.9.0 (2021-04-28)

Features

Bug Fixes

4.8.0 (2021-03-05)

Features

  • generate response mocks (5b2d148)

Bug Fixes

  • mock: binary response and enum in object (f6dfbcc)
  • description (f3afab7)

4.7.0 (2021-01-28)

Features

  • add prefix to swagger.config (bb99b35)

Bug Fixes

4.6.0 (2021-01-27)

Features

Bug Fixes

4.5.0 (2021-01-20)

Features

4.4.0 (2021-01-15)

Features

  • reguestBody to formData multipart/form-data (37a83c2)

4.3.0 (2021-01-07)

Features

4.2.1 (2020-12-30)

Bug Fixes

  • syntax error when generating signalr (be9ac3f)

4.2.0 (2020-12-30)

Features

  • generate signalr hub interface (9e58f32)

4.1.1 (2020-12-26)

Bug Fixes

4.1.0 (2020-12-26)

Features

4.0.13 (2020-12-22)

Bug Fixes

  • add tsc-prog to dependencies (b8d9585)

4.0.12 (2020-12-16)