1.1.0 • Published 8 months ago
tswagger v1.1.0
TSwagger
TS-Swagger plugin generator CLI
CLI to generate Axios or other API client from Swagger schema
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
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 |
path | Output path | lib/api | api.ts |
export-name | Export name | createApi | '' (export default) |
type-path | Path for scheme type file | {dir of path}/types.ts | ./types/models.ts |
tag | Tags 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