openapi-definition-to-editor

generation of API client libraries (SDK generation)


Keywords
openapi, openapi3, swagger, typescript
License
ISC
Install
npm install openapi-definition-to-editor@1.0.122

Documentation

Install

Run Tests Create Tag And Release And Publish To NPM

npm i openapi-toolkit

Auto Generate Client/Server (Cli)

npm i -g openapi-toolkit

# example
openapi-toolkit -i https://petstore3.swagger.io/api/v3/openapi.json -g typescript-axios -o ./src/services/petStore --modelNamePrefix My --modelNameSuffix .dto

# show all options
openapi-toolkit -h

# with docker
docker run --rm --name openapi-toolkit -v "$(pwd)/output:/output" -e CLI_PARAMS="-i https://petstore3.swagger.io/api/v3/openapi.json -g typescript-axios --modelNamePrefix My --modelNameSuffix .dto" barnuri/openapi-toolkit

Help output

openapi-toolkit <command>, default command 'generate'

Commands:
  generate    auto generate proxy client from swagger file             [default]
  generators  generators list
  completion  generate completion script

Options:
      --version                Show version number                     [boolean]
  -h, --help                   Show help                               [boolean]
  -i, --pathOrUrl              path or url for swagger file           [required]
  -o, --output                 output path                            [required]
  -g, --generator              generator name      [default: "typescript-axios"]
  -t, --type                   [choices: "client", "server"] [default: "client"]
  -n, --namepsace                         [default: "OpenapiDefinitionGenerate"]
      --modelsFolderName                                     [default: "models"]
      --modelNamePrefix                                            [default: ""]
      --modelNameSuffix                                            [default: ""]
      --controllersFolderName                           [default: "controllers"]
      --controllerNamePrefix                                       [default: ""]
      --controllerNameSuffix                             [default: "Controller"]

Vscode Plugin For Auto Generate

Examples

1) React Example

2) Angular Example


3) Look at editorInputToHtml.ts to handle editors by yourself

editorInputToHtml.ts

Example of using editorInputToHtml.ts

Download this file

https://petstore.swagger.io/v2/swagger.json

Then use this code

import { writeFileSync } from 'fs';
import openapiSchemaExample from './openapiSchemaExample.json';
import { editorInputToHtml, getEditor, OpenApiDocument } from 'openapi-toolkit';

const ex = async () = {
    await axios.get('https://petstore.swagger.io/v2/swagger.json')
    const editors = ['Order', 'User', 'Category', 'Tag', 'Pet', 'ApiResponse'].map(tabName => getEditor((openapiSchemaExample as any) as OpenApiDocument, tabName));
    const html = editorInputToHtml(editors);
    writeFileSync('./openapiSchemaExample.result.json', JSON.stringify(editors, undefined, 4), 'utf-8');
    writeFileSync('./openapiSchemaExample.html', html, 'utf-8');
    var start = process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open';
    require('child_process').exec(start + ' ' + './openapiSchemaExample.html');
}

Result

Example