Skip to content

Commit 533062e

Browse files
authored
Merge pull request #74 from alovajs/feature/[email protected]
docs: 新增开发工具插件文档
2 parents 4c3f31b + 7753e48 commit 533062e

File tree

24 files changed

+2623
-455
lines changed

24 files changed

+2623
-455
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: Renaming
3+
---
4+
5+
## Introduction
6+
7+
This plugin is used to rename URLs and parameters of API interfaces, supporting multiple naming styles and custom transformation rules. Key features include:
8+
9+
- Renaming for URLs, request parameters, path parameters, request bodies, response data, and reference type names.
10+
- Built-in naming conversions: camelCase, pascalCase, kebabCase, and snakeCase.
11+
- Support for custom matching rules and transformation functions.
12+
- Support for multi-rule configurations.
13+
14+
## Basic Usage
15+
16+
```javascript title="alova.config.js"
17+
import { defineConfig } from '@alova/wormhole';
18+
import { rename } from '@alova/wormhole/plugin';
19+
20+
export default defineConfig({
21+
generator: [
22+
{
23+
// ...
24+
plugin: [
25+
// Convert underscores in URLs to camelCase
26+
rename({ style: 'camelCase' })
27+
]
28+
}
29+
]
30+
});
31+
```
32+
33+
## Configuration Parameters
34+
35+
```typescript
36+
interface Config {
37+
/**
38+
* Scope of application, defaults to 'url'.
39+
* url: API path
40+
* params: Query parameters
41+
* pathParams: Path parameters
42+
* data: Request body data
43+
* response: Response data
44+
* refName: Reference type name
45+
*/
46+
scope?: 'url' | 'params' | 'pathParams' | 'data' | 'response' | 'refName';
47+
48+
/**
49+
* Matching rule. If not specified, all will be converted.
50+
* string: Contains this string
51+
* RegExp: Matches this regex
52+
* function: Custom matching function
53+
*/
54+
match?: string | RegExp | ((key: string) => boolean);
55+
56+
/**
57+
* Naming style.
58+
* camelCase: camelCase (userName)
59+
* kebabCase: kebab-case (user-name)
60+
* snakeCase: snake_case (user_name)
61+
* pascalCase: PascalCase (UserName)
62+
*/
63+
style?: 'camelCase' | 'kebabCase' | 'snakeCase' | 'pascalCase';
64+
65+
/**
66+
* Custom transformation function.
67+
* Executed before style conversion.
68+
*/
69+
transform?: (apiDescriptor: ApiDescriptor) => string;
70+
}
71+
72+
function rename(config: Config | Config[]): ApiPlugin;
73+
```
74+
75+
> **Note** 1. `refName` and `kebabCase` cannot be configured simultaneously because type names cannot be in kebab-case format. 2. `params`, `pathParams`, `data`, and `response` only rename the first-level names.
76+
77+
## Multi-Rule Configuration
78+
79+
```javascript
80+
// Configure multiple transformation rules simultaneously.
81+
// Before: /api/get_data/{item_id}
82+
// After: /api/getData/{itemId}
83+
rename([
84+
{
85+
scope: 'url',
86+
style: 'camelCase',
87+
match: /_/ // Only convert parts containing underscores
88+
},
89+
{
90+
scope: 'pathParams',
91+
style: 'camelCase'
92+
}
93+
]);
94+
```
95+
96+
### Custom Matching Rules
97+
98+
```javascript
99+
// Only rename parameter names longer than 5 characters.
100+
rename({
101+
scope: 'params',
102+
match: key => key.length > 5,
103+
style: 'camelCase'
104+
});
105+
```
106+
107+
### Custom Transformation Function
108+
109+
```javascript
110+
// Rename specific parameters.
111+
rename({
112+
scope: 'data',
113+
match: ['user_info', 'order_list'],
114+
transform: api => {
115+
const map = {
116+
user_info: 'user',
117+
order_list: 'orders'
118+
};
119+
return map[api.key] || api.key;
120+
}
121+
});
122+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: Tag Modifier
3+
---
4+
5+
## Introduction
6+
7+
This plugin is used to modify the `tags` of APIs from OpenAPI files.
8+
9+
## Basic Usage
10+
11+
```javascript title="alova.config.js"
12+
import { defineConfig } from '@alova/wormhole';
13+
import { tagModifier } from '@alova/wormhole/plugin';
14+
15+
export default defineConfig({
16+
generator: [
17+
{
18+
// ...
19+
plugin: [
20+
// Each API will call the callback function to return a new tag. If you don't want to modify a tag, return the original tag directly.
21+
tagModifier(tag => tag.toUpperCase()); // Example: Convert tag to uppercase
22+
]
23+
}
24+
]
25+
});
26+
```
27+
28+
## Configuration Parameters
29+
30+
```typescript
31+
type ModifierHandler = (tag: string) => string;
32+
33+
function tagModifier(handler: ModifierHandler): ApiPlugin;
34+
```
35+
36+
- `handler`: A function that takes the current `tag` as a parameter and returns the modified `tag`. If you don't want to modify it, return the original `tag` directly.
37+
38+
## Examples
39+
40+
### Example 1: Add a Prefix
41+
42+
```javascript
43+
// Add "api_" prefix to all tags
44+
tagModifier(tag => `api_${tag}`);
45+
```
46+
47+
### Example 2: Filter Specific Tags
48+
49+
```javascript
50+
// Only modify tags containing "user"
51+
tagModifier(tag => {
52+
if (tag.includes('user')) {
53+
return tag.replace('user', 'member');
54+
}
55+
return tag; // Keep other tags unchanged
56+
});
57+
```
58+
59+
### Example 3: Convert to Camel Case
60+
61+
```javascript
62+
// Convert tags to camel case
63+
tagModifier(tag => {
64+
return tag
65+
.split('_')
66+
.map((part, index) => {
67+
if (index === 0) return part;
68+
return part.charAt(0).toUpperCase() + part.slice(1);
69+
})
70+
.join('');
71+
});
72+
```
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
---
2+
title: Payload Modifier
3+
---
4+
5+
## Introduction
6+
7+
This plugin is designed to flexibly modify request and response parameters of API interfaces. It supports adding, deleting, and modifying parameter types, as well as adjusting parameter hierarchies via the `flat` feature.
8+
9+
Key features include:
10+
11+
- Supports modifying parameters in the following scopes: `params`, `pathParams`, `data`, and `response`.
12+
- Allows precise control over modification scope using parameter name matching rules (`match`).
13+
- Enables dynamic modification of parameter types and required status via the `handler` function.
14+
15+
## Basic Usage
16+
17+
```javascript title="alova.config.js"
18+
import { defineConfig } from '@alova/wormhole';
19+
import { payloadModifier } from '@alova/wormhole/plugin';
20+
21+
export default defineConfig({
22+
generator: [
23+
{
24+
// ...
25+
plugin: [
26+
// Modify the `userId` field in request parameters
27+
payloadModifier([
28+
{
29+
scope: 'params',
30+
match: key => key === 'userId',
31+
handler: schema => {
32+
return {
33+
'attr1?': 'string', // Mark as optional
34+
attr2: 'number', // Mark as required
35+
attr3: {
36+
// Nested data
37+
innerAttr: ['string', 'number', 'boolean']
38+
}
39+
};
40+
}
41+
}
42+
])
43+
]
44+
}
45+
]
46+
});
47+
```
48+
49+
## Configuration Parameters
50+
51+
### Type Definitions
52+
53+
```typescript
54+
/**
55+
* Scope of parameter modification
56+
*/
57+
type ModifierScope = 'params' | 'pathParams' | 'data' | 'response';
58+
59+
/**
60+
* Primitive types
61+
*/
62+
type SchemaPrimitive =
63+
| 'number'
64+
| 'string'
65+
| 'boolean'
66+
| 'undefined'
67+
| 'null'
68+
| 'unknown'
69+
| 'any'
70+
| 'never';
71+
72+
/**
73+
* Array type
74+
*/
75+
type SchemaArray = {
76+
type: 'array';
77+
items: Schema;
78+
};
79+
80+
/**
81+
* Reference type (optional parameters are marked with `?` at the end of the key)
82+
*/
83+
type SchemaReference = {
84+
[attr: string]: Schema;
85+
};
86+
87+
/**
88+
* Data Schema (supports union types)
89+
*/
90+
type Schema =
91+
| SchemaPrimitive
92+
| SchemaReference
93+
| SchemaArray
94+
| Array<SchemaPrimitive | SchemaReference | SchemaArray>
95+
| { oneOf: Schema[] }
96+
| { anyOf: Schema[] }
97+
| { allOf: Schema[] };
98+
99+
/**
100+
* Configuration interface
101+
*/
102+
interface Config<T extends Schema> {
103+
/**
104+
* Scope of application
105+
*/
106+
scope: ModifierScope;
107+
108+
/**
109+
* Matching rule
110+
* - string: Parameter name contains this string
111+
* - RegExp: Parameter name matches this regex
112+
* - function: Custom matching function
113+
*/
114+
match?: string | RegExp | ((key: string) => boolean);
115+
116+
/**
117+
* Parameter modification handler
118+
* @param schema Current parameter's Schema
119+
* @returns Returns various parameter types:
120+
* - Schema: Modified type
121+
* - { required: boolean, value: Schema }: Marks the parameter as required/optional
122+
* - void | null | undefined: Removes the field
123+
*/
124+
handler: (
125+
schema: T
126+
) => Schema | { required: boolean; value: Schema } | void | null | undefined;
127+
}
128+
129+
/**
130+
* Plugin function
131+
*/
132+
function payloadModifier(configs: Config<Schema>[]): ApiPlugin;
133+
```
134+
135+
### Example Configurations
136+
137+
#### Modify Parameter Type
138+
139+
```javascript
140+
// Change the `age` field in `params` to `number` type
141+
payloadModifier([
142+
{
143+
scope: 'params',
144+
match: 'age',
145+
handler: () => 'number'
146+
}
147+
]);
148+
```
149+
150+
#### Modify Nested Parameters
151+
152+
```javascript
153+
// Modify nested parameters in `data`
154+
payloadModifier([
155+
{
156+
scope: 'data',
157+
match: 'user',
158+
handler: () => ({
159+
name: 'string',
160+
age: 'number',
161+
address: {
162+
city: 'string',
163+
zipCode: 'number'
164+
}
165+
})
166+
}
167+
]);
168+
```
169+
170+
#### Remove a Parameter
171+
172+
```javascript
173+
// Remove the `debugInfo` field from `response`
174+
payloadModifier([
175+
{
176+
scope: 'response',
177+
match: 'debugInfo',
178+
handler: () => undefined
179+
}
180+
]);
181+
```
182+
183+
#### Union Types
184+
185+
```javascript
186+
// Change the `id` field in `pathParams` to `string | number` type
187+
payloadModifier([
188+
{
189+
scope: 'pathParams',
190+
match: 'id',
191+
handler: () => ['string', 'number']
192+
}
193+
]);
194+
```
195+
196+
## Advanced Usage
197+
198+
### Dynamically Modify Required Status
199+
200+
```javascript
201+
// Mark the `email` field in `data` as required
202+
```

0 commit comments

Comments
 (0)