@modern-js/app-tools 3.6.0 → 3.8.0
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/bin/modern-bundle-docs.js +7 -0
- package/dist/cjs/builder/generator/createBuilderProviderConfig.js +5 -1
- package/dist/cjs/bundleDocs.js +114 -0
- package/dist/cjs/commands/build.js +2 -1
- package/dist/cjs/commands/deploy.js +4 -2
- package/dist/cjs/commands/index.js +0 -4
- package/dist/cjs/config/default.js +2 -0
- package/dist/cjs/index.js +52 -14
- package/dist/cjs/plugins/analyze/index.js +2 -2
- package/dist/cjs/plugins/analyze/utils.js +3 -2
- package/dist/cjs/plugins/initialize/index.js +4 -3
- package/dist/cjs/plugins/serverBuild.js +3 -2
- package/dist/esm/builder/generator/createBuilderProviderConfig.mjs +5 -1
- package/dist/esm/bundleDocs.mjs +62 -0
- package/dist/esm/commands/build.mjs +2 -1
- package/dist/esm/commands/deploy.mjs +4 -2
- package/dist/esm/commands/index.mjs +0 -4
- package/dist/esm/config/default.mjs +2 -0
- package/dist/esm/index.mjs +15 -1
- package/dist/esm/plugins/analyze/index.mjs +2 -2
- package/dist/esm/plugins/analyze/utils.mjs +3 -2
- package/dist/esm/plugins/initialize/index.mjs +4 -3
- package/dist/esm/plugins/serverBuild.mjs +3 -2
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.mjs +5 -1
- package/dist/esm-node/bundleDocs.mjs +63 -0
- package/dist/esm-node/commands/build.mjs +2 -1
- package/dist/esm-node/commands/deploy.mjs +4 -2
- package/dist/esm-node/commands/index.mjs +0 -4
- package/dist/esm-node/config/default.mjs +2 -0
- package/dist/esm-node/index.mjs +15 -1
- package/dist/esm-node/plugins/analyze/index.mjs +2 -2
- package/dist/esm-node/plugins/analyze/utils.mjs +3 -2
- package/dist/esm-node/plugins/initialize/index.mjs +4 -3
- package/dist/esm-node/plugins/serverBuild.mjs +3 -2
- package/dist/types/bundleDocs.d.ts +20 -0
- package/dist/types/commands/deploy.d.ts +2 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/plugins/analyze/utils.d.ts +1 -1
- package/dist/types/types/config/dev.d.ts +6 -0
- package/docs/apis/app/commands.md +241 -0
- package/docs/apis/app/hooks/api/lambda.md +9 -0
- package/docs/apis/app/hooks/config/favicon.md +24 -0
- package/docs/apis/app/hooks/config/html.md +5 -0
- package/docs/apis/app/hooks/config/icon.md +24 -0
- package/docs/apis/app/hooks/config/mock.md +6 -0
- package/docs/apis/app/hooks/config/public.md +26 -0
- package/docs/apis/app/hooks/config/upload.md +50 -0
- package/docs/apis/app/hooks/modern-config.md +5 -0
- package/docs/apis/app/hooks/server/server.md +5 -0
- package/docs/apis/app/hooks/shared.md +3 -0
- package/docs/apis/app/hooks/src/app.md +30 -0
- package/docs/apis/app/hooks/src/entry.md +34 -0
- package/docs/apis/app/hooks/src/entry.server.md +51 -0
- package/docs/apis/app/hooks/src/modern.runtime.md +5 -0
- package/docs/apis/app/hooks/src/routes.md +86 -0
- package/docs/apis/app/hooks/src/server.md +3 -0
- package/docs/apis/app/runtime/bff/use-hono-context.md +27 -0
- package/docs/apis/app/runtime/core/create-root.md +19 -0
- package/docs/apis/app/runtime/core/render.md +39 -0
- package/docs/apis/app/runtime/core/runtime-context.md +156 -0
- package/docs/apis/app/runtime/router/router.md +280 -0
- package/docs/apis/app/runtime/ssr/no-ssr.md +35 -0
- package/docs/apis/app/runtime/ssr/renderStreaming.md +67 -0
- package/docs/apis/app/runtime/ssr/renderString.md +60 -0
- package/docs/apis/app/runtime/ssr/requestHandler.md +44 -0
- package/docs/apis/app/runtime/utility/css-in-js.md +40 -0
- package/docs/apis/app/runtime/utility/head.md +35 -0
- package/docs/apis/app/runtime/utility/loadable.md +82 -0
- package/docs/community/blog/2022-0708-updates.md +91 -0
- package/docs/community/blog/2022-0910-updates.md +76 -0
- package/docs/community/blog/overview.md +63 -0
- package/docs/community/blog/v2-release-note.md +238 -0
- package/docs/community/blog/v3-release-note.md +622 -0
- package/docs/community/contributing-guide.md +253 -0
- package/docs/community/releases.md +27 -0
- package/docs/community/showcase.md +34 -0
- package/docs/community/team.md +14 -0
- package/docs/configure/app/bff/cross-project.md +20 -0
- package/docs/configure/app/bff/prefix.md +29 -0
- package/docs/configure/app/builder-plugins.md +66 -0
- package/docs/configure/app/dev/asset-prefix.md +13 -0
- package/docs/configure/app/dev/before-start-url.md +17 -0
- package/docs/configure/app/dev/client.md +41 -0
- package/docs/configure/app/dev/hmr.md +10 -0
- package/docs/configure/app/dev/host.md +18 -0
- package/docs/configure/app/dev/https.md +77 -0
- package/docs/configure/app/dev/lazy-compilation.md +39 -0
- package/docs/configure/app/dev/live-reload.md +10 -0
- package/docs/configure/app/dev/mock-dir.md +31 -0
- package/docs/configure/app/dev/progress-bar.md +19 -0
- package/docs/configure/app/dev/server.md +124 -0
- package/docs/configure/app/dev/setup-middlewares.md +32 -0
- package/docs/configure/app/dev/start-url.md +48 -0
- package/docs/configure/app/dev/watch-files.md +27 -0
- package/docs/configure/app/dev/write-to-disk.md +10 -0
- package/docs/configure/app/experiments/source-build.md +31 -0
- package/docs/configure/app/html/app-icon.md +28 -0
- package/docs/configure/app/html/crossorigin.md +10 -0
- package/docs/configure/app/html/favicon.md +16 -0
- package/docs/configure/app/html/inject.md +10 -0
- package/docs/configure/app/html/meta.md +24 -0
- package/docs/configure/app/html/mount-id.md +10 -0
- package/docs/configure/app/html/output-structure.md +10 -0
- package/docs/configure/app/html/script-loading.md +10 -0
- package/docs/configure/app/html/tags.md +15 -0
- package/docs/configure/app/html/template-parameters.md +33 -0
- package/docs/configure/app/html/template.md +10 -0
- package/docs/configure/app/html/title.md +18 -0
- package/docs/configure/app/output/asset-prefix.md +11 -0
- package/docs/configure/app/output/assets-retry.md +77 -0
- package/docs/configure/app/output/charset.md +10 -0
- package/docs/configure/app/output/clean-dist-path.md +16 -0
- package/docs/configure/app/output/convert-to-rem.md +79 -0
- package/docs/configure/app/output/copy.md +10 -0
- package/docs/configure/app/output/css-modules.md +48 -0
- package/docs/configure/app/output/data-uri-limit.md +26 -0
- package/docs/configure/app/output/disable-css-module-extension.md +55 -0
- package/docs/configure/app/output/disable-inline-runtime-chunk.md +41 -0
- package/docs/configure/app/output/disable-svgr.md +16 -0
- package/docs/configure/app/output/disable-ts-checker.md +49 -0
- package/docs/configure/app/output/dist-path.md +43 -0
- package/docs/configure/app/output/enable-asset-manifest.md +36 -0
- package/docs/configure/app/output/enable-css-module-tsdeclaration.md +28 -0
- package/docs/configure/app/output/enable-inline-route-manifests.md +16 -0
- package/docs/configure/app/output/externals.md +20 -0
- package/docs/configure/app/output/filename-hash.md +10 -0
- package/docs/configure/app/output/filename.md +55 -0
- package/docs/configure/app/output/inject-styles.md +10 -0
- package/docs/configure/app/output/inline-scripts.md +29 -0
- package/docs/configure/app/output/inline-styles.md +29 -0
- package/docs/configure/app/output/legal-comments.md +18 -0
- package/docs/configure/app/output/minify.md +22 -0
- package/docs/configure/app/output/override-browserslist.md +22 -0
- package/docs/configure/app/output/polyfill.md +12 -0
- package/docs/configure/app/output/source-map.md +30 -0
- package/docs/configure/app/output/split-route-chunks.md +16 -0
- package/docs/configure/app/output/ssg.md +81 -0
- package/docs/configure/app/output/ssgByEntries.md +90 -0
- package/docs/configure/app/output/svg-default-export.md +30 -0
- package/docs/configure/app/output/temp-dir.md +20 -0
- package/docs/configure/app/performance/build-cache.md +39 -0
- package/docs/configure/app/performance/chunk-split.md +40 -0
- package/docs/configure/app/performance/dns-prefetch.md +15 -0
- package/docs/configure/app/performance/preconnect.md +16 -0
- package/docs/configure/app/performance/prefetch.md +21 -0
- package/docs/configure/app/performance/preload.md +23 -0
- package/docs/configure/app/performance/print-file-size.md +40 -0
- package/docs/configure/app/performance/profile.md +10 -0
- package/docs/configure/app/performance/remove-console.md +10 -0
- package/docs/configure/app/performance/remove-moment-locale.md +10 -0
- package/docs/configure/app/plugins.md +59 -0
- package/docs/configure/app/resolve/alias-strategy.md +10 -0
- package/docs/configure/app/resolve/alias.md +9 -0
- package/docs/configure/app/resolve/condition-names.md +13 -0
- package/docs/configure/app/resolve/dedupe.md +9 -0
- package/docs/configure/app/resolve/extensions.md +13 -0
- package/docs/configure/app/runtime/0-intro.md +58 -0
- package/docs/configure/app/runtime/plugins.md +58 -0
- package/docs/configure/app/runtime/router.md +35 -0
- package/docs/configure/app/security/check-syntax.md +69 -0
- package/docs/configure/app/security/nonce.md +15 -0
- package/docs/configure/app/security/sri.md +20 -0
- package/docs/configure/app/server/base-url.md +26 -0
- package/docs/configure/app/server/port.md +18 -0
- package/docs/configure/app/server/public-routes.md +22 -0
- package/docs/configure/app/server/routes.md +86 -0
- package/docs/configure/app/server/rsc.md +26 -0
- package/docs/configure/app/server/ssr-by-entries.md +25 -0
- package/docs/configure/app/server/ssr.md +78 -0
- package/docs/configure/app/server/tsconfig-path.md +59 -0
- package/docs/configure/app/source/alias-strategy.md +14 -0
- package/docs/configure/app/source/alias.md +23 -0
- package/docs/configure/app/source/config-dir.md +20 -0
- package/docs/configure/app/source/decorators.md +25 -0
- package/docs/configure/app/source/define.md +16 -0
- package/docs/configure/app/source/disable-default-entries.md +28 -0
- package/docs/configure/app/source/enable-async-entry.md +54 -0
- package/docs/configure/app/source/enable-async-pre-entry.md +26 -0
- package/docs/configure/app/source/entries-dir.md +35 -0
- package/docs/configure/app/source/entries.md +179 -0
- package/docs/configure/app/source/exclude.md +10 -0
- package/docs/configure/app/source/global-vars.md +106 -0
- package/docs/configure/app/source/include.md +36 -0
- package/docs/configure/app/source/main-entry-name.md +24 -0
- package/docs/configure/app/source/pre-entry.md +10 -0
- package/docs/configure/app/source/react-compiler.md +68 -0
- package/docs/configure/app/source/transform-import.md +27 -0
- package/docs/configure/app/split-chunks.md +17 -0
- package/docs/configure/app/tools/autoprefixer.md +44 -0
- package/docs/configure/app/tools/bundler-chain.md +26 -0
- package/docs/configure/app/tools/css-extract.md +33 -0
- package/docs/configure/app/tools/css-loader.md +17 -0
- package/docs/configure/app/tools/dev-server.md +113 -0
- package/docs/configure/app/tools/html-plugin.md +41 -0
- package/docs/configure/app/tools/less.md +81 -0
- package/docs/configure/app/tools/lightningcss-loader.md +35 -0
- package/docs/configure/app/tools/minify-css.md +53 -0
- package/docs/configure/app/tools/postcss.md +34 -0
- package/docs/configure/app/tools/rspack.md +10 -0
- package/docs/configure/app/tools/sass.md +78 -0
- package/docs/configure/app/tools/style-loader.md +10 -0
- package/docs/configure/app/tools/swc.md +65 -0
- package/docs/configure/app/tools/ts-checker.md +109 -0
- package/docs/configure/app/usage.md +276 -0
- package/docs/guides/advanced-features/bff/cross-project.md +109 -0
- package/docs/guides/advanced-features/bff/extend-server.md +120 -0
- package/docs/guides/advanced-features/bff/frameworks.md +124 -0
- package/docs/guides/advanced-features/bff/function.md +314 -0
- package/docs/guides/advanced-features/bff/operators.md +554 -0
- package/docs/guides/advanced-features/bff/sdk.md +116 -0
- package/docs/guides/advanced-features/bff/upload.md +101 -0
- package/docs/guides/advanced-features/bff.md +18 -0
- package/docs/guides/advanced-features/build-performance.md +130 -0
- package/docs/guides/advanced-features/compatibility.md +120 -0
- package/docs/guides/advanced-features/international/advanced.md +128 -0
- package/docs/guides/advanced-features/international/api.md +231 -0
- package/docs/guides/advanced-features/international/best-practices.md +286 -0
- package/docs/guides/advanced-features/international/configuration.md +227 -0
- package/docs/guides/advanced-features/international/locale-detection.md +126 -0
- package/docs/guides/advanced-features/international/quick-start.md +128 -0
- package/docs/guides/advanced-features/international/resource-loading.md +154 -0
- package/docs/guides/advanced-features/international/routing.md +130 -0
- package/docs/guides/advanced-features/international.md +27 -0
- package/docs/guides/advanced-features/low-level.md +46 -0
- package/docs/guides/advanced-features/page-performance/code-split.md +77 -0
- package/docs/guides/advanced-features/page-performance/inline-assets.md +159 -0
- package/docs/guides/advanced-features/page-performance/optimize-bundle.md +97 -0
- package/docs/guides/advanced-features/page-performance/react-compiler.md +69 -0
- package/docs/guides/advanced-features/server-monitor/logger.md +41 -0
- package/docs/guides/advanced-features/server-monitor/metrics.md +58 -0
- package/docs/guides/advanced-features/server-monitor/monitors.md +242 -0
- package/docs/guides/advanced-features/source-build.md +164 -0
- package/docs/guides/advanced-features/web-server.md +288 -0
- package/docs/guides/basic-features/alias.md +102 -0
- package/docs/guides/basic-features/css/css-in-js.md +72 -0
- package/docs/guides/basic-features/css/css-modules.md +212 -0
- package/docs/guides/basic-features/css/css.md +27 -0
- package/docs/guides/basic-features/css/tailwindcss.md +27 -0
- package/docs/guides/basic-features/data/data-cache.md +510 -0
- package/docs/guides/basic-features/data/data-fetch.md +415 -0
- package/docs/guides/basic-features/data/data-write.md +227 -0
- package/docs/guides/basic-features/debug/mock.md +109 -0
- package/docs/guides/basic-features/debug/proxy.md +21 -0
- package/docs/guides/basic-features/debug/rsdoctor.md +62 -0
- package/docs/guides/basic-features/debug/using-storybook.md +112 -0
- package/docs/guides/basic-features/deploy.md +458 -0
- package/docs/guides/basic-features/env-vars.md +177 -0
- package/docs/guides/basic-features/html.md +255 -0
- package/docs/guides/basic-features/output-files.md +141 -0
- package/docs/guides/basic-features/render/before-render.md +108 -0
- package/docs/guides/basic-features/render/overview.md +47 -0
- package/docs/guides/basic-features/render/rsc.md +525 -0
- package/docs/guides/basic-features/render/ssg.md +228 -0
- package/docs/guides/basic-features/render/ssr-cache.md +201 -0
- package/docs/guides/basic-features/render/ssr.md +321 -0
- package/docs/guides/basic-features/render/streaming-ssr.md +264 -0
- package/docs/guides/basic-features/routes/config-routes.md +426 -0
- package/docs/guides/basic-features/routes/routes.md +498 -0
- package/docs/guides/basic-features/static-assets/json-files.md +120 -0
- package/docs/guides/basic-features/static-assets/svg-assets.md +168 -0
- package/docs/guides/basic-features/static-assets/wasm-assets.md +62 -0
- package/docs/guides/basic-features/static-assets.md +160 -0
- package/docs/guides/basic-features/testing/playwright.md +120 -0
- package/docs/guides/basic-features/testing/rstest.md +251 -0
- package/docs/guides/concept/builder.md +37 -0
- package/docs/guides/concept/entries.md +319 -0
- package/docs/guides/concept/server.md +35 -0
- package/docs/guides/get-started/ai-coding-agents.md +58 -0
- package/docs/guides/get-started/glossary.md +63 -0
- package/docs/guides/get-started/introduction.md +36 -0
- package/docs/guides/get-started/quick-start.md +236 -0
- package/docs/guides/get-started/tech-stack.md +82 -0
- package/docs/guides/get-started/upgrade.md +123 -0
- package/docs/guides/topic-detail/module-federation/application.md +116 -0
- package/docs/guides/topic-detail/module-federation/deploy.md +104 -0
- package/docs/guides/topic-detail/module-federation/i18n.md +670 -0
- package/docs/guides/topic-detail/module-federation/introduce.md +35 -0
- package/docs/guides/topic-detail/module-federation/ssr.md +118 -0
- package/docs/guides/topic-detail/module-federation/usage.md +219 -0
- package/docs/guides/troubleshooting/builder.md +110 -0
- package/docs/guides/troubleshooting/cli.md +35 -0
- package/docs/guides/troubleshooting/dependencies.md +119 -0
- package/docs/guides/troubleshooting/hmr.md +144 -0
- package/docs/guides/upgrade/config.md +963 -0
- package/docs/guides/upgrade/entry.md +463 -0
- package/docs/guides/upgrade/other.md +183 -0
- package/docs/guides/upgrade/overview.md +33 -0
- package/docs/guides/upgrade/tailwindcss.md +91 -0
- package/docs/guides/upgrade/web-server.md +109 -0
- package/docs/index.md +33 -0
- package/docs/llms.txt +285 -0
- package/docs/plugin/cli-plugins/api.md +573 -0
- package/docs/plugin/cli-plugins/life-cycle.md +2 -0
- package/docs/plugin/introduction.md +152 -0
- package/docs/plugin/official/cli-plugins/plugin-bff.md +5 -0
- package/docs/plugin/official/cli-plugins/plugin-ssg.md +5 -0
- package/docs/plugin/official/cli-plugins/plugin-styled-components.md +5 -0
- package/docs/plugin/official/cli-plugins.md +4 -0
- package/docs/plugin/plugin-system.md +238 -0
- package/docs/plugin/runtime-plugins/api.md +194 -0
- package/docs/plugin/runtime-plugins/life-cycle.md +2 -0
- package/docs/plugin/server-plugins/api.md +209 -0
- package/docs/plugin/server-plugins/life-cycle.md +13 -0
- package/docs/tutorials/examples/csr-auth.md +9 -0
- package/docs/tutorials/foundations/introduction.md +16 -0
- package/package.json +48 -15
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Runtime Framework
|
|
2
|
+
|
|
3
|
+
Modern.js uses [Hono.js](https://hono.dev/) as the BFF and Server runtime framework, so you can [extend BFF Server](/guides/advanced-features/bff/extend-server.md) based on the Hono.js ecosystem.
|
|
4
|
+
|
|
5
|
+
### Getting Request Context
|
|
6
|
+
|
|
7
|
+
Sometimes in BFF functions, it's necessary to obtain the request context to handle more logic. In such cases, you can use `useHonoContext` to get it:
|
|
8
|
+
|
|
9
|
+
```ts title="api/lambda/hello.ts"
|
|
10
|
+
import { useHonoContext } from '@modern-js/server-runtime';
|
|
11
|
+
|
|
12
|
+
export const get = async () => {
|
|
13
|
+
const c = useHonoContext();
|
|
14
|
+
console.info(`access url: ${c.req.url}`);
|
|
15
|
+
return 'Hello Modern.js';
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
:::info
|
|
20
|
+
For more details, refer to [useHonoContext](/apis/app/runtime/bff/use-hono-context.md).
|
|
21
|
+
:::
|
|
22
|
+
|
|
23
|
+
### Getting Cookies
|
|
24
|
+
|
|
25
|
+
When getting cookies in BFF functions, you need to get the request context through `useHonoContext`, then use `c.req.header('cookie')` to get the Cookie string and parse it manually:
|
|
26
|
+
|
|
27
|
+
```ts title="api/lambda/cookies.ts"
|
|
28
|
+
import { Api, Get } from '@modern-js/plugin-bff/server';
|
|
29
|
+
import { useHonoContext } from '@modern-js/server-runtime';
|
|
30
|
+
|
|
31
|
+
// Helper function to parse Cookie string
|
|
32
|
+
function parseCookies(
|
|
33
|
+
cookieHeader: string | undefined,
|
|
34
|
+
): Record<string, string> {
|
|
35
|
+
const cookies: Record<string, string> = {};
|
|
36
|
+
if (!cookieHeader) return cookies;
|
|
37
|
+
|
|
38
|
+
cookieHeader.split(';').forEach(cookie => {
|
|
39
|
+
const [name, ...rest] = cookie.trim().split('=');
|
|
40
|
+
if (name) {
|
|
41
|
+
cookies[name] = rest.join('=');
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return cookies;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const getCookies = Api(Get('/cookies'), async () => {
|
|
49
|
+
const c = useHonoContext();
|
|
50
|
+
const cookieHeader = c.req.header('cookie');
|
|
51
|
+
const cookies = parseCookies(cookieHeader);
|
|
52
|
+
const token = cookies.token;
|
|
53
|
+
const sessionId = cookies.sessionId;
|
|
54
|
+
return {
|
|
55
|
+
hasToken: !!token,
|
|
56
|
+
token: token || null,
|
|
57
|
+
sessionId: sessionId || null,
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
:::caution Note
|
|
63
|
+
The `c.req.cookie()` method does not exist in the current version. You need to use `c.req.header('cookie')` to get the Cookie string and parse it manually.
|
|
64
|
+
:::
|
|
65
|
+
|
|
66
|
+
### Defining BFF Functions
|
|
67
|
+
|
|
68
|
+
When using Hono as the runtime framework, you can define interfaces through [Api functions](/guides/advanced-features/bff/operators.md):
|
|
69
|
+
|
|
70
|
+
```ts title="api/lambda/user.ts"
|
|
71
|
+
import { Api, Get, Query } from '@modern-js/plugin-bff/server';
|
|
72
|
+
import { z } from 'zod';
|
|
73
|
+
|
|
74
|
+
const QuerySchema = z.object({
|
|
75
|
+
id: z.string(),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
export const getUser = Api(
|
|
79
|
+
Get('/user'),
|
|
80
|
+
Query(QuerySchema),
|
|
81
|
+
async ({ query }) => {
|
|
82
|
+
return {
|
|
83
|
+
id: query.id,
|
|
84
|
+
name: 'Modern.js',
|
|
85
|
+
email: 'modernjs@bytedance.com',
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
:::info
|
|
92
|
+
For more details about Api functions and operators, refer to [Creating Extensible BFF Functions](/guides/advanced-features/bff/operators.md).
|
|
93
|
+
:::
|
|
94
|
+
|
|
95
|
+
### Using Middleware
|
|
96
|
+
|
|
97
|
+
Hono supports a rich middleware ecosystem, and you can use middleware in BFF functions:
|
|
98
|
+
|
|
99
|
+
```ts title="api/lambda/user.ts"
|
|
100
|
+
import { Api, Get, Middleware } from '@modern-js/plugin-bff/server';
|
|
101
|
+
|
|
102
|
+
export const getUser = Api(
|
|
103
|
+
Get('/user'),
|
|
104
|
+
Middleware(async (c, next) => {
|
|
105
|
+
// You can access Hono's Context in middleware
|
|
106
|
+
c.res.headers.set('X-Powered-By', 'Modern.js');
|
|
107
|
+
await next();
|
|
108
|
+
}),
|
|
109
|
+
async () => {
|
|
110
|
+
return {
|
|
111
|
+
name: 'Modern.js',
|
|
112
|
+
email: 'modernjs@bytedance.com',
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
:::info
|
|
119
|
+
For more details about middleware, refer to [Creating Extensible BFF Functions](/guides/advanced-features/bff/operators.md#middleware).
|
|
120
|
+
:::
|
|
121
|
+
|
|
122
|
+
### More Hono Documentation
|
|
123
|
+
|
|
124
|
+
For more detailed information about Hono, please refer to the [Hono official documentation](https://hono.dev/).
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
# Basic Usage
|
|
2
|
+
|
|
3
|
+
In a Modern.js application, developers can define API files under the `api/lambda` directory and export API functions from these files. In the frontend code, these API functions can be directly invoked by importing the file, which initiates the API requests.
|
|
4
|
+
|
|
5
|
+
This invocation method is called **unified invocation**, where developers do not need to write glue code for the frontend and backend separately, thereby ensuring type safety across both.
|
|
6
|
+
|
|
7
|
+
## Enable BFF
|
|
8
|
+
|
|
9
|
+
To enable BFF functionality in a Modern.js project, follow these steps to modify the code:
|
|
10
|
+
|
|
11
|
+
1. Install BFF plugin dependencies
|
|
12
|
+
|
|
13
|
+
If the BFF plugin is not yet installed in your project, install it first:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @modern-js/plugin-bff
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
:::tip Version Consistency
|
|
20
|
+
Make sure the version of `@modern-js/plugin-bff` matches the version of `@modern-js/app-tools` in your project. All Modern.js official packages are released with a uniform version number, and version mismatches may cause compatibility issues.
|
|
21
|
+
|
|
22
|
+
Check the version of `@modern-js/app-tools` first, then install the same version of `@modern-js/plugin-bff`:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# Check the current version of @modern-js/app-tools
|
|
26
|
+
pnpm list @modern-js/app-tools
|
|
27
|
+
|
|
28
|
+
# Install the same version of @modern-js/plugin-bff
|
|
29
|
+
pnpm add @modern-js/plugin-bff@<version>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
:::
|
|
33
|
+
|
|
34
|
+
2. Configure `modern.config.ts`
|
|
35
|
+
|
|
36
|
+
Import and add the BFF plugin in the `modern.config.ts` file:
|
|
37
|
+
|
|
38
|
+
```ts title="modern.config.ts"
|
|
39
|
+
import { defineConfig, appTools } from '@modern-js/app-tools';
|
|
40
|
+
import { bffPlugin } from '@modern-js/plugin-bff';
|
|
41
|
+
|
|
42
|
+
export default defineConfig({
|
|
43
|
+
plugins: [appTools(), bffPlugin()],
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
3. Configure TypeScript alias
|
|
48
|
+
|
|
49
|
+
To correctly recognize the `@api` alias in TypeScript, it is recommended to add path mapping in `tsconfig.json`:
|
|
50
|
+
|
|
51
|
+
```json title="tsconfig.json"
|
|
52
|
+
{
|
|
53
|
+
"compilerOptions": {
|
|
54
|
+
"paths": {
|
|
55
|
+
..., // other paths,
|
|
56
|
+
"@api/*": ["./api/lambda/*"]
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"include": [
|
|
60
|
+
..., // other include directories
|
|
61
|
+
"api" // Add api directory
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## BFF Functions
|
|
67
|
+
|
|
68
|
+
Functions that allow unified invocation are called **BFF Functions**. Here is an example of the simplest BFF function. First, create the `api/lambda/hello.ts` file:
|
|
69
|
+
|
|
70
|
+
```ts title="api/lambda/hello.ts"
|
|
71
|
+
export const get = async () => 'Hello Modern.js';
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Then, import and invoke the function directly in `src/routes/page.tsx`:
|
|
75
|
+
|
|
76
|
+
```tsx title="src/routes/page.tsx"
|
|
77
|
+
import { useState, useEffect } from 'react';
|
|
78
|
+
import { get as hello } from '@api/hello';
|
|
79
|
+
|
|
80
|
+
export default () => {
|
|
81
|
+
const [text, setText] = useState('');
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
hello().then(setText);
|
|
85
|
+
}, []);
|
|
86
|
+
return <div>{text}</div>;
|
|
87
|
+
};
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The function imported in `src/routes/page.tsx` will be automatically converted into an API call, eliminating the need to use an SDK or Web Fetch to call the API.
|
|
91
|
+
|
|
92
|
+
After running `pnpm run dev`, open `http://localhost:8080/` and you can see that the page displays the content returned by the BFF function. In the Network tab, you can see a request was made to `http://localhost:8080/api/hello`.
|
|
93
|
+
|
|
94
|
+

|
|
95
|
+
|
|
96
|
+
## Function Routes
|
|
97
|
+
|
|
98
|
+
In Modern.js, the routing system for BFF functions is implemented based on the file system, which is another form of **conventional routing**. Each BFF function in the `api/lambda` directory is mapped to an API route. Here are some routing conventions.
|
|
99
|
+
|
|
100
|
+
:::info
|
|
101
|
+
All routes generated by BFF functions have a common prefix, which defaults to `/api`. This can be configured using [bff.prefix](/configure/app/bff/prefix.md).
|
|
102
|
+
:::
|
|
103
|
+
|
|
104
|
+
### Default Routes
|
|
105
|
+
|
|
106
|
+
Files named `index.ts` will be mapped to the parent directory.
|
|
107
|
+
|
|
108
|
+
- `api/lambda/index.ts` -> `{prefix}/`
|
|
109
|
+
- `api/lambda/user/index.ts` -> `{prefix}/user`
|
|
110
|
+
|
|
111
|
+
### Nested Routes
|
|
112
|
+
|
|
113
|
+
Nested directories are supported, and files will be automatically parsed into routes in the same way.
|
|
114
|
+
|
|
115
|
+
- `api/lambda/hello.ts` -> `{prefix}/hello`
|
|
116
|
+
- `api/lambda/user/list.ts` -> `{prefix}/user/list`
|
|
117
|
+
|
|
118
|
+
### Dynamic Routes
|
|
119
|
+
|
|
120
|
+
Similarly, creating a directory or file with `[xxx]` in the name supports dynamic route parameters. The rules for dynamic route function parameters can be found in [dynamic-path](/guides/advanced-features/bff/function.md#dynamic-path).
|
|
121
|
+
|
|
122
|
+
- `api/lambda/user/[username]/info.ts` -> `{prefix}/user/:username/info`
|
|
123
|
+
- `api/lambda/user/username/[action].ts` -> `{prefix}/user/username/:action`
|
|
124
|
+
|
|
125
|
+
### Whitelist
|
|
126
|
+
|
|
127
|
+
By default, all files under the `api/lambda/` directory are parsed as BFF function files, but the following files are ignored:
|
|
128
|
+
|
|
129
|
+
- Files starting with an underscore `_`. For example: `_utils.ts`.
|
|
130
|
+
- Files under directories starting with an underscore `_`. For example: `_utils/index.ts`, `_utils/cp.ts`.
|
|
131
|
+
- Test files. For example: `foo.test.ts`.
|
|
132
|
+
- TypeScript type files. For example: `hello.d.ts`.
|
|
133
|
+
- Files under `node_modules`.
|
|
134
|
+
|
|
135
|
+
## RESTful API
|
|
136
|
+
|
|
137
|
+
Modern.js BFF functions need to follow RESTful API standards for definition. Developers must define BFF functions according to a set of rules.
|
|
138
|
+
|
|
139
|
+
:::tip Design Principles
|
|
140
|
+
BFF functions should not only be invoked within the project but also be accessible to other projects via an SDK or Web fetch. Therefore, Modern.js does not define a **private protocol** for unified invocation but uses standard HTTP methods along with common HTTP request parameters like `params`, `query`, and `body` to define functions.
|
|
141
|
+
:::
|
|
142
|
+
|
|
143
|
+
### Function Export Rules
|
|
144
|
+
|
|
145
|
+
#### HTTP Method Named Functions
|
|
146
|
+
|
|
147
|
+
Modern.js BFF functions' export names determine the HTTP method for the corresponding API, such as `get`, `post`, etc. For example, to export a GET API:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
export const get = async () => {
|
|
151
|
+
return {
|
|
152
|
+
name: 'Modern.js',
|
|
153
|
+
desc: 'A modern web engineering solution',
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The following example exports a `POST` API:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
export const post = async () => {
|
|
162
|
+
return {
|
|
163
|
+
name: 'Modern.js',
|
|
164
|
+
desc: 'A modern web engineering solution',
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- Modern.js supports 9 HTTP methods: `GET`, `POST`, `PUT`, `DELETE`, `CONNECT`, `TRACE`, `PATCH`, `OPTIONS`, and `HEAD`, which can be used as function export names.
|
|
170
|
+
|
|
171
|
+
- Names are case-insensitive. If the method is `GET`, it can be written as `get`, `Get`, `GEt`, or `GET`, and the default export, i.e., `export default xxx`, will be mapped to `Get`.
|
|
172
|
+
|
|
173
|
+
#### Using Async Functions
|
|
174
|
+
|
|
175
|
+
Modern.js recommends defining BFF functions as async functions, even if there is no asynchronous process in the function, for example:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
export const get = async () => {
|
|
179
|
+
return {
|
|
180
|
+
name: 'Modern.js',
|
|
181
|
+
desc: 'A modern web engineering solution',
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
This is because, during frontend invocation, the BFF function will be automatically converted into an HTTP API call, and HTTP API calls are asynchronous. On the frontend, it is typically used like this:
|
|
187
|
+
|
|
188
|
+
```tsx title="src/routes/page.tsx"
|
|
189
|
+
import { useState, useEffect } from 'react';
|
|
190
|
+
import { get as hello } from '@api/hello';
|
|
191
|
+
|
|
192
|
+
export default () => {
|
|
193
|
+
const [text, setText] = useState('');
|
|
194
|
+
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
hello().then(setText);
|
|
197
|
+
}, []);
|
|
198
|
+
return <div>{text}</div>;
|
|
199
|
+
};
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Therefore, to keep the type definitions consistent with the actual invocation experience, we recommend defining BFF functions as async functions.
|
|
203
|
+
|
|
204
|
+
### Function Parameter Rules
|
|
205
|
+
|
|
206
|
+
Function parameter rules are divided into two parts: dynamic routes in the request path (`Dynamic Path`) and request options (`RequestOption`).
|
|
207
|
+
|
|
208
|
+
#### Dynamic Path
|
|
209
|
+
|
|
210
|
+
Dynamic routes will be the first part of the BFF function parameters, with each parameter corresponding to a segment of the dynamic route. For example, the `level` and `id` parameters will be passed to the function in the following example:
|
|
211
|
+
|
|
212
|
+
```ts title="api/lambda/[level]/[id].ts"
|
|
213
|
+
export default async (level: number, id: number) => {
|
|
214
|
+
const userData = await queryUser(level, uid);
|
|
215
|
+
return userData;
|
|
216
|
+
};
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Invoke the function by directly passing in the dynamic parameters:
|
|
220
|
+
|
|
221
|
+
```tsx title="src/routes/page.tsx"
|
|
222
|
+
import { useState, useEffect } from 'react';
|
|
223
|
+
import { get as getUser } from '@api/[level]/[id]';
|
|
224
|
+
|
|
225
|
+
export default () => {
|
|
226
|
+
const [name, setName] = useState('');
|
|
227
|
+
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
getUser(6, 001).then(userData => setName(userData.name));
|
|
230
|
+
}, []);
|
|
231
|
+
|
|
232
|
+
return <div>{name}</div>;
|
|
233
|
+
};
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### RequestOption
|
|
237
|
+
|
|
238
|
+
Parameters following the dynamic path are an object called `RequestOption`, which includes the query string and request body. This field is used to define the types for `data` and `query`.
|
|
239
|
+
|
|
240
|
+
In a standard function without dynamic routes, `RequestOption` can be obtained from the first parameter, for example:
|
|
241
|
+
|
|
242
|
+
```ts title="api/lambda/hello.ts"
|
|
243
|
+
import type { RequestOption } from '@modern-js/plugin-bff/server';
|
|
244
|
+
|
|
245
|
+
export async function post({
|
|
246
|
+
query,
|
|
247
|
+
data,
|
|
248
|
+
}: RequestOption<Record<string, string>, Record<string, string>>) {
|
|
249
|
+
// do something
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Custom types can also be used here:
|
|
254
|
+
|
|
255
|
+
```ts title="api/lambda/hello.ts"
|
|
256
|
+
import type { RequestOption } from '@modern-js/plugin-bff/server';
|
|
257
|
+
|
|
258
|
+
type IQuery = {
|
|
259
|
+
// some types
|
|
260
|
+
};
|
|
261
|
+
type IData = {
|
|
262
|
+
// some types
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export async function post({ query, data }: { query: IQuery; data: IData }) {
|
|
266
|
+
// do something
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
When the function file uses dynamic routing, dynamic routes will precede the `RequestOption` object parameter.
|
|
271
|
+
|
|
272
|
+
```ts title="api/lambda/[sku]/[id]/item.ts"
|
|
273
|
+
export async function post(
|
|
274
|
+
sku: string,
|
|
275
|
+
id: string,
|
|
276
|
+
{
|
|
277
|
+
data,
|
|
278
|
+
query,
|
|
279
|
+
}: RequestOption<Record<string, string>, Record<string, string>>,
|
|
280
|
+
) {
|
|
281
|
+
// do somethings
|
|
282
|
+
}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Pass the corresponding parameters when invoking the function according to its definition:
|
|
286
|
+
|
|
287
|
+
```ts title="src/routes/page.tsx"
|
|
288
|
+
import { post } from '@api/[sku]/[id]/item';
|
|
289
|
+
|
|
290
|
+
export default () => {
|
|
291
|
+
const addSku = () => {
|
|
292
|
+
post('0001' /* sku */, '1234' /* id */, {
|
|
293
|
+
query: {
|
|
294
|
+
/* ... */
|
|
295
|
+
},
|
|
296
|
+
data: {
|
|
297
|
+
/* ... */
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
return <div onClick={addSku}>Add SKU</div>;
|
|
303
|
+
};
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Extend BFF Function
|
|
307
|
+
|
|
308
|
+
The standard BFF function writing method may not always meet your needs. For example, complex TS type requirements in business scenarios. Modern.js provides a more powerful BFF function writing method.
|
|
309
|
+
|
|
310
|
+
For more details, please refer to - [Creating Extensible BFF Functions](/guides/advanced-features/bff/operators.md).
|
|
311
|
+
|
|
312
|
+
## Code Sharing
|
|
313
|
+
|
|
314
|
+
Besides the BFF functions in the `api/` directory, which can be referenced in the `src/` directory through an integrated calling method, the `src/` and `api/` directories cannot directly reference each other's code by default. To achieve code sharing, a [`shared`](/apis/app/hooks/shared.md) directory can be created at the root of the project for both `src/` and `api/` to use commonly.
|