Giter Club home page Giter Club logo

openapi-ts-generator's Introduction

Build Status npm version npm downloads Quality Gate Status Issues Coverage Lines of Code Security Rating CodeQL

OpenApi-TS-Generator

NPM package based on swagger-ts-generator to generate typescript models for endpoints documented by swagger using the relatively new OpenAPI spec.

TypeScript usage with a hosted OpenApi Spec document.

import { generateTsModels } from 'openapi-ts-generator';

generateTsModels({
  openApiJsonUrl: '{Your Swagger Enpoint URL here}',
  outputPath: './{outputFolder}/',
});

TypeScript usage with an OpenApi Spec document stored on your local computer.

import { generateTsModels } from 'openapi-ts-generator';

generateTsModels({
  openApiJsonFileName: '{location and file name of your OpenApi document}',
  outputPath: './{outputFolder}/',
});

Javascript usage with a hosted OpenApi Spec document.

const generator = require('openapi-ts-generator');

generator.generateTsModels({
  openApiJsonUrl: '{Your Swagger Enpoint URL here}',
  outputPath: './{outputFolder}/',
  genAngularFormGroups: true /* Set this to true if only if you're in an Angular project*/
});

Javascript usage with an OpenApi Spec document stored on your local computer.

const generator = require('openapi-ts-generator');

generator.generateTsModels({
  openApiJsonFileName: '{location and file name of your OpenApi document}',
  outputPath: './{outputFolder}/',
});

Want to include Angular FormGroup Factories?

const generator = require('openapi-ts-generator');

generator.generateTsModels({
  openApiJsonFileName: '{location and file name of your OpenApi document}',
  outputPath: './{outputFolder}/',
  genAngularFormGroups: true
});

Working example with NRSRx based service

import { generateTsModels } from 'openapi-ts-generator';

generateTsModels({
  openApiJsonUrl: 'https://im-wa-cmpo-nrsr.azurewebsites.net/swagger/v1/swagger.json',
  outputPath: './models/',
  typeFilterCallBack: (val, i, arr) => !val.name.endsWith('ODataEnvelope'),
  valuePropertyTypeFilterCallBack: (val, i, arr) => !val.name.startsWith('created') && !val.name.startsWith('updated'),
});

Check out openapi-mermaid, it will auto-generate mermaid entity diagrams using your swagger docs.

openapi-ts-generator's People

Contributors

dependabot[bot] avatar dimre-lab49 avatar ikemtz avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

openapi-ts-generator's Issues

Customize templates, generate only needed files

Hello, I use you lib, but for React. So I use only models, the other Angular stuff is beyond my interest. Is it possible to add custom templates and generate templates that I need only?

Add support for local OpenAPI spec

For now it is throwing a error:

(node:63009) UnhandledPromiseRejectionWarning: TypeError: Only absolute URLs are supported
    at getNodeRequestOptions (/Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/node-fetch/lib/index.js:1299:9)
    at /Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/node-fetch/lib/index.js:1404:19
    at new Promise (<anonymous>)
    at Function.fetch [as default] (/Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/node-fetch/lib/index.js:1401:9)
    at /Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/openapi-ts-generator/lib/index.js:72:66
    at step (/Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/openapi-ts-generator/lib/index.js:33:23)
    at Object.next (/Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/openapi-ts-generator/lib/index.js:14:53)
    at /Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/openapi-ts-generator/lib/index.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/arikon/projects/smart-oil/openapi-ts-generator-test/node_modules/openapi-ts-generator/lib/index.js:4:12)
(node:63009) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:63009) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The code is:

const generator = require('openapi-ts-generator');

generator.generateTsModels({
    openApiJsonUrl: '../har2openapi/output/generated.json',
    outputPath: './src/',
});

Error When Generating

$ node update-services.ts 
C:\dev\betsy\app\node_modules\openapi-ts-generator\openapidoc-converter.js:266
            isEnum: (propertySchema.enum || []).length > 0,
                                    ^

TypeError: Cannot read properties of undefined (reading 'enum')
    at OpenApiDocConverter.convertReferenceObjectToPropertyType (C:\dev\betsy\app\node_modules\openapi-ts-generator\openapidoc-converter.js:266:37)
    at OpenApiDocConverter.buildSchemaWrapperInfo (C:\dev\betsy\app\node_modules\openapi-ts-generator\openapidoc-converter.js:104:69)
    at OpenApiDocConverter.convertEntities (C:\dev\betsy\app\node_modules\openapi-ts-generator\openapidoc-converter.js:55:26)
    at OpenApiDocConverter.convertDocument (C:\dev\betsy\app\node_modules\openapi-ts-generator\openapidoc-converter.js:27:29)
    at Object.<anonymous> (C:\dev\betsy\app\node_modules\openapi-ts-generator\index.js:59:46)
    at step (C:\dev\betsy\app\node_modules\openapi-ts-generator\index.js:33:23)
    at Object.next (C:\dev\betsy\app\node_modules\openapi-ts-generator\index.js:14:53)
    at fulfilled (C:\dev\betsy\app\node_modules\openapi-ts-generator\index.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.15.0

Can find a copy of my openapi spec here: https://qa.betsybot.xyz/betsyapi-qa/swagger/v1/swagger.json
It is "openapi": "3.0.1", generated from a .NET project. Works in other systems.

Generates just empty file index.ts

The result:

/**
 * This file is generated by the openapi-ts-generator
 * #generate-barrel-ts.hbs
 * For issues or feature request, visit the repo: https://github.com/ikemtz/openapi-ts-generator
 * Do not edit.
 */
/* tslint:disable */

Input file:

{
  "openapi": "3.0.0",
  "info": {
    "title": "LoopBack Application",
    "version": "1.0.0"
  },
  "paths": {
    "/fuel-types": {
      "get": {
        "description": "",
        "operationId": "get-fuel-types",
        "parameters": [
          {
            "description": "type",
            "in": "query",
            "name": "type",
            "schema": {
              "default": "fuelTypeList",
              "example": "fuelTypeList",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "List ",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/fuel-types?type=fuelTypeList"
        }
      },
      "parameters": []
    },
    "/fueling": {
      "get": {
        "description": "",
        "operationId": "get-fueling",
        "parameters": [
          {
            "description": "to",
            "in": "query",
            "name": "to",
            "schema": {
              "default": "25",
              "example": "25",
              "type": "string"
            }
          },
          {
            "description": "page",
            "in": "query",
            "name": "page",
            "schema": {
              "default": "1",
              "example": "1",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "List ",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/fueling?to=25&page=1"
        }
      },
      "parameters": [],
      "post": {
        "description": "",
        "operationId": "post-fueling",
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "examples": {
                "example-1": {
                  "value": {
                    "account_id": "",
                    "comment": "",
                    "counter_log": {
                      "primary": {
                        "counter_id": 899,
                        "locked": null,
                        "value": "102616"
                      }
                    },
                    "date": "2020-06-02T21:06:21.000Z",
                    "fuel_type_id": 1031,
                    "liters": "10",
                    "price": "46",
                    "sum": 460,
                    "vehicle_id": 450,
                    "vendor_id": 209
                  }
                }
              },
              "schema": {
                "properties": {},
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Item created"
          }
        },
        "summary": "Create ",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/fueling"
        }
      }
    },
    "/fueling-transaction": {
      "get": {
        "description": "",
        "operationId": "get-fueling-transaction",
        "parameters": [
          {
            "description": "distributed",
            "in": "query",
            "name": "distributed",
            "schema": {
              "default": "not-distributed",
              "example": "not-distributed",
              "type": "string"
            }
          },
          {
            "description": "to",
            "in": "query",
            "name": "to",
            "schema": {
              "default": "10",
              "example": "10",
              "type": "string"
            }
          },
          {
            "description": "page",
            "in": "query",
            "name": "page",
            "schema": {
              "default": "1",
              "example": "1",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "List ",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/fueling-transaction?distributed=not-distributed&to=10&page=1"
        }
      },
      "parameters": []
    },
    "/fueling-transaction/log": {
      "get": {
        "description": "",
        "operationId": "get-fueling-transaction-log",
        "parameters": [
          {
            "description": "to",
            "in": "query",
            "name": "to",
            "schema": {
              "default": "25",
              "example": "25",
              "type": "string"
            }
          },
          {
            "description": "page",
            "in": "query",
            "name": "page",
            "schema": {
              "default": "1",
              "example": "1",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "List fueling-transactions",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/fueling-transaction/log?to=25&page=1"
        }
      },
      "parameters": []
    },
    "/fueling/{id}": {
      "delete": {
        "description": "",
        "operationId": "delete-fueling-id",
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "examples": {
                "example-1": {
                  "value": {}
                }
              },
              "schema": {
                "properties": {},
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Item deleted"
          }
        },
        "summary": "Delete fuelings",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/fueling/3739"
        }
      },
      "parameters": [
        {
          "description": "Unique ID of the id you are working with",
          "in": "path",
          "name": "id",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ]
    },
    "/vehicle-counter-logs/hint": {
      "get": {
        "description": "",
        "operationId": "get-vehicle-counter-logs-hint",
        "parameters": [
          {
            "description": "primary",
            "in": "query",
            "name": "primary",
            "schema": {
              "default": "primary",
              "example": "primary",
              "type": "string"
            }
          },
          {
            "description": "vehicle_id",
            "in": "query",
            "name": "vehicle_id",
            "schema": {
              "default": "450",
              "example": "450",
              "type": "string"
            }
          },
          {
            "description": "user_date",
            "in": "query",
            "name": "user_date",
            "schema": {
              "default": "2020-06-02T21:06:21.000Z",
              "example": "2020-06-02T21:06:21.000Z",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "List vehicle-counter-logs",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/vehicle-counter-logs/hint?primary=primary&vehicle_id=450&user_date=2020-06-02T21:06:21.000Z"
        }
      },
      "parameters": []
    },
    "/vehicles": {
      "get": {
        "description": "",
        "operationId": "get-vehicles",
        "parameters": [
          {
            "description": "type",
            "in": "query",
            "name": "type",
            "schema": {
              "default": "names",
              "example": "names",
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "List ",
        "x-meta": {
          "element": "",
          "originalPath": "https://app.zavgar.online/api/vehicles"
        }
      },
      "parameters": []
    }
  },
  "servers": [
    {
      "url": "/"
    }
  ]
}

Code:

const generator = require('openapi-ts-generator');

generator.generateTsModels({
    openApiJsonFileName: require.resolve('../har2openapi/output/generated.json'),
    outputPath: './src/',
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.