Skip to content

Commit d5cff3e

Browse files
committed
feat: add documentation and configuration for new docs package, enhance ESLint settings, and update pnpm workspace
1 parent ac39452 commit d5cff3e

24 files changed

Lines changed: 13682 additions & 2636 deletions

docs/.config/docs.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# yaml-language-server: $schema=https://unpkg.com/undocs/schema/config.json
2+
3+
name: genapi
4+
shortDescription: API generator that converts OpenAPI (v2~v3) and other input sources into TypeScript/JavaScript APIs
5+
description: Convert OpenAPI specifications into TypeScript/JavaScript API code with flexible customization options.
6+
github: hairyf/genapi
7+
url: 'https://genapi.hairyf.com'
8+
lang: en
9+
themeColor: green
10+
landing:
11+
heroCode: npm i @genapi/core @genapi/presets -D
12+
contributors: true
13+
features:
14+
- title: Multiple HTTP Clients
15+
icon: 🚀
16+
description: Support for axios, fetch, ky, got, ofetch, uni and more HTTP clients
17+
- title: TypeScript/JavaScript
18+
icon: 🔄
19+
description: Generate both TypeScript and JavaScript API code
20+
- title: Flexible & Customizable
21+
icon: 🛠️
22+
description: Flexible pipeline system for customizing the generation process
23+
- title: OpenAPI Support
24+
icon: 📋
25+
description: Full support for OpenAPI 2.0 and 3.x specifications
26+
automd: true
316 KB
Binary file not shown.

docs/.docs/public/icon.svg

Lines changed: 18 additions & 0 deletions
Loading

docs/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.nuxt
3+
.output
4+
dist

docs/1.guide/.navigation.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
icon: i-lucide-book-open

docs/1.guide/1.index.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Getting Started
2+
3+
genapi is a powerful API generator that converts OpenAPI (v2~v3) and other input sources into TypeScript/JavaScript APIs.
4+
5+
## Installation
6+
7+
You can install genapi via npm:
8+
9+
:pm-install{name="@genapi/core"}
10+
11+
You also need to install the presets package:
12+
13+
:pm-install{name="@genapi/presets"}
14+
15+
## Basic Usage
16+
17+
Create a configuration file `genapi.config.ts` in your project root:
18+
19+
```ts
20+
import { defineConfig } from '@genapi/core'
21+
import { axios } from '@genapi/presets'
22+
23+
export default defineConfig({
24+
preset: axios.ts,
25+
// your input source (swagger api url or json)
26+
input: 'http://example.com/api-docs',
27+
output: {
28+
main: 'src/api/index.ts',
29+
type: 'src/api/index.type.ts',
30+
},
31+
meta: {
32+
// your API baseUrl
33+
baseURL: 'import.meta.env.VITE_APP_BASE_API',
34+
// customize the output response type. default 'T'
35+
responseType: 'T extends { data?: infer V } ? V : void',
36+
},
37+
})
38+
```
39+
40+
Then run:
41+
42+
```bash
43+
npm run genapi
44+
```
45+
46+
## Features
47+
48+
- 🚀 **Multiple HTTP Clients** - Support for axios、fetch、ky、got、ofetch、uni and more
49+
- 🔄 **Language Support** - Generate both TypeScript and JavaScript APIs
50+
- 🛠️ **Customizable** - Flexible pipeline system for customizing the generation process
51+
- 📋 **OpenAPI Support** - Full support for OpenAPI 2.0 and 3.x specifications
52+
53+
## Next Steps
54+
55+
Check out the [Configuration Guide](/en/guide/configuration) to learn more about configuration options.

docs/1.guide/2.configuration.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Configuration
2+
3+
genapi defines API generation behavior through configuration files. Configuration files support TypeScript, JavaScript, or JSON formats.
4+
5+
## Configuration File
6+
7+
Create one of the following configuration files in your project root:
8+
9+
- `genapi.config.ts`
10+
- `genapi.config.js`
11+
- `genapi.config.json`
12+
13+
## Basic Configuration
14+
15+
```ts
16+
import { defineConfig } from '@genapi/core'
17+
import { axios } from '@genapi/presets'
18+
19+
export default defineConfig({
20+
preset: axios.ts,
21+
input: 'http://example.com/api-docs',
22+
output: {
23+
main: 'src/api/index.ts',
24+
type: 'src/api/index.type.ts',
25+
},
26+
})
27+
```
28+
29+
## Configuration Options
30+
31+
### preset
32+
33+
Preset configuration that defines how APIs are generated. genapi provides multiple presets:
34+
35+
- `axios.ts` / `axios.js` - Use axios client
36+
- `fetch.ts` / `fetch.js` - Use native fetch API
37+
- `ky.ts` / `ky.js` - Use ky client
38+
- `got.ts` / `got.js` - Use got client
39+
- `ofetch.ts` / `ofetch.js` - Use ofetch client
40+
- `uni.ts` / `uni.js` - Use uni-app network request library
41+
42+
### input
43+
44+
Input source, which can be:
45+
46+
- **URL string**: Directly pass in the API documentation URL
47+
```ts
48+
input: 'http://example.com/api-docs'
49+
```
50+
51+
- **Object**: Contains `uri` or `json` field
52+
```ts
53+
input: {
54+
uri: 'http://example.com/api-docs'
55+
}
56+
// or
57+
input: {
58+
json: { /* OpenAPI JSON object */ }
59+
}
60+
```
61+
62+
### output
63+
64+
Output configuration, defines the generated file paths:
65+
66+
```ts
67+
output: {
68+
main: 'src/api/index.ts', // Main file path
69+
type: 'src/api/index.type.ts', // Type definition file path (optional)
70+
}
71+
```
72+
73+
### meta
74+
75+
Metadata configuration for customizing generated code:
76+
77+
```ts
78+
meta: {
79+
// API base URL
80+
baseURL: 'import.meta.env.VITE_APP_BASE_API',
81+
// Custom response type transformation
82+
responseType: 'T extends { data?: infer V } ? V : void',
83+
}
84+
```
85+
86+
## Multiple Services
87+
88+
For projects with multiple services, use the `servers` configuration:
89+
90+
```ts
91+
export default defineConfig({
92+
// All servers inherit the upper layer configuration
93+
meta: {
94+
baseURL: 'https://example.com/api',
95+
},
96+
servers: [
97+
{
98+
input: 'http://service1/api-docs',
99+
output: { main: 'src/api/service1.ts' }
100+
},
101+
{
102+
input: 'http://service2/api-docs',
103+
output: { main: 'src/api/service2.ts' }
104+
},
105+
]
106+
})
107+
```
108+
109+
## Patch - Static Patches
110+
111+
Make exact-match modifications to operations and type definitions:
112+
113+
```ts
114+
export default defineConfig({
115+
preset: axios.ts,
116+
input: 'https://petstore.swagger.io/v2/swagger.json',
117+
patch: {
118+
operations: {
119+
// Rename function
120+
postUpdateUserUsingPOST: 'updateUserInfo',
121+
// Modify parameters and return type
122+
getUserUsingGET: {
123+
name: 'getUser',
124+
parameters: [{ name: 'id', type: 'string', required: true }],
125+
responseType: 'UserResponse'
126+
}
127+
},
128+
definitions: {
129+
// Rename type
130+
UserDto: 'User',
131+
// Override type (creates type alias)
132+
SessionDto: {
133+
name: 'Session',
134+
type: '{ name: string, age: number }'
135+
}
136+
}
137+
}
138+
})
139+
```
140+
141+
## Transform - Global Transformations
142+
143+
Batch transform operations and type definitions via functions:
144+
145+
```ts
146+
export default defineConfig({
147+
preset: axios.ts,
148+
input: 'https://petstore.swagger.io/v2/swagger.json',
149+
transform: {
150+
operation: name => `api_${name}`, // Batch add prefix
151+
definition: name => name.endsWith('Dto') ? name.slice(0, -3) : name
152+
},
153+
patch: {
154+
// transform executes first, patch executes after
155+
operations: { api_getUser: 'fetchUser' }
156+
}
157+
})
158+
```
159+
160+
## MockJS - Mock Data Generation
161+
162+
Automatically generate mock methods for each API function (requires `better-mock`):
163+
164+
```ts
165+
export default defineConfig({
166+
preset: axios.ts,
167+
input: 'https://petstore.swagger.io/v2/swagger.json',
168+
mockjs: true,
169+
})
170+
```
171+
172+
Usage:
173+
174+
```ts
175+
import { getUser } from './api'
176+
177+
const mockUser = getUser.mock() // Returns mock data conforming to type definitions
178+
```

docs/1.guide/3.presets.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Presets
2+
3+
genapi provides multiple preset configurations supporting different HTTP clients and languages.
4+
5+
## Available Presets
6+
7+
### axios
8+
9+
Use the popular axios HTTP client:
10+
11+
```ts
12+
import { axios } from '@genapi/presets'
13+
14+
export default defineConfig({
15+
preset: axios.ts, // TypeScript
16+
// or
17+
preset: axios.js, // JavaScript
18+
})
19+
```
20+
21+
### fetch
22+
23+
Use the native browser fetch API:
24+
25+
```ts
26+
import { fetch } from '@genapi/presets'
27+
28+
export default defineConfig({
29+
preset: fetch.ts,
30+
// or
31+
preset: fetch.js,
32+
})
33+
```
34+
35+
### ky
36+
37+
Use the tiny and elegant ky HTTP client:
38+
39+
```ts
40+
import { ky } from '@genapi/presets'
41+
42+
export default defineConfig({
43+
preset: ky.ts,
44+
// or
45+
preset: ky.js,
46+
})
47+
```
48+
49+
### got
50+
51+
Use the human-friendly got HTTP request library:
52+
53+
```ts
54+
import { got } from '@genapi/presets'
55+
56+
export default defineConfig({
57+
preset: got.ts,
58+
// or
59+
preset: got.js,
60+
})
61+
```
62+
63+
### ofetch
64+
65+
Use a better fetch API with TypeScript support:
66+
67+
```ts
68+
import { ofetch } from '@genapi/presets'
69+
70+
export default defineConfig({
71+
preset: ofetch.ts,
72+
// or
73+
preset: ofetch.js,
74+
})
75+
```
76+
77+
### uni
78+
79+
Use `@uni-helper/uni-network` uniapp network request library:
80+
81+
```ts
82+
import { uni } from '@genapi/presets'
83+
84+
export default defineConfig({
85+
preset: uni.ts,
86+
// or
87+
preset: uni.js,
88+
})
89+
```
90+
91+
## TypeScript vs JavaScript
92+
93+
- **TypeScript presets** (`*.ts`): Generate TypeScript code with complete type definitions
94+
- **JavaScript presets** (`*.js`): Generate JavaScript code with corresponding type definition files (`.d.ts`)
95+
96+
## Recommendations
97+
98+
- **Web projects**: Recommended to use `axios.ts` or `fetch.ts`
99+
- **Node.js projects**: Recommended to use `got.ts` or `ky.ts`
100+
- **Nuxt/UnJS projects**: Recommended to use `ofetch.ts`
101+
- **UniApp projects**: Use `uni.ts`

0 commit comments

Comments
 (0)