|
| 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 | +``` |
0 commit comments