JSPM

  • Created
  • Published
  • Downloads 764
  • Score
    100M100P100Q97026F
  • License ISC

TS-Swagger plugin generator CLI

Package Exports

  • tswagger
  • tswagger/template
  • tswagger/tsDoc

Readme

TSwagger

TS-Swagger plugin generator CLI

npm package github stars

Installation

npm i -D tswagger

Basic Usage

in project directory

npx tswagger https://api.server.foo/swagger.json

in script code

import { createApi } from './lib/api'
const api = createApi()
const foo = await api.foo.bar(1).get(2)
api.foo.bar.get()

Use fetch or other request library

npx tswagger https://api.server.foo/swagger.json --mode request
import { createApi } from './lib/api'
const fetchApi = createApi((path, method, { params, formData, body }) => {
  const url = new URL(`http://localhost${path}`)
  const url.searchParams = (...)
  return fetch(url, {
    method,
    body: formData ?? body ?? JSON.stringify(body),
  })
}, response => response.json())
const {data} = await fetchApi.foo.bar(1).get(2)
fetchApi.foo.bar.get()


import axios, { AxiosError } from 'axios'
const axiosApi = createApi((path, method, { params, formData, body }) =>
  axios({ url: path, method, params, data: formData ?? body }),
  response => response.data)
const {data} = axiosApi.foo.bar(1).get(2)

Options

options priority : command line > tswagger.config > package.json

npx tswagger argument1 --option1 value1 --option2 value2
option description default example
(first argument) Swagger schema JSON path (required) http://.. or ./foo/swagger.json
src same as first argument first argument same as above
mode Code generation mode axios request
plugins-dir Directory lib
plugin-name Name for generated flile api
export-name Export name createApi ''(export default)
type-path Path for scheme type file {plugins-dir}/{plugin-name}/{types.ts} ./types/models.ts
base-path base path /v1 /v2
skip-header Ignore parameter in header false true

Set options using tswagger.config

import { TSwaggerOptions } from 'tswagger'

const option: Partial<TSwaggerOptions> = {
  pluginName: 'foo'
}

export default option

Set options using package.json

{
  "tswagger": {
    "pluginsDir": "api"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "types": ["tswagger/types"]
  }
}

and npm run swagger or npx tswagger

License

ISC License Copyright (c) 2023, Elevista