Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/0x-jerry/openapi-ts
Openapi schema to client typescript code
https://github.com/0x-jerry/openapi-ts
generate-code openapi swagger typescript
Last synced: 6 days ago
JSON representation
Openapi schema to client typescript code
- Host: GitHub
- URL: https://github.com/0x-jerry/openapi-ts
- Owner: 0x-jerry
- Created: 2023-12-19T14:49:52.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-11-05T01:32:05.000Z (13 days ago)
- Last Synced: 2024-11-05T02:28:03.504Z (13 days ago)
- Topics: generate-code, openapi, swagger, typescript
- Language: TypeScript
- Homepage:
- Size: 292 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# OpenAPI Code Generator
This package provide a quick way to transform OpenAPI schema or Swagger schema to client typescript code.
## Usage
1. Generate client typescript code
```ts
import { generate } from '@0x-jerry/openapi-ts'const apiUrl = 'http://doc-url/api-docs'
const swaggerSchema = await (await fetch(apiUrl)).json()
await generate({
schema: swaggerSchema,
output: 'api/generated',
format: true,
clean: true,
})
```2. Implement custom API adapter `api/_adapter.ts`
```ts
import { type RequestParams } from '@0x-jerry/openapi-ts/runtime'export interface Config {
customOption?: any
}export const _request = async (data: RequestParams) => {
const resp = await doRequest({
url: data.url,
method: data.method,
searchParams: data.query,
body: data.data,
config: data.config,
})return resp
}
```3. Use generated code
```ts
import * as api from './api/generated'const resp = await api.segment.to.path.$get({...})
```