@miaixz/sdk 0.5.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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +412 -0
- package/dist/api/client.d.ts +223 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +1069 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/endpoint.d.ts +10 -0
- package/dist/api/endpoint.d.ts.map +1 -0
- package/dist/api/endpoint.js +51 -0
- package/dist/api/endpoint.js.map +1 -0
- package/dist/api/errors.d.ts +180 -0
- package/dist/api/errors.d.ts.map +1 -0
- package/dist/api/errors.js +173 -0
- package/dist/api/errors.js.map +1 -0
- package/dist/api/index.d.ts +6 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +5 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/request.d.ts +135 -0
- package/dist/api/request.d.ts.map +1 -0
- package/dist/api/request.js +2 -0
- package/dist/api/request.js.map +1 -0
- package/dist/api/response.d.ts +69 -0
- package/dist/api/response.d.ts.map +1 -0
- package/dist/api/response.js +43 -0
- package/dist/api/response.js.map +1 -0
- package/dist/api/telemetry.d.ts +139 -0
- package/dist/api/telemetry.d.ts.map +1 -0
- package/dist/api/telemetry.js +2 -0
- package/dist/api/telemetry.js.map +1 -0
- package/dist/appearance/appearance.d.ts +161 -0
- package/dist/appearance/appearance.d.ts.map +1 -0
- package/dist/appearance/appearance.js +614 -0
- package/dist/appearance/appearance.js.map +1 -0
- package/dist/appearance/index.d.ts +3 -0
- package/dist/appearance/index.d.ts.map +1 -0
- package/dist/appearance/index.js +3 -0
- package/dist/appearance/index.js.map +1 -0
- package/dist/auth/auth.d.ts +197 -0
- package/dist/auth/auth.d.ts.map +1 -0
- package/dist/auth/auth.js +314 -0
- package/dist/auth/auth.js.map +1 -0
- package/dist/auth/index.d.ts +2 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +2 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/config/config.d.ts +117 -0
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +241 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +2 -0
- package/dist/config/index.js.map +1 -0
- package/dist/consts/index.d.ts +35 -0
- package/dist/consts/index.d.ts.map +1 -0
- package/dist/consts/index.js +35 -0
- package/dist/consts/index.js.map +1 -0
- package/dist/context/context.d.ts +123 -0
- package/dist/context/context.d.ts.map +1 -0
- package/dist/context/context.js +252 -0
- package/dist/context/context.js.map +1 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +2 -0
- package/dist/context/index.js.map +1 -0
- package/dist/contracts/host-context.d.ts +132 -0
- package/dist/contracts/host-context.d.ts.map +1 -0
- package/dist/contracts/host-context.js +2 -0
- package/dist/contracts/host-context.js.map +1 -0
- package/dist/contracts/index.d.ts +8 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +2 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/contracts/lifecycle.d.ts +49 -0
- package/dist/contracts/lifecycle.d.ts.map +1 -0
- package/dist/contracts/lifecycle.js +2 -0
- package/dist/contracts/lifecycle.js.map +1 -0
- package/dist/contracts/module-manifest.d.ts +110 -0
- package/dist/contracts/module-manifest.d.ts.map +1 -0
- package/dist/contracts/module-manifest.js +317 -0
- package/dist/contracts/module-manifest.js.map +1 -0
- package/dist/contracts/navigation.d.ts +36 -0
- package/dist/contracts/navigation.d.ts.map +1 -0
- package/dist/contracts/navigation.js +11 -0
- package/dist/contracts/navigation.js.map +1 -0
- package/dist/contracts/permission.d.ts +8 -0
- package/dist/contracts/permission.d.ts.map +1 -0
- package/dist/contracts/permission.js +14 -0
- package/dist/contracts/permission.js.map +1 -0
- package/dist/contracts/post-message.d.ts +124 -0
- package/dist/contracts/post-message.d.ts.map +1 -0
- package/dist/contracts/post-message.js +2 -0
- package/dist/contracts/post-message.js.map +1 -0
- package/dist/contracts/route.d.ts +45 -0
- package/dist/contracts/route.d.ts.map +1 -0
- package/dist/contracts/route.js +35 -0
- package/dist/contracts/route.js.map +1 -0
- package/dist/events/events.d.ts +191 -0
- package/dist/events/events.d.ts.map +1 -0
- package/dist/events/events.js +486 -0
- package/dist/events/events.js.map +1 -0
- package/dist/events/index.d.ts +2 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +2 -0
- package/dist/events/index.js.map +1 -0
- package/dist/files/files.d.ts +138 -0
- package/dist/files/files.d.ts.map +1 -0
- package/dist/files/files.js +144 -0
- package/dist/files/files.js.map +1 -0
- package/dist/files/index.d.ts +2 -0
- package/dist/files/index.d.ts.map +1 -0
- package/dist/files/index.js +2 -0
- package/dist/files/index.js.map +1 -0
- package/dist/formatters/index.d.ts +44 -0
- package/dist/formatters/index.d.ts.map +1 -0
- package/dist/formatters/index.js +50 -0
- package/dist/formatters/index.js.map +1 -0
- package/dist/i18n/default-translator.d.ts +17 -0
- package/dist/i18n/default-translator.d.ts.map +1 -0
- package/dist/i18n/default-translator.js +19 -0
- package/dist/i18n/default-translator.js.map +1 -0
- package/dist/i18n/i18n.d.ts +248 -0
- package/dist/i18n/i18n.d.ts.map +1 -0
- package/dist/i18n/i18n.js +617 -0
- package/dist/i18n/i18n.js.map +1 -0
- package/dist/i18n/index.d.ts +2 -0
- package/dist/i18n/index.d.ts.map +1 -0
- package/dist/i18n/index.js +2 -0
- package/dist/i18n/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/models/index.d.ts +2 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +2 -0
- package/dist/models/index.js.map +1 -0
- package/dist/permissions/index.d.ts +2 -0
- package/dist/permissions/index.d.ts.map +1 -0
- package/dist/permissions/index.js +2 -0
- package/dist/permissions/index.js.map +1 -0
- package/dist/permissions/permissions.d.ts +67 -0
- package/dist/permissions/permissions.d.ts.map +1 -0
- package/dist/permissions/permissions.js +123 -0
- package/dist/permissions/permissions.js.map +1 -0
- package/dist/runtime/direct-host-bridge.d.ts +11 -0
- package/dist/runtime/direct-host-bridge.d.ts.map +1 -0
- package/dist/runtime/direct-host-bridge.js +299 -0
- package/dist/runtime/direct-host-bridge.js.map +1 -0
- package/dist/runtime/index.d.ts +5 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +3 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/post-message-bridge.d.ts +20 -0
- package/dist/runtime/post-message-bridge.d.ts.map +1 -0
- package/dist/runtime/post-message-bridge.js +1115 -0
- package/dist/runtime/post-message-bridge.js.map +1 -0
- package/dist/sdk.d.ts +177 -0
- package/dist/sdk.d.ts.map +1 -0
- package/dist/sdk.js +214 -0
- package/dist/sdk.js.map +1 -0
- package/dist/storage/index.d.ts +2 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/storage.d.ts +246 -0
- package/dist/storage/storage.d.ts.map +1 -0
- package/dist/storage/storage.js +358 -0
- package/dist/storage/storage.js.map +1 -0
- package/dist/types/api.d.ts +82 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +2 -0
- package/dist/types/api.js.map +1 -0
- package/dist/types/appearance.d.ts +85 -0
- package/dist/types/appearance.d.ts.map +1 -0
- package/dist/types/appearance.js +48 -0
- package/dist/types/appearance.js.map +1 -0
- package/dist/types/config.d.ts +49 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +2 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/context.d.ts +41 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/file.d.ts +48 -0
- package/dist/types/file.d.ts.map +1 -0
- package/dist/types/file.js +2 -0
- package/dist/types/file.js.map +1 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +12 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/organization.d.ts +56 -0
- package/dist/types/organization.d.ts.map +1 -0
- package/dist/types/organization.js +2 -0
- package/dist/types/organization.js.map +1 -0
- package/dist/types/pagination.d.ts +55 -0
- package/dist/types/pagination.d.ts.map +1 -0
- package/dist/types/pagination.js +13 -0
- package/dist/types/pagination.js.map +1 -0
- package/dist/types/permissions.d.ts +26 -0
- package/dist/types/permissions.d.ts.map +1 -0
- package/dist/types/permissions.js +2 -0
- package/dist/types/permissions.js.map +1 -0
- package/dist/types/space.d.ts +43 -0
- package/dist/types/space.d.ts.map +1 -0
- package/dist/types/space.js +2 -0
- package/dist/types/space.js.map +1 -0
- package/dist/types/tenant.d.ts +31 -0
- package/dist/types/tenant.d.ts.map +1 -0
- package/dist/types/tenant.js +2 -0
- package/dist/types/tenant.js.map +1 -0
- package/dist/types/user.d.ts +48 -0
- package/dist/types/user.d.ts.map +1 -0
- package/dist/types/user.js +2 -0
- package/dist/types/user.js.map +1 -0
- package/dist/utils/date.d.ts +9 -0
- package/dist/utils/date.d.ts.map +1 -0
- package/dist/utils/date.js +11 -0
- package/dist/utils/date.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/number.d.ts +11 -0
- package/dist/utils/number.d.ts.map +1 -0
- package/dist/utils/number.js +13 -0
- package/dist/utils/number.js.map +1 -0
- package/dist/utils/object.d.ts +9 -0
- package/dist/utils/object.d.ts.map +1 -0
- package/dist/utils/object.js +11 -0
- package/dist/utils/object.js.map +1 -0
- package/dist/utils/string.d.ts +9 -0
- package/dist/utils/string.d.ts.map +1 -0
- package/dist/utils/string.js +11 -0
- package/dist/utils/string.js.map +1 -0
- package/dist/validators/index.d.ts +8 -0
- package/dist/validators/index.d.ts.map +1 -0
- package/dist/validators/index.js +8 -0
- package/dist/validators/index.js.map +1 -0
- package/package.json +156 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { MiaixzEventBus, MiaixzSdkEventMap } from "../events/index.js";
|
|
2
|
+
import { type MiaixzTranslator } from "../i18n/index.js";
|
|
3
|
+
import { type MiaixzKeyValueStorage, type MiaixzStorageMigration } from "../storage/index.js";
|
|
4
|
+
import type { MiaixzRuntimeContext } from "../types/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* Configures a Miaixz runtime-context store.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export interface MiaixzContextStoreOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Identifies the consuming frontend application and its physical storage namespace.
|
|
13
|
+
*/
|
|
14
|
+
readonly appId: string;
|
|
15
|
+
/**
|
|
16
|
+
* Optional context merged over persisted state.
|
|
17
|
+
*/
|
|
18
|
+
readonly initialContext?: MiaixzRuntimeContext;
|
|
19
|
+
/**
|
|
20
|
+
* Optional key-value storage used for persistence.
|
|
21
|
+
*/
|
|
22
|
+
readonly storage?: MiaixzKeyValueStorage;
|
|
23
|
+
/**
|
|
24
|
+
* Indicates whether context should be persisted.
|
|
25
|
+
*
|
|
26
|
+
* @defaultValue true
|
|
27
|
+
*/
|
|
28
|
+
readonly persist?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Supplies optional sequential migrations for older Context schemas.
|
|
31
|
+
*/
|
|
32
|
+
readonly migrations?: readonly MiaixzStorageMigration[];
|
|
33
|
+
/**
|
|
34
|
+
* Optional event bus used to synchronize service instances.
|
|
35
|
+
*/
|
|
36
|
+
readonly events?: MiaixzEventBus<MiaixzSdkEventMap>;
|
|
37
|
+
/**
|
|
38
|
+
* Optional translator used for context errors.
|
|
39
|
+
*/
|
|
40
|
+
readonly translate?: MiaixzTranslator;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Determines whether a value is a valid runtime context.
|
|
44
|
+
*
|
|
45
|
+
* @param value - Value to inspect.
|
|
46
|
+
* @returns Whether `value` is a valid string-valued runtime context.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export declare function isMiaixzRuntimeContext(value: unknown): value is MiaixzRuntimeContext;
|
|
50
|
+
/**
|
|
51
|
+
* Converts tenant, organization, space, locale, and trace context into request headers.
|
|
52
|
+
*
|
|
53
|
+
* @param context - Runtime context to convert.
|
|
54
|
+
* @returns Headers containing all defined runtime-context values.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export declare function miaixzContextToHeaders(context: Readonly<MiaixzRuntimeContext>): Headers;
|
|
58
|
+
/**
|
|
59
|
+
* Stores request context independently of URL route parameters.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export declare class MiaixzContextStore {
|
|
64
|
+
#private;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a runtime-context store.
|
|
67
|
+
*
|
|
68
|
+
* @param options - Initial state plus persistence, events, and translation adapters.
|
|
69
|
+
* @throws MiaixzSdkError When app, migration, or explicit Context configuration is invalid.
|
|
70
|
+
*/
|
|
71
|
+
constructor(options: MiaixzContextStoreOptions);
|
|
72
|
+
/**
|
|
73
|
+
* Returns the active request context.
|
|
74
|
+
*
|
|
75
|
+
* @returns An immutable copy of the current request context.
|
|
76
|
+
*/
|
|
77
|
+
getSnapshot(): Readonly<MiaixzRuntimeContext>;
|
|
78
|
+
/**
|
|
79
|
+
* Replaces, persists, and broadcasts the full runtime context.
|
|
80
|
+
*
|
|
81
|
+
* @param context - Complete runtime context to activate.
|
|
82
|
+
* @throws MiaixzSdkError When the supplied Context contains an invalid value.
|
|
83
|
+
*/
|
|
84
|
+
set(context: MiaixzRuntimeContext): void;
|
|
85
|
+
/**
|
|
86
|
+
* Merges a partial update into the current runtime context.
|
|
87
|
+
*
|
|
88
|
+
* @param context - Partial context values to merge.
|
|
89
|
+
* @throws MiaixzSdkError When the merged Context contains an invalid value.
|
|
90
|
+
*/
|
|
91
|
+
patch(context: Partial<MiaixzRuntimeContext>): void;
|
|
92
|
+
/**
|
|
93
|
+
* Clears all runtime context values.
|
|
94
|
+
*/
|
|
95
|
+
clear(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Registers a context listener and returns its unsubscribe function.
|
|
98
|
+
*
|
|
99
|
+
* @param listener - Callback invoked with each context snapshot.
|
|
100
|
+
* @returns Function that unregisters the listener.
|
|
101
|
+
*/
|
|
102
|
+
subscribe(listener: (context: Readonly<MiaixzRuntimeContext>) => void): () => void;
|
|
103
|
+
/**
|
|
104
|
+
* Request-client provider that returns headers for the latest context.
|
|
105
|
+
*
|
|
106
|
+
* @returns Headers for the active runtime context.
|
|
107
|
+
*/
|
|
108
|
+
readonly headersProvider: () => Headers;
|
|
109
|
+
/**
|
|
110
|
+
* Releases event subscriptions and local listeners.
|
|
111
|
+
*/
|
|
112
|
+
destroy(): void;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Creates a context store with optional browser persistence.
|
|
116
|
+
*
|
|
117
|
+
* @param options - Application identity, optional initial state, and runtime adapters.
|
|
118
|
+
* @returns Configured runtime-context store.
|
|
119
|
+
* @throws MiaixzSdkError When app, migration, or explicit Context configuration is invalid.
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export declare function createMiaixzContextStore(options: MiaixzContextStoreOptions): MiaixzContextStore;
|
|
123
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/context/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAqB,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC5E,OAAO,EAIL,KA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ACH,SAAS,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAGjF;IAED;;;;OAIG;IACH,QAAQ,CAAC,eAAe,QAAO,OAAO,CAA0C;IAEhF;;OAEG;IACH,OAAO,IAAI,IAAI,CAGd;CAyBF;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,kBAAkB,CAE/F"}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { miaixzHeaders } from "../consts/index.js";
|
|
2
|
+
import { MiaixzSdkError } from "../api/errors.js";
|
|
3
|
+
import { miaixzDefaultI18n } from "../i18n/index.js";
|
|
4
|
+
import { getMiaixzBrowserStorage, readMiaixzVersionedValue, writeMiaixzVersionedValue, } from "../storage/index.js";
|
|
5
|
+
import { isRecord } from "../utils/object.js";
|
|
6
|
+
const miaixzContextSchemaVersion = 1;
|
|
7
|
+
/**
|
|
8
|
+
* Determines whether a value is a valid runtime context.
|
|
9
|
+
*
|
|
10
|
+
* @param value - Value to inspect.
|
|
11
|
+
* @returns Whether `value` is a valid string-valued runtime context.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export function isMiaixzRuntimeContext(value) {
|
|
15
|
+
if (!isRecord(value))
|
|
16
|
+
return false;
|
|
17
|
+
return Object.values(value).every((entry) => entry === undefined || typeof entry === "string");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parses and canonicalizes a runtime context by removing undefined fields.
|
|
21
|
+
*
|
|
22
|
+
* @param value - Runtime value to validate and normalize.
|
|
23
|
+
* @param translate - Translator used for explicit invalid Context errors.
|
|
24
|
+
* @returns A canonical runtime context containing only defined string values.
|
|
25
|
+
* @throws MiaixzSdkError When the value is not a valid runtime context.
|
|
26
|
+
*/
|
|
27
|
+
function parseRuntimeContext(value, translate) {
|
|
28
|
+
if (!isMiaixzRuntimeContext(value)) {
|
|
29
|
+
throw new MiaixzSdkError(translate("sdk.error.context.invalid"), {
|
|
30
|
+
code: "CONTEXT_INVALID",
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return Object.fromEntries(Object.entries(value).filter((entry) => entry[1] !== undefined));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Applies explicit Context fields over persisted state, treating undefined as deletion.
|
|
37
|
+
*
|
|
38
|
+
* @param persisted - Canonical persisted Context state.
|
|
39
|
+
* @param initial - Valid explicit Context fields supplied by the host.
|
|
40
|
+
* @param translate - Translator used to normalize the merged state.
|
|
41
|
+
* @returns Canonical merged Context state.
|
|
42
|
+
*/
|
|
43
|
+
function mergeInitialContext(persisted, initial, translate) {
|
|
44
|
+
const merged = { ...persisted };
|
|
45
|
+
for (const [key, value] of Object.entries(initial)) {
|
|
46
|
+
if (value === undefined)
|
|
47
|
+
delete merged[key];
|
|
48
|
+
else
|
|
49
|
+
merged[key] = value;
|
|
50
|
+
}
|
|
51
|
+
return parseRuntimeContext(merged, translate);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Converts tenant, organization, space, locale, and trace context into request headers.
|
|
55
|
+
*
|
|
56
|
+
* @param context - Runtime context to convert.
|
|
57
|
+
* @returns Headers containing all defined runtime-context values.
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
export function miaixzContextToHeaders(context) {
|
|
61
|
+
const headers = new Headers();
|
|
62
|
+
const mapping = [
|
|
63
|
+
["userId", miaixzHeaders.userId],
|
|
64
|
+
["tenantId", miaixzHeaders.tenantId],
|
|
65
|
+
["organizationId", miaixzHeaders.organizationId],
|
|
66
|
+
["departmentId", miaixzHeaders.departmentId],
|
|
67
|
+
["spaceId", miaixzHeaders.spaceId],
|
|
68
|
+
["locale", miaixzHeaders.locale],
|
|
69
|
+
["timezone", miaixzHeaders.timezone],
|
|
70
|
+
["traceId", miaixzHeaders.traceId],
|
|
71
|
+
];
|
|
72
|
+
for (const [key, header] of mapping) {
|
|
73
|
+
const value = context[key];
|
|
74
|
+
if (value)
|
|
75
|
+
headers.set(header, value);
|
|
76
|
+
}
|
|
77
|
+
return headers;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Compares string-valued context records to suppress duplicate event delivery.
|
|
81
|
+
*
|
|
82
|
+
* @param first - First context to compare.
|
|
83
|
+
* @param second - Second context to compare.
|
|
84
|
+
* @returns Whether both contexts contain equivalent values.
|
|
85
|
+
*/
|
|
86
|
+
function contextsEqual(first, second) {
|
|
87
|
+
const keys = new Set([...Object.keys(first), ...Object.keys(second)]);
|
|
88
|
+
return [...keys].every((key) => first[key] === second[key]);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Stores request context independently of URL route parameters.
|
|
92
|
+
*
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
export class MiaixzContextStore {
|
|
96
|
+
#storage;
|
|
97
|
+
#appId;
|
|
98
|
+
#persist;
|
|
99
|
+
#migrations;
|
|
100
|
+
#listeners = new Set();
|
|
101
|
+
#events;
|
|
102
|
+
#translate;
|
|
103
|
+
#context;
|
|
104
|
+
#stopEventListener;
|
|
105
|
+
/**
|
|
106
|
+
* Creates a runtime-context store.
|
|
107
|
+
*
|
|
108
|
+
* @param options - Initial state plus persistence, events, and translation adapters.
|
|
109
|
+
* @throws MiaixzSdkError When app, migration, or explicit Context configuration is invalid.
|
|
110
|
+
*/
|
|
111
|
+
constructor(options) {
|
|
112
|
+
this.#translate = options.translate ?? miaixzDefaultI18n.t;
|
|
113
|
+
readMiaixzVersionedValue({
|
|
114
|
+
scope: { appId: options.appId },
|
|
115
|
+
kind: "context",
|
|
116
|
+
schemaVersion: miaixzContextSchemaVersion,
|
|
117
|
+
...(options.migrations === undefined ? {} : { migrations: options.migrations }),
|
|
118
|
+
parse: (value) => parseRuntimeContext(value, this.#translate),
|
|
119
|
+
});
|
|
120
|
+
if (options.initialContext !== undefined) {
|
|
121
|
+
parseRuntimeContext(options.initialContext, this.#translate);
|
|
122
|
+
}
|
|
123
|
+
this.#storage = options.storage ?? getMiaixzBrowserStorage();
|
|
124
|
+
this.#appId = options.appId;
|
|
125
|
+
this.#persist = options.persist ?? true;
|
|
126
|
+
this.#migrations = options.migrations;
|
|
127
|
+
this.#events = options.events;
|
|
128
|
+
const persisted = this.#persist
|
|
129
|
+
? readMiaixzVersionedValue({
|
|
130
|
+
...(this.#storage === undefined ? {} : { storage: this.#storage }),
|
|
131
|
+
scope: { appId: this.#appId },
|
|
132
|
+
kind: "context",
|
|
133
|
+
schemaVersion: miaixzContextSchemaVersion,
|
|
134
|
+
...(this.#migrations === undefined ? {} : { migrations: this.#migrations }),
|
|
135
|
+
parse: (value) => parseRuntimeContext(value, this.#translate),
|
|
136
|
+
})
|
|
137
|
+
: undefined;
|
|
138
|
+
this.#context =
|
|
139
|
+
options.initialContext === undefined
|
|
140
|
+
? { ...persisted }
|
|
141
|
+
: mergeInitialContext(persisted ?? {}, options.initialContext, this.#translate);
|
|
142
|
+
if (this.#persist && options.initialContext !== undefined)
|
|
143
|
+
this.#persistSnapshot();
|
|
144
|
+
this.#stopEventListener = this.#events?.on("context:changed", (context) => {
|
|
145
|
+
if (isMiaixzRuntimeContext(context))
|
|
146
|
+
this.#set(context, false);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns the active request context.
|
|
151
|
+
*
|
|
152
|
+
* @returns An immutable copy of the current request context.
|
|
153
|
+
*/
|
|
154
|
+
getSnapshot() {
|
|
155
|
+
return Object.freeze({ ...this.#context });
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Replaces, persists, and broadcasts the full runtime context.
|
|
159
|
+
*
|
|
160
|
+
* @param context - Complete runtime context to activate.
|
|
161
|
+
* @throws MiaixzSdkError When the supplied Context contains an invalid value.
|
|
162
|
+
*/
|
|
163
|
+
set(context) {
|
|
164
|
+
this.#set(context, true);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Commits validated context and optionally broadcasts it.
|
|
168
|
+
*
|
|
169
|
+
* @param context - Complete runtime context to commit.
|
|
170
|
+
* @param broadcast - Whether to publish the change through the event bus.
|
|
171
|
+
*/
|
|
172
|
+
#set(context, broadcast) {
|
|
173
|
+
const normalized = parseRuntimeContext(context, this.#translate);
|
|
174
|
+
if (contextsEqual(this.#context, normalized))
|
|
175
|
+
return;
|
|
176
|
+
this.#context = normalized;
|
|
177
|
+
this.#commit();
|
|
178
|
+
if (broadcast)
|
|
179
|
+
this.#events?.emit("context:changed", this.getSnapshot());
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Merges a partial update into the current runtime context.
|
|
183
|
+
*
|
|
184
|
+
* @param context - Partial context values to merge.
|
|
185
|
+
* @throws MiaixzSdkError When the merged Context contains an invalid value.
|
|
186
|
+
*/
|
|
187
|
+
patch(context) {
|
|
188
|
+
this.set({ ...this.#context, ...context });
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Clears all runtime context values.
|
|
192
|
+
*/
|
|
193
|
+
clear() {
|
|
194
|
+
this.#set({}, true);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Registers a context listener and returns its unsubscribe function.
|
|
198
|
+
*
|
|
199
|
+
* @param listener - Callback invoked with each context snapshot.
|
|
200
|
+
* @returns Function that unregisters the listener.
|
|
201
|
+
*/
|
|
202
|
+
subscribe(listener) {
|
|
203
|
+
this.#listeners.add(listener);
|
|
204
|
+
return () => this.#listeners.delete(listener);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Request-client provider that returns headers for the latest context.
|
|
208
|
+
*
|
|
209
|
+
* @returns Headers for the active runtime context.
|
|
210
|
+
*/
|
|
211
|
+
headersProvider = () => miaixzContextToHeaders(this.#context);
|
|
212
|
+
/**
|
|
213
|
+
* Releases event subscriptions and local listeners.
|
|
214
|
+
*/
|
|
215
|
+
destroy() {
|
|
216
|
+
this.#stopEventListener?.();
|
|
217
|
+
this.#listeners.clear();
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Persists the current context and notifies local listeners.
|
|
221
|
+
*/
|
|
222
|
+
#commit() {
|
|
223
|
+
if (this.#persist)
|
|
224
|
+
this.#persistSnapshot();
|
|
225
|
+
const snapshot = this.getSnapshot();
|
|
226
|
+
for (const listener of this.#listeners)
|
|
227
|
+
listener(snapshot);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Writes the current Context through the shared versioned storage primitive.
|
|
231
|
+
*/
|
|
232
|
+
#persistSnapshot() {
|
|
233
|
+
writeMiaixzVersionedValue({
|
|
234
|
+
...(this.#storage === undefined ? {} : { storage: this.#storage }),
|
|
235
|
+
scope: { appId: this.#appId },
|
|
236
|
+
kind: "context",
|
|
237
|
+
schemaVersion: miaixzContextSchemaVersion,
|
|
238
|
+
}, this.#context);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Creates a context store with optional browser persistence.
|
|
243
|
+
*
|
|
244
|
+
* @param options - Application identity, optional initial state, and runtime adapters.
|
|
245
|
+
* @returns Configured runtime-context store.
|
|
246
|
+
* @throws MiaixzSdkError When app, migration, or explicit Context configuration is invalid.
|
|
247
|
+
* @public
|
|
248
|
+
*/
|
|
249
|
+
export function createMiaixzContextStore(options) {
|
|
250
|
+
return new MiaixzContextStore(options);
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/context/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,iBAAiB,EAAyB,MAAM,kBAAkB,CAAC;AAC5E,OAAO,EACL,uBAAuB,EACvB,wBAAwB,EACxB,yBAAyB,GAG1B,MAAM,qBAAqB,CAA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AI,CAAC,QAAQ;YAAE,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QACpC,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU;YAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC7D,CAAC;IAED;;OAEG;IACH,gBAAgB;QACd,yBAAyB,CACvB;YACE,GAAG,CAAC,IAAI,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClE,KAAK,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE;YAC7B,IAAI,EAAE,SAAS;YACf,aAAa,EAAE,0BAA0B;SAC1C,EACD,IAAI,CAAC,QAAQ,CACd,CAAC;IACJ,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,wBAAwB,CAAC,OAAkC;IACzE,OAAO,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC","sourcesContent":["import { miaixzHeaders } from \"../consts/index.js\";\nimport { MiaixzSdkError } from \"../api/errors.js\";\nimport type { MiaixzEventBus, MiaixzSdkEventMap } from \"../events/index.js\";\nimport { miaixzDefaultI18n, type MiaixzTranslator } from \"../i18n/index.js\";\nimport {\n getMiaixzBrowserStorage,\n readMiaixzVersionedValue,\n writeMiaixzVersionedValue,\n type MiaixzKeyValueStorage,\n type MiaixzStorageMigration,\n} from \"../storage/index.js\";\nimport type { MiaixzRuntimeContext } from \"../types/index.js\";\nimport { isRecord } from \"../utils/object.js\";\n\n/**\n * Configures a Miaixz runtime-context store.\n *\n * @public\n */\nexport interface MiaixzContextStoreOptions {\n /**\n * Identifies the consuming frontend application and its physical storage namespace.\n */\n readonly appId: string;\n\n /**\n * Optional context merged over persisted state.\n */\n readonly initialContext?: MiaixzRuntimeContext;\n\n /**\n * Optional key-value storage used for persistence.\n */\n readonly storage?: MiaixzKeyValueStorage;\n\n /**\n * Indicates whether context should be persisted.\n *\n * @defaultValue true\n */\n readonly persist?: boolean;\n\n /**\n * Supplies optional sequential migrations for older Context schemas.\n */\n readonly migrations?: readonly MiaixzStorageMigration[];\n\n /**\n * Optional event bus used to synchronize service instances.\n */\n readonly events?: MiaixzEventBus<MiaixzSdkEventMap>;\n\n /**\n * Optional translator used for context errors.\n */\n readonly translate?: MiaixzTranslator;\n}\n\nconst miaixzContextSchemaVersion = 1;\n\n/**\n * Determines whether a value is a valid runtime context.\n *\n * @param value - Value to inspect.\n * @returns Whether `value` is a valid string-valued runtime context.\n * @public\n */\nexport function isMiaixzRuntimeContext(value: unknown): value is MiaixzRuntimeContext {\n if (!isRecord(value)) return false;\n return Object.values(value).every((entry) => entry === undefined || typeof entry === \"string\");\n}\n\n/**\n * Parses and canonicalizes a runtime context by removing undefined fields.\n *\n * @param value - Runtime value to validate and normalize.\n * @param translate - Translator used for explicit invalid Context errors.\n * @returns A canonical runtime context containing only defined string values.\n * @throws MiaixzSdkError When the value is not a valid runtime context.\n */\nfunction parseRuntimeContext(value: unknown, translate: MiaixzTranslator): MiaixzRuntimeContext {\n if (!isMiaixzRuntimeContext(value)) {\n throw new MiaixzSdkError(translate(\"sdk.error.context.invalid\"), {\n code: \"CONTEXT_INVALID\",\n });\n }\n return Object.fromEntries(\n Object.entries(value).filter((entry): entry is [string, string] => entry[1] !== undefined),\n ) as MiaixzRuntimeContext;\n}\n\n/**\n * Applies explicit Context fields over persisted state, treating undefined as deletion.\n *\n * @param persisted - Canonical persisted Context state.\n * @param initial - Valid explicit Context fields supplied by the host.\n * @param translate - Translator used to normalize the merged state.\n * @returns Canonical merged Context state.\n */\nfunction mergeInitialContext(\n persisted: Readonly<MiaixzRuntimeContext>,\n initial: Readonly<MiaixzRuntimeContext>,\n translate: MiaixzTranslator,\n): MiaixzRuntimeContext {\n const merged: Record<string, string | undefined> = { ...persisted };\n for (const [key, value] of Object.entries(initial)) {\n if (value === undefined) delete merged[key];\n else merged[key] = value;\n }\n return parseRuntimeContext(merged, translate);\n}\n\n/**\n * Converts tenant, organization, space, locale, and trace context into request headers.\n *\n * @param context - Runtime context to convert.\n * @returns Headers containing all defined runtime-context values.\n * @public\n */\nexport function miaixzContextToHeaders(context: Readonly<MiaixzRuntimeContext>): Headers {\n const headers = new Headers();\n const mapping: readonly [keyof MiaixzRuntimeContext, string][] = [\n [\"userId\", miaixzHeaders.userId],\n [\"tenantId\", miaixzHeaders.tenantId],\n [\"organizationId\", miaixzHeaders.organizationId],\n [\"departmentId\", miaixzHeaders.departmentId],\n [\"spaceId\", miaixzHeaders.spaceId],\n [\"locale\", miaixzHeaders.locale],\n [\"timezone\", miaixzHeaders.timezone],\n [\"traceId\", miaixzHeaders.traceId],\n ];\n for (const [key, header] of mapping) {\n const value = context[key];\n if (value) headers.set(header, value);\n }\n return headers;\n}\n\n/**\n * Compares string-valued context records to suppress duplicate event delivery.\n *\n * @param first - First context to compare.\n * @param second - Second context to compare.\n * @returns Whether both contexts contain equivalent values.\n */\nfunction contextsEqual(\n first: Readonly<MiaixzRuntimeContext>,\n second: Readonly<MiaixzRuntimeContext>,\n): boolean {\n const keys = new Set([...Object.keys(first), ...Object.keys(second)]);\n return [...keys].every(\n (key) => first[key as keyof MiaixzRuntimeContext] === second[key as keyof MiaixzRuntimeContext],\n );\n}\n\n/**\n * Stores request context independently of URL route parameters.\n *\n * @public\n */\nexport class MiaixzContextStore {\n readonly #storage: MiaixzKeyValueStorage | undefined;\n readonly #appId: string;\n readonly #persist: boolean;\n readonly #migrations: readonly MiaixzStorageMigration[] | undefined;\n readonly #listeners = new Set<(context: Readonly<MiaixzRuntimeContext>) => void>();\n readonly #events: MiaixzEventBus<MiaixzSdkEventMap> | undefined;\n readonly #translate: MiaixzTranslator;\n #context: MiaixzRuntimeContext;\n #stopEventListener: (() => void) | undefined;\n\n /**\n * Creates a runtime-context store.\n *\n * @param options - Initial state plus persistence, events, and translation adapters.\n * @throws MiaixzSdkError When app, migration, or explicit Context configuration is invalid.\n */\n constructor(options: MiaixzContextStoreOptions) {\n this.#translate = options.translate ?? miaixzDefaultI18n.t;\n readMiaixzVersionedValue({\n scope: { appId: options.appId },\n kind: \"context\",\n schemaVersion: miaixzContextSchemaVersion,\n ...(options.migrations === undefined ? {} : { migrations: options.migrations }),\n parse: (value) => parseRuntimeContext(value, this.#translate),\n });\n if (options.initialContext !== undefined) {\n parseRuntimeContext(options.initialContext, this.#translate);\n }\n this.#storage = options.storage ?? getMiaixzBrowserStorage();\n this.#appId = options.appId;\n this.#persist = options.persist ?? true;\n this.#migrations = options.migrations;\n this.#events = options.events;\n const persisted = this.#persist\n ? readMiaixzVersionedValue({\n ...(this.#storage === undefined ? {} : { storage: this.#storage }),\n scope: { appId: this.#appId },\n kind: \"context\",\n schemaVersion: miaixzContextSchemaVersion,\n ...(this.#migrations === undefined ? {} : { migrations: this.#migrations }),\n parse: (value) => parseRuntimeContext(value, this.#translate),\n })\n : undefined;\n this.#context =\n options.initialContext === undefined\n ? { ...persisted }\n : mergeInitialContext(persisted ?? {}, options.initialContext, this.#translate);\n if (this.#persist && options.initialContext !== undefined) this.#persistSnapshot();\n this.#stopEventListener = this.#events?.on(\"context:changed\", (context) => {\n if (isMiaixzRuntimeContext(context)) this.#set(context, false);\n });\n }\n\n /**\n * Returns the active request context.\n *\n * @returns An immutable copy of the current request context.\n */\n getSnapshot(): Readonly<MiaixzRuntimeContext> {\n return Object.freeze({ ...this.#context });\n }\n\n /**\n * Replaces, persists, and broadcasts the full runtime context.\n *\n * @param context - Complete runtime context to activate.\n * @throws MiaixzSdkError When the supplied Context contains an invalid value.\n */\n set(context: MiaixzRuntimeContext): void {\n this.#set(context, true);\n }\n\n /**\n * Commits validated context and optionally broadcasts it.\n *\n * @param context - Complete runtime context to commit.\n * @param broadcast - Whether to publish the change through the event bus.\n */\n #set(context: MiaixzRuntimeContext, broadcast: boolean): void {\n const normalized = parseRuntimeContext(context, this.#translate);\n if (contextsEqual(this.#context, normalized)) return;\n this.#context = normalized;\n this.#commit();\n if (broadcast) this.#events?.emit(\"context:changed\", this.getSnapshot());\n }\n\n /**\n * Merges a partial update into the current runtime context.\n *\n * @param context - Partial context values to merge.\n * @throws MiaixzSdkError When the merged Context contains an invalid value.\n */\n patch(context: Partial<MiaixzRuntimeContext>): void {\n this.set({ ...this.#context, ...context });\n }\n\n /**\n * Clears all runtime context values.\n */\n clear(): void {\n this.#set({}, true);\n }\n\n /**\n * Registers a context listener and returns its unsubscribe function.\n *\n * @param listener - Callback invoked with each context snapshot.\n * @returns Function that unregisters the listener.\n */\n subscribe(listener: (context: Readonly<MiaixzRuntimeContext>) => void): () => void {\n this.#listeners.add(listener);\n return () => this.#listeners.delete(listener);\n }\n\n /**\n * Request-client provider that returns headers for the latest context.\n *\n * @returns Headers for the active runtime context.\n */\n readonly headersProvider = (): Headers => miaixzContextToHeaders(this.#context);\n\n /**\n * Releases event subscriptions and local listeners.\n */\n destroy(): void {\n this.#stopEventListener?.();\n this.#listeners.clear();\n }\n\n /**\n * Persists the current context and notifies local listeners.\n */\n #commit(): void {\n if (this.#persist) this.#persistSnapshot();\n const snapshot = this.getSnapshot();\n for (const listener of this.#listeners) listener(snapshot);\n }\n\n /**\n * Writes the current Context through the shared versioned storage primitive.\n */\n #persistSnapshot(): void {\n writeMiaixzVersionedValue(\n {\n ...(this.#storage === undefined ? {} : { storage: this.#storage }),\n scope: { appId: this.#appId },\n kind: \"context\",\n schemaVersion: miaixzContextSchemaVersion,\n },\n this.#context,\n );\n }\n}\n\n/**\n * Creates a context store with optional browser persistence.\n *\n * @param options - Application identity, optional initial state, and runtime adapters.\n * @returns Configured runtime-context store.\n * @throws MiaixzSdkError When app, migration, or explicit Context configuration is invalid.\n * @public\n */\nexport function createMiaixzContextStore(options: MiaixzContextStoreOptions): MiaixzContextStore {\n return new MiaixzContextStore(options);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC","sourcesContent":["export * from \"./context.js\";\n"]}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { MiaixzMessageCatalog } from "../i18n/index.js";
|
|
2
|
+
import type { MiaixzRuntimeContext } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Describes a navigation request issued by a remote module through its host bridge.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export interface MiaixzNavigationRequest {
|
|
9
|
+
/**
|
|
10
|
+
* Same-site absolute path requested by the module.
|
|
11
|
+
*/
|
|
12
|
+
readonly path: string;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the host should replace the active history entry.
|
|
15
|
+
*/
|
|
16
|
+
readonly replace?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Optional structured-clone-compatible navigation state.
|
|
19
|
+
*/
|
|
20
|
+
readonly state?: unknown;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Defines the capabilities a host exposes to an integrated or iframe module.
|
|
24
|
+
*
|
|
25
|
+
* This task freezes the public shape only; runtime implementations are supplied by Host Bridge
|
|
26
|
+
* tasks and never expose authentication credentials.
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export interface MiaixzHostBridge {
|
|
31
|
+
/**
|
|
32
|
+
* Micro-frontend protocol version implemented by the bridge.
|
|
33
|
+
*/
|
|
34
|
+
readonly protocolVersion: "1.0.0";
|
|
35
|
+
/**
|
|
36
|
+
* Reads the current non-sensitive host runtime context.
|
|
37
|
+
*
|
|
38
|
+
* @returns Immutable runtime context from the host.
|
|
39
|
+
*/
|
|
40
|
+
getContext(): Promise<Readonly<MiaixzRuntimeContext>>;
|
|
41
|
+
/**
|
|
42
|
+
* Requests same-site navigation from the host.
|
|
43
|
+
*
|
|
44
|
+
* @param request - Validated navigation request.
|
|
45
|
+
* @returns Promise resolved after the host handles navigation.
|
|
46
|
+
*/
|
|
47
|
+
navigate(request: Readonly<MiaixzNavigationRequest>): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Checks whether all requested permissions are available.
|
|
50
|
+
*
|
|
51
|
+
* @param permissions - Permission names to evaluate.
|
|
52
|
+
* @returns Whether every requested permission is available.
|
|
53
|
+
*/
|
|
54
|
+
hasPermissions(permissions: readonly string[]): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Registers a module-owned internationalization catalog with the host.
|
|
57
|
+
*
|
|
58
|
+
* @param namespace - Module message namespace.
|
|
59
|
+
* @param catalog - Localized messages owned by the module.
|
|
60
|
+
* @returns Promise resolved after registration.
|
|
61
|
+
*/
|
|
62
|
+
registerMessages(namespace: string, catalog: MiaixzMessageCatalog): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Emits one module-scoped event through the host.
|
|
65
|
+
*
|
|
66
|
+
* @typeParam T - Event payload type.
|
|
67
|
+
* @param type - Module-scoped event name.
|
|
68
|
+
* @param payload - Structured-clone-compatible event payload.
|
|
69
|
+
* @returns Promise resolved after event delivery.
|
|
70
|
+
*/
|
|
71
|
+
emit<T = unknown>(type: string, payload: T): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Subscribes to one module-scoped host event.
|
|
74
|
+
*
|
|
75
|
+
* @typeParam T - Event payload type.
|
|
76
|
+
* @param type - Module-scoped event name.
|
|
77
|
+
* @param listener - Callback that receives event payloads.
|
|
78
|
+
* @returns Asynchronous unsubscribe function.
|
|
79
|
+
*/
|
|
80
|
+
subscribe<T = unknown>(type: string, listener: (payload: T) => void): Promise<() => Promise<void>>;
|
|
81
|
+
/**
|
|
82
|
+
* Releases resources and pending work owned by the bridge.
|
|
83
|
+
*/
|
|
84
|
+
dispose(): void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Supplies the optional capabilities exposed by one Miaixz host.
|
|
88
|
+
*
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
91
|
+
export interface MiaixzHostAdapter {
|
|
92
|
+
/**
|
|
93
|
+
* Reads the current non-sensitive host runtime context when available.
|
|
94
|
+
*/
|
|
95
|
+
readonly getContext?: MiaixzHostBridge["getContext"];
|
|
96
|
+
/**
|
|
97
|
+
* Handles validated same-site navigation requests when available.
|
|
98
|
+
*/
|
|
99
|
+
readonly navigate?: MiaixzHostBridge["navigate"];
|
|
100
|
+
/**
|
|
101
|
+
* Evaluates validated module permissions when available.
|
|
102
|
+
*/
|
|
103
|
+
readonly hasPermissions?: MiaixzHostBridge["hasPermissions"];
|
|
104
|
+
/**
|
|
105
|
+
* Registers module-owned internationalization resources when available.
|
|
106
|
+
*/
|
|
107
|
+
readonly registerMessages?: MiaixzHostBridge["registerMessages"];
|
|
108
|
+
/**
|
|
109
|
+
* Emits module-scoped events when available.
|
|
110
|
+
*/
|
|
111
|
+
readonly emit?: MiaixzHostBridge["emit"];
|
|
112
|
+
/**
|
|
113
|
+
* Subscribes to module-scoped events when available.
|
|
114
|
+
*/
|
|
115
|
+
readonly subscribe?: MiaixzHostBridge["subscribe"];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Configures a same-runtime bridge for one identified module.
|
|
119
|
+
*
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export interface MiaixzDirectHostBridgeOptions {
|
|
123
|
+
/**
|
|
124
|
+
* Identifies the module allowed to use the bridge.
|
|
125
|
+
*/
|
|
126
|
+
readonly moduleId: string;
|
|
127
|
+
/**
|
|
128
|
+
* Supplies the host capabilities exposed to the module.
|
|
129
|
+
*/
|
|
130
|
+
readonly adapter: MiaixzHostAdapter;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=host-context.d.ts.map
|
|
@@ -0,0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GAC7B,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAEhC;;OAEG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAErD;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAEjD;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;IAE7D;;OAEG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,gBAAgB,CAAC,kBAAkB,CAAC,CAAC;IAEjE;;OAEG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAEzC;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,gBAAgB,CAAC,WAAW,CAAC,CAAC;CACpD;AAED;;;;GAIG;AACH,MAAM,WAAW,6BAA6B;IAC5C;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;CACrC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host-context.js","sourceRoot":"","sources":["../../src/contracts/host-context.ts"],"names":[],"mappings":"","sourcesContent":["import type { MiaixzMessageCatalog } from \"../i18n/index.js\";\nimport type { MiaixzRuntimeContext } from \"../types/index.js\";\n\n/**\n * Describes a navigation request issued by a remote module through its host bridge.\n *\n * @public\n */\nexport interface MiaixzNavigationRequest {\n /**\n * Same-site absolute path requested by the module.\n */\n readonly path: string;\n\n /**\n * Whether the host should replace the active history entry.\n */\n readonly replace?: boolean;\n\n /**\n * Optional structured-clone-compatible navigation state.\n */\n readonly state?: unknown;\n}\n\n/**\n * Defines the capabilities a host exposes to an integrated or iframe module.\n *\n * This task freezes the public shape only; runtime implementations are supplied by Host Bridge\n * tasks and never expose authentication credentials.\n *\n * @public\n */\nexport interface MiaixzHostBridge {\n /**\n * Micro-frontend protocol version implemented by the bridge.\n */\n readonly protocolVersion: \"1.0.0\";\n\n /**\n * Reads the current non-sensitive host runtime context.\n *\n * @returns Immutable runtime context from the host.\n */\n getContext(): Promise<Readonly<MiaixzRuntimeContext>>;\n\n /**\n * Requests same-site navigation from the host.\n *\n * @param request - Validated navigation request.\n * @returns Promise resolved after the host handles navigation.\n */\n navigate(request: Readonly<MiaixzNavigationRequest>): Promise<void>;\n\n /**\n * Checks whether all requested permissions are available.\n *\n * @param permissions - Permission names to evaluate.\n * @returns Whether every requested permission is available.\n */\n hasPermissions(permissions: readonly string[]): Promise<boolean>;\n\n /**\n * Registers a module-owned internationalization catalog with the host.\n *\n * @param namespace - Module message namespace.\n * @param catalog - Localized messages owned by the module.\n * @returns Promise resolved after registration.\n */\n registerMessages(namespace: string, catalog: MiaixzMessageCatalog): Promise<void>;\n\n /**\n * Emits one module-scoped event through the host.\n *\n * @typeParam T - Event payload type.\n * @param type - Module-scoped event name.\n * @param payload - Structured-clone-compatible event payload.\n * @returns Promise resolved after event delivery.\n */\n emit<T = unknown>(type: string, payload: T): Promise<void>;\n\n /**\n * Subscribes to one module-scoped host event.\n *\n * @typeParam T - Event payload type.\n * @param type - Module-scoped event name.\n * @param listener - Callback that receives event payloads.\n * @returns Asynchronous unsubscribe function.\n */\n subscribe<T = unknown>(\n type: string,\n listener: (payload: T) => void,\n ): Promise<() => Promise<void>>;\n\n /**\n * Releases resources and pending work owned by the bridge.\n */\n dispose(): void;\n}\n\n/**\n * Supplies the optional capabilities exposed by one Miaixz host.\n *\n * @public\n */\nexport interface MiaixzHostAdapter {\n /**\n * Reads the current non-sensitive host runtime context when available.\n */\n readonly getContext?: MiaixzHostBridge[\"getContext\"];\n\n /**\n * Handles validated same-site navigation requests when available.\n */\n readonly navigate?: MiaixzHostBridge[\"navigate\"];\n\n /**\n * Evaluates validated module permissions when available.\n */\n readonly hasPermissions?: MiaixzHostBridge[\"hasPermissions\"];\n\n /**\n * Registers module-owned internationalization resources when available.\n */\n readonly registerMessages?: MiaixzHostBridge[\"registerMessages\"];\n\n /**\n * Emits module-scoped events when available.\n */\n readonly emit?: MiaixzHostBridge[\"emit\"];\n\n /**\n * Subscribes to module-scoped events when available.\n */\n readonly subscribe?: MiaixzHostBridge[\"subscribe\"];\n}\n\n/**\n * Configures a same-runtime bridge for one identified module.\n *\n * @public\n */\nexport interface MiaixzDirectHostBridgeOptions {\n /**\n * Identifies the module allowed to use the bridge.\n */\n readonly moduleId: string;\n\n /**\n * Supplies the host capabilities exposed to the module.\n */\n readonly adapter: MiaixzHostAdapter;\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { MiaixzDirectHostBridgeOptions, MiaixzHostAdapter, MiaixzHostBridge, MiaixzNavigationRequest, } from "./host-context.js";
|
|
2
|
+
export type { MiaixzIntegratedModule, MiaixzModuleHandle, MiaixzModuleMountContext, } from "./lifecycle.js";
|
|
3
|
+
export { MIAIXZ_MODULE_PROTOCOL_VERSION, isMiaixzHostVersionCompatible, parseMiaixzModuleManifest, } from "./module-manifest.js";
|
|
4
|
+
export type { MiaixzHostCapability, MiaixzModuleKind, MiaixzModuleManifest, MiaixzModuleManifestParseOptions, } from "./module-manifest.js";
|
|
5
|
+
export type { MiaixzModuleNavigationItem } from "./navigation.js";
|
|
6
|
+
export type { MiaixzBridgeEnvelope, MiaixzPostMessageChildOptions, MiaixzPostMessageHost, MiaixzPostMessageHostOptions, } from "./post-message.js";
|
|
7
|
+
export type { MiaixzModuleRoute } from "./route.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/contracts/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,6BAA6B,EAC7B,iBAAiB,EACjB,gBAAgB,EAChB,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,wBAAwB,GACzB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,8BAA8B,EAC9B,6BAA6B,EAC7B,yBAAyB,GAC1B,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EACV,oBAAoB,EACpB,gBAAgB,EAChB,oBAAoB,EACpB,gCAAgC,GACjC,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAClE,YAAY,EACV,oBAAoB,EACpB,6BAA6B,EAC7B,qBAAqB,EACrB,4BAA4B,GAC7B,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/contracts/index.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,8BAA8B,EAC9B,6BAA6B,EAC7B,yBAAyB,GAC1B,MAAM,sBAAsB,CAAC","sourcesContent":["export type {\n MiaixzDirectHostBridgeOptions,\n MiaixzHostAdapter,\n MiaixzHostBridge,\n MiaixzNavigationRequest,\n} from \"./host-context.js\";\nexport type {\n MiaixzIntegratedModule,\n MiaixzModuleHandle,\n MiaixzModuleMountContext,\n} from \"./lifecycle.js\";\nexport {\n MIAIXZ_MODULE_PROTOCOL_VERSION,\n isMiaixzHostVersionCompatible,\n parseMiaixzModuleManifest,\n} from \"./module-manifest.js\";\nexport type {\n MiaixzHostCapability,\n MiaixzModuleKind,\n MiaixzModuleManifest,\n MiaixzModuleManifestParseOptions,\n} from \"./module-manifest.js\";\nexport type { MiaixzModuleNavigationItem } from \"./navigation.js\";\nexport type {\n MiaixzBridgeEnvelope,\n MiaixzPostMessageChildOptions,\n MiaixzPostMessageHost,\n MiaixzPostMessageHostOptions,\n} from \"./post-message.js\";\nexport type { MiaixzModuleRoute } from \"./route.js\";\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { MiaixzHostBridge } from "./host-context.js";
|
|
2
|
+
import type { MiaixzModuleManifest } from "./module-manifest.js";
|
|
3
|
+
/**
|
|
4
|
+
* Provides the host-owned resources required to mount an integrated module.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export interface MiaixzModuleMountContext {
|
|
9
|
+
/**
|
|
10
|
+
* DOM container owned by the host application.
|
|
11
|
+
*/
|
|
12
|
+
readonly container: HTMLElement;
|
|
13
|
+
/**
|
|
14
|
+
* Capability-limited bridge supplied by the host.
|
|
15
|
+
*/
|
|
16
|
+
readonly bridge: MiaixzHostBridge;
|
|
17
|
+
/**
|
|
18
|
+
* Validated immutable manifest associated with the module.
|
|
19
|
+
*/
|
|
20
|
+
readonly manifest: Readonly<MiaixzModuleManifest>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Represents the resources returned by a mounted integrated module.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export interface MiaixzModuleHandle {
|
|
28
|
+
/**
|
|
29
|
+
* Releases every resource created by the mounted module.
|
|
30
|
+
*
|
|
31
|
+
* @returns Nothing, or a promise resolved after asynchronous cleanup.
|
|
32
|
+
*/
|
|
33
|
+
unmount(): void | Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Defines the only lifecycle entry exposed by an integrated module.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export interface MiaixzIntegratedModule {
|
|
41
|
+
/**
|
|
42
|
+
* Mounts the module into its host-owned container.
|
|
43
|
+
*
|
|
44
|
+
* @param context - Validated host resources supplied to the module.
|
|
45
|
+
* @returns Handle used by the host to unmount the module.
|
|
46
|
+
*/
|
|
47
|
+
mount(context: MiaixzModuleMountContext): MiaixzModuleHandle | Promise<MiaixzModuleHandle>;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|