@modern-js/main-doc 3.0.0-alpha.0 → 3.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/apis/app/commands.mdx +6 -30
- package/docs/en/components/bff-upload.mdx +3 -5
- package/docs/en/components/bundler.mdx +1 -1
- package/docs/en/components/enable-bff.mdx +6 -2
- package/docs/en/components/enable-ssg.mdx +1 -0
- package/docs/en/components/esbuild.mdx +2 -2
- package/docs/en/components/extend-bff-function.mdx +2 -4
- package/docs/en/components/hono.mdx +119 -0
- package/docs/en/components/international/custom-instance-code.mdx +16 -0
- package/docs/en/components/international/init-options-desc.mdx +1 -0
- package/docs/en/components/international/install-command.mdx +15 -1
- package/docs/en/components/international/instance-code.mdx +26 -0
- package/docs/en/configure/app/builder-plugins.mdx +1 -2
- package/docs/en/configure/app/dev/server.mdx +108 -0
- package/docs/en/configure/app/experiments/source-build.mdx +0 -1
- package/docs/en/configure/app/output/assets-retry.mdx +1 -1
- package/docs/en/configure/app/output/disable-inline-runtime-chunk.mdx +2 -2
- package/docs/en/configure/app/output/filename.mdx +2 -4
- package/docs/en/configure/app/output/temp-dir.mdx +3 -3
- package/docs/en/configure/app/performance/build-cache.mdx +1 -1
- package/docs/en/configure/app/performance/profile.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +1 -3
- package/docs/en/configure/app/runtime/router.mdx +0 -4
- package/docs/en/configure/app/security/sri.mdx +0 -1
- package/docs/en/configure/app/source/alias.mdx +1 -1
- package/docs/en/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/en/configure/app/source/include.mdx +2 -14
- package/docs/en/configure/app/tools/dev-server.mdx +8 -8
- package/docs/en/configure/app/usage.mdx +0 -12
- package/docs/en/guides/_meta.json +5 -0
- package/docs/en/guides/advanced-features/bff/_meta.json +9 -1
- package/docs/en/guides/advanced-features/bff/cross-project.mdx +1 -1
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +2 -15
- package/docs/en/guides/advanced-features/bff/function.mdx +4 -4
- package/docs/en/guides/advanced-features/bff/operators.mdx +628 -0
- package/docs/en/guides/advanced-features/bff/sdk.mdx +17 -9
- package/docs/en/guides/advanced-features/bff/upload.mdx +3 -1
- package/docs/en/guides/advanced-features/international/configuration.mdx +7 -16
- package/docs/en/guides/advanced-features/international/quick-start.mdx +4 -32
- package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +18 -4
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -1
- package/docs/en/guides/advanced-features/server-monitor/monitors.mdx +62 -5
- package/docs/en/guides/basic-features/data/data-cache.mdx +60 -76
- package/docs/en/guides/basic-features/data/data-fetch.mdx +15 -14
- package/docs/en/guides/basic-features/debug/proxy.mdx +6 -9
- package/docs/en/guides/basic-features/render/rsc.mdx +24 -19
- package/docs/en/guides/basic-features/render/ssg.mdx +4 -9
- package/docs/en/guides/basic-features/render/ssr-cache.mdx +0 -4
- package/docs/en/guides/basic-features/static-assets/svg-assets.mdx +0 -4
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/upgrade/_meta.json +1 -0
- package/docs/en/guides/upgrade/config.mdx +936 -0
- package/docs/en/guides/upgrade/entry.mdx +463 -0
- package/docs/en/guides/upgrade/other.mdx +83 -0
- package/docs/en/guides/upgrade/overview.mdx +33 -0
- package/docs/en/guides/upgrade/tailwindcss.mdx +130 -0
- package/docs/en/guides/upgrade/web-server.mdx +91 -0
- package/docs/en/plugin/_meta.json +5 -0
- package/docs/en/plugin/cli-plugins/_meta.json +1 -1
- package/docs/en/plugin/cli-plugins/api.mdx +13 -63
- package/docs/en/plugin/cli-plugins/life-cycle.mdx +0 -4
- package/docs/en/plugin/introduction.mdx +8 -20
- package/docs/en/plugin/plugin-system.mdx +3 -3
- package/docs/en/plugin/runtime-plugins/_meta.json +1 -1
- package/docs/en/plugin/runtime-plugins/api.mdx +1 -1
- package/docs/en/plugin/server-plugins/_meta.json +1 -0
- package/docs/en/plugin/server-plugins/api.mdx +210 -1
- package/docs/en/plugin/server-plugins/life-cycle.mdx +41 -1
- package/docs/zh/apis/app/commands.mdx +6 -30
- package/docs/zh/components/bff-operator-code.mdx +5 -0
- package/docs/zh/components/bff-upload.mdx +0 -2
- package/docs/zh/components/bundler.mdx +1 -1
- package/docs/zh/components/enable-bff.mdx +6 -2
- package/docs/zh/components/enable-ssg.mdx +3 -1
- package/docs/zh/components/esbuild.mdx +2 -2
- package/docs/zh/components/extend-bff-function.mdx +2 -4
- package/docs/zh/components/hono.mdx +119 -0
- package/docs/zh/components/international/custom-instance-code.mdx +16 -0
- package/docs/zh/components/international/init-options-desc.mdx +1 -0
- package/docs/zh/components/international/install-command.mdx +15 -0
- package/docs/zh/components/international/instance-code.mdx +26 -0
- package/docs/zh/configure/app/builder-plugins.mdx +1 -2
- package/docs/zh/configure/app/dev/server.mdx +109 -2
- package/docs/zh/configure/app/experiments/source-build.mdx +0 -1
- package/docs/zh/configure/app/output/assets-retry.mdx +1 -1
- package/docs/zh/configure/app/output/disable-inline-runtime-chunk.mdx +2 -2
- package/docs/zh/configure/app/output/filename.mdx +2 -4
- package/docs/zh/configure/app/output/temp-dir.mdx +3 -3
- package/docs/zh/configure/app/performance/build-cache.mdx +1 -1
- package/docs/zh/configure/app/performance/profile.mdx +1 -1
- package/docs/zh/configure/app/plugins.mdx +1 -2
- package/docs/zh/configure/app/runtime/router.mdx +0 -4
- package/docs/zh/configure/app/security/sri.mdx +0 -1
- package/docs/zh/configure/app/source/alias.mdx +1 -1
- package/docs/zh/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/zh/configure/app/source/include.mdx +2 -16
- package/docs/zh/configure/app/tools/dev-server.mdx +5 -5
- package/docs/zh/configure/app/usage.mdx +0 -12
- package/docs/zh/guides/advanced-features/bff/_meta.json +9 -1
- package/docs/zh/guides/advanced-features/bff/frameworks.mdx +2 -16
- package/docs/zh/guides/advanced-features/bff/operators.mdx +628 -0
- package/docs/zh/guides/advanced-features/bff/sdk.mdx +19 -12
- package/docs/zh/guides/advanced-features/bff/upload.mdx +3 -1
- package/docs/zh/guides/advanced-features/international/configuration.mdx +7 -16
- package/docs/zh/guides/advanced-features/international/quick-start.mdx +2 -25
- package/docs/zh/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +18 -4
- package/docs/zh/guides/advanced-features/server-monitor/monitors.mdx +60 -5
- package/docs/zh/guides/basic-features/data/data-cache.mdx +47 -54
- package/docs/zh/guides/basic-features/data/data-fetch.mdx +9 -12
- package/docs/zh/guides/basic-features/debug/proxy.mdx +4 -7
- package/docs/zh/guides/basic-features/render/rsc.mdx +23 -37
- package/docs/zh/guides/basic-features/render/ssr-cache.mdx +0 -4
- package/docs/zh/guides/basic-features/static-assets/svg-assets.mdx +0 -4
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/troubleshooting/builder.mdx +1 -1
- package/docs/zh/guides/upgrade/config.mdx +132 -1
- package/docs/zh/plugin/_meta.json +5 -0
- package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
- package/docs/zh/plugin/cli-plugins/api.mdx +15 -65
- package/docs/zh/plugin/cli-plugins/life-cycle.mdx +0 -4
- package/docs/zh/plugin/introduction.mdx +4 -16
- package/docs/zh/plugin/plugin-system.mdx +3 -14
- package/docs/zh/plugin/runtime-plugins/_meta.json +1 -1
- package/docs/zh/plugin/runtime-plugins/api.mdx +1 -1
- package/docs/zh/plugin/server-plugins/_meta.json +1 -0
- package/docs/zh/plugin/server-plugins/api.mdx +210 -1
- package/docs/zh/plugin/server-plugins/life-cycle.mdx +41 -1
- package/package.json +2 -2
- package/src/components/FrameworkCode/index.tsx +605 -0
- package/docs/en/configure/app/performance/bundle-analyze.mdx +0 -24
- package/docs/en/configure/app/tools/babel.mdx +0 -225
- package/docs/en/plugin/cli-plugins/migration.mdx +0 -83
- package/docs/en/plugin/runtime-plugins/migration.mdx +0 -110
- package/docs/zh/components/default-mwa-generate.mdx +0 -4
- package/docs/zh/configure/app/performance/bundle-analyze.mdx +0 -24
- package/docs/zh/configure/app/tools/babel.mdx +0 -224
- package/docs/zh/plugin/cli-plugins/migration.mdx +0 -83
- package/docs/zh/plugin/runtime-plugins/migration.mdx +0 -110
- /package/docs/en/components/{router-legacy-tip.mdx → upgrade-config-deploy.mdx} +0 -0
- /package/docs/zh/components/{router-legacy-tip.mdx → upgrade-config-deploy.mdx} +0 -0
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 2
|
|
3
|
+
title: Creating Extensible BFF Functions
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Creating Extensible BFF Functions
|
|
7
|
+
|
|
8
|
+
The previous section showed how to export a simple BFF function in a file. In more complex scenarios, each BFF function may need to do independent type validation, pre-logic, etc.
|
|
9
|
+
|
|
10
|
+
Therefore, Modern.js exposes `Api`, which supports creating BFF functions through this API. BFF functions created in this way can be easily extended with functionality.
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
:::caution Note
|
|
15
|
+
|
|
16
|
+
- The `Api` function can only be used in TypeScript projects, not in pure JavaScript projects.
|
|
17
|
+
- Operator functions (such as `Get`, `Query`, etc. below) depend on [`zod`](https://www.npmjs.com/package/zod), which needs to be installed in the project first.
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
pnpm add zod
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
:::
|
|
24
|
+
|
|
25
|
+
A BFF function created by the `Api` function consists of the following parts:
|
|
26
|
+
|
|
27
|
+
- `Api()`, the function that defines the interface.
|
|
28
|
+
- `Get(path?: string)`, specifies the interface route.
|
|
29
|
+
- `Query(schema: T)`, `Redirect(url: string)`, extends the interface, such as specifying interface input parameters.
|
|
30
|
+
- `Handler: (...args: any[]) => any | Promise<any>`, the function that handles the request logic of the interface.
|
|
31
|
+
|
|
32
|
+
The server can define the input parameters and types of the interface. Based on the types, the server will automatically perform type validation at runtime:
|
|
33
|
+
|
|
34
|
+
import BFFOperatorCode from '@site-docs/components/bff-operator-code';
|
|
35
|
+
|
|
36
|
+
<BFFOperatorCode>
|
|
37
|
+
|
|
38
|
+
```typescript title="api/lambda/user.ts"
|
|
39
|
+
import { Api, Post, Query, Data } from '@modern-js/plugin-bff/server';
|
|
40
|
+
import { z } from 'zod';
|
|
41
|
+
|
|
42
|
+
const UserSchema = z.object({
|
|
43
|
+
name: z.string().min(2).max(10),
|
|
44
|
+
email: z.string().email(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const DataSchema = z.object({
|
|
48
|
+
phone: z.string(),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const addUser = Api(
|
|
52
|
+
Post('/user'),
|
|
53
|
+
Query(UserSchema),
|
|
54
|
+
Data(DataSchema),
|
|
55
|
+
async ({ query, data }) => ({
|
|
56
|
+
name: query.name,
|
|
57
|
+
phone: data.phone,
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
</BFFOperatorCode>
|
|
63
|
+
|
|
64
|
+
:::caution Note
|
|
65
|
+
When using the `Api` function, ensure that all code logic is placed inside the function. Operations such as `console.log` or using `fs` outside the function are not allowed.
|
|
66
|
+
|
|
67
|
+
:::
|
|
68
|
+
|
|
69
|
+
The browser side can also use the integrated call method with static type hints:
|
|
70
|
+
|
|
71
|
+
```typescript title="routes/page.tsx"
|
|
72
|
+
import { addUser } from '@api/user';
|
|
73
|
+
|
|
74
|
+
addUser({
|
|
75
|
+
query: {
|
|
76
|
+
name: 'modern.js',
|
|
77
|
+
email: 'modern.js@example.com',
|
|
78
|
+
},
|
|
79
|
+
data: {
|
|
80
|
+
phone: '12345',
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Interface Route
|
|
86
|
+
|
|
87
|
+
As shown in the example below, you can specify the route and HTTP Method through the `Get` function:
|
|
88
|
+
|
|
89
|
+
<BFFOperatorCode>
|
|
90
|
+
|
|
91
|
+
```typescript title="api/lambda/user.ts"
|
|
92
|
+
import { Api, Get, Query, Data } from '@modern-js/plugin-bff/server';
|
|
93
|
+
|
|
94
|
+
// Specify the interface route, Modern.js sets `bff.prefix` to `/api` by default,
|
|
95
|
+
// so the interface route is `/api/user`, and the HTTP Method is GET.
|
|
96
|
+
export const getHello = Api(
|
|
97
|
+
Get('/hello'),
|
|
98
|
+
Query(HelloSchema),
|
|
99
|
+
async ({ query }) => query,
|
|
100
|
+
);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
</BFFOperatorCode>
|
|
104
|
+
|
|
105
|
+
When the route is not specified, the interface route is defined according to the file convention. As shown in the example below, with the function writing method, there is a code path `api/lambda/user.ts`, which will register the corresponding interface `/api/user`.
|
|
106
|
+
|
|
107
|
+
<BFFOperatorCode>
|
|
108
|
+
|
|
109
|
+
```typescript title="api/lambda/user.ts"
|
|
110
|
+
import { Api, Get, Query, Data } from '@modern-js/plugin-bff/server';
|
|
111
|
+
|
|
112
|
+
// No interface route specified, according to file convention and function name, the interface is api/user, HTTP Method is get.
|
|
113
|
+
export const get = Api(Query(UserSchema), async ({ query }) => query);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
</BFFOperatorCode>
|
|
117
|
+
|
|
118
|
+
:::info
|
|
119
|
+
Modern.js recommends defining interfaces based on file conventions to keep routes clear in the project. For specific rules, see [Function Routes](/guides/advanced-features/bff/function#function-routes).
|
|
120
|
+
|
|
121
|
+
:::
|
|
122
|
+
|
|
123
|
+
In addition to the `Get` function, you can use the following functions to define HTTP interfaces:
|
|
124
|
+
|
|
125
|
+
| Function | Description |
|
|
126
|
+
| :--------------------- | :---------------------- |
|
|
127
|
+
| Get(path?: string) | Accept GET requests |
|
|
128
|
+
| Post(path?: string) | Accept POST requests |
|
|
129
|
+
| Put(path?: string) | Accept PUT requests |
|
|
130
|
+
| Delete(path?: string) | Accept DELETE requests |
|
|
131
|
+
| Patch(path?: string) | Accept PATCH requests |
|
|
132
|
+
| Head(path?: string) | Accept HEAD requests |
|
|
133
|
+
| Options(path?: string) | Accept OPTIONS requests |
|
|
134
|
+
|
|
135
|
+
## Request
|
|
136
|
+
|
|
137
|
+
The following are request-related operators. Operators can be combined, but must comply with HTTP protocol. For example, GET requests cannot use the Data operator.
|
|
138
|
+
|
|
139
|
+
### Query Parameters
|
|
140
|
+
|
|
141
|
+
Using the `Query` function, you can define the type of query. After using the `Query` function, the query information can be obtained in the input parameters of the interface processing function, and the `query` field can be added to the input parameters of the frontend request function:
|
|
142
|
+
|
|
143
|
+
<BFFOperatorCode>
|
|
144
|
+
|
|
145
|
+
```typescript title="api/lambda/user.ts"
|
|
146
|
+
// Server-side code
|
|
147
|
+
import { Api, Query } from '@modern-js/plugin-bff/server';
|
|
148
|
+
import { z } from 'zod';
|
|
149
|
+
|
|
150
|
+
const UserSchema = z.object({
|
|
151
|
+
name: z.string().min(2).max(10),
|
|
152
|
+
email: z.string().email(),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
export const get = Api(Query(UserSchema), async ({ query }) => ({
|
|
156
|
+
name: query.name,
|
|
157
|
+
}));
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
</BFFOperatorCode>
|
|
161
|
+
|
|
162
|
+
```typescript title="routes/page.tsx"
|
|
163
|
+
// Frontend code
|
|
164
|
+
get({
|
|
165
|
+
query: {
|
|
166
|
+
name: 'modern.js',
|
|
167
|
+
email: 'modern.js@example.com',
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### Query Parameter Type Conversion
|
|
173
|
+
|
|
174
|
+
URL query parameters are strings by default. If you need numeric types, you need to use `z.coerce.number()` for type conversion:
|
|
175
|
+
|
|
176
|
+
<BFFOperatorCode>
|
|
177
|
+
|
|
178
|
+
```typescript title="api/lambda/user.ts"
|
|
179
|
+
import { Api, Get, Query } from '@modern-js/plugin-bff/server';
|
|
180
|
+
import { z } from 'zod';
|
|
181
|
+
|
|
182
|
+
const QuerySchema = z.object({
|
|
183
|
+
id: z.string(),
|
|
184
|
+
page: z.coerce.number().min(1).max(100), // Use z.coerce.number() to convert string to number
|
|
185
|
+
status: z.enum(['active', 'inactive']),
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
export const getUser = Api(
|
|
189
|
+
Get('/user'),
|
|
190
|
+
Query(QuerySchema),
|
|
191
|
+
async ({ query }) => {
|
|
192
|
+
return {
|
|
193
|
+
id: query.id,
|
|
194
|
+
page: query.page, // page is a number type
|
|
195
|
+
status: query.status,
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
</BFFOperatorCode>
|
|
202
|
+
|
|
203
|
+
:::caution Note
|
|
204
|
+
URL query parameters are all string types. If you need numeric types, you need to use `z.coerce.number()` for conversion, not `z.number()` directly.
|
|
205
|
+
:::
|
|
206
|
+
|
|
207
|
+
### Pass Data
|
|
208
|
+
|
|
209
|
+
Using the `Data` function, you can define the type of data passed by the interface. After using `Data`, the interface data information can be obtained in the input parameters of the interface processing function.
|
|
210
|
+
|
|
211
|
+
:::caution
|
|
212
|
+
If you use the Data function, you must follow the HTTP protocol. When the HTTP Method is GET or HEAD, the Data function cannot be used.
|
|
213
|
+
|
|
214
|
+
:::
|
|
215
|
+
|
|
216
|
+
<BFFOperatorCode>
|
|
217
|
+
|
|
218
|
+
```typescript title="api/lambda/user.ts"
|
|
219
|
+
import { Api, Data } from '@modern-js/plugin-bff/server';
|
|
220
|
+
import { z } from 'zod';
|
|
221
|
+
|
|
222
|
+
const DataSchema = z.object({
|
|
223
|
+
name: z.string(),
|
|
224
|
+
phone: z.string(),
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
export const post = Api(Data(DataSchema), async ({ data }) => ({
|
|
228
|
+
name: data.name,
|
|
229
|
+
phone: data.phone,
|
|
230
|
+
}));
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
</BFFOperatorCode>
|
|
234
|
+
|
|
235
|
+
```typescript title="routes/page.tsx"
|
|
236
|
+
// Frontend code
|
|
237
|
+
post({
|
|
238
|
+
data: {
|
|
239
|
+
name: 'modern.js',
|
|
240
|
+
phone: '12345',
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Route Parameters
|
|
246
|
+
|
|
247
|
+
Route parameters can implement dynamic routes and get parameters from the path. You can specify path parameters through `Params<T>(schema: z.ZodType<T>)`
|
|
248
|
+
|
|
249
|
+
<BFFOperatorCode>
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { Api, Get, Params } from '@modern-js/plugin-bff/server';
|
|
253
|
+
import { z } from 'zod';
|
|
254
|
+
|
|
255
|
+
const UserSchema = z.object({
|
|
256
|
+
id: z.string(),
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
export const queryUser = Api(
|
|
260
|
+
Get('/user/:id'),
|
|
261
|
+
Params(UserSchema),
|
|
262
|
+
async ({ params }) => ({
|
|
263
|
+
name: params.id,
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
</BFFOperatorCode>
|
|
269
|
+
|
|
270
|
+
### Request Headers
|
|
271
|
+
|
|
272
|
+
You can define the request headers required by the interface through the `Headers<T>(schema: z.ZodType<T>)` function and pass the request headers through integrated calls:
|
|
273
|
+
|
|
274
|
+
<BFFOperatorCode>
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
import { Api, Headers } from '@modern-js/plugin-bff/server';
|
|
278
|
+
import { z } from 'zod';
|
|
279
|
+
|
|
280
|
+
const headerSchema = z.object({
|
|
281
|
+
token: z.string(),
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
export const queryUser = Api(Headers(headerSchema), async ({ headers }) => ({
|
|
285
|
+
name: headers.token,
|
|
286
|
+
}));
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
</BFFOperatorCode>
|
|
290
|
+
|
|
291
|
+
## Parameter Validation
|
|
292
|
+
|
|
293
|
+
As mentioned earlier, when using functions such as `Query` and `Data` to define interfaces, the server will automatically validate the data passed from the frontend based on the schema passed to these functions.
|
|
294
|
+
|
|
295
|
+
When validation fails, you can catch errors through Try/Catch:
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
try {
|
|
299
|
+
const res = await postUser({
|
|
300
|
+
query: {
|
|
301
|
+
user: 'modern.js',
|
|
302
|
+
},
|
|
303
|
+
data: {
|
|
304
|
+
message: 'hello',
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
return res;
|
|
308
|
+
} catch (error) {
|
|
309
|
+
console.log(error.data.code); // VALIDATION_ERROR
|
|
310
|
+
console.log(JSON.parse(error.data.message));
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
At the same time, you can get complete error information through `error.data.message`:
|
|
315
|
+
|
|
316
|
+
```json
|
|
317
|
+
[
|
|
318
|
+
{
|
|
319
|
+
code: 'invalid_string',
|
|
320
|
+
message: "Invalid email",
|
|
321
|
+
path: [0, 'user'],
|
|
322
|
+
validation: "email"
|
|
323
|
+
},
|
|
324
|
+
];
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Middleware
|
|
328
|
+
|
|
329
|
+
You can set function middleware through the `Middleware` operator. Function middleware will execute before validation and interface logic.
|
|
330
|
+
|
|
331
|
+
:::info
|
|
332
|
+
The `Middleware` operator can be configured multiple times, and the execution order of middleware is from top to bottom
|
|
333
|
+
|
|
334
|
+
:::
|
|
335
|
+
|
|
336
|
+
<BFFOperatorCode>
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
import { Api, Query, Middleware } from '@modern-js/plugin-bff/server';
|
|
340
|
+
import { z } from 'zod';
|
|
341
|
+
|
|
342
|
+
const UserSchema = z.object({
|
|
343
|
+
name: z.string().min(2).max(10),
|
|
344
|
+
email: z.string().email(),
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
export const get = Api(
|
|
348
|
+
Query(UserSchema),
|
|
349
|
+
Middleware(async (c, next) => {
|
|
350
|
+
console.info(`access url: ${c.req.url}`);
|
|
351
|
+
await next();
|
|
352
|
+
}),
|
|
353
|
+
async ({ query }) => ({
|
|
354
|
+
name: query.name,
|
|
355
|
+
}),
|
|
356
|
+
);
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
</BFFOperatorCode>
|
|
360
|
+
|
|
361
|
+
## Data Transformation Pipe
|
|
362
|
+
|
|
363
|
+
The `Pipe` operator can pass in a function that executes after middleware and validation are completed. It can be used in the following scenarios:
|
|
364
|
+
|
|
365
|
+
1. Transform query parameters or data carried by the request.
|
|
366
|
+
2. Perform custom validation on request data. If validation fails, you can choose to throw an exception or directly return error information.
|
|
367
|
+
3. If you only want to do validation without executing interface logic (for example, the frontend does not do separate validation, uses the interface for validation, but in some scenarios you don't want the interface logic to execute), you can terminate subsequent execution in this function.
|
|
368
|
+
|
|
369
|
+
`Pipe` defines a transformation function. The input parameters of the transformation function are `query`, `data`, and `headers` carried by the interface request. The return value will be passed to the next `Pipe` function or interface processing function as input parameters, so the data structure of the return value generally needs to be the same as the input parameters.
|
|
370
|
+
|
|
371
|
+
:::info
|
|
372
|
+
The `Pipe` operator can be configured multiple times. The execution order of functions is from top to bottom. The return value of the previous function is the input parameter of the next function.
|
|
373
|
+
|
|
374
|
+
:::
|
|
375
|
+
|
|
376
|
+
<BFFOperatorCode>
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
import { Api, Query, Pipe } from '@modern-js/plugin-bff/server';
|
|
380
|
+
import { z } from 'zod';
|
|
381
|
+
|
|
382
|
+
const UserSchema = z.object({
|
|
383
|
+
name: z.string().min(2).max(10),
|
|
384
|
+
email: z.string(),
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
export const get = Api(
|
|
388
|
+
Query(UserSchema),
|
|
389
|
+
Pipe<{
|
|
390
|
+
query: z.infer<typeof UserSchema>;
|
|
391
|
+
}>(input => {
|
|
392
|
+
const { query } = input;
|
|
393
|
+
if (!query.email.includes('@')) {
|
|
394
|
+
query.email = `${query.email}@example.com`;
|
|
395
|
+
}
|
|
396
|
+
return input;
|
|
397
|
+
}),
|
|
398
|
+
async ({ query }) => ({
|
|
399
|
+
name: query.name,
|
|
400
|
+
}),
|
|
401
|
+
);
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
</BFFOperatorCode>
|
|
405
|
+
|
|
406
|
+
Also,
|
|
407
|
+
|
|
408
|
+
<BFFOperatorCode>
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
import { Api, Query, Pipe } from '@modern-js/plugin-bff/server';
|
|
412
|
+
import { z } from 'zod';
|
|
413
|
+
|
|
414
|
+
const UserSchema = z.object({
|
|
415
|
+
name: z.string().min(2).max(10),
|
|
416
|
+
email: z.string().email(),
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
export const get = Api(
|
|
420
|
+
Query(UserSchema),
|
|
421
|
+
Pipe<{
|
|
422
|
+
query: z.infer<typeof UserSchema>;
|
|
423
|
+
}>((input, end) => {
|
|
424
|
+
const { query } = input;
|
|
425
|
+
const { name, email } = query;
|
|
426
|
+
if (!email.startsWith(name)) {
|
|
427
|
+
return end({
|
|
428
|
+
message: 'email must start with name',
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
return input;
|
|
432
|
+
}),
|
|
433
|
+
async ({ query }) => ({
|
|
434
|
+
name: query.name,
|
|
435
|
+
}),
|
|
436
|
+
);
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
</BFFOperatorCode>
|
|
440
|
+
|
|
441
|
+
If you need to do more custom operations on the response, you can pass a function to the `end` function. The input parameter of the function is Hono's Context (`c`), and you can operate on `c.req` and `c.res`:
|
|
442
|
+
|
|
443
|
+
<BFFOperatorCode>
|
|
444
|
+
|
|
445
|
+
```typescript
|
|
446
|
+
import { Api, Query, Pipe } from '@modern-js/plugin-bff/server';
|
|
447
|
+
import { z } from 'zod';
|
|
448
|
+
|
|
449
|
+
const UserSchema = z.object({
|
|
450
|
+
name: z.string().min(2).max(10),
|
|
451
|
+
email: z.string().email(),
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
export const get = Api(
|
|
455
|
+
Query(UserSchema),
|
|
456
|
+
Pipe<{
|
|
457
|
+
query: z.infer<typeof UserSchema>;
|
|
458
|
+
}>((input, end) => {
|
|
459
|
+
const { query } = input;
|
|
460
|
+
const { name, email } = query;
|
|
461
|
+
if (!email.startsWith(name)) {
|
|
462
|
+
return end(c => {
|
|
463
|
+
c.res.status = 400;
|
|
464
|
+
c.res.body = {
|
|
465
|
+
message: 'email must start with name',
|
|
466
|
+
};
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
return input;
|
|
470
|
+
}),
|
|
471
|
+
async ({ query }) => ({
|
|
472
|
+
name: query.name,
|
|
473
|
+
}),
|
|
474
|
+
);
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
</BFFOperatorCode>
|
|
478
|
+
|
|
479
|
+
## Response
|
|
480
|
+
|
|
481
|
+
The following are response-related operators. Through response operators, you can process responses.
|
|
482
|
+
|
|
483
|
+
### Status Code HttpCode
|
|
484
|
+
|
|
485
|
+
You can specify the status code returned by the interface through the `HttpCode(statusCode: number)` function
|
|
486
|
+
|
|
487
|
+
<BFFOperatorCode>
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
import { Api, Query, Data, HttpCode } from '@modern-js/plugin-bff/server';
|
|
491
|
+
import { z } from 'zod';
|
|
492
|
+
|
|
493
|
+
const UserSchema = z.object({
|
|
494
|
+
name: z.string().min(2).max(10),
|
|
495
|
+
email: z.string().email(),
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const DataSchema = z.object({
|
|
499
|
+
phone: z.string(),
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
export const post = Api(
|
|
503
|
+
Query(UserSchema),
|
|
504
|
+
Data(DataSchema),
|
|
505
|
+
HttpCode(202),
|
|
506
|
+
async ({ query, data }) => {
|
|
507
|
+
someTask({
|
|
508
|
+
user: {
|
|
509
|
+
...query,
|
|
510
|
+
...data,
|
|
511
|
+
},
|
|
512
|
+
});
|
|
513
|
+
},
|
|
514
|
+
);
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
</BFFOperatorCode>
|
|
518
|
+
|
|
519
|
+
### Response Headers SetHeaders
|
|
520
|
+
|
|
521
|
+
Supports setting response headers through the `SetHeaders(headers: Record<string, string>)` function
|
|
522
|
+
|
|
523
|
+
<BFFOperatorCode>
|
|
524
|
+
|
|
525
|
+
```typescript
|
|
526
|
+
import { Api, Get, SetHeaders } from '@modern-js/plugin-bff/server';
|
|
527
|
+
|
|
528
|
+
export default Api(
|
|
529
|
+
Get('/hello'),
|
|
530
|
+
SetHeaders({
|
|
531
|
+
'x-log-id': 'xxx',
|
|
532
|
+
}),
|
|
533
|
+
async () => 'Hello World!',
|
|
534
|
+
);
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
</BFFOperatorCode>
|
|
538
|
+
|
|
539
|
+
### Redirect
|
|
540
|
+
|
|
541
|
+
Supports redirecting the interface through `Redirect(url: string)`:
|
|
542
|
+
|
|
543
|
+
<BFFOperatorCode>
|
|
544
|
+
|
|
545
|
+
```typescript
|
|
546
|
+
import { Api, Get, Redirect } from '@modern-js/plugin-bff/server';
|
|
547
|
+
|
|
548
|
+
export default Api(
|
|
549
|
+
Get('/hello'),
|
|
550
|
+
Redirect('https://modernjs.dev/'),
|
|
551
|
+
async () => 'Hello Modern.js!',
|
|
552
|
+
);
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
</BFFOperatorCode>
|
|
556
|
+
|
|
557
|
+
## Request Context
|
|
558
|
+
|
|
559
|
+
As mentioned above, through operators, you can get `query`, `data`, `params`, etc. in the input parameters of the interface processing function. But sometimes we need to get more request context information. At this time, we can get it through [`useHonoContext`](/apis/app/runtime/bff/use-hono-context):
|
|
560
|
+
|
|
561
|
+
<BFFOperatorCode>
|
|
562
|
+
|
|
563
|
+
```typescript title="api/lambda/user.ts"
|
|
564
|
+
import { Api, Get, Query, useHonoContext } from '@modern-js/plugin-bff/server';
|
|
565
|
+
import { z } from 'zod';
|
|
566
|
+
|
|
567
|
+
const UserSchema = z.object({
|
|
568
|
+
name: z.string().min(2).max(10),
|
|
569
|
+
email: z.string().email(),
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
export const queryUser = Api(
|
|
573
|
+
Get('/user'),
|
|
574
|
+
Query(UserSchema),
|
|
575
|
+
async ({ query }) => {
|
|
576
|
+
const c = useHonoContext();
|
|
577
|
+
const userAgent = c.req.header('user-agent');
|
|
578
|
+
return {
|
|
579
|
+
name: query.name,
|
|
580
|
+
userAgent,
|
|
581
|
+
};
|
|
582
|
+
},
|
|
583
|
+
);
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
</BFFOperatorCode>
|
|
587
|
+
|
|
588
|
+
## FAQ
|
|
589
|
+
|
|
590
|
+
### Can I use TypeScript instead of zod schema
|
|
591
|
+
|
|
592
|
+
If you want to use TypeScript instead of zod schema, you can use [ts-to-zod](https://www.npmjs.com/package/ts-to-zod) to convert TypeScript to zod schema first, and then use the converted schema.
|
|
593
|
+
|
|
594
|
+
The reasons we chose zod instead of pure TypeScript to define input parameter type information are:
|
|
595
|
+
|
|
596
|
+
- zod has a low learning curve.
|
|
597
|
+
- In the validation scenario, zod schema has stronger expressiveness than TypeScript.
|
|
598
|
+
- zod is easier to extend.
|
|
599
|
+
- Solutions for obtaining TypeScript static type information at runtime are not mature enough.
|
|
600
|
+
|
|
601
|
+
For specific comparisons of different solutions, you can refer to [Why Use Zod](https://bytedance.feishu.cn/wiki/wikcnrNnidvxHLY2SIT4nadXOCh#doxcnGoki68KEOiw8UD1fYd3lRh). If you have more ideas and questions, please feel free to contact us.
|
|
602
|
+
|
|
603
|
+
## More Practices
|
|
604
|
+
|
|
605
|
+
### Add HTTP Cache to Interface
|
|
606
|
+
|
|
607
|
+
In frontend development, some server interfaces (such as some configuration interfaces) have long response times, but actually don't need to be updated for a long time. For such interfaces, we can set HTTP cache to improve page performance:
|
|
608
|
+
|
|
609
|
+
<BFFOperatorCode>
|
|
610
|
+
|
|
611
|
+
```typescript
|
|
612
|
+
import { Api, SetHeaders } from '@modern-js/plugin-bff/server';
|
|
613
|
+
|
|
614
|
+
export const get = Api(
|
|
615
|
+
// Cache will only take effect when using integrated calls or fetch for requests
|
|
616
|
+
// Within 1s, the cache does not validate and directly returns the response
|
|
617
|
+
// Within 1s-60s, first return the old cache information, and at the same time re-initiate a validation request to fill the cache with new values
|
|
618
|
+
SetHeaders({
|
|
619
|
+
'Cache-Control': 'max-age=1, stale-while-revalidate=59',
|
|
620
|
+
}),
|
|
621
|
+
async () => {
|
|
622
|
+
await wait(500);
|
|
623
|
+
return 'Hello Modern.js';
|
|
624
|
+
},
|
|
625
|
+
);
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
</BFFOperatorCode>
|
|
@@ -17,9 +17,9 @@ import { configure } from '@modern-js/plugin-bff/client';
|
|
|
17
17
|
|
|
18
18
|
configure({
|
|
19
19
|
// ...
|
|
20
|
-
})
|
|
20
|
+
});
|
|
21
21
|
|
|
22
|
-
const Index = () => <div>Hello world</div
|
|
22
|
+
const Index = () => <div>Hello world</div>;
|
|
23
23
|
export default Index;
|
|
24
24
|
```
|
|
25
25
|
|
|
@@ -32,15 +32,15 @@ For example, imagine a project with a page URL `https://website.com`. This page
|
|
|
32
32
|
Currently, the following request headers are automatically passed through in Modern.js:
|
|
33
33
|
|
|
34
34
|
```ts
|
|
35
|
-
['cookie', 'user-agent', 'x-tt-logid', 'x-tt-stress']
|
|
35
|
+
['cookie', 'user-agent', 'x-tt-logid', 'x-tt-stress'];
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
You can configure additional request headers using `configure`. For example, in the following snippet, Modern.js will automatically pass the `x-uid` information from the SSR page request to the BFF service:
|
|
39
39
|
|
|
40
40
|
```tsx
|
|
41
41
|
configure({
|
|
42
|
-
allowedHeaders: ['x-uid']
|
|
43
|
-
})
|
|
42
|
+
allowedHeaders: ['x-uid'],
|
|
43
|
+
});
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
## Adding Interceptors
|
|
@@ -54,9 +54,14 @@ configure({
|
|
|
54
54
|
interceptor(request) {
|
|
55
55
|
return async (url, params) => {
|
|
56
56
|
const res = await request(url, params);
|
|
57
|
-
return
|
|
57
|
+
// Interceptors may return Response objects, which need to be manually parsed as JSON
|
|
58
|
+
if (res instanceof Response) {
|
|
59
|
+
return res.json();
|
|
60
|
+
}
|
|
61
|
+
// If it's already parsed data, return directly
|
|
62
|
+
return res;
|
|
58
63
|
};
|
|
59
|
-
}
|
|
64
|
+
},
|
|
60
65
|
});
|
|
61
66
|
```
|
|
62
67
|
|
|
@@ -68,7 +73,10 @@ If configuring interceptors alone cannot meet your needs and you want to customi
|
|
|
68
73
|
import nodeFetch from 'node-fetch';
|
|
69
74
|
|
|
70
75
|
const customFetch = (input: RequestInfo | URL, init: RequestInit) => {
|
|
71
|
-
const curFetch =
|
|
76
|
+
const curFetch =
|
|
77
|
+
process.env.MODERN_TARGET !== 'node'
|
|
78
|
+
? fetch
|
|
79
|
+
: (nodeFetch as unknown as typeof fetch);
|
|
72
80
|
return curFetch(input, init).then(async res => {
|
|
73
81
|
const data = await res.json();
|
|
74
82
|
data.hello = 'hello custom sdk';
|
|
@@ -97,7 +105,7 @@ configure({
|
|
|
97
105
|
async request(...config: Parameters<typeof fetch>) {
|
|
98
106
|
const [url, params] = config;
|
|
99
107
|
const res = await axios({
|
|
100
|
-
url: url as string,
|
|
108
|
+
url: url as string, // Here we need to use `as` because fetch and axios types are somewhat incompatible
|
|
101
109
|
method: params?.method as Method,
|
|
102
110
|
data: params?.body,
|
|
103
111
|
headers: params?.headers as Headers,
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# File Upload
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
BFF combined with runtime framework provides file upload capabilities, supporting integrated calls and pure function manual calls.
|
|
4
|
+
|
|
5
|
+
import BffUpload from '@site-docs-en/components/bff-upload';
|
|
4
6
|
|
|
5
7
|
<BffUpload />
|