soma-core 1.0.2 → 1.1.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/app/SomaPageClient.tsx +11 -20
- package/app/[...path]/page.tsx +21 -0
- package/app/api/[...path]/route.ts +86 -77
- package/app/components/SomaHostPage.tsx +133 -0
- package/app/components/SomaHydrationShell.tsx +1 -1
- package/app/favicon.ico/route.ts +13 -0
- package/app/layout.tsx +10 -6
- package/app/page.tsx +7 -18
- package/app/soma/[...path]/route.ts +9 -0
- package/app/{api/soma-manifest → soma/manifest}/route.ts +3 -3
- package/app/soma/page/route.ts +29 -0
- package/app/soma/page-object/route.ts +158 -0
- package/app/soma/route.ts +9 -0
- package/bin/soma-mock.mjs +38 -0
- package/bin/soma.cjs +1 -1
- package/core/api/load-host-service.ts +5 -2
- package/core/base/SomaHttpClient.ts +5 -3
- package/core/compiler/full-manifest-builder.ts +84 -69
- package/core/compiler/host-config-sync.cjs +32 -6
- package/core/compiler/page-base-resolver.ts +3 -0
- package/core/compiler/soma-manifest-webpack-plugin.js +160 -10
- package/core/engine/dom-renderer.ts +160 -24
- package/core/engine/soma-headless-page.ts +86 -0
- package/core/host/app-config-composer.ts +90 -0
- package/core/host/app-config.stub.ts +2 -0
- package/core/host/host-config-paths.cjs +100 -0
- package/core/host/host-config-paths.ts +132 -0
- package/core/host/invoke-host-request-hook.ts +37 -0
- package/core/host/load-host-domain-class.ts +87 -0
- package/core/host/locale.stub.ts +2 -0
- package/core/host/page-response-mode.cjs +96 -0
- package/core/host/page-response-request.ts +35 -0
- package/core/host/request-origin.ts +18 -0
- package/core/host/resolve-host-request-hook.ts +29 -0
- package/core/host/server-page-assets.ts +193 -0
- package/core/host/soma-domain-loaders.stub.ts +6 -0
- package/core/host/soma-domain-loaders.ts +69 -0
- package/core/host/soma-request-hook-host.stub.ts +4 -0
- package/core/host/source-packages.ts +153 -0
- package/core/index.ts +22 -0
- package/core/infra/http/SomaHttpService.ts +23 -14
- package/core/infra/http/soma-server-request-context.ts +126 -0
- package/core/mock/node/catch-all.mjs +35 -0
- package/core/mock/node/cli.d.mts +58 -0
- package/core/mock/node/cli.mjs +243 -0
- package/core/mock/node/context.mjs +167 -0
- package/core/mock/node/environment-policy.mjs +52 -0
- package/core/mock/node/plugin-loader.mjs +212 -0
- package/core/mock/node/preload.mjs +91 -0
- package/core/mock/node/process-lifecycle.mjs +236 -0
- package/core/mock/node/public.d.mts +110 -0
- package/core/mock/node/public.mjs +4 -0
- package/core/mock/node/registry.mjs +336 -0
- package/core/mock/node/schema-generator.mjs +1310 -0
- package/core/mock/testing/host-harness.d.mts +66 -0
- package/core/mock/testing/host-harness.mjs +276 -0
- package/core/mock/testing/host-runtime.mjs +48 -0
- package/core/runtime/SomaRuntimeServices.ts +5 -1
- package/core/runtime/serialize-page-state.ts +152 -0
- package/dist/cli/soma.js +375 -4
- package/dist/core/index.cjs +2957 -2387
- package/dist/tsconfig.types.tsbuildinfo +1 -1
- package/dist/types/base/SomaHttpClient.d.ts +2 -0
- package/dist/types/compiler/full-manifest-builder.d.ts +2 -0
- package/dist/types/engine/dom-renderer.d.ts +6 -0
- package/dist/types/engine/soma-headless-page.d.ts +10 -0
- package/dist/types/host/app-config-composer.d.ts +6 -0
- package/dist/types/host/app-config.stub.d.ts +3 -0
- package/dist/types/host/host-config-paths.d.ts +23 -0
- package/dist/types/host/invoke-host-request-hook.d.ts +8 -0
- package/dist/types/host/load-host-domain-class.d.ts +12 -0
- package/dist/types/host/locale.stub.d.ts +3 -0
- package/dist/types/host/page-response-request.d.ts +9 -0
- package/dist/types/host/request-origin.d.ts +1 -0
- package/dist/types/host/resolve-host-request-hook.d.ts +16 -0
- package/dist/types/host/server-page-assets.d.ts +29 -0
- package/dist/types/host/soma-domain-loaders.d.ts +19 -0
- package/dist/types/host/soma-domain-loaders.stub.d.ts +4 -0
- package/dist/types/host/soma-request-hook-host.stub.d.ts +2 -0
- package/dist/types/host/source-packages.d.ts +12 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/infra/http/SomaHttpService.d.ts +2 -0
- package/dist/types/infra/http/soma-server-request-context.d.ts +5 -0
- package/dist/types/runtime/serialize-page-state.d.ts +10 -0
- package/docs//351/241/265/351/235/242JSON/346/250/241/345/274/217/346/216/245/345/205/245/346/214/207/345/215/227.md +141 -0
- package/global.d.ts +74 -33
- package/middleware.ts +33 -0
- package/next.config.js +166 -16
- package/package.json +47 -13
- package/tailwind.config.ts +21 -17
- package/tsconfig.json +1 -5
- package/app/api/page/route.ts +0 -99
- package/app/loading.tsx +0 -5
|
@@ -6,6 +6,8 @@ export interface SomaHttpClientConfig {
|
|
|
6
6
|
baseURL: string;
|
|
7
7
|
/** 请求超时时间(毫秒),默认 30000 */
|
|
8
8
|
timeout?: number;
|
|
9
|
+
/** Generic headers supplied by the host runtime, such as page context. */
|
|
10
|
+
defaultHeaders?: Record<string, string> | (() => Record<string, string>);
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* SomaHttpClient — API 请求封装
|
|
@@ -11,6 +11,7 @@ export interface SomaPrecompiledPage {
|
|
|
11
11
|
triggers: PropertyTrigger[];
|
|
12
12
|
timers: ParsedTimer[];
|
|
13
13
|
styleConfig?: StyleConfig;
|
|
14
|
+
appConfig?: unknown;
|
|
14
15
|
}
|
|
15
16
|
export interface SomaPrecompiledManifest {
|
|
16
17
|
routes: SomaRouteEntry[];
|
|
@@ -31,5 +32,6 @@ export declare function precompileAll(inputs: {
|
|
|
31
32
|
uiYaml: string;
|
|
32
33
|
eventYaml: string;
|
|
33
34
|
styleYaml?: string;
|
|
35
|
+
appConfigYaml?: string;
|
|
34
36
|
}>;
|
|
35
37
|
}): SomaPrecompiledManifest;
|
|
@@ -31,5 +31,11 @@ export declare class SomaDomRenderer {
|
|
|
31
31
|
private appendListItem;
|
|
32
32
|
private renderObj;
|
|
33
33
|
private applyShowConfig;
|
|
34
|
+
private applyNestedShowConfig;
|
|
35
|
+
private appendNestedTemplateItem;
|
|
36
|
+
private applyExtendedShowConfig;
|
|
34
37
|
private resolveShowValue;
|
|
38
|
+
private parseConditionalPath;
|
|
39
|
+
private isNestedShowMapping;
|
|
40
|
+
private isExtendedShowValue;
|
|
35
41
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type SomaJsonValue } from '../runtime/serialize-page-state';
|
|
2
|
+
import type { SomaPageRoot } from '../types/domain.types';
|
|
3
|
+
export interface SomaHeadlessPageAssets {
|
|
4
|
+
DomainClass: new () => SomaPageRoot;
|
|
5
|
+
uiYaml?: string;
|
|
6
|
+
localeYaml?: string;
|
|
7
|
+
appConfigYaml?: string;
|
|
8
|
+
routerPath: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function runSomaHeadlessPage(assets: SomaHeadlessPageAssets): Promise<SomaJsonValue>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function parseAppConfigYaml(source: string): unknown;
|
|
2
|
+
/** Source packages provide defaults; the Host remains the final App-config owner. */
|
|
3
|
+
export declare function mergeSourcePackageAppConfig(sourcePackageConfig: unknown, hostConfig: unknown): unknown;
|
|
4
|
+
export declare function composeAppConfigYaml(sourcePackageConfigYaml: string, hostConfigYaml: string): string;
|
|
5
|
+
/** Browser metadata uses the Host App's displayName, never a source-package brand. */
|
|
6
|
+
export declare function readHostAppDisplayName(hostDir?: string | undefined): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type SomaEnvName = 'test' | 'prod';
|
|
2
|
+
export declare function normalizeSomaEnv(raw?: string): SomaEnvName;
|
|
3
|
+
/**
|
|
4
|
+
* Resolve SOMA env from framework-neutral variables only.
|
|
5
|
+
* Priority: SOMA_ENV → APP_ENV → NODE_ENV.
|
|
6
|
+
*/
|
|
7
|
+
export declare function readSomaEnv(env?: NodeJS.ProcessEnv): SomaEnvName;
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the Host config directory.
|
|
10
|
+
* Prefer package.json `soma.configRoot` (relative to Host root, not hostRoot).
|
|
11
|
+
* Fallback: `<hostDir>/config`, then `<hostRoot>/config`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveHostConfigRoot(hostDir: string): string;
|
|
14
|
+
export declare function getSomaHostConfigRoot(hostDir: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve an app config file inside an already-resolved config root with
|
|
17
|
+
* env-specific priority:
|
|
18
|
+
* `app.config.<env>.yaml` → `app.config.yaml`.
|
|
19
|
+
* Never falls back across environments (e.g. prod must not load test yaml).
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveAppConfigPath(configRoot: string, envName?: SomaEnvName): string | null;
|
|
22
|
+
/** Resolve the Host app config from its package-level configRoot contract. */
|
|
23
|
+
export declare function resolveHostAppConfigPath(hostDir: string, envName?: SomaEnvName): string | null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type SomaHostRequestContext, type SomaRequestHook } from './resolve-host-request-hook';
|
|
2
|
+
export type SomaRequestHookResolver = () => Promise<SomaRequestHook | null>;
|
|
3
|
+
export declare function isSomaInternalRequestPath(path: string): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Calls the Host extension exactly once for Host-owned page/API requests.
|
|
6
|
+
* SOMA's reserved /soma namespace never reaches the Host extension.
|
|
7
|
+
*/
|
|
8
|
+
export declare function invokeHostRequestHook(request: Request, context: SomaHostRequestContext, resolveHook?: SomaRequestHookResolver): Promise<Response | undefined>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SomaPageRoot } from '../types/domain.types';
|
|
2
|
+
import { type SomaDomainLoadContext, type SomaDomainModule } from './soma-domain-loaders';
|
|
3
|
+
export type SomaDomainConstructor = new () => SomaPageRoot;
|
|
4
|
+
export declare class SomaDomainLoadError extends Error {
|
|
5
|
+
readonly code = "SOMA_DOMAIN_LOAD_FAILED";
|
|
6
|
+
readonly context: SomaDomainLoadContext;
|
|
7
|
+
constructor(context: SomaDomainLoadContext, message: string);
|
|
8
|
+
}
|
|
9
|
+
export declare function resolveSomaDomainConstructor(module: SomaDomainModule, label: string): SomaDomainConstructor;
|
|
10
|
+
export declare function loadHostDomainClass(context: SomaDomainLoadContext, options?: {
|
|
11
|
+
allowLocaleOnlyFallback?: boolean;
|
|
12
|
+
}): Promise<SomaDomainConstructor>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type SomaPageResponseMode = 'html' | 'json';
|
|
2
|
+
export declare const SOMA_PAGE_REQUEST_URL_HEADER = "x-soma-page-request-url";
|
|
3
|
+
export declare function isJsonPageResponseMode(mode: string | undefined): boolean;
|
|
4
|
+
export declare function isSomaPageRequestPath(pathname: string): boolean;
|
|
5
|
+
export declare function shouldUseJsonPageResponse(options: {
|
|
6
|
+
mode: string | undefined;
|
|
7
|
+
method: string;
|
|
8
|
+
pathname: string;
|
|
9
|
+
}): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function resolveSomaRequestOrigin(requestHeaders: Headers): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type SomaHostRequestKind = 'page' | 'api';
|
|
2
|
+
export interface SomaHostRequestContext {
|
|
3
|
+
kind: SomaHostRequestKind;
|
|
4
|
+
path: string;
|
|
5
|
+
pageHref?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Generic server ingress extension. soma-core does not know authentication,
|
|
9
|
+
* authorization, Admin, or any business application.
|
|
10
|
+
*/
|
|
11
|
+
export type SomaRequestHook = (request: Request, context: SomaHostRequestContext) => Response | undefined | Promise<Response | undefined>;
|
|
12
|
+
export interface SomaRequestHookModule {
|
|
13
|
+
somaRequestHook?: SomaRequestHook;
|
|
14
|
+
handleSomaRequest?: SomaRequestHook;
|
|
15
|
+
}
|
|
16
|
+
export declare function resolveHostRequestHook(): Promise<SomaRequestHook | null>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type SomaAssetSource } from './source-packages';
|
|
2
|
+
export type SomaPageAssetLoadProfile = 'render' | 'headless';
|
|
3
|
+
export interface SomaServerPageAssets {
|
|
4
|
+
base: string;
|
|
5
|
+
sourceType: SomaAssetSource['type'];
|
|
6
|
+
sourcePackage: string | null;
|
|
7
|
+
html?: string;
|
|
8
|
+
ui: string;
|
|
9
|
+
event: string;
|
|
10
|
+
styleYaml: string;
|
|
11
|
+
localeYaml: string;
|
|
12
|
+
appConfigYaml: string;
|
|
13
|
+
includes?: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
export declare class SomaPageAssetError extends Error {
|
|
16
|
+
readonly code: string;
|
|
17
|
+
readonly status: number;
|
|
18
|
+
constructor(code: string, status: number, message: string);
|
|
19
|
+
}
|
|
20
|
+
export declare function resolveSomaPageSource(sources: SomaAssetSource[], urlPath: string): {
|
|
21
|
+
source: SomaAssetSource;
|
|
22
|
+
base: string;
|
|
23
|
+
} | null;
|
|
24
|
+
export declare function loadSomaServerPageAssets(options: {
|
|
25
|
+
hostDir: string | undefined;
|
|
26
|
+
urlPath: string;
|
|
27
|
+
profile?: SomaPageAssetLoadProfile;
|
|
28
|
+
watch?: boolean;
|
|
29
|
+
}): SomaServerPageAssets;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type SomaDomainModule = {
|
|
2
|
+
default?: unknown;
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
export interface SomaDomainLoadContext {
|
|
6
|
+
base: string;
|
|
7
|
+
sourcePackage?: string | null;
|
|
8
|
+
}
|
|
9
|
+
export type SomaDomainLoader = (ctx: SomaDomainLoadContext) => SomaDomainModule | Promise<SomaDomainModule>;
|
|
10
|
+
export type SomaDomainLoaderMap = Record<string, SomaDomainLoader>;
|
|
11
|
+
export type SomaDomainModuleResolver = (ctx: SomaDomainLoadContext) => SomaDomainModule | null | undefined | Promise<SomaDomainModule | null | undefined>;
|
|
12
|
+
export interface SomaDomainLoaderModule {
|
|
13
|
+
loadSomaDomainModule?: SomaDomainModuleResolver;
|
|
14
|
+
somaDomainLoaders?: SomaDomainLoaderMap;
|
|
15
|
+
default?: SomaDomainLoaderMap | SomaDomainModuleResolver;
|
|
16
|
+
}
|
|
17
|
+
export declare function createSomaDomainLoaderKey(sourcePackage: string | null | undefined, base: string): string;
|
|
18
|
+
export declare function findSomaDomainLoader(loaders: SomaDomainLoaderMap | null | undefined, ctx: SomaDomainLoadContext): SomaDomainLoader | null;
|
|
19
|
+
export declare function loadSomaDomainModuleFromRegistry(registry: SomaDomainLoaderModule | null | undefined, ctx: SomaDomainLoadContext): Promise<SomaDomainModule | null>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface SomaAssetSource {
|
|
2
|
+
type: 'host' | 'package';
|
|
3
|
+
name: string;
|
|
4
|
+
assetRoot: string;
|
|
5
|
+
codeRoot: string;
|
|
6
|
+
configRoot: string;
|
|
7
|
+
packageName?: string;
|
|
8
|
+
}
|
|
9
|
+
export { getSomaHostConfigRoot, normalizeSomaEnv, readSomaEnv, resolveAppConfigPath, resolveHostAppConfigPath, resolveHostConfigRoot, } from './host-config-paths';
|
|
10
|
+
export type { SomaEnvName } from './host-config-paths';
|
|
11
|
+
export declare function getSomaHostSourceRoot(hostDir: string): string;
|
|
12
|
+
export declare function getSomaAssetSources(hostDir: string): SomaAssetSource[];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -33,3 +33,6 @@ export { createPermissionMiddleware } from './middleware/createPermissionMiddlew
|
|
|
33
33
|
export type { SomaEventMiddlewareContext } from './engine/event-engine';
|
|
34
34
|
export type { SomaBootstrapConfigurator } from './host/resolve-host-bootstrap';
|
|
35
35
|
export { resolveHostBootstrapOptions } from './host/resolve-host-bootstrap';
|
|
36
|
+
export type { SomaHostRequestContext, SomaHostRequestKind, SomaRequestHook, } from './host/resolve-host-request-hook';
|
|
37
|
+
export type { SomaDomainLoadContext, SomaDomainLoader, SomaDomainLoaderMap, SomaDomainLoaderModule, SomaDomainModule, SomaDomainModuleResolver, } from './host/soma-domain-loaders';
|
|
38
|
+
export { createSomaDomainLoaderKey, findSomaDomainLoader, loadSomaDomainModuleFromRegistry, } from './host/soma-domain-loaders';
|
|
@@ -10,6 +10,7 @@ import { IHttpClient, HttpRequestConfig, UploadOptions } from './IHttpClient';
|
|
|
10
10
|
export declare class SomaHttpService implements IHttpClient {
|
|
11
11
|
private client;
|
|
12
12
|
private baseURL;
|
|
13
|
+
private defaultHeaders;
|
|
13
14
|
private abortControllers;
|
|
14
15
|
private onHttpError;
|
|
15
16
|
constructor(config: SomaHttpClientConfig);
|
|
@@ -24,4 +25,5 @@ export declare class SomaHttpService implements IHttpClient {
|
|
|
24
25
|
private uploadWithProgress;
|
|
25
26
|
abortAll(): void;
|
|
26
27
|
private requestWithConfig;
|
|
28
|
+
private resolveDefaultHeaders;
|
|
27
29
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type SomaJsonPrimitive = string | number | boolean | null;
|
|
2
|
+
export type SomaJsonValue = SomaJsonPrimitive | SomaJsonValue[] | {
|
|
3
|
+
[key: string]: SomaJsonValue;
|
|
4
|
+
};
|
|
5
|
+
export declare class SomaPageSerializationError extends Error {
|
|
6
|
+
readonly code: 'SOMA_PAGE_STATE_CIRCULAR' | 'SOMA_PAGE_STATE_UNSUPPORTED' | 'SOMA_PAGE_STATE_GETTER_FAILED';
|
|
7
|
+
readonly path: string;
|
|
8
|
+
constructor(code: SomaPageSerializationError['code'], path: string, message: string);
|
|
9
|
+
}
|
|
10
|
+
export declare function serializeSomaPageState(root: unknown): SomaJsonValue;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# SOMA 页面 JSON 模式接入指南
|
|
2
|
+
|
|
3
|
+
## 1. 用途
|
|
4
|
+
|
|
5
|
+
页面 JSON 模式用于自动化测试。它让测试程序继续访问原来的页面 URL,但不挂载或渲染 HTML,而是在页面 Domain 完成初始化后直接返回 JSON 状态。
|
|
6
|
+
|
|
7
|
+
该模式适合:
|
|
8
|
+
|
|
9
|
+
- 对页面初始化数据做字段级断言。
|
|
10
|
+
- 对 Domain onMount 调用 BFF 后的结果做快照测试。
|
|
11
|
+
- 在不依赖 DOM 选择器和浏览器渲染时序的情况下验证页面业务状态。
|
|
12
|
+
|
|
13
|
+
该模式不是 DOM 模拟器,不执行样式绑定、浏览器事件、声明式定时器或 HTML 渲染。
|
|
14
|
+
|
|
15
|
+
## 2. App 开启方式
|
|
16
|
+
|
|
17
|
+
在 App 的测试环境配置中增加:
|
|
18
|
+
|
|
19
|
+
# config/app.config.test.yaml
|
|
20
|
+
soma:
|
|
21
|
+
pageResponseMode: json
|
|
22
|
+
|
|
23
|
+
未声明该字段或声明为 html 时,SOMA 保持普通 HTML 渲染。
|
|
24
|
+
|
|
25
|
+
配置约束:
|
|
26
|
+
|
|
27
|
+
- pageResponseMode 只接受 html 或 json。
|
|
28
|
+
- json 只允许 SOMA test 环境使用。
|
|
29
|
+
- 在生产环境配置 json 会在 Next 配置加载阶段直接失败。
|
|
30
|
+
- 修改配置后需要重启 dev 服务;已经构建的服务需要重新 build。
|
|
31
|
+
|
|
32
|
+
Host App 会保留项目根目录作为 soma-core 的 Host 根,并传递 SOMA_ENV,因此业务 App 不需要维护生成 Host 中的配置副本。
|
|
33
|
+
|
|
34
|
+
## 3. 请求范围
|
|
35
|
+
|
|
36
|
+
JSON 模式会处理以下 GET 或 HEAD 页面请求:
|
|
37
|
+
|
|
38
|
+
| 请求 | 对应页面 |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| / | index.html |
|
|
41
|
+
| /demo | demo.html 或 demo/index.html |
|
|
42
|
+
| /demo/index | demo/index.html |
|
|
43
|
+
| /demo/index.html | demo/index.html |
|
|
44
|
+
|
|
45
|
+
以下路径不进入页面 JSON 模式:
|
|
46
|
+
|
|
47
|
+
- /api/**
|
|
48
|
+
- /soma/**
|
|
49
|
+
- /_next/**
|
|
50
|
+
- favicon.ico
|
|
51
|
+
- JS、CSS、图片、字体等非 HTML 静态资源
|
|
52
|
+
|
|
53
|
+
## 4. 成功响应
|
|
54
|
+
|
|
55
|
+
成功响应为 HTTP 200、application/json,并带有:
|
|
56
|
+
|
|
57
|
+
Cache-Control: no-store
|
|
58
|
+
X-Content-Type-Options: nosniff
|
|
59
|
+
|
|
60
|
+
响应体直接是页面对象状态,不增加 data 或 page 包装层:
|
|
61
|
+
|
|
62
|
+
{
|
|
63
|
+
"title": "业务接口联调",
|
|
64
|
+
"items": [],
|
|
65
|
+
"pageLoading": false,
|
|
66
|
+
"lastErrorMessage": ""
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
Core 会为每次页面请求创建新的 Domain 实例,依次完成:
|
|
70
|
+
|
|
71
|
+
1. 注入 locale、app config、router 和运行时服务。
|
|
72
|
+
2. 应用 UI YAML 中的 initProps。
|
|
73
|
+
3. 等待 Domain onMount 完成。
|
|
74
|
+
4. 序列化当前状态。
|
|
75
|
+
5. 调用 onUnmount 清理资源。
|
|
76
|
+
|
|
77
|
+
## 5. 精确控制 JSON 字段
|
|
78
|
+
|
|
79
|
+
未声明 toJSON 时,Core 返回 Domain 的可序列化字段和 getter 值,并排除以下内容:
|
|
80
|
+
|
|
81
|
+
- 以下划线开头的框架内部字段。
|
|
82
|
+
- 函数、undefined 和 Symbol。
|
|
83
|
+
- 无法安全表示的循环引用。
|
|
84
|
+
|
|
85
|
+
为了得到稳定、明确且不会暴露 api/client 等实现字段的测试契约,推荐页面 Domain 实现 toJSON:
|
|
86
|
+
|
|
87
|
+
export default class DemoPage {
|
|
88
|
+
title = "";
|
|
89
|
+
items = [];
|
|
90
|
+
private readonly api = new DemoApiClient();
|
|
91
|
+
|
|
92
|
+
toJSON() {
|
|
93
|
+
return {
|
|
94
|
+
title: this.title,
|
|
95
|
+
items: this.items,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
toJSON 的返回值就是最终 HTTP JSON 响应。它应当无副作用,并只返回 JSON 可表示的数据。
|
|
101
|
+
|
|
102
|
+
## 6. onMount 中的 API 请求
|
|
103
|
+
|
|
104
|
+
Headless Domain 在 Node.js 中运行。Core 会建立请求级 Fetch 上下文,使 onMount 中的相对请求继续工作:
|
|
105
|
+
|
|
106
|
+
- /api/list 会解析到当前 Host Origin。
|
|
107
|
+
- 同源请求会透传 Cookie、Authorization、X-Request-Id 和链路追踪头。
|
|
108
|
+
- 自动增加 X-Soma-Page-Href。
|
|
109
|
+
- 跨域请求不会继承当前页面 Cookie 或 Authorization。
|
|
110
|
+
- 并发请求使用 AsyncLocalStorage 隔离,不共享 Origin、Cookie 或页面路径。
|
|
111
|
+
|
|
112
|
+
页面请求 Hook 在 Domain 执行前调用一次。onMount 发起的每个 /api 请求仍通过现有 API Route,并分别执行自己的 API Hook。
|
|
113
|
+
|
|
114
|
+
## 7. 错误与权限
|
|
115
|
+
|
|
116
|
+
框架错误使用结构化 JSON:
|
|
117
|
+
|
|
118
|
+
{
|
|
119
|
+
"error": {
|
|
120
|
+
"code": "SOMA_PAGE_NOT_FOUND",
|
|
121
|
+
"message": "Page not found: /missing.html",
|
|
122
|
+
"path": "/missing.html"
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
常见状态:
|
|
127
|
+
|
|
128
|
+
- 400:目标不是合法 Host 页面路径。
|
|
129
|
+
- 404:页面不存在、模式未开启。
|
|
130
|
+
- 500:Domain 加载、onMount 或状态序列化失败。
|
|
131
|
+
- 503:SOMA_HOST_DIR 未设置。
|
|
132
|
+
|
|
133
|
+
Host 页面 Hook 的拒绝响应会原样返回,包括登录跳转、401 或 403。JSON 模式不会绕过或改写 App 的认证授权语义。
|
|
134
|
+
|
|
135
|
+
## 8. 测试示例
|
|
136
|
+
|
|
137
|
+
curl -i \
|
|
138
|
+
-H "Cookie: session=test-session" \
|
|
139
|
+
"http://localhost:3000/demo/index.html?id=42"
|
|
140
|
+
|
|
141
|
+
测试应按 JSON 字段断言,不依赖字段顺序。对于包含当前时间或随机值的页面,应由 App 的测试数据、Mock 或 toJSON 投影提供确定性。
|
package/global.d.ts
CHANGED
|
@@ -1,33 +1,74 @@
|
|
|
1
|
-
declare module '*.html' {
|
|
2
|
-
const content: string;
|
|
3
|
-
export default content;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
declare module '*.yaml' {
|
|
7
|
-
const content: string;
|
|
8
|
-
export default content;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
declare module '*.yml' {
|
|
12
|
-
const content: string;
|
|
13
|
-
export default content;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
declare module '*.html' {
|
|
2
|
+
const content: string;
|
|
3
|
+
export default content;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare module '*.yaml' {
|
|
7
|
+
const content: string;
|
|
8
|
+
export default content;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module '*.yml' {
|
|
12
|
+
const content: string;
|
|
13
|
+
export default content;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare module '@soma-host/bootstrap/soma-bootstrap' {
|
|
17
|
+
export const configureSomaBootstrap:
|
|
18
|
+
| import('./core/host/resolve-host-bootstrap').SomaBootstrapConfigurator
|
|
19
|
+
| undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare module '@soma-host/bootstrap/soma-request-hook' {
|
|
23
|
+
export const somaRequestHook:
|
|
24
|
+
| import('./core/host/resolve-host-request-hook').SomaRequestHook
|
|
25
|
+
| undefined;
|
|
26
|
+
export const handleSomaRequest:
|
|
27
|
+
| import('./core/host/resolve-host-request-hook').SomaRequestHook
|
|
28
|
+
| undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare module '@soma-host/bootstrap/soma-domain-loaders' {
|
|
32
|
+
export const somaDomainLoaders:
|
|
33
|
+
| import('./core/host/soma-domain-loaders').SomaDomainLoaderMap
|
|
34
|
+
| undefined;
|
|
35
|
+
export const loadSomaDomainModule:
|
|
36
|
+
| import('./core/host/soma-domain-loaders').SomaDomainModuleResolver
|
|
37
|
+
| undefined;
|
|
38
|
+
const defaultExport:
|
|
39
|
+
| import('./core/host/soma-domain-loaders').SomaDomainLoaderMap
|
|
40
|
+
| import('./core/host/soma-domain-loaders').SomaDomainModuleResolver
|
|
41
|
+
| undefined;
|
|
42
|
+
export default defaultExport;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare module '@soma-host/config/locale.yaml' {
|
|
46
|
+
const content: Record<string, unknown>;
|
|
47
|
+
export default content;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare module '@soma-host/config/app.config.yaml' {
|
|
51
|
+
const content: Record<string, unknown>;
|
|
52
|
+
export default content;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Host dynamic asset paths (domain/ui/event); runtime resolves via webpack alias */
|
|
56
|
+
declare module '@soma-host/*' {
|
|
57
|
+
const content: unknown;
|
|
58
|
+
export default content;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Dev console API (window.__SOMA__ in npm run dev) */
|
|
62
|
+
interface SomaDevConsole {
|
|
63
|
+
getDiagnosticsSnapshot: () => import('./core/diagnostics/SomaDiagnostics').SomaDiagnosticsSnapshot | null;
|
|
64
|
+
getTelemetrySnapshot: () => Record<string, { count: number; avg: number; max: number; min: number }> | null;
|
|
65
|
+
getAvailableThemes?: () => string[];
|
|
66
|
+
getDomainInstance: () => unknown;
|
|
67
|
+
captureSnapshot: () => number | null;
|
|
68
|
+
restoreSnapshot: (id: number) => void;
|
|
69
|
+
switchTheme: (name: string | null) => void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface Window {
|
|
73
|
+
__SOMA__?: SomaDevConsole;
|
|
74
|
+
}
|
package/middleware.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import {
|
|
3
|
+
SOMA_PAGE_REQUEST_URL_HEADER,
|
|
4
|
+
shouldUseJsonPageResponse,
|
|
5
|
+
} from './core/host/page-response-request';
|
|
6
|
+
|
|
7
|
+
export function middleware(request: NextRequest) {
|
|
8
|
+
if (!shouldUseJsonPageResponse({
|
|
9
|
+
mode: process.env.SOMA_PAGE_RESPONSE_MODE,
|
|
10
|
+
method: request.method,
|
|
11
|
+
pathname: request.nextUrl.pathname,
|
|
12
|
+
})) {
|
|
13
|
+
return NextResponse.next();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const requestHeaders = new Headers(request.headers);
|
|
17
|
+
requestHeaders.set(
|
|
18
|
+
SOMA_PAGE_REQUEST_URL_HEADER,
|
|
19
|
+
request.nextUrl.pathname + request.nextUrl.search,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const destination = new URL('/soma/page-object', request.url);
|
|
23
|
+
destination.search = '';
|
|
24
|
+
destination.searchParams.set('path', request.nextUrl.pathname);
|
|
25
|
+
|
|
26
|
+
return NextResponse.rewrite(destination, {
|
|
27
|
+
request: { headers: requestHeaders },
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const config = {
|
|
32
|
+
matcher: ['/:path*'],
|
|
33
|
+
};
|