Skip to content

Auto generate typescript models and endpoints from swagger.json

License

Notifications You must be signed in to change notification settings

nightmaregaurav/swagger-endpoints-gen

Repository files navigation

Swagger Endpoints Generator (Ts/Js)

npm version HitCount
NPM


Description

Automatically generate typescript models and endpoints from swagger.json so api call is as simple as writing endpoints.API__GET__my_object.call({data:value});

Installation

Install swagger-endpoints-gen globally (Preferred)

npm install -g @nightmaregaurav/swagger-endpoints-gen

install swagger-endpoints-gen locally as dev dependency

npm install -D @nightmaregaurav/swagger-endpoints-gen

Usage

For command line usage

Contents of in.json:

[
  {
    "outDir": "/path/to/output/folder1",
    "baseUrl": "http://base.url.system",
    "bearerTokenImportPath": "@/auth/authHelpers",
    "successErrorMiddlewarePath": "@/middlewares/baseMiddlewares",
    "cacheHelperPath": "@/helpers/cacheHelpers",
    "swaggers": [],
    "swaggerUrls": ["http://base.url.system/swagger/v1/swagger.json"],
    "removeComments": true
  },
  {
    "outDir": "/path/to/output/folder2",
    "baseUrl": "http://base.url.system",
    "bearerTokenImportPath": "@/auth/authHelpers",
    "successErrorMiddlewarePath": "@/middlewares/baseMiddlewares",
    "cacheHelperPath": "@/helpers/cacheHelpers",
    "swaggers": [],
    "swaggerUrls": ["http://base.url.system/swagger/v1/swagger2.json"],
    "removeComments": false
  }
]
npx generate-endpoints-from-swagger in.json 

For programmatic usage

import { createEndpointsAndModels } from '@nightmaregaurav/swagger-endpoints-gen';
import swagger1 from './swagger1.json';
import swagger2 from './swagger2.json';
import swagger3 from './swagger3.json';
import swagger4 from './swagger4.json';

createEndpointsAndModels({
    outDir: "./endpoints",
    bearerTokenImportPath: "@/auth/authHelpers",
    successErrorMiddlewarePath: "@/middlewares/baseMiddlewares",
    cacheHelperPath: "@/helpers/cacheHelpers",
    baseUrl: "https://api.example.com",
    swaggers: [swagger1, swagger2, swagger3, swagger4],
    swaggerUrls: ["https://api.example.com/swagger/v1/swagger.json", "https://api.example.com/swagger/v2/swagger.json", "https://api.example.com/swagger/v3/swagger.json"],
    "removeComments": true
});

Example usage of generated endpoints

import { endpoints } from './endpoints';

let userInfo: UserModel = await endpoints.API__GET__user__userId.call({userId: 1});
let userId: number = await endpoints.API__POST__user__schoolId.call({schoolId:2}, {data: {name: "John Doe"}});
// rest of the codes

Technical Details

  • Language: Typescript

How to Contribute

  • Fork the repository
  • Clone the forked repository
  • Make changes
  • Commit and push the changes
  • Create a pull request
  • Wait for the pull request to be merged
  • Celebrate
  • Repeat

If you are new to open source, you can read this to learn how to contribute to open source projects.
If you are new to GitHub, you can read this to learn how to use GitHub.
If you are new to Git, you can read this to learn how to use Git.
If you are new to TypeScript, you can read this to learn how to use TypeScript.

License

Swagger Endpoints Generator is released under the MIT License. You can find the full license details in the LICENSE file.

Made with ❤️ by NightmareGaurav.


Open For Contribution