Skip to content

Commit 8e2c3e0

Browse files
committed
docs: add wormhole docs and update extension videos
1 parent 79d8ecb commit 8e2c3e0

File tree

8 files changed

+469
-28
lines changed

8 files changed

+469
-28
lines changed

docs/api/07-wormhole.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: wormhole API
3+
---
4+
5+
`@alova/wormhole` is a more modern openAPI generation solution for the alova library. It can generate API functions, complete API types, and complete API documents at the same time. Alova's development tools can eliminate the intermediate API documents for you, shortening the collaboration distance between the front-end and back-end like a wormhole. It is also the underlying implementation of the vscode extension.
6+
7+
## Installation
8+
9+
```bash
10+
# npm
11+
npm i @alova/wormhole -D
12+
# yarn
13+
yarn add @alova/wormhole -D
14+
# pnpm
15+
pnpm add @alova/wormhole -D
16+
```
17+
18+
:::info extension installation tips
19+
20+
If you are using vscode, it is strongly recommended that you use `@alova/wormhole` with alova's vscode extension. Please refer to [Editor Extension Integration](/tutorial/getting-started/extension-integration) to install vscode extension
21+
.
22+
23+
If you are using other editors, you can also use the `@alova/wormhole` command to generate complete API information.
24+
25+
:::
26+
27+
## Commands
28+
29+
### gen
30+
31+
```bash
32+
alova gen [-f, --force] [-c --cwd <path>] [-w --workspace]
33+
```
34+
35+
gen will look for the `alova.config.{cjs,js,mjs,ts}` configuration file and use it to automatically generate API related information.
36+
37+
**Parameters:**
38+
39+
- **-f, --force**: By default, the latest openAPI file will be checked for updates. If this parameter is specified, the check will be ignored and regeneration will be forced.
40+
41+
- **-c, --cwd \<path\>**: Specify the working directory of the configuration file to be generated. The default is the current directory.
42+
43+
- **-w, --workspace**: Specifies whether to generate in workspace mode. It will search for configuration files based on `workspaces` in `package.json` or subpackages defined in `pnpm-workspace.yaml`, and generate API-related information for all subpackages.
44+
45+
### init
46+
47+
```bash
48+
alova init [-t, --type <type>] [-c --cwd <path>]
49+
```
50+
51+
Generate alova.config configuration file in the current directory. It will automatically generate configuration files with different suffixes according to the project type.
52+
53+
**Parameters: **
54+
55+
- **-t, --type**: Specifies the type of configuration file to be generated. The optional values ​​are: `auto/ts/typescript/module/commonjs`. The default is `auto`. It will automatically generate configuration files with different suffixes according to the project type.
56+
57+
- **-c, --cwd \<path\>**: Specifies the working directory of the configuration file to be generated. The default is the current directory.
58+
59+
## Node API
60+
61+
### createConfig()
62+
63+
Create a configuration file.
64+
65+
- **Type**
66+
67+
```ts
68+
type TemplateType = 'typescript' | 'module' | 'commonjs';
69+
interface ConfigCreationOptions {
70+
projectPath?: string;
71+
type?: TemplateType;
72+
}
73+
declare function createConfig(options?: ConfigCreationOptions): Promise<void>;
74+
```
75+
76+
- **Parameter**
77+
78+
1. `projectPath`: project path, default is `process.cwd()`.
79+
2. `type`: configuration file type, optional values ​​are `typescript`, `module`, `commonjs`.
80+
81+
- **Example**
82+
83+
```ts
84+
import { createConfig } from '@alova/wormhole';
85+
86+
await createConfig();
87+
```
88+
89+
### resolveWorkspaces()
90+
91+
Search for all directories containing alova.config configuration files under the monorepo project. It will search for configuration files based on `workspaces` in `package.json` or subpackages defined in `pnpm-workspace.yaml`
92+
93+
```ts
94+
declare function resolveWorkspaces(projectPath?: string): Promise<string[]>;
95+
```
96+
97+
- **Parameter**
98+
99+
1. `projectPath`: The project path to search, defaults to `process.cwd()`.
100+
101+
- **Return value**
102+
103+
An array of relative paths to directories containing alova.config configuration files.
104+
105+
- **Example**
106+
107+
```ts
108+
import { resolveWorkspaces } from '@alova/wormhole';
109+
const workspaces = await resolveWorkspaces();
110+
```
111+
112+
### readConfig()
113+
114+
Read the alova.config configuration file and return the parsed configuration object.
115+
116+
```ts
117+
declare function readConfig(projectPath?: string): Promise<Config>;
118+
```
119+
120+
- **Parameter**
121+
122+
1. `projectPath`: The project path where the configuration file is located. The default value is `process.cwd()`.
123+
124+
- **Return value**
125+
126+
Configuration object.
127+
128+
```ts
129+
import { generate } from '@alova/wormhole';
130+
const config = await readConfig();
131+
```
132+
133+
### generate()
134+
135+
Generate relevant API information based on the configuration object. Generally, it needs to be used with `readConfig()`.
136+
137+
```ts
138+
type GenerateApiOptions = {
139+
force?: boolean;
140+
projectPath?: string;
141+
};
142+
declare function generate(config: Config, rules?: GenerateApiOptions): Promise<boolean[]>;
143+
```
144+
145+
- **Parameters**
146+
147+
1. `config`: configuration object, which is usually read by `readConfig` function.
148+
2. `rules`: generation rules, optional parameters are:
149+
- `force`: whether to force regeneration, the default is `false`.
150+
- `projectPath`: project path, the default is `process.cwd()`.
151+
152+
- **Return value**
153+
154+
An array that contains the result of `generator` items in configuration whether generation is successful.
155+
156+
- **Example 1**
157+
158+
Configuration files only exist in the root directory.
159+
160+
```ts
161+
import { readConfig, generate } from '@alova/wormhole';
162+
163+
const config = await readConfig();
164+
const results = await generate(config);
165+
```
166+
167+
- **Example 2**
168+
169+
Configuration files exist in multiple subpackages (monorepo).
170+
171+
```ts
172+
import { readConfig, generate, resolveWorkspaces } from '@alova/wormhole';
173+
const workspaces = await resolveWorkspaces();
174+
for (const workspace of workspaces) {
175+
const config = await readConfig(workspace);
176+
await generate(config);
177+
}
178+
```

docs/tutorial/02-getting-started/09-extension-integration.md

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,47 @@ Integrating Alova's editor extension can make it more powerful.
88
2. Embed API documents in the code to experience the effect of checking and using APIs.
99
3. Update APIs regularly and actively notify front-end developers, no longer relying on server-side developers to notify.
1010

11-
<a className="button button--primary" href="vscode:extension/Alova.alova-vscode-extension">Install VS Code extension</a>
12-
13-
> Automatically generate support for swagger-v2 and openapi-v3 specifications.
14-
15-
The following is an extended demonstration video
11+
## Demo video
1612

1713
import vscodeDemoVideo from '@site/static/video/vscode-demo-video-en.mp4';
1814

1915
<video width="100%" controls controlsList="nodownload" src={vscodeDemoVideo} />
2016

17+
## Install
18+
19+
<a className="button button--primary" href="vscode:extension/Alova.alova-vscode-extension">Install VSCode extension (supports swagger-v2 and openapi-v3 specifications)</a>
20+
21+
import Tabs from '@theme/Tabs';
22+
import TabItem from '@theme/TabItem';
23+
24+
<Tabs>
25+
<TabItem value="1" label="npm">
26+
27+
```bash
28+
npm install @alova/wormhole --save
29+
```
30+
31+
</TabItem>
32+
<TabItem value="2" label="yarn">
33+
34+
```bash
35+
yarn add @alova/wormhole
36+
```
37+
38+
</TabItem>
39+
<TabItem value="3" label="pnpm">
40+
41+
```bash
42+
pnpm add @alova/wormhole
43+
```
44+
45+
</TabItem>
46+
</Tabs>
47+
48+
Install `@alova/wormhole` and alova's vscode extension at the same time to enjoy the complete features. `@alova/wormhole` provides automatic generation features. The vscode extension can quickly call `@alova/wormhole` and provide shortcut keys for quickly finding interface documents in the editor.
49+
50+
If you are using an editor such as WebStorm, you can use [@alova/wormhole's commands](/api/wormhole#commands) to automatically generate API call functions, complete TypeScript types of APIs, and API documentation information.
51+
2152
## Configuration
2253

2354
When using the extension, you need to specify the input source and output directory from the openapi file, etc. You can create a configuration file in the project root directory, which supports the following formats:
@@ -35,27 +66,27 @@ The specific configuration parameters are as follows, taking commonjs as an exam
3566
```js
3667
// alova.config.js
3768
module.exports = {
38-
// API generation setting array, each item represents an automatically generated rule, including the generated input and output directories, standard file addresses, etc.
69+
// API generation setting array, each item represents an automatically generated rule, including the generated input and output directories, standard file paths, etc.
3970
generator: [
4071
// Server 1
4172
{
42-
// Input parameter 1: openapi json file url address
73+
// Input parameter 1: openapi json file url url
4374
input: 'http://localhost:3000/openapi.json',
4475

45-
// Input parameter 2: local address with the current project as the relative directory
76+
// Input parameter 2: local url with the current project as the relative directory
4677
// input: 'openapi/api.json'
4778

48-
// Input parameter 3: When there is no direct reference to the openapi file, it is a document address, and the document type must be specified with the platform parameter
79+
// Input parameter 3: When there is no direct reference to the openapi file, it is a document url, and the document type must be specified with the platform parameter
4980
// input: 'http://192.168.5.123:8080'
5081

5182
// (Optional) platform is a platform that supports openapi. Currently only swagger is supported. The default is empty
52-
// When this parameter is specified, the input field only needs to specify the document address without specifying the openapi file
83+
// When this parameter is specified, the input field only needs to specify the document url without specifying the openapi file
5384
platform: 'swagger',
5485

55-
// Output path of interface file and type file. Multiple generators cannot have the same address, otherwise the generated code will overwrite each other.
86+
// Output path of interface file and type file. Multiple generators cannot have the same output path, otherwise the generated code will overwrite each other.
5687
output: 'src/api',
5788

58-
// (Optional) Specify the mediaType of the generated response data. Use this data type to generate the ts format of the response with a 200 status code. The default is application/json.
89+
// (Optional) Specify the mediaType of the generated response data. Use this data type to generate the ts format of the response with a 2xx status code. The default is application/json.
5990
responseMediaType: 'application/json',
6091

6192
// (Optional) Specify the bodyMediaType of the generated request body data. Use this data type to generate the ts format of the request body. The default is application/json.
@@ -77,8 +108,16 @@ module.exports = {
77108
*/
78109
global: 'Apis',
79110

111+
/**
112+
* The host object of global mounting, default is `globalThis`, it means `window` in browser and `global` in nodejs
113+
*/
114+
globalHost: 'globalThis'
115+
80116
/**
81117
* (Optional) Filter or convert the generated api interface function, return a new apiDescriptor to generate the api call function, if this function is not specified, the apiDescripor object is not converted
118+
*
119+
* The type of `apiDescriptor` is the same as the api item of openapi file.
120+
* @see https://spec.openapis.org/oas/v3.1.0.html#operation-object
82121
*/
83122
handleApi: apiDescriptor => {
84123
// Returning a falsy value means filtering this api
@@ -262,4 +301,6 @@ In this way, you can integrate the automatically generated code without changing
262301

263302
1. In a ts project, if you find that vscode cannot correctly prompt, please set `"strictNullChecks": true` in `tsconfig.json`.
264303

265-
2. Sometimes the api will prompt as `any` type, you can try to solve it as follows: Step 1, confirm whether this api is introduced in the entry file, and step 2, restart vscode
304+
2. Sometimes the api will prompt as `any` type, you can try to solve it as follows:
305+
- Step 1, confirm whether this api is introduced in the entry file.
306+
- Step 2, restart vscode

docusaurus.config.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,12 @@ const config: Config = {
211211
},
212212
announcementBar: {
213213
id: 'support_us',
214-
content: `⭐️
215-
If you also like alova,
216-
<a
217-
href="https://github.com/alovajs/alova"
218-
target="_blank">
219-
star it on GitHub!
220-
</a>
221-
⭐️`,
214+
content: `<span class="announcement">⭐️If you also like alova,
215+
<a
216+
href="https://github.com/alovajs/alova"
217+
target="_blank">
218+
star it on GitHub!
219+
</a>⭐️</span>`,
222220
backgroundColor: 'rgba(var(--ifm-color-primary-rgb), 0.05)'
223221
// textColor: '#fff'
224222
},

0 commit comments

Comments
 (0)