1.1.0 • Published 8 months ago

tswagger v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

TSwagger

TS-Swagger plugin generator CLI
CLI to generate Axios or other API client from Swagger schema

npm package github stars

Installation

npm i -D tswagger

Basic Usage

in project directory

npx tswagger https://petstore3.swagger.io/api/v3/openapi.json

in script code

import { createApi } from './lib/api'
const api = createApi()
const foo = await api.foo.bar(1).post(requestBody) // Post /foo/{bar} 
api.foo.bar.get() // GET /foo/bar

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) // GET /foo/{bar} 
fetchApi.foo.bar.get() // GET /foo/bar


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

npx tswagger argument1 --option1 value1 --option2 value2
optiondescriptiondefaultexample
(first argument)Swagger schema JSON path(required)http://.. or ./foo/swagger.json
srcsame as first argumentfirst argumentsame as above
modeCode generation modeaxiosrequest
pathOutput pathlib/apiapi.ts
export-nameExport namecreateApi''(export default)
type-pathPath for scheme type file{dir of path}/types.ts./types/models.ts
tagTags to generate(All tags)--tag AA --tag BB

Options from file

Options can be an array

tswagger.config.js

// import { TSwaggerOptions } from 'tswagger'
export default { src: '...' } // satisfies Partial<TSwaggerOptions>

package.json

{
  "tswagger": {
    "src": "..."
  }
}

License

ISC License Copyright (c) 2023, Elevista

1.1.0

8 months ago

1.0.2

1 year ago

1.1.0-9

10 months ago

1.1.0-8

11 months ago

1.1.0-7

11 months ago

1.0.3

12 months ago

1.1.0-6

11 months ago

1.1.0-5

11 months ago

1.1.0-4

11 months ago

1.1.0-3

11 months ago

1.1.0-2

11 months ago

1.1.0-10

10 months ago

1.1.0-1

11 months ago

1.1.0-0

11 months ago

1.0.1

1 year ago

1.0.0

2 years ago

1.0.0-alpha.5

2 years ago

1.0.0-alpha.4

2 years ago

1.0.0-alpha.3

2 years ago

1.0.0-alpha.2

2 years ago

1.0.0-alpha.1

2 years ago

1.0.0-alpha.0

2 years ago