@bitakit/core 0.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/LICENSE +21 -0
- package/README.md +382 -0
- package/discovery/entries.mjs +124 -0
- package/discovery/generate.mjs +327 -0
- package/discovery/host.mjs +12 -0
- package/discovery/index.d.ts +41 -0
- package/discovery/index.mjs +4 -0
- package/discovery/modules.mjs +13 -0
- package/discovery/packages.mjs +57 -0
- package/discovery/watch.mjs +23 -0
- package/dist/actions/action-list.d.ts +69 -0
- package/dist/actions/action-list.js +247 -0
- package/dist/actions/action.d.ts +96 -0
- package/dist/actions/action.js +145 -0
- package/dist/actions/base/url-action.d.ts +8 -0
- package/dist/actions/base/url-action.js +15 -0
- package/dist/actions/core.d.ts +14 -0
- package/dist/actions/core.js +8 -0
- package/dist/actions/dependencies.d.ts +8 -0
- package/dist/actions/dependencies.js +34 -0
- package/dist/actions/execution.d.ts +4 -0
- package/dist/actions/execution.js +64 -0
- package/dist/actions/feedback.d.ts +17 -0
- package/dist/actions/feedback.js +21 -0
- package/dist/actions/groups.d.ts +13 -0
- package/dist/actions/groups.js +21 -0
- package/dist/actions/instance-bindings.d.ts +16 -0
- package/dist/actions/instance-bindings.js +93 -0
- package/dist/actions/interceptors.d.ts +24 -0
- package/dist/actions/interceptors.js +42 -0
- package/dist/actions/navigation.d.ts +12 -0
- package/dist/actions/navigation.js +42 -0
- package/dist/actions/reference.d.ts +17 -0
- package/dist/actions/reference.js +39 -0
- package/dist/actions/state.d.ts +11 -0
- package/dist/actions/state.js +8 -0
- package/dist/actions/types.d.ts +99 -0
- package/dist/actions/types.js +1 -0
- package/dist/app/access.d.ts +4 -0
- package/dist/app/access.js +85 -0
- package/dist/app/action-access.d.ts +4 -0
- package/dist/app/action-access.js +20 -0
- package/dist/app/service-error.d.ts +8 -0
- package/dist/app/service-error.js +19 -0
- package/dist/app/types.d.ts +47 -0
- package/dist/app/types.js +1 -0
- package/dist/config-service.d.ts +49 -0
- package/dist/config-service.js +83 -0
- package/dist/configuration.d.ts +4 -0
- package/dist/configuration.js +42 -0
- package/dist/confirmation.d.ts +14 -0
- package/dist/confirmation.js +26 -0
- package/dist/contracts.d.ts +27 -0
- package/dist/contracts.js +3 -0
- package/dist/contribution-origin.d.ts +3 -0
- package/dist/contribution-origin.js +13 -0
- package/dist/contributions/command.d.ts +8 -0
- package/dist/contributions/command.js +50 -0
- package/dist/contributions/dialog.d.ts +8 -0
- package/dist/contributions/dialog.js +18 -0
- package/dist/contributions/form.d.ts +10 -0
- package/dist/contributions/form.js +67 -0
- package/dist/contributions/host.d.ts +40 -0
- package/dist/contributions/host.js +1 -0
- package/dist/contributions/index.d.ts +26 -0
- package/dist/contributions/index.js +18 -0
- package/dist/contributions/native.d.ts +11 -0
- package/dist/contributions/native.js +87 -0
- package/dist/contributions/shared.d.ts +19 -0
- package/dist/contributions/shared.js +48 -0
- package/dist/contributions/stateful-action.d.ts +29 -0
- package/dist/contributions/stateful-action.js +33 -0
- package/dist/conventions.d.ts +4 -0
- package/dist/conventions.js +38 -0
- package/dist/core.d.ts +21 -0
- package/dist/core.js +18 -0
- package/dist/definitions.d.ts +356 -0
- package/dist/definitions.js +60 -0
- package/dist/discovered.d.ts +49 -0
- package/dist/discovered.js +120 -0
- package/dist/interceptors/service-observers.d.ts +3 -0
- package/dist/interceptors/service-observers.js +80 -0
- package/dist/interceptors/service.d.ts +37 -0
- package/dist/interceptors/service.js +7 -0
- package/dist/messages.d.ts +9 -0
- package/dist/messages.js +21 -0
- package/dist/metadata.d.ts +3 -0
- package/dist/metadata.js +13 -0
- package/dist/pages.d.ts +8 -0
- package/dist/pages.js +22 -0
- package/dist/presentation.d.ts +4 -0
- package/dist/presentation.js +1 -0
- package/dist/replacements.d.ts +22 -0
- package/dist/replacements.js +54 -0
- package/dist/runtime.d.ts +88 -0
- package/dist/runtime.js +445 -0
- package/dist/scope.d.ts +34 -0
- package/dist/scope.js +78 -0
- package/dist/service-key.d.ts +13 -0
- package/dist/service-key.js +14 -0
- package/dist/services.d.ts +6 -0
- package/dist/services.js +53 -0
- package/dist/translation.d.ts +13 -0
- package/dist/translation.js +43 -0
- package/dist/validation.d.ts +2 -0
- package/dist/validation.js +16 -0
- package/docs/file-contributions.md +161 -0
- package/package.json +48 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { statefulAction } from './contributions/stateful-action.js';
|
|
2
|
+
import { URLAction } from '@bitakit/core/actions';
|
|
3
|
+
export function defineService(definition) {
|
|
4
|
+
return definition;
|
|
5
|
+
}
|
|
6
|
+
export function defineInterceptor(definition) {
|
|
7
|
+
return definition;
|
|
8
|
+
}
|
|
9
|
+
/** Explicit Action discriminator; the legacy defineInterceptor helper remains supported. */
|
|
10
|
+
export function defineActionInterceptor(definition) {
|
|
11
|
+
return { ...definition, kind: 'action' };
|
|
12
|
+
}
|
|
13
|
+
export function defineProvider(definition) {
|
|
14
|
+
return definition;
|
|
15
|
+
}
|
|
16
|
+
export function defineFormAction(definition) {
|
|
17
|
+
return { ...definition, kind: 'form' };
|
|
18
|
+
}
|
|
19
|
+
export function defineDialogAction(definition) {
|
|
20
|
+
return { ...definition, kind: 'dialog' };
|
|
21
|
+
}
|
|
22
|
+
/** Navigation retains native link semantics and programmatic URLAction execution. */
|
|
23
|
+
export function defineURLAction(definition) {
|
|
24
|
+
return {
|
|
25
|
+
...definition,
|
|
26
|
+
kind: 'native',
|
|
27
|
+
load: () => new URLAction({ href: definition.href, title: definition.title ?? '' }),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function defineAction(definition) {
|
|
31
|
+
if (definition === undefined)
|
|
32
|
+
return statefulAction();
|
|
33
|
+
const { unavailable, ...action } = definition;
|
|
34
|
+
if (unavailable &&
|
|
35
|
+
action.metadata != null &&
|
|
36
|
+
(typeof action.metadata !== 'object' || Array.isArray(action.metadata)))
|
|
37
|
+
throw new Error('Unavailable messages require object metadata.');
|
|
38
|
+
return {
|
|
39
|
+
...action,
|
|
40
|
+
kind: 'action',
|
|
41
|
+
metadata: unavailable
|
|
42
|
+
? {
|
|
43
|
+
...action.metadata,
|
|
44
|
+
foundationUnavailableKey: unavailable.messageKey,
|
|
45
|
+
}
|
|
46
|
+
: action.metadata,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export function defineSetup(setup) {
|
|
50
|
+
return setup;
|
|
51
|
+
}
|
|
52
|
+
export function defineView(view) {
|
|
53
|
+
return view;
|
|
54
|
+
}
|
|
55
|
+
export function definePage(page) {
|
|
56
|
+
return page;
|
|
57
|
+
}
|
|
58
|
+
export function definePlugin(plugin) {
|
|
59
|
+
return plugin;
|
|
60
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type Contribution, type ServiceDefinition, type InterceptorEntry, type ActionEntry } from './definitions.js';
|
|
2
|
+
import type { PublicServiceKeys } from './app/types.js';
|
|
3
|
+
import { type ConfigValues, type ConfigDefinition } from './config-service.js';
|
|
4
|
+
export type DiscoveredFile<T = unknown, P extends string = string> = {
|
|
5
|
+
value: T;
|
|
6
|
+
path: P;
|
|
7
|
+
source: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function discoveredFile<T, const P extends string>(value: T, path: P, source: string): DiscoveredFile<T, P>;
|
|
10
|
+
export declare function discoverActions(files: readonly DiscoveredFile[], moduleId?: string, conventions?: boolean): Contribution[];
|
|
11
|
+
export declare function discoverServices(files: readonly DiscoveredFile[], moduleId?: string): ServiceDefinition[];
|
|
12
|
+
/** Public names follow explicit IDs or local file paths; storage IDs retain their module scope. */
|
|
13
|
+
export declare function discoverPublicServices(files: readonly DiscoveredFile[], moduleId?: string): PublicServiceKeys;
|
|
14
|
+
export declare function discoverInterceptors(files: readonly DiscoveredFile[]): InterceptorEntry[];
|
|
15
|
+
/** File assembly rejects overlapping leaves; intentional overrides happen later in ConfigService. */
|
|
16
|
+
export declare function discoverConfig(files: readonly DiscoveredFile[]): ConfigValues | ConfigDefinition;
|
|
17
|
+
type Join<A extends string, B extends string> = A extends '' ? B : B extends '' ? A : `${A}.${B}`;
|
|
18
|
+
type UnionToIntersection<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never;
|
|
19
|
+
type ActionTree<T, P extends string> = T extends (...args: any[]) => any ? {} : T extends abstract new (...args: any[]) => any ? {} : T extends {
|
|
20
|
+
kind: string;
|
|
21
|
+
} ? ActionEntry<T, P> : T extends {
|
|
22
|
+
execute: (...args: any[]) => any;
|
|
23
|
+
} ? ActionEntry<T & {
|
|
24
|
+
kind: 'action';
|
|
25
|
+
}, P> : T extends object ? UnionToIntersection<{
|
|
26
|
+
[K in keyof T & string]: ActionTree<T[K], Join<P, K>>;
|
|
27
|
+
}[keyof T & string]> : {};
|
|
28
|
+
export type DiscoveredActionEntry<F, Module extends string = '', Conventions extends boolean = true> = F extends DiscoveredFile<infer T, infer P> ? ActionTree<T, Conventions extends false ? never : Join<Module, P>> : {};
|
|
29
|
+
type ServiceTree<T, P extends string> = T extends {
|
|
30
|
+
create: (...args: any[]) => infer R;
|
|
31
|
+
} ? {
|
|
32
|
+
[K in T extends {
|
|
33
|
+
id: infer ID extends string;
|
|
34
|
+
} ? ID : P]: R;
|
|
35
|
+
} : T extends object ? UnionToIntersection<{
|
|
36
|
+
[K in keyof T & string]: ServiceTree<T[K], Join<P, K>>;
|
|
37
|
+
}[keyof T & string]> : {};
|
|
38
|
+
export type DiscoveredServiceEntry<F, Module extends string = ''> = F extends DiscoveredFile<infer T, infer P> ? ServiceTree<T, Join<Module, P>> : {};
|
|
39
|
+
export type DiscoveredPublicServiceEntry<F> = DiscoveredServiceEntry<F>;
|
|
40
|
+
type ConfigOutput<T> = T extends ConfigDefinition<infer V> ? V : T extends readonly unknown[] ? T : T extends object ? {
|
|
41
|
+
[K in keyof T]: ConfigOutput<T[K]>;
|
|
42
|
+
} : T;
|
|
43
|
+
type ConfigTree<T, P extends string> = P extends '' ? ConfigOutput<T> : P extends `${infer Head}.${infer Tail}` ? {
|
|
44
|
+
[K in Head]: ConfigTree<T, Tail>;
|
|
45
|
+
} : {
|
|
46
|
+
[K in P]: ConfigOutput<T>;
|
|
47
|
+
};
|
|
48
|
+
export type DiscoveredConfigEntry<F> = F extends DiscoveredFile<infer T, infer P> ? ConfigTree<T, P> : {};
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { setContributionPath } from './contribution-origin.js';
|
|
2
|
+
import { defineAction, } from './definitions.js';
|
|
3
|
+
import { resolveContribution } from './conventions.js';
|
|
4
|
+
import { serviceKey } from './service-key.js';
|
|
5
|
+
import { isConfigDefinition, isConfigObject, } from './config-service.js';
|
|
6
|
+
export function discoveredFile(value, path, source) {
|
|
7
|
+
return { value, path, source };
|
|
8
|
+
}
|
|
9
|
+
function expand(files, leaf, arrays = false) {
|
|
10
|
+
const results = [];
|
|
11
|
+
const seen = new Map();
|
|
12
|
+
function visit(value, segments, source) {
|
|
13
|
+
if (leaf(value)) {
|
|
14
|
+
const key = segments.join('.');
|
|
15
|
+
if (!key)
|
|
16
|
+
throw new Error(`Collection needs named entries: ${source}`);
|
|
17
|
+
if (seen.has(key))
|
|
18
|
+
throw new Error(`Duplicate contribution "${key}": ${seen.get(key)} and ${source}`);
|
|
19
|
+
seen.set(key, source);
|
|
20
|
+
results.push({ value, path: segments, source });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (arrays && Array.isArray(value)) {
|
|
24
|
+
value.forEach((entry, index) => visit(entry, [...segments, String(index)], source));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!isConfigObject(value))
|
|
28
|
+
throw new Error(`Invalid contribution in ${source}`);
|
|
29
|
+
for (const [key, entry] of Object.entries(value))
|
|
30
|
+
visit(entry, [...segments, ...key.split('.')], source);
|
|
31
|
+
}
|
|
32
|
+
for (const file of files)
|
|
33
|
+
visit(file.value, file.path ? file.path.split('.') : [], file.source);
|
|
34
|
+
return results;
|
|
35
|
+
}
|
|
36
|
+
export function discoverActions(files, moduleId = '', conventions = true) {
|
|
37
|
+
return expand(files, (value) => typeof value === 'function' ||
|
|
38
|
+
(!!value &&
|
|
39
|
+
typeof value === 'object' &&
|
|
40
|
+
('kind' in value || typeof value.execute === 'function'))).map(({ value, path }) => {
|
|
41
|
+
const definition = typeof value === 'object' && !value.kind ? defineAction(value) : value;
|
|
42
|
+
const resolved = resolveContribution(definition, [moduleId, ...path].filter(Boolean).join('.'), conventions);
|
|
43
|
+
setContributionPath(resolved, path);
|
|
44
|
+
return resolved;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export function discoverServices(files, moduleId = '') {
|
|
48
|
+
return expand(files, (value) => !!value && typeof value.create === 'function').map(({ value, path }) => ({
|
|
49
|
+
...value,
|
|
50
|
+
id: value.id ?? [moduleId, ...path].filter(Boolean).join('.'),
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
/** Public names follow explicit IDs or local file paths; storage IDs retain their module scope. */
|
|
54
|
+
export function discoverPublicServices(files, moduleId = '') {
|
|
55
|
+
const aliases = Object.create(null);
|
|
56
|
+
const sources = new Map();
|
|
57
|
+
for (const { value, path, source } of expand(files, (value) => !!value && typeof value.create === 'function')) {
|
|
58
|
+
const name = value.id ?? path.join('.');
|
|
59
|
+
if (sources.has(name))
|
|
60
|
+
throw new Error(`Duplicate public service "${name}": ${sources.get(name)} and ${source}`);
|
|
61
|
+
sources.set(name, source);
|
|
62
|
+
aliases[name] = serviceKey(value.id ?? [moduleId, ...path].filter(Boolean).join('.'));
|
|
63
|
+
}
|
|
64
|
+
return aliases;
|
|
65
|
+
}
|
|
66
|
+
export function discoverInterceptors(files) {
|
|
67
|
+
return expand(files, (value) => typeof value === 'function' ||
|
|
68
|
+
(!!value &&
|
|
69
|
+
(value.kind === 'service' ||
|
|
70
|
+
typeof value.create === 'function' ||
|
|
71
|
+
typeof value.before === 'function')), true).map(({ value }) => (typeof value === 'function' ? { before: value } : value));
|
|
72
|
+
}
|
|
73
|
+
/** File assembly rejects overlapping leaves; intentional overrides happen later in ConfigService. */
|
|
74
|
+
export function discoverConfig(files) {
|
|
75
|
+
const schemaRoot = files.find((file) => !file.path && isConfigDefinition(file.value));
|
|
76
|
+
if (schemaRoot) {
|
|
77
|
+
if (files.length !== 1)
|
|
78
|
+
throw new Error(`Root config schema must own the complete config: ${schemaRoot.source}`);
|
|
79
|
+
return schemaRoot.value;
|
|
80
|
+
}
|
|
81
|
+
const root = Object.create(null);
|
|
82
|
+
const sources = new Map();
|
|
83
|
+
function insert(value, segments, source) {
|
|
84
|
+
if (isConfigObject(value) &&
|
|
85
|
+
!isConfigDefinition(value) &&
|
|
86
|
+
(Object.keys(value).length || !segments.length)) {
|
|
87
|
+
for (const [key, item] of Object.entries(value))
|
|
88
|
+
insert(item, [...segments, ...key.split('.')], source);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (!segments.length)
|
|
92
|
+
throw new Error(`Root config must be an object: ${source}`);
|
|
93
|
+
let target = root;
|
|
94
|
+
for (let i = 0; i < segments.length; i++) {
|
|
95
|
+
const key = segments[i];
|
|
96
|
+
const prefix = segments.slice(0, i + 1).join('.');
|
|
97
|
+
if (Object.hasOwn(target, key) &&
|
|
98
|
+
(i === segments.length - 1 ||
|
|
99
|
+
!isConfigObject(target[key]) ||
|
|
100
|
+
isConfigDefinition(target[key])))
|
|
101
|
+
throw new Error(`Duplicate config path "${prefix}": ${sources.get(prefix)} and ${source}`);
|
|
102
|
+
sources.set(prefix, sources.get(prefix) ?? source);
|
|
103
|
+
if (i === segments.length - 1)
|
|
104
|
+
Object.defineProperty(target, key, {
|
|
105
|
+
value,
|
|
106
|
+
enumerable: true,
|
|
107
|
+
configurable: true,
|
|
108
|
+
writable: true,
|
|
109
|
+
});
|
|
110
|
+
else {
|
|
111
|
+
if (!Object.hasOwn(target, key))
|
|
112
|
+
Object.defineProperty(target, key, { value: Object.create(null), enumerable: true });
|
|
113
|
+
target = target[key];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const file of files)
|
|
118
|
+
insert(file.value, file.path ? file.path.split('.') : [], file.source);
|
|
119
|
+
return root;
|
|
120
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Plugin } from '../definitions.js';
|
|
2
|
+
/** Creates a runtime-local projection; all container consumers receive the same service wrapper. */
|
|
3
|
+
export declare function serviceObserver(modules: readonly Plugin[]): (id: string, instance: any, active: () => boolean) => any;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { isServiceInterceptor } from './service.js';
|
|
2
|
+
/** Observer errors are isolated from business execution, as with Action lifecycle observers. */
|
|
3
|
+
function notify(callback, event) {
|
|
4
|
+
try {
|
|
5
|
+
const result = callback?.(Object.freeze(event));
|
|
6
|
+
if (result && typeof result.then === 'function')
|
|
7
|
+
void Promise.resolve(result).catch(() => { });
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
/* Observation must not change the service outcome. */
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Creates a runtime-local projection; all container consumers receive the same service wrapper. */
|
|
14
|
+
export function serviceObserver(modules) {
|
|
15
|
+
const aliases = new Map(modules.flatMap((module) => Object.entries(module.publicServices ?? {})));
|
|
16
|
+
const entries = modules
|
|
17
|
+
.flatMap((module) => (module.interceptors ?? []).filter(isServiceInterceptor))
|
|
18
|
+
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
19
|
+
return (id, instance, active) => {
|
|
20
|
+
const selected = entries.filter((entry) => (aliases.get(entry.service) ?? entry.service) === id);
|
|
21
|
+
if (!selected.length)
|
|
22
|
+
return instance;
|
|
23
|
+
const cache = new Map();
|
|
24
|
+
// A forwarding shell also supports frozen service objects without mutating their methods.
|
|
25
|
+
return new Proxy(Object.create(Object.getPrototypeOf(instance)), {
|
|
26
|
+
has: (_target, key) => key in instance,
|
|
27
|
+
ownKeys: () => Reflect.ownKeys(instance),
|
|
28
|
+
getOwnPropertyDescriptor: (_target, key) => {
|
|
29
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(instance, key);
|
|
30
|
+
return descriptor ? { ...descriptor, configurable: true } : undefined;
|
|
31
|
+
},
|
|
32
|
+
set: (_target, key, value) => Reflect.set(instance, key, value, instance),
|
|
33
|
+
defineProperty: () => false,
|
|
34
|
+
deleteProperty: (_target, key) => Reflect.deleteProperty(instance, key),
|
|
35
|
+
preventExtensions: () => false,
|
|
36
|
+
get(_target, key) {
|
|
37
|
+
const target = instance;
|
|
38
|
+
const value = Reflect.get(target, key, target);
|
|
39
|
+
if (typeof value !== 'function' || key === 'constructor')
|
|
40
|
+
return value;
|
|
41
|
+
if (cache.get(key)?.original === value)
|
|
42
|
+
return cache.get(key).wrapped;
|
|
43
|
+
const observers = typeof key === 'string'
|
|
44
|
+
? selected.filter((entry) => entry.method === undefined || entry.method === key)
|
|
45
|
+
: [];
|
|
46
|
+
const wrapped = (...args) => {
|
|
47
|
+
const emit = (phase, extra = {}) => {
|
|
48
|
+
if (!active())
|
|
49
|
+
return;
|
|
50
|
+
for (const entry of observers)
|
|
51
|
+
notify(entry[phase], {
|
|
52
|
+
service: entry.service,
|
|
53
|
+
method: key,
|
|
54
|
+
args: Object.freeze([...args]),
|
|
55
|
+
...extra,
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
emit('before');
|
|
59
|
+
let result;
|
|
60
|
+
try {
|
|
61
|
+
result = Reflect.apply(value, target, args);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
emit('onError', { error });
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
if (result && typeof result.then === 'function') {
|
|
68
|
+
// Observe settlement without replacing the original Promise or changing sync methods.
|
|
69
|
+
void Promise.resolve(result).then((value) => emit('after', { result: value }), (error) => emit('onError', { error }));
|
|
70
|
+
}
|
|
71
|
+
else
|
|
72
|
+
emit('after', { result });
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
75
|
+
cache.set(key, { original: value, wrapped });
|
|
76
|
+
return wrapped;
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PublicServiceMap } from '../app/types.js';
|
|
2
|
+
import type { ServiceMap } from '../definitions.js';
|
|
3
|
+
type Contracts = PublicServiceMap & ServiceMap;
|
|
4
|
+
type Methods<S> = {
|
|
5
|
+
[K in keyof S & string]: S[K] extends (...args: any[]) => any ? K : never;
|
|
6
|
+
}[keyof S & string];
|
|
7
|
+
type Service<Name extends string> = Name extends keyof Contracts ? NonNullable<Contracts[Name]> : Record<string, (...args: any[]) => unknown>;
|
|
8
|
+
type Method<Name extends string, Key extends string> = Key extends keyof Service<Name> ? Extract<Service<Name>[Key], (...args: any[]) => any> : (...args: any[]) => unknown;
|
|
9
|
+
type Call<Name extends string, Key extends string> = {
|
|
10
|
+
readonly service: Name;
|
|
11
|
+
readonly method: Key;
|
|
12
|
+
readonly args: Readonly<Parameters<Method<Name, Key>>>;
|
|
13
|
+
};
|
|
14
|
+
export type ServiceInterceptor = {
|
|
15
|
+
readonly kind: 'service';
|
|
16
|
+
readonly service: string;
|
|
17
|
+
readonly method?: string;
|
|
18
|
+
readonly order?: number;
|
|
19
|
+
before?: (call: any) => void;
|
|
20
|
+
after?: (call: any) => void;
|
|
21
|
+
onError?: (call: any) => void;
|
|
22
|
+
};
|
|
23
|
+
/** Observes one method or all public methods; callbacks never replace arguments or results. */
|
|
24
|
+
export declare function defineServiceInterceptor<const Name extends string, const Key extends Methods<Service<Name>> = Methods<Service<Name>>>(definition: {
|
|
25
|
+
service: Name;
|
|
26
|
+
method?: Key;
|
|
27
|
+
order?: number;
|
|
28
|
+
before?: (call: Call<Name, Key>) => void;
|
|
29
|
+
after?: (call: Call<Name, Key> & {
|
|
30
|
+
readonly result: Awaited<ReturnType<Method<Name, Key>>>;
|
|
31
|
+
}) => void;
|
|
32
|
+
onError?: (call: Call<Name, Key> & {
|
|
33
|
+
readonly error: unknown;
|
|
34
|
+
}) => void;
|
|
35
|
+
}): ServiceInterceptor;
|
|
36
|
+
export declare function isServiceInterceptor(value: object): value is ServiceInterceptor;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Observes one method or all public methods; callbacks never replace arguments or results. */
|
|
2
|
+
export function defineServiceInterceptor(definition) {
|
|
3
|
+
return { ...definition, kind: 'service' };
|
|
4
|
+
}
|
|
5
|
+
export function isServiceInterceptor(value) {
|
|
6
|
+
return 'kind' in value && value.kind === 'service';
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type Messages = {
|
|
2
|
+
[key: string]: string | Messages;
|
|
3
|
+
};
|
|
4
|
+
export declare function mergePluginMessages({ app, plugins, locale, fallbackLocale, }: {
|
|
5
|
+
app: Record<string, Messages>;
|
|
6
|
+
plugins: Record<string, Messages>;
|
|
7
|
+
locale: string;
|
|
8
|
+
fallbackLocale?: string;
|
|
9
|
+
}): Messages;
|
package/dist/messages.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Per-key overlay: no mutation, no cross-plugin namespace search. */
|
|
2
|
+
function merge(base, override) {
|
|
3
|
+
const result = { ...base };
|
|
4
|
+
for (const [key, value] of Object.entries(override)) {
|
|
5
|
+
if (['__proto__', 'prototype', 'constructor'].includes(key))
|
|
6
|
+
throw new Error('Invalid translation key.');
|
|
7
|
+
result[key] =
|
|
8
|
+
typeof value === 'string'
|
|
9
|
+
? value
|
|
10
|
+
: merge(typeof result[key] === 'object' ? result[key] : {}, value);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
export function mergePluginMessages({ app, plugins, locale, fallbackLocale = 'en', }) {
|
|
15
|
+
return [
|
|
16
|
+
plugins[fallbackLocale],
|
|
17
|
+
app[fallbackLocale],
|
|
18
|
+
plugins[locale],
|
|
19
|
+
app[locale],
|
|
20
|
+
].reduce((messages, layer) => merge(messages, layer ?? {}), {});
|
|
21
|
+
}
|
package/dist/metadata.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Copy public package fields only; never bundle scripts, config or arbitrary manifest data. */
|
|
2
|
+
export function pluginMetadata(source) {
|
|
3
|
+
const { name, version, description, author, homepage, bugs, license } = source;
|
|
4
|
+
return {
|
|
5
|
+
...(name === undefined ? {} : { name }),
|
|
6
|
+
...(version === undefined ? {} : { version }),
|
|
7
|
+
...(description === undefined ? {} : { description }),
|
|
8
|
+
...(author === undefined ? {} : { author }),
|
|
9
|
+
...(homepage === undefined ? {} : { homepage }),
|
|
10
|
+
...(bugs === undefined ? {} : { bugs }),
|
|
11
|
+
...(license === undefined ? {} : { license }),
|
|
12
|
+
};
|
|
13
|
+
}
|
package/dist/pages.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Plugin } from './definitions.js';
|
|
2
|
+
/** Exact, canonical paths only. Routers remain responsible for dynamic parameters. */
|
|
3
|
+
export declare function validatePluginPages(plugins: readonly Plugin[]): void;
|
|
4
|
+
export declare function findPluginPage(plugins: readonly Plugin[], pathname: string): {
|
|
5
|
+
key: string;
|
|
6
|
+
path: string;
|
|
7
|
+
component: import("./presentation.js").PresentationType<"component">;
|
|
8
|
+
} | undefined;
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Exact, canonical paths only. Routers remain responsible for dynamic parameters. */
|
|
2
|
+
export function validatePluginPages(plugins) {
|
|
3
|
+
const paths = new Set();
|
|
4
|
+
for (const plugin of plugins) {
|
|
5
|
+
for (const page of plugin.pages ?? []) {
|
|
6
|
+
if (!/^\/(?:[a-zA-Z0-9_-]+(?:\/[a-zA-Z0-9_-]+)*)?$/.test(page.path))
|
|
7
|
+
throw new Error(`Invalid plugin page path "${page.path}" in "${plugin.id}".`);
|
|
8
|
+
if (paths.has(page.path))
|
|
9
|
+
throw new Error(`Duplicate plugin page path "${page.path}".`);
|
|
10
|
+
paths.add(page.path);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function findPluginPage(plugins, pathname) {
|
|
15
|
+
const path = pathname.length > 1 ? pathname.replace(/\/$/, '') : pathname;
|
|
16
|
+
for (const plugin of plugins) {
|
|
17
|
+
const page = plugin.pages?.find((entry) => entry.path === path);
|
|
18
|
+
if (page)
|
|
19
|
+
return { ...page, key: `${plugin.id}:${page.path}` };
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Contribution, Plugin } from './definitions.js';
|
|
2
|
+
/** Replace only a named plugin contribution, retaining its public ID and placements. */
|
|
3
|
+
export declare function applyActionReplacements(plugins: readonly Plugin[], appActions: readonly Contribution[]): {
|
|
4
|
+
plugins: {
|
|
5
|
+
actions: (import("./definitions.js").FormDefinition<any, any> | import("./definitions.js").DialogDefinition<any> | import("./definitions.js").NativeDefinition | import("./definitions.js").PlainDefinition<any, any> | (new (context: import("./definitions.js").ActionEnvironment) => import("./core.js").Action<any, any>) | ((context: import("./definitions.js").ActionEnvironment) => import("./core.js").Action<any, any>))[];
|
|
6
|
+
publicServices?: import("./core.js").PublicServiceKeys;
|
|
7
|
+
appExtensions?: Readonly<Record<string, (runtime: import("./runtime.js").FoundationRuntime) => unknown>>;
|
|
8
|
+
config?: import("./config-service.js").ConfigValues | import("./config-service.js").ConfigDefinition;
|
|
9
|
+
interceptors?: readonly import("./definitions.js").InterceptorEntry[];
|
|
10
|
+
providers?: readonly import("./definitions.js").ProviderDefinition[];
|
|
11
|
+
metadata?: import("./definitions.js").PluginMetadata;
|
|
12
|
+
pages?: readonly import("./definitions.js").PluginPageDefinition[];
|
|
13
|
+
views?: readonly import("./definitions.js").PluginView[];
|
|
14
|
+
id: string;
|
|
15
|
+
dependencies?: readonly string[];
|
|
16
|
+
services?: readonly import("./definitions.js").ServiceDefinition<any>[];
|
|
17
|
+
setup?: import("./definitions.js").Setup;
|
|
18
|
+
contributions?: import("./definitions.js").ContributionSet;
|
|
19
|
+
installs?: import("./definitions.js").ContributionInstallers;
|
|
20
|
+
}[];
|
|
21
|
+
actions: Contribution[];
|
|
22
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { preserveContributionPath } from './contribution-origin.js';
|
|
2
|
+
import { resolveContribution } from './conventions.js';
|
|
3
|
+
/** Replace only a named plugin contribution, retaining its public ID and placements. */
|
|
4
|
+
export function applyActionReplacements(plugins, appActions) {
|
|
5
|
+
const replacements = new Map();
|
|
6
|
+
const ownActions = [];
|
|
7
|
+
for (const action of appActions) {
|
|
8
|
+
const definition = resolveContribution(action);
|
|
9
|
+
if (!definition.replaces) {
|
|
10
|
+
ownActions.push(action);
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (replacements.has(definition.replaces))
|
|
14
|
+
throw new Error(`Multiple app replacements for action "${definition.replaces}".`);
|
|
15
|
+
replacements.set(definition.replaces, definition);
|
|
16
|
+
}
|
|
17
|
+
const targets = new Map();
|
|
18
|
+
for (const plugin of plugins)
|
|
19
|
+
for (const action of plugin.actions ?? []) {
|
|
20
|
+
const definition = resolveContribution(action);
|
|
21
|
+
if (definition.replaces)
|
|
22
|
+
throw new Error('Only app actions may replace plugin actions.');
|
|
23
|
+
// Bare native exports can establish their ID at construction; registration validates those.
|
|
24
|
+
if (!definition.id)
|
|
25
|
+
continue;
|
|
26
|
+
if (targets.has(definition.id))
|
|
27
|
+
throw new Error(`Duplicate plugin action "${definition.id}".`);
|
|
28
|
+
targets.set(definition.id, definition);
|
|
29
|
+
}
|
|
30
|
+
for (const id of replacements.keys()) {
|
|
31
|
+
if (!targets.has(id))
|
|
32
|
+
throw new Error(`Cannot replace missing plugin action "${id}". The target needs a discovery or explicit definition ID.`);
|
|
33
|
+
}
|
|
34
|
+
const resolvedPlugins = plugins.map((plugin) => ({
|
|
35
|
+
...plugin,
|
|
36
|
+
actions: (plugin.actions ?? []).map((action) => {
|
|
37
|
+
const original = resolveContribution(action);
|
|
38
|
+
const replacement = replacements.get(original.id);
|
|
39
|
+
if (!replacement)
|
|
40
|
+
return action;
|
|
41
|
+
return preserveContributionPath(original, {
|
|
42
|
+
...replacement,
|
|
43
|
+
id: original.id,
|
|
44
|
+
groups: replacement.groups ?? original.groups,
|
|
45
|
+
order: replacement.order ?? original.order,
|
|
46
|
+
title: replacement.title ?? original.title,
|
|
47
|
+
description: replacement.description ?? original.description,
|
|
48
|
+
icon: replacement.icon ?? original.icon,
|
|
49
|
+
replaces: undefined,
|
|
50
|
+
});
|
|
51
|
+
}),
|
|
52
|
+
}));
|
|
53
|
+
return { plugins: resolvedPlugins, actions: ownActions };
|
|
54
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ConfigService } from './config-service.js';
|
|
2
|
+
import { ActionGroup, type ActionList } from '@bitakit/core/actions';
|
|
3
|
+
import type { ContributionMap, ResolvedContribution, ResolvedForm, Field, FoundationConfig, Plugin, Services, Translator } from './definitions.js';
|
|
4
|
+
import type { FoundationApp } from './app/types.js';
|
|
5
|
+
import { type Feedback, type MessageValues } from './actions/feedback.js';
|
|
6
|
+
import { type ContributionHost, type DialogState, type FeedbackOutcome, type OpenableDefinition, type Override } from './contributions/index.js';
|
|
7
|
+
export type { DialogState } from './contributions/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Mounts plugins into the host's ActionList. Each contribution kind is normalized by an adapter
|
|
10
|
+
* in `./contributions`; the runtime owns services, translation, dialogs, ordering and cleanup.
|
|
11
|
+
*/
|
|
12
|
+
export declare class FoundationRuntime implements ContributionHost {
|
|
13
|
+
readonly actions: ActionList;
|
|
14
|
+
readonly config: FoundationConfig;
|
|
15
|
+
private providedServices;
|
|
16
|
+
readonly app: FoundationApp;
|
|
17
|
+
readonly configuration: ConfigService;
|
|
18
|
+
readonly modules: readonly Plugin[];
|
|
19
|
+
private readonly actionConfigs;
|
|
20
|
+
private ready;
|
|
21
|
+
private readinessListeners;
|
|
22
|
+
isReady: () => boolean;
|
|
23
|
+
subscribeReady: (listener: () => void) => () => void;
|
|
24
|
+
private setReady;
|
|
25
|
+
private listeners;
|
|
26
|
+
private state;
|
|
27
|
+
private registrations;
|
|
28
|
+
private scopes;
|
|
29
|
+
private extensionCleanups;
|
|
30
|
+
private container?;
|
|
31
|
+
private translate;
|
|
32
|
+
private translationRevision;
|
|
33
|
+
private translationListeners;
|
|
34
|
+
getTranslationRevision: () => number;
|
|
35
|
+
subscribeTranslations: (listener: () => void) => () => void;
|
|
36
|
+
private notifier?;
|
|
37
|
+
private formFeedback;
|
|
38
|
+
readonly groups: Map<string, ActionGroup>;
|
|
39
|
+
constructor(actions: ActionList, config: FoundationConfig, providedServices?: Partial<Services>);
|
|
40
|
+
private get appId();
|
|
41
|
+
private order?;
|
|
42
|
+
/** Modules in install order: dependencies first, the application module included. */
|
|
43
|
+
private moduleOrder;
|
|
44
|
+
/**
|
|
45
|
+
* Declarative contributions of one kind in install order. Pure and available before mount, so
|
|
46
|
+
* UI integrations can derive an initial render snapshot equivalent to the mounted state.
|
|
47
|
+
*/
|
|
48
|
+
contributionsOf<K extends string>(kind: K): readonly {
|
|
49
|
+
module: string;
|
|
50
|
+
app: boolean;
|
|
51
|
+
values: readonly (K extends keyof ContributionMap ? ContributionMap[K] : unknown)[];
|
|
52
|
+
}[];
|
|
53
|
+
getGroup: (entry: string | ActionGroup) => ActionGroup<unknown>;
|
|
54
|
+
get services(): Services;
|
|
55
|
+
getSnapshot: () => DialogState | null;
|
|
56
|
+
getServerSnapshot: () => null;
|
|
57
|
+
subscribe: (listener: () => void) => () => void;
|
|
58
|
+
private publish;
|
|
59
|
+
t: (key: string) => string;
|
|
60
|
+
text(key: string | undefined, fallback: string, values?: MessageValues): string;
|
|
61
|
+
title(definition: ResolvedContribution): string;
|
|
62
|
+
description(definition: ResolvedContribution): string | undefined;
|
|
63
|
+
fieldLabel(definition: ResolvedForm, name: string, field: Field): string;
|
|
64
|
+
/** Installs one presentation sink; disposing an older owner cannot clear its replacement. */
|
|
65
|
+
setNotifier(notify: (message: string, kind: 'success' | 'failure') => unknown): () => void;
|
|
66
|
+
/** Presentation failures, including rejected promises, never change the Action outcome. */
|
|
67
|
+
notify(message: string | false, kind?: 'success' | 'failure'): void;
|
|
68
|
+
translator(): Translator;
|
|
69
|
+
override(id: string): Override;
|
|
70
|
+
dialog(): DialogState | null;
|
|
71
|
+
openDialog(definition: OpenableDefinition, input: unknown): void;
|
|
72
|
+
rememberFormFeedback(state: DialogState, outcome: FeedbackOutcome): void;
|
|
73
|
+
feedback<C>(definition: ResolvedContribution, kind: 'success' | 'failure', option: Feedback<C>, context: C): FeedbackOutcome;
|
|
74
|
+
/** Public submission path; the internal submit registration owns validation and pending guards. */
|
|
75
|
+
submitForm(definition: ResolvedForm, values: Record<string, string>): Promise<void>;
|
|
76
|
+
setTranslations(translate: Translator): void;
|
|
77
|
+
close: () => void;
|
|
78
|
+
mount(): () => void;
|
|
79
|
+
/** Routes each contribution kind to the one module that installs it; cleanup stays with the contributor. */
|
|
80
|
+
private installContributions;
|
|
81
|
+
private connectTranslation;
|
|
82
|
+
private installInterceptors;
|
|
83
|
+
private requireServices;
|
|
84
|
+
/** One path for every kind: adapt, check dependencies, register, remember for re-translation. */
|
|
85
|
+
private install;
|
|
86
|
+
private observe;
|
|
87
|
+
private unmount;
|
|
88
|
+
}
|