https://github.com/hairyf/genapi
API pipeline generator, which is used to convert OpenApi (v2~v3) and any input sources into TS/JS APIs, and currently supports axios, fetch, ky, got, ofetch
https://github.com/hairyf/genapi
api-generator generator javascript openapi swagger typescript
Last synced: about 1 month ago
JSON representation
API pipeline generator, which is used to convert OpenApi (v2~v3) and any input sources into TS/JS APIs, and currently supports axios, fetch, ky, got, ofetch
- Host: GitHub
- URL: https://github.com/hairyf/genapi
- Owner: hairyf
- License: mit
- Created: 2023-02-22T06:28:10.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-19T04:35:53.000Z (11 months ago)
- Last Synced: 2025-04-23T18:08:31.178Z (11 months ago)
- Topics: api-generator, generator, javascript, openapi, swagger, typescript
- Language: TypeScript
- Homepage:
- Size: 11.7 MB
- Stars: 40
- Watchers: 1
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# genapi
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![bundle][bundle-src]][bundle-href]
[![JSDocs][jsdocs-src]][jsdocs-href]
[![License][license-src]][license-href]
> [δΈζ](./README_CN.md) | **English**
> [English Docs](http://genapi-docs.vercel.app/)
A lightweight API code generatorβonly the code you need.
## Features
- π **Multiple HTTP Clients** - Support for [axios](https://github.com/axios/axios), [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), [ky](https://github.com/sindresorhus/ky), [got](https://github.com/sindresorhus/got), [ofetch](https://github.com/unjs/ofetch), [tanstack-query](https://github.com/TanStack/query), [uni](https://github.com/uni-helper/uni-network)
- π **TypeScript & JavaScript** - Generate both TS and JS APIs with full type definitions
- π **Schema Mode** - Type-safe fetch APIs with schema-based typing (supports `fetch` and `ofetch` presets)
- π **Multiple Data Sources** - Support for OpenAPI 2.0/3.x, Swagger and other input sources
- π§ **Interactive CLI** - Use `genapi init` for guided setup with preset selection
- π οΈ **Customizable Pipeline** - Flexible pipeline system for customizing the generation process
- π **Transform & Patch** - Batch transform operations and types, or make exact-match modifications
- π **Mock Data** - Automatically generate mock methods for each API function (requires `better-mock`)
- π **Multiple Services** - Support for projects with multiple API services via `servers` configuration
- β‘οΈ **Type Safety** - Full TypeScript support with type inference and IntelliSense
- π¦ **Zero Config** - Works out of the box with sensible defaults, customize as needed
## Usage
### Init Project
Just run the following command to init your project:
```bash
# pnpm (recommended)
pnpm dlx @genapi/core genapi init
# npx @genapi/core genapi init
# yarn dlx @genapi/core genapi init
```
Or install and configure manually:
```bash
pnpm i @genapi/core @genapi/presets -D
```
Create `genapi.config.ts`:
```ts
import { defineConfig } from '@genapi/core'
import { axios } from '@genapi/presets'
export default defineConfig({
preset: axios.ts,
input: 'https://petstore3.swagger.io/api/v3/openapi.json',
output: {
main: 'src/api/index.ts',
type: 'src/api/index.type.ts',
},
})
```
Then run to generate API code like:
```bash
npx genapi
```
```ts
/*
* @title Swagger Petstore - OpenAPI 3.0
* ... other metadata ...
*/
import type { AxiosRequestConfig } from 'axios'
import type * as Types from './index.type'
import http from 'axios'
/**
* @summary Update an existing pet.
* @description Update an existing pet by Id.
* @method put
* @tags pet
*/
export function putPet(data?: Types.Pet, config?: AxiosRequestConfig) {
const url = '/pet'
return http.request({ method: 'put', url, data, ...config })
}
/**
* @summary Add a new pet to the store.
* @description Add a new pet to the store.
* @method post
* @tags pet
*/
export function postPet(data?: Types.Pet, config?: AxiosRequestConfig) {
const url = '/pet'
return http.request({ method: 'post', url, data, ...config })
}
```
For more details and features, visit the [documentation site](http://genapi-docs.vercel.app/).
## License
[MIT](./LICENSE) License Β© [Hairyf](https://github.com/hairyf)
[npm-version-src]: https://img.shields.io/npm/v/@genapi/core?style=flat&colorA=080f12&colorB=1fa669
[npm-version-href]: https://npmjs.com/package/@genapi/core
[npm-downloads-src]: https://img.shields.io/npm/dm/@genapi/core?style=flat&colorA=080f12&colorB=1fa669
[npm-downloads-href]: https://npmjs.com/package/@genapi/core
[bundle-src]: https://img.shields.io/bundlephobia/minzip/@genapi/core?style=flat&colorA=080f12&colorB=1fa669&label=minzip
[bundle-href]: https://bundlephobia.com/result?p=@genapi/core
[license-src]: https://img.shields.io/github/license/hairyf/genapi.svg?style=flat&colorA=080f12&colorB=1fa669
[license-href]: https://github.com/hairyf/genapi/blob/main/LICENSE
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
[jsdocs-href]: https://www.jsdocs.io/package/@genapi/core