@modern-js/main-doc 0.0.0-next-1685369216913 → 0.0.0-next-1685418932858

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.
Files changed (54) hide show
  1. package/CHANGELOG.md +4 -4
  2. package/docs/en/apis/app/hooks/api/api.mdx +80 -0
  3. package/docs/en/apis/app/hooks/api/app.mdx +12 -0
  4. package/docs/en/apis/app/hooks/api/lambda.mdx +57 -0
  5. package/docs/en/apis/app/hooks/api/test.mdx +1 -1
  6. package/docs/en/apis/app/hooks/config/html.mdx +2 -2
  7. package/docs/en/apis/app/hooks/config/icon.mdx +19 -19
  8. package/docs/en/apis/app/hooks/config/mock.mdx +1 -1
  9. package/docs/en/apis/app/hooks/config/public.mdx +10 -10
  10. package/docs/en/apis/app/hooks/config/storybook.mdx +3 -3
  11. package/docs/en/apis/app/hooks/config/upload.mdx +13 -13
  12. package/docs/en/apis/app/hooks/modern-config.mdx +4 -4
  13. package/docs/en/apis/app/hooks/server/index_.mdx +2 -9
  14. package/docs/en/apis/app/hooks/server/test.mdx +5 -2
  15. package/docs/en/apis/app/hooks/shared.mdx +1 -1
  16. package/docs/en/apis/app/hooks/src/app.mdx +17 -27
  17. package/docs/en/apis/app/hooks/src/index_.mdx +6 -6
  18. package/docs/en/apis/app/hooks/src/pages.mdx +41 -37
  19. package/docs/en/apis/app/hooks/src/routes.mdx +16 -36
  20. package/docs/en/apis/app/hooks/src/server.mdx +1 -1
  21. package/docs/en/apis/app/hooks/src/stories.mdx +6 -3
  22. package/docs/en/apis/app/hooks/src/test.mdx +4 -3
  23. package/docs/en/guides/advanced-features/bff/frameworks.mdx +2 -2
  24. package/docs/en/guides/advanced-features/bff/function.mdx +4 -4
  25. package/docs/en/guides/advanced-features/bff/type.mdx +5 -5
  26. package/docs/en/guides/basic-features/routes.mdx +11 -7
  27. package/docs/zh/apis/app/hooks/api/{functions/api.mdx → api.mdx} +4 -4
  28. package/docs/zh/apis/app/hooks/api/app.mdx +12 -0
  29. package/docs/zh/apis/app/hooks/api/{framework/lambda.mdx → lambda.mdx} +5 -5
  30. package/docs/zh/apis/app/hooks/api/test.mdx +3 -3
  31. package/docs/zh/apis/app/hooks/config/icon.mdx +15 -15
  32. package/docs/zh/apis/app/hooks/config/mock.mdx +1 -1
  33. package/docs/zh/apis/app/hooks/config/public.mdx +3 -3
  34. package/docs/zh/apis/app/hooks/config/upload.mdx +1 -1
  35. package/docs/zh/apis/app/hooks/modern-config.mdx +3 -3
  36. package/docs/zh/apis/app/hooks/server/index_.mdx +1 -6
  37. package/docs/zh/apis/app/hooks/shared.mdx +1 -1
  38. package/docs/zh/apis/app/hooks/src/app.mdx +15 -25
  39. package/docs/zh/apis/app/hooks/src/index_.mdx +6 -6
  40. package/docs/zh/apis/app/hooks/src/pages.mdx +7 -3
  41. package/docs/zh/apis/app/hooks/src/routes.mdx +4 -4
  42. package/docs/zh/apis/app/hooks/src/stories.mdx +1 -1
  43. package/docs/zh/guides/basic-features/routes.mdx +7 -4
  44. package/package.json +5 -5
  45. package/docs/en/apis/app/hooks/api/framework/_category_.json +0 -4
  46. package/docs/en/apis/app/hooks/api/framework/lambda.mdx +0 -57
  47. package/docs/en/apis/app/hooks/api/functions/_category_.json +0 -4
  48. package/docs/en/apis/app/hooks/api/functions/api.mdx +0 -81
  49. package/docs/en/apis/app/hooks/api/functions/app.mdx +0 -12
  50. package/docs/en/apis/app/hooks/api/functions/common.mdx +0 -9
  51. package/docs/zh/apis/app/hooks/api/framework/_category_.json +0 -4
  52. package/docs/zh/apis/app/hooks/api/functions/_category_.json +0 -4
  53. package/docs/zh/apis/app/hooks/api/functions/app.mdx +0 -12
  54. package/docs/zh/apis/app/hooks/api/functions/common.mdx +0 -9
@@ -4,13 +4,13 @@ sidebar_position: 3
4
4
  ---
5
5
  # public/
6
6
 
7
- `public/` 目录中可以放置任意格式的静态资源文件,文件会被 Serve 在 Web 应用域名下。
7
+ `public/` 目录中可以放置任意格式的静态资源文件,文件会被服务端部署到对应的应用域名下。
8
8
 
9
9
  ## 说明
10
10
 
11
- 被 Serve 的文件路由基于目录结构的约定,其中,`public/` 为根目录,对应 Web 应用根路径。
11
+ 文件路由基于目录结构的约定,其中,`public/` 为根目录,对应 Web 应用根路径。
12
12
 
13
- 例如 `config/public/sdk/index.js` 文件,在部署后将会被 Serve 在 `${domain}/sdk/index.js` 下。
13
+ 例如 `config/public/sdk/index.js` 文件,在部署后将会被部署在 `${domain}/sdk/index.js` 下。
14
14
 
15
15
  ## 场景
16
16
 
@@ -14,7 +14,7 @@ sidebar_position: 4
14
14
 
15
15
  ## 场景
16
16
 
17
- 例如 `google-analysis.js` 等项目自用的 SDK(通常需要 http 缓存)。
17
+ 例如 `google-analysis.js` 等项目自用的 SDK(通常需要 HTTP 缓存)。
18
18
 
19
19
  图片、字体文件、通用 CSS 等。
20
20
 
@@ -1,9 +1,9 @@
1
1
  ---
2
- title: modern.config.js
2
+ title: modern.config.[tj]s
3
3
  sidebar_position: 8
4
4
  ---
5
- # modern.config.js
5
+ # modern.config.[tj]s
6
6
 
7
7
  Modern.js 配置文件, 通过该文件可以对当前项目的各个方面进行个性化配置。
8
8
 
9
- 了解配置的具体用法,请参考 [配置使用](/configure/app/usage)。
9
+ 了解配置的具体用法,请参考[配置使用](/configure/app/usage)。
@@ -4,11 +4,6 @@ sidebar_position: 1
4
4
  ---
5
5
  # index.[tj]s
6
6
 
7
- 扩展 Modern.js Web Server 的文件,在此文件中可以给应用工程启动的 Web Server 添加 Hook 或 Middleware。
7
+ 扩展 Modern.js Web Server 的文件,在此文件中可以给应用工程启动的 Web Server 添加 [Hook](/apis/app/runtime/web-server/hook)[Middleware](/apis/app/runtime/web-server/middleware)
8
8
 
9
9
  可以对请求和响应进行拦截处理,鉴权与角色、请求预处理、异常兜底等。也可在内置处理逻辑(包括路由匹配、资源寻址、头部注入、页面渲染,静态 Web 托管)插入特定的业务逻辑。
10
-
11
- :::info
12
- 具体使用示例可查看 [Hook](/apis/app/runtime/web-server/hook) & [Middleware](/apis/app/runtime/web-server/middleware)。
13
-
14
- :::
@@ -4,4 +4,4 @@ sidebar_position: 5
4
4
  ---
5
5
  # shared/
6
6
 
7
- 共享源码目录。当项目在 `api/`、`server/`、`src/` 下有公共代码时,可将这些代码放到 shared 目录下,而不是直接引用。
7
+ 共享源码目录。当项目在 `api/`、`server/`、`src/` 下有公共代码时,可将这些代码放到 `shared` 目录下,而不是直接引用。
@@ -4,46 +4,36 @@ sidebar_position: 1
4
4
  ---
5
5
  # App.[tj]sx
6
6
 
7
- 应用使用自控路由时的入口标识符。
7
+ 应用使用[自控路由](/guides/basic-features/routes.html#self-controlled-routing)时的入口标识符。
8
8
 
9
9
  `App.[tj]sx` 并不是实际的应用入口,Modern.js 会自动生成真正的构建打包的入口文件, 内容大致如下:
10
10
 
11
11
  ```js
12
12
  import React from 'react';
13
+ import ReactDOM from 'react-dom/client';
13
14
  import { createApp, bootstrap } from '@modern-js/runtime';
14
15
  // App.[jt]sx
15
16
  import App from '@_modern_js_src/App';
16
- import { state } from '@modern-js/runtime/plugins';
17
- import {
18
- immer,
19
- effects,
20
- autoActions,
21
- devtools,
22
- } from '@modern-js/runtime/model';
23
-
24
- const createStatePlugins = config => {
25
- const plugins = [];
26
- plugins.push(immer(config['immer']));
27
- plugins.push(effects(config['effects']));
28
- plugins.push(autoActions(config['autoActions']));
29
- plugins.push(devtools(config['devtools']));
30
- return plugins;
31
- };
17
+ // runtime plugin
18
+ import { router } from '@modern-js/runtime/plugins';
19
+
20
+ const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
21
+ const MOUNT_ID = 'root';
22
+
32
23
  let AppWrapper = null;
24
+
33
25
  function render() {
34
26
  AppWrapper = createApp({
35
27
  plugins: [
36
- state({
37
- ...{ plugins: createStatePlugins(true) },
38
- ...App?.config?.state,
39
- }),
40
- ],
41
- })(App);
28
+ router({...{"serverBase":["/"]}, ...App.config?.router}),
29
+ ]
30
+ })(App)
42
31
  if (IS_BROWSER) {
43
- bootstrap(AppWrapper, MOUNT_ID);
32
+ bootstrap(AppWrapper, MOUNT_ID, null, ReactDOM);
44
33
  }
45
- return AppWrapper;
34
+ return AppWrapper
46
35
  }
36
+
47
37
  AppWrapper = render();
48
38
  export default AppWrapper;
49
39
  ```
@@ -4,13 +4,13 @@ sidebar_position: 4
4
4
  ---
5
5
  # index.[tj]s
6
6
 
7
- 应用项目入口标识。
7
+ 应用使用自定义 `bootstrap` 时的入口标识。
8
8
 
9
- 通常情况下 [`src/App.[tj]sx, src/[entry]/App.[tj]sx`](/apis/app/hooks/src/app) 钩子文件已经能满足我们的需求,当我们需要在 `bootstrap` 之前添加自定义行为或者完全接管 webpack 打包入口时,可以在 `src` 或者入口目录下放置 `index.[tj]s`。 下面有分两种情况进行讨论:
9
+ 通常情况下 [`App.[tj]sx`](/apis/app/hooks/src/app) 钩子文件已经能满足我们的需求,当我们需要在 `bootstrap` 之前添加自定义行为或者完全接管 webpack 打包入口时,可以在 `src` 或者入口目录下放置 `index.[tj]s`。 下面有分两种情况进行讨论:
10
10
 
11
- 1. 在 bootstrap 之前添加自定义行为:
11
+ ## 在 bootstrap 之前添加自定义行为
12
12
 
13
- 只需要 `src/index.[tj]s` 默认导出函数:
13
+ 只需要 `src/index.[tj]s` 默认导出函数:
14
14
 
15
15
  ```js title=src/index.js
16
16
  import ReactDOM from 'react-dom/client';
@@ -22,9 +22,9 @@ export default (App: React.ComponentType) => {
22
22
  };
23
23
  ```
24
24
 
25
- 2. 完全接管 webpack 入口:
25
+ ## 完全接管 webpack 入口
26
26
 
27
- 当 `src/index.[tj]sx?` 下没有默认导出函数时,该文件即为真正的 webpack 打包入口文件, 可以直接像使用 create-react-app 等脚手架一样组织代码:
27
+ 当 `src/index.[tj]sx?` 下没有默认导出函数时,该文件即为真正的 webpack 打包入口文件, 可以直接像使用 create-react-app 等脚手架一样组织代码:
28
28
 
29
29
  ```js title=src/index.jsx
30
30
  import React from 'react';
@@ -4,9 +4,13 @@ sidebar_position: 3
4
4
  ---
5
5
  # pages/
6
6
 
7
- 应用使用基于文件系统路由时的入口标识。
7
+ 应用使用 [`Pages` 入口](https://modernjs.dev/v1/docs/guides/usages/entries#pages-%E5%85%A5%E5%8F%A3)时的入口标识。
8
8
 
9
- 当项目结构为 `Pages 入口` 类型时, 会分析 `src/pages` 目录下的文件得到客户端路由配置。
9
+ :::info
10
+ 兼容 Modern.js 1.0 `Pages` 入口,推荐使用[约定式路由](guides/basic-features/routes.html#约定式路由)。
11
+ :::
12
+
13
+ 当项目结构为 `Pages 入口`类型时, 会分析 `src/pages` 目录下的文件得到客户端路由配置。
10
14
 
11
15
  举例说明,例如以下目录结构:
12
16
 
@@ -20,7 +24,7 @@ sidebar_position: 3
20
24
  └── info.jsx
21
25
  ```
22
26
 
23
- 对应生成的路由配置为:
27
+ 对应生成的路由配置为:
24
28
 
25
29
  ```bash
26
30
  [
@@ -4,13 +4,13 @@ sidebar_position: 2
4
4
  ---
5
5
  # routes/
6
6
 
7
- 应用使用基于文件系统路由时的入口标识。
7
+ 应用使用[约定式路由](guides/basic-features/routes.html#约定式路由)时的入口标识。
8
8
 
9
- 当项目结构为 `Routes 入口` 类型时, 会分析 `src/routes` 目录下的文件得到客户端路由配置。具体用法请查看[约定式路由](/guides/basic-features/routes)
9
+ 约定式路由以 `routes/` 为约定的入口, 会分析 `src/routes` 目录下的文件得到客户端路由配置。
10
10
 
11
11
  任何在 `src/routes` 下的 `layout.[tj]sx` 和 `page.[tj]sx` 都会作为应用的路由:
12
12
 
13
- ```bash {3}
13
+ ```bash {3,4}
14
14
  .
15
15
  └── routes
16
16
  ├── layout.tsx
@@ -54,7 +54,7 @@ sidebar_position: 2
54
54
 
55
55
  在组件中,可以通过 [useParams](/apis/app/runtime/router/router#useparams) 获取对应命名的参数。
56
56
 
57
- loader 中,params 会作为 [loader](/guides/basic-features/data-fetch#loader-函数) 的入参,通过 `params` 的属性可以获取到对应的参数。
57
+ 在使用 [loader](/guides/basic-features/data-fetch#loader-函数) 函数获取数据时,`params` 会作为 `loader` 函数的入参,通过 `params` 的属性可以获取到对应的参数。
58
58
 
59
59
  ## 布局组件
60
60
 
@@ -8,7 +8,7 @@ sidebar_position: 7
8
8
 
9
9
  可以在项目源码目录 `src/` 下创建 `*.stories.[tj]sx` 格式的文件作为 Storybook 的调试文件。
10
10
 
11
- 在项目下执行 dev story 命令,支持使用这些文件在 Storybook 中对相关内容进行调试。
11
+ 在项目下执行 `npm run dev story` 命令,支持使用这些文件在 Storybook 中对相关内容进行调试。
12
12
 
13
13
  :::info
14
14
  使用 Storybook 需要提前在项目下执行 new 命令启用「Visual Testing (Storybook)」模式。
@@ -449,7 +449,7 @@ export const init = (context: RuntimeContext) => {
449
449
  在约定式路由下, Modern.js 会根据路由,自动地对路由进行分片,当用户访问具体的路由时,会自动加载对应的分片,这样可以有效地减少首屏加载的时间。但这也带来了一个问题,当用户访问一个路由时,如果该路由对应的分片还未加载完成,就会出现白屏的情况。
450
450
  这种情况下你可以通过定义 `Loading` 组件,在静态资源加载完成前,展示一个自定义的 `Loading` 组件。
451
451
 
452
- 为了进一步提升用户体验,减少 loading 的时间,Modern.js 支持在 Link 组件上定义 `prefetch` 属性,可以提前对静态资源和数据进行加载, `prefetch` 属性有三个可选值:
452
+ 为了进一步提升用户体验,减少 loading 的时间,Modern.js 支持在 Link 组件上定义 `prefetch` 属性,可以提前对静态资源和数据进行加载:
453
453
 
454
454
  ```tsx
455
455
  <Link prefetch="intent" to="page">
@@ -461,6 +461,8 @@ export const init = (context: RuntimeContext) => {
461
461
 
462
462
  :::
463
463
 
464
+ `prefetch` 属性有三个可选值:
465
+
464
466
  - `none`, 默认值,不会做 prefetch,没有任何额外的行为。
465
467
  - `intent`,这是我们推荐大多数场景下使用的值,当你把鼠标放在 Link 上时,会自动开始加载对应的分片和 Data Loader 中定义的数据,当鼠标移开时,会自动取消加载。在我们的测试中,即使是快速点击,也能减少大约 200ms 的加载时间。
466
468
  - `render`,当 Link 组件渲染时,就会加载对应的分片和 Data Loader 中定义的数据。
@@ -505,8 +507,6 @@ export default () => {
505
507
  Modern.js 默认对约定式路由做了一系列资源加载及渲染上的优化,并且提供了开箱即用的 SSR 能力,而这些能力,在使用自控路由时,都需要开发者自行封装,推荐开发者使用约定式路由。
506
508
  :::
507
509
 
508
- 使用自控式路由时,如果开发者关闭了 [`runtime.router`](/configure/app/runtime/router) 配置,并直接使用 `react-router-dom`,那还需要根据 React Router 文档自行包裹 `Provider`。
509
-
510
510
  ## 其他路由方案
511
511
 
512
512
  默认情况下,Modern.js 会开启内置的路由方案,即 React Router。
@@ -519,7 +519,9 @@ export default defineConfig({
519
519
  });
520
520
  ```
521
521
 
522
- Modern.js `@modern-js/runtime/router` 命名空间暴露了 React Router 的 API 供开发者使用,保证开发者和 Modern.js 中使用同一份代码。另外,这种情况下,React Router 的代码会被打包到 JS 产物中。如果项目已经有自己的路由方案,或者不需要使用客户端路由,可以关闭这个功能。
522
+ 如上述配置,当开启 [`runtime.router`](/configure/app/runtime/router) 配置时,Modern.js 会从 `@modern-js/runtime/router` 命名空间导出 React Router 的 API 供开发者使用,保证开发者和 Modern.js 中使用同一份代码,并自动根据 router 配置包裹 `Provider` 组件。另外,这种情况下,React Router 的代码会被打包到 JS 产物中。
523
+
524
+ 如果项目已经有自己的路由方案,或者不需要使用客户端路由,可以关闭这个功能。
523
525
 
524
526
  ```js
525
527
  export default defineConfig({
@@ -529,5 +531,6 @@ export default defineConfig({
529
531
  });
530
532
  ```
531
533
 
534
+ 如上述配置, 如果关闭了 [`runtime.router`](/configure/app/runtime/router) 配置,并直接使用 `react-router-dom` 进行项目路由管理时,还需要根据 React Router 文档自行包裹 `Provider`。
532
535
 
533
536
 
package/package.json CHANGED
@@ -15,13 +15,13 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "0.0.0-next-1685369216913",
18
+ "version": "0.0.0-next-1685418932858",
19
19
  "publishConfig": {
20
20
  "registry": "https://registry.npmjs.org/",
21
21
  "access": "public"
22
22
  },
23
23
  "peerDependencies": {
24
- "@modern-js/builder-doc": "0.0.0-next-1685369216913"
24
+ "@modern-js/builder-doc": "0.0.0-next-1685418932858"
25
25
  },
26
26
  "devDependencies": {
27
27
  "classnames": "^2",
@@ -33,9 +33,9 @@
33
33
  "fs-extra": "^10",
34
34
  "@types/node": "^16",
35
35
  "@types/fs-extra": "^9",
36
- "@modern-js/builder-doc": "0.0.0-next-1685369216913",
37
- "@modern-js/doc-tools": "0.0.0-next-1685369216913",
38
- "@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-1685369216913"
36
+ "@modern-js/builder-doc": "0.0.0-next-1685418932858",
37
+ "@modern-js/doc-tools": "0.0.0-next-1685418932858",
38
+ "@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-1685418932858"
39
39
  },
40
40
  "scripts": {
41
41
  "dev": "modern dev",
@@ -1,4 +0,0 @@
1
- {
2
- "label": "框架写法",
3
- "position": 2
4
- }
@@ -1,57 +0,0 @@
1
- ---
2
- title: lambda/*.[tj]s
3
- sidebar_position: 1
4
- ---
5
- # lambda/*.[tj]s
6
-
7
- Declaring API routing in BFF framework mode. Except [some files](/apis/app/hooks/api/framework/lambda#allow-list), files in `api/` are registered as routes.
8
-
9
- :::info
10
- use `api/` need execute new command to enable the 「BFF」 feature.
11
-
12
- :::
13
-
14
- :::tip
15
- this file supports the use `js` or `ts`, but the functions must be exported using the ESM syntax.
16
-
17
- :::
18
-
19
- ## Routing Rule
20
-
21
- ### Default Route
22
-
23
- The files named `index` will be upper level routing:
24
-
25
- - `api/lambda/index.ts` -> `$BASENAME/`
26
- - `api/lambda/user/index.ts` -> `$BASENAME/user`
27
-
28
- ### Multi Level Route
29
-
30
- The routing system also supports parsing multiple levels of files. and if you create a folder, the files will still be automatically parsed in the same way.
31
-
32
- - `api/lambda/hello.ts` -> `$BASENAME/hello`
33
- - `api/lambda/user/list.ts` -> `$BASENAME/user/list`
34
-
35
- ### Dynamic Route
36
-
37
- Dynamic named routing parameters can be supported by creating folders or files with `[xxx]`.
38
-
39
- - `api/lambda/user/[username]/info.ts` -> `$BASENAME/user/:username/info`
40
- - `api/lambda/user/[username]/delete.ts` -> `$BASENAME/user/:username/delete`
41
- - `api/lambda/article/[id]/info.ts` -> `$BASENAME/article/:id/info`
42
-
43
- the `$BASENAME` can be configured in `modern.config.js`, the default value is `/api`.
44
-
45
- ### Allow List
46
-
47
- By default, all files in the `api/` will be parsed as BFF function. but we also set a allow list, and these files will not be parsed:
48
-
49
- - file name start with `_`, for example: `_utils.ts`.
50
- - files in directory that name start with `_`, for example: `_utils/index.ts`、`_utils/cp.ts`.
51
- - test files, for example: `foo.test.ts`.
52
- - TypeScript define files, for example: `hello.d.ts`.
53
- - files in `node_module`.
54
-
55
- ## Define Function
56
-
57
- the same as [Define Function](/apis/app/hooks/api/functions/api#define-function).
@@ -1,4 +0,0 @@
1
- {
2
- "label": "函数写法",
3
- "position": 1
4
- }
@@ -1,81 +0,0 @@
1
- ---
2
- title: '**/*.[tj]s'
3
- sidebar_position: 1
4
- ---
5
- # **/*.[tj]s
6
-
7
- Declaring API routing in BFF function mode. Except [some files](/apis/app/hooks/api/functions/api#allow-list), files in `api/` are registered as routes.
8
-
9
- :::info
10
- use `api/` need execute new command to enable the 「BFF」 feature.
11
-
12
- :::
13
-
14
- :::tip
15
- this file supports the use `js` or `ts`, but the functions must be exported using the ESM syntax.
16
-
17
- :::
18
-
19
- ## Routing Rule
20
-
21
- ### Default Route
22
-
23
- The files named `index` will be upper level routing:
24
-
25
- - `api/index.ts` -> `$BASENAME/`
26
- - `api/user/index.ts` -> `$BASENAME/user`
27
-
28
- ### Multi Level Route
29
-
30
- The routing system also supports parsing multiple levels of files. and if you create a folder, the files will still be automatically parsed in the same way.
31
-
32
- - `api/hello.ts` -> `$BASENAME/hello`
33
- - `api/user/list.ts` -> `$BASENAME/user/list`
34
-
35
- ### Dynamic Route
36
-
37
- Dynamic named routing parameters can be supported by creating folders or files with `[xxx]`.
38
-
39
- - `api/user/[username]/info.ts` -> `$BASENAME/user/:username/info`
40
- - `api/user/[username]/delete.ts` -> `$BASENAME/user/:username/delete`
41
- - `api/article/[id]/info.ts` -> `$BASENAME/article/:id/info`
42
-
43
- the `$BASENAME` can be configured in `modern.config.js`, the default value is `/api`.
44
-
45
- ### Allow List
46
-
47
- By default, all files in the `api/` will be parsed as BFF function. but we also set a allow list, and these files will not be parsed:
48
-
49
- - file name start with `_`, for example: `_utils.ts`.
50
- - files in directory that name start with `_`, for example: `_utils/index.ts`、`_utils/cp.ts`.
51
- - test files, for example: `foo.test.ts`.
52
- - TypeScript define files, for example: `hello.d.ts`.
53
- - files in `node_module`.
54
-
55
- ## Define Function
56
-
57
- In addition to the above routing rules, the function definition and export in the code also have conventions.
58
-
59
- function need named exports, and the name of the exported function is the HTTP Method:
60
-
61
- ```ts
62
- export const get = async () => {
63
- return {
64
- name: 'Modern.js',
65
- desc: 'Modern web Solutions',
66
- };
67
- };
68
- ```
69
-
70
- Export the function like above will generate a `POST` interface.
71
-
72
- App support 9 Method definitions: `GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`. so App can use these name as function export nane.
73
-
74
- The name is insensitive, whaterver `get`、`Get`、`GEt`、`GET`, can be accurately identified. And default export, `export default xxx` will be `Get` method.
75
-
76
- because `delete` is a keyword in JavaScript, use `del` or `DELETE` instead.
77
-
78
- Multiple functions of different Methods can be defined in one file, but if multiple functions of the same Method are defined, only the first can work.
79
-
80
- :::info
81
- It should be noted that the defined functions should be asynchronous, which is related to the type when the function is called.
@@ -1,12 +0,0 @@
1
- ---
2
- title: _app.[tj]s
3
- sidebar_position: 3
4
- ---
5
- # _app.[tj]s
6
-
7
- in BFF function mode, this file can add middleware before [BFF 函数](/apis/app/hooks/api/functions/api).
8
-
9
- :::note
10
- For detail, see [hook](/apis/app/runtime/bff/hook)
11
-
12
- :::
@@ -1,9 +0,0 @@
1
- ---
2
- title: '**/_*.[tj]s, _*/**'
3
- sidebar_position: 2
4
- ---
5
- # **/_*.[tj]s, _*/**
6
-
7
- under BFF function mode, these files are not registered as routes.
8
-
9
- Any files that not routes, but required in the project can be named in this way.
@@ -1,4 +0,0 @@
1
- {
2
- "label": "Framework Mode",
3
- "position": 2
4
- }
@@ -1,4 +0,0 @@
1
- {
2
- "label": "Function Mode",
3
- "position": 1
4
- }
@@ -1,12 +0,0 @@
1
- ---
2
- title: _app.[tj]s
3
- sidebar_position: 3
4
- ---
5
- # _app.[tj]s
6
-
7
- 在函数写法下,该文件可以为 [BFF 函数](/apis/app/hooks/api/functions/api)添加前置中间件。
8
-
9
- :::note
10
- 具体示例请参考 [hook](/apis/app/runtime/bff/hook)
11
-
12
- :::
@@ -1,9 +0,0 @@
1
- ---
2
- title: '**/_*.[tj]s, _*/**'
3
- sidebar_position: 2
4
- ---
5
- # **/_*.[tj]s, _*/**
6
-
7
- 在 BFF 函数写法下,这些文件不会注册为路由。
8
-
9
- 你可以在这种目录或文件下放任意项目中需要的代码,文件等。