openapi2aspida
Version:
Convert OpenAPI 3.0 or Swagger 2.0 definitions into aspida
132 lines (93 loc) • 2.95 kB
Markdown
<br />
<img src="https://aspida.github.io/aspida/logos/png/logo.png" alt="aspida" title="aspida" />
<div align="center">
<a href="https://www.npmjs.com/package/openapi2aspida">
<img src="https://img.shields.io/npm/v/openapi2aspida" alt="npm version" />
</a>
<a href="https://www.npmjs.com/package/openapi2aspida">
<img src="https://img.shields.io/npm/dm/openapi2aspida" alt="npm download" />
</a>
</div>
<br />
<p align="center">Convert OpenAPI 3.0 and Swagger 2.0 definitions into <a href="https://github.com/aspida/aspida/tree/master/packages/aspida">aspida</a>.</p>
<br />
<br />
Since openapi2aspida >= `0.18.0` , decision whether to be required follows the OpenAPI spec correctly. Dropping the support for original implementation, that was defaulting to required.
Since openapi2aspida >= `0.16.0` , requires TypeSciprt 3.8 or higher for Type-Only Imports.
Since openapi2aspida >= `0.14.0` , request headers are forced to be optional.
Since openapi2aspida >= `0.13.0` , optional for aspida only if the 'required' property of OpenAPI is set to `false`.
Compatible with yaml/json of OpenAPI3.0/Swagger2.0
```sh
$ mkdir petstore-api
$ cd petstore-api
$ npx openapi2aspida -i https://petstore.swagger.io/v2/swagger.json # or ../local-swagger.yaml
$ npm init -y
$ npm install @aspida/axios axios typescript ts-node @types/node
```
`index.ts`
```ts
import axiosClient from "@aspida/axios"
import api from "./api/$api"
import type { Pet } from "./api/@types"
;(async () => {
const client = api(axiosClient())
const petId = 100
const body: Pet = {
id: petId,
name: "hoge",
photoUrls: [],
status: "available"
}
await client.pet.$post({ body })
const pet = await client.pet._petId(petId).$get()
console.log(pet)
})()
```
`package.json`
```json
{
"scripts": {
"start": "ts-node index.ts"
}
}
```
```sh
$ npm start
```
Create config file in project root
`aspida.config.js`
```js
module.exports = {
input: "api", // "input" of aspida is "output" for openapi2aspida
outputEachDir: true, // Generate $api.ts in each endpoint directory
openapi: { inputFile: "https://petstore.swagger.io/v2/swagger.json" }
}
```
```sh
$ npx openapi2aspida
```
path to an OpenAPI spec file for input
can change aspida output directory
path to an aspida config file
displays version of openapi2aspida
```bash
npx openapi2aspida -i=openApi/sample.yaml -o=lib/api/sample
```
openapi2aspida is licensed under a [MIT License](https://github.com/aspida/openapi2aspida/blob/master/LICENSE).