Skip to content
/ genapi Public

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

License

Notifications You must be signed in to change notification settings

hairyf/genapi

Repository files navigation

genapi

npm version npm downloads bundle JSDocs License

中文 | English English Docs

A lightweight API code generator—only the code you need.

Features

  • 🚀 Multiple HTTP Clients - Support for axios, fetch, ky, got, ofetch, tanstack-query, uni
  • 🔄 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:

# pnpm (recommended)
pnpm dlx @genapi/core genapi init
# npx @genapi/core genapi init
# yarn dlx @genapi/core genapi init

Or install and configure manually:

pnpm i @genapi/core @genapi/presets -D

Create genapi.config.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:

npx genapi
/*
 * @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<Types.Pet>({ 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<Types.Pet>({ method: 'post', url, data, ...config })
}

For more details and features, visit the documentation site.

License

MIT License © Hairyf

About

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

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •