@cullet/erp-core 1.1.0 → 1.3.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/KIT_CONTEXT.md +1 -67
- package/dist/app-error.d.cts +108 -0
- package/dist/application/index.cjs +26 -0
- package/dist/application/index.d.cts +2 -0
- package/dist/application/index.d.ts +1 -1
- package/dist/application/index.js +2 -1
- package/dist/domain/index.cjs +16 -0
- package/dist/domain/index.d.cts +3 -0
- package/dist/domain/index.d.ts +3 -0
- package/dist/domain/index.js +3 -0
- package/dist/domain-event-contracts.cjs +33 -0
- package/dist/domain-event-contracts.cjs.map +1 -0
- package/dist/domain-event-contracts.d.cts +27 -0
- package/dist/domain-event-contracts.d.ts +27 -0
- package/dist/domain-event-contracts.js +22 -0
- package/dist/domain-event-contracts.js.map +1 -0
- package/dist/domain-exception.cjs +17 -0
- package/dist/domain-exception.cjs.map +1 -0
- package/dist/domain-exception.d.cts +7 -0
- package/dist/domain-exception.d.ts +7 -0
- package/dist/domain-exception.js +12 -0
- package/dist/domain-exception.js.map +1 -0
- package/dist/errors/index.cjs +41 -0
- package/dist/errors/index.d.cts +3 -0
- package/dist/errors/index.js +1 -1
- package/dist/exceptions/index.cjs +17 -0
- package/dist/exceptions/index.d.cts +5 -0
- package/dist/exceptions/index.d.ts +5 -0
- package/dist/exceptions/index.js +7 -0
- package/dist/exceptions/validation-field.cjs +3 -0
- package/dist/exceptions/validation-field.d.cts +2 -0
- package/dist/gate-engine-registry.cjs +308 -0
- package/dist/gate-engine-registry.cjs.map +1 -0
- package/dist/gate-engine-registry.d.cts +81 -0
- package/dist/gate-engine-registry.d.ts +2 -1
- package/dist/gate-engine-registry.js +2 -1
- package/dist/gate-engine-registry.js.map +1 -1
- package/dist/gate-types.d.cts +171 -0
- package/dist/gate-types.d.ts +3 -139
- package/dist/gate-v1-payload.schema.cjs +638 -0
- package/dist/gate-v1-payload.schema.cjs.map +1 -0
- package/dist/gate-v1-payload.schema.js +2 -116
- package/dist/gate-v1-payload.schema.js.map +1 -1
- package/dist/hashing.cjs +66 -0
- package/dist/hashing.cjs.map +1 -0
- package/dist/immutable.cjs +77 -0
- package/dist/immutable.cjs.map +1 -0
- package/dist/immutable.d.cts +6 -0
- package/dist/immutable.d.ts +6 -0
- package/dist/immutable.js +54 -0
- package/dist/immutable.js.map +1 -0
- package/dist/index.cjs +163 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +15 -160
- package/dist/index.js +19 -176
- package/dist/index.js.map +1 -1
- package/dist/invalid-state-transition-exception.cjs +47 -0
- package/dist/invalid-state-transition-exception.cjs.map +1 -0
- package/dist/invalid-state-transition-exception.js +30 -0
- package/dist/invalid-state-transition-exception.js.map +1 -0
- package/dist/invariant-violation-exception.cjs +16 -0
- package/dist/invariant-violation-exception.cjs.map +1 -0
- package/dist/invariant-violation-exception.js +11 -0
- package/dist/invariant-violation-exception.js.map +1 -0
- package/dist/not-found-error.cjs +65 -0
- package/dist/not-found-error.cjs.map +1 -0
- package/dist/not-found-error.js +1 -1
- package/dist/outcome.cjs +97 -0
- package/dist/outcome.cjs.map +1 -0
- package/dist/outcome.d.cts +140 -0
- package/dist/outcome.d.ts +140 -0
- package/dist/outcome.js +1 -1
- package/dist/parse-gate-payload.d.cts +62 -0
- package/dist/parse-gate-payload.d.ts +2 -1
- package/dist/path.d.cts +90 -0
- package/dist/path.d.ts +2 -1
- package/dist/plugin.cjs +79 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.d.cts +85 -0
- package/dist/plugin.d.ts +85 -0
- package/dist/plugin.js +74 -0
- package/dist/plugin.js.map +1 -0
- package/dist/plugins/index.cjs +3 -0
- package/dist/plugins/index.d.cts +2 -0
- package/dist/plugins/index.d.ts +2 -0
- package/dist/plugins/index.js +2 -0
- package/dist/policies/engines/index.cjs +10 -0
- package/dist/policies/engines/index.d.cts +4 -0
- package/dist/policies/engines/index.d.ts +1 -1
- package/dist/policies/engines/v1/gate/index.cjs +91 -0
- package/dist/policies/engines/v1/gate/index.cjs.map +1 -0
- package/dist/policies/engines/v1/gate/index.d.cts +121 -0
- package/dist/policies/engines/v1/gate/index.d.ts +2 -1
- package/dist/policies/engines/v1/gate/index.js +2 -1
- package/dist/policies/engines/v1/gate/index.js.map +1 -1
- package/dist/policies/index.cjs +41 -0
- package/dist/policies/index.d.cts +7 -0
- package/dist/policies/index.d.ts +2 -1
- package/dist/policies/index.js +2 -1
- package/dist/policy-service.cjs +1190 -0
- package/dist/policy-service.cjs.map +1 -0
- package/dist/policy-service.d.cts +559 -0
- package/dist/policy-service.d.ts +2 -1
- package/dist/policy-service.js +6 -3
- package/dist/policy-service.js.map +1 -1
- package/dist/result/index.cjs +7 -0
- package/dist/result/index.d.cts +2 -0
- package/dist/result/index.d.ts +2 -0
- package/dist/result/index.js +3 -0
- package/dist/result.cjs +135 -0
- package/dist/result.cjs.map +1 -0
- package/dist/result.js +118 -0
- package/dist/result.js.map +1 -0
- package/dist/ruleset-registry.cjs +47 -0
- package/dist/ruleset-registry.cjs.map +1 -0
- package/dist/ruleset-registry.js +42 -0
- package/dist/ruleset-registry.js.map +1 -0
- package/dist/rulesets/index.cjs +3 -0
- package/dist/rulesets/index.d.cts +2 -0
- package/dist/rulesets/index.d.ts +2 -0
- package/dist/rulesets/index.js +2 -0
- package/dist/temporal-guards.cjs +32 -0
- package/dist/temporal-guards.cjs.map +1 -0
- package/dist/temporal-guards.js +2 -17
- package/dist/temporal-guards.js.map +1 -1
- package/dist/temporal-use-case.cjs +191 -0
- package/dist/temporal-use-case.cjs.map +1 -0
- package/dist/temporal-use-case.d.cts +304 -0
- package/dist/temporal-use-case.d.ts +4 -9
- package/dist/temporal-use-case.js +6 -140
- package/dist/temporal-use-case.js.map +1 -1
- package/dist/unexpected-error.cjs +208 -0
- package/dist/unexpected-error.cjs.map +1 -0
- package/dist/unexpected-error.js +179 -0
- package/dist/unexpected-error.js.map +1 -0
- package/dist/use-case.cjs +96 -0
- package/dist/use-case.cjs.map +1 -0
- package/dist/use-case.js +91 -0
- package/dist/use-case.js.map +1 -0
- package/dist/uuid-identifier.cjs +65 -0
- package/dist/uuid-identifier.cjs.map +1 -0
- package/dist/uuid-identifier.d.cts +230 -0
- package/dist/uuid-identifier.d.ts +230 -0
- package/dist/uuid-identifier.js +60 -0
- package/dist/uuid-identifier.js.map +1 -0
- package/dist/validation-code.cjs +60 -0
- package/dist/validation-code.cjs.map +1 -0
- package/dist/validation-code.d.cts +23 -0
- package/dist/validation-code.d.ts +23 -0
- package/dist/validation-code.js +1 -177
- package/dist/validation-code.js.map +1 -1
- package/dist/validation-error.cjs +1020 -0
- package/dist/validation-error.cjs.map +1 -0
- package/dist/validation-error.d.cts +777 -0
- package/dist/validation-error.d.ts +1 -21
- package/dist/validation-error.js +1 -1
- package/dist/validation-exception.cjs +41 -0
- package/dist/validation-exception.cjs.map +1 -0
- package/dist/validation-exception.d.cts +50 -0
- package/dist/validation-exception.d.ts +50 -0
- package/dist/validation-exception.js +8 -2
- package/dist/validation-exception.js.map +1 -1
- package/dist/validation-field.cjs +28 -0
- package/dist/validation-field.cjs.map +1 -0
- package/dist/validation-field.d.cts +12 -0
- package/dist/value-object-ruleset.contracts.d.cts +36 -0
- package/dist/value-object-ruleset.contracts.d.ts +36 -0
- package/dist/value-object.cjs +208 -0
- package/dist/value-object.cjs.map +1 -0
- package/dist/value-object.js +191 -0
- package/dist/value-object.js.map +1 -0
- package/dist/version.d.cts +10 -0
- package/dist/version.d.ts +10 -0
- package/dist/versioning/index.cjs +7 -0
- package/dist/versioning/index.d.cts +3 -0
- package/dist/versioning/index.d.ts +3 -0
- package/dist/versioning/index.js +3 -0
- package/meta.json +18 -3
- package/package.json +165 -17
- package/src/core/domain/rulesets/index.ts +7 -0
- package/src/core/domain/uuid-identifier.ts +72 -0
- package/src/core/domain/value-object.ts +37 -5
- package/src/core/exceptions/index.ts +13 -0
- package/src/core/index.ts +14 -2
- package/src/core/plugins/index.ts +7 -0
- package/src/core/plugins/plugin.ts +99 -0
- package/src/core/plugins/types.ts +53 -0
- package/src/core/versioning/index.ts +14 -0
- package/src/domain/index.ts +7 -0
- package/src/exceptions/index.ts +1 -0
- package/src/plugins/index.ts +1 -0
- package/src/result/index.ts +2 -0
- package/src/rulesets/index.ts +1 -0
- package/src/version.ts +1 -1
- package/src/versioning/index.ts +1 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
//#region src/core/result/result.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents the outcome of an operation that may succeed or fail.
|
|
4
|
+
* Useful for avoiding excessive exceptions and making error flows explicit.
|
|
5
|
+
*/
|
|
6
|
+
declare abstract class Result<T, E> {
|
|
7
|
+
protected constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Creates a success result containing a value.
|
|
10
|
+
*/
|
|
11
|
+
static ok<T>(value: T): Result<T, never>;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an error result containing an error object.
|
|
14
|
+
*/
|
|
15
|
+
static err<E>(error: E): Result<never, E>;
|
|
16
|
+
/**
|
|
17
|
+
* Returns true when this is a success result.
|
|
18
|
+
*/
|
|
19
|
+
abstract isOk(): this is Ok<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Returns true when this is an error result.
|
|
22
|
+
*/
|
|
23
|
+
abstract isErr(): this is Err<E>;
|
|
24
|
+
/**
|
|
25
|
+
* Runs a handler based on the result state (similar to pattern matching).
|
|
26
|
+
*/
|
|
27
|
+
abstract match<R>(handlers: {
|
|
28
|
+
ok: (value: T) => R;
|
|
29
|
+
err: (error: E) => R;
|
|
30
|
+
}): R;
|
|
31
|
+
/**
|
|
32
|
+
* Returns the value when this is success, or null when this is an error.
|
|
33
|
+
*/
|
|
34
|
+
getOrNull(): T | null;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the error when this is a failure, or null when this is a success.
|
|
37
|
+
*/
|
|
38
|
+
errorOrNull(): E | null;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the value when this is a success, or throws the error when this is a failure.
|
|
41
|
+
*/
|
|
42
|
+
getOrThrow(): T;
|
|
43
|
+
/**
|
|
44
|
+
* Transforms the success value using the provided function. Keeps the error when this is a failure.
|
|
45
|
+
*/
|
|
46
|
+
map<R>(transform: (value: T) => R): Result<R, E>;
|
|
47
|
+
/**
|
|
48
|
+
* Transforms the error using the provided function. Keeps the value when this is a success.
|
|
49
|
+
*/
|
|
50
|
+
mapError<F>(transform: (error: E) => F): Result<T, F>;
|
|
51
|
+
/**
|
|
52
|
+
* Chains another operation that returns a Result when this result is a success.
|
|
53
|
+
*/
|
|
54
|
+
flatMap<R>(transform: (value: T) => Result<R, E>): Result<R, E>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Success variant of Result.
|
|
58
|
+
*/
|
|
59
|
+
declare class Ok<T> extends Result<T, never> {
|
|
60
|
+
readonly value: T;
|
|
61
|
+
constructor(value: T);
|
|
62
|
+
isOk(): this is Ok<T>;
|
|
63
|
+
isErr(): this is Err<never>;
|
|
64
|
+
match<R>(handlers: {
|
|
65
|
+
ok: (value: T) => R;
|
|
66
|
+
err: (error: never) => R;
|
|
67
|
+
}): R;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Error variant of Result.
|
|
71
|
+
*/
|
|
72
|
+
declare class Err<E> extends Result<never, E> {
|
|
73
|
+
readonly error: E;
|
|
74
|
+
constructor(error: E);
|
|
75
|
+
isOk(): this is Ok<never>;
|
|
76
|
+
isErr(): this is Err<E>;
|
|
77
|
+
match<R>(handlers: {
|
|
78
|
+
ok: (value: never) => R;
|
|
79
|
+
err: (error: E) => R;
|
|
80
|
+
}): R;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/core/result/outcome.d.ts
|
|
84
|
+
/**
|
|
85
|
+
* Outcome — Business decision abstraction.
|
|
86
|
+
*
|
|
87
|
+
* Represents the semantic result of a domain decision, policy evaluation,
|
|
88
|
+
* or business rule. NOT for technical success/failure — use Result for that.
|
|
89
|
+
*
|
|
90
|
+
* Mental model:
|
|
91
|
+
* "What was the business decision?"
|
|
92
|
+
* NOT "Did the operation succeed?"
|
|
93
|
+
*
|
|
94
|
+
* The status axis represents valid business outcomes (APPROVED, REJECTED,
|
|
95
|
+
* ALLOW, DENY, NO_OP, etc.), never technical Ok/Err.
|
|
96
|
+
*
|
|
97
|
+
* @typeParam S - Union of valid status literals for this outcome
|
|
98
|
+
* @typeParam D - Shape of the decision data carried by this outcome
|
|
99
|
+
*/
|
|
100
|
+
declare class Outcome<S extends string, D = undefined> {
|
|
101
|
+
readonly status: S;
|
|
102
|
+
readonly data: D;
|
|
103
|
+
readonly reason: string | undefined;
|
|
104
|
+
readonly metadata: Readonly<Record<string, unknown>>;
|
|
105
|
+
private constructor();
|
|
106
|
+
static of<S extends string, D = undefined>(status: S, data: D, reason?: string, metadata?: Record<string, unknown>): Outcome<S, D>;
|
|
107
|
+
static approved<D = undefined>(data: D, reason?: string): Outcome<"APPROVED", D>;
|
|
108
|
+
static rejected<D = undefined>(data: D, reason: string): Outcome<"REJECTED", D>;
|
|
109
|
+
static noOp<D = undefined>(data: D, reason?: string): Outcome<"NO_OP", D>;
|
|
110
|
+
static deferred<D = undefined>(data: D, reason: string): Outcome<"DEFERRED", D>;
|
|
111
|
+
static requiresReview<D = undefined>(data: D, reason: string): Outcome<"REQUIRES_REVIEW", D>;
|
|
112
|
+
/**
|
|
113
|
+
* Narrows this Outcome to a specific status.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* if (outcome.is('DENY')) {
|
|
117
|
+
* outcome.data.violations // TS knows status is 'DENY'
|
|
118
|
+
* }
|
|
119
|
+
*/
|
|
120
|
+
is<T extends S>(status: T): this is Outcome<T, D>;
|
|
121
|
+
/**
|
|
122
|
+
* Exhaustive pattern match over all possible statuses.
|
|
123
|
+
* TypeScript enforces that every status has a handler.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* outcome.match({
|
|
127
|
+
* ALLOW: (o) => handleAllow(o.data),
|
|
128
|
+
* DENY: (o) => handleDeny(o.data.violations),
|
|
129
|
+
* })
|
|
130
|
+
*/
|
|
131
|
+
match<THandlers extends { [K in S]: (outcome: Outcome<K, D>) => unknown }>(handlers: THandlers): ReturnType<THandlers[S]>;
|
|
132
|
+
withMetadata(extra: Record<string, unknown>): Outcome<S, D>;
|
|
133
|
+
withReason(reason: string): Outcome<S, D>;
|
|
134
|
+
toString(): string;
|
|
135
|
+
}
|
|
136
|
+
/** Common business outcome statuses. Extend per domain as needed. */
|
|
137
|
+
type CommonOutcomeStatus = "APPROVED" | "REJECTED" | "NO_OP" | "DEFERRED" | "REQUIRES_REVIEW";
|
|
138
|
+
//#endregion
|
|
139
|
+
export { Result as a, Ok as i, Outcome as n, Err as r, CommonOutcomeStatus as t };
|
|
140
|
+
//# sourceMappingURL=outcome.d.cts.map
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
//#region src/core/result/result.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents the outcome of an operation that may succeed or fail.
|
|
4
|
+
* Useful for avoiding excessive exceptions and making error flows explicit.
|
|
5
|
+
*/
|
|
6
|
+
declare abstract class Result<T, E> {
|
|
7
|
+
protected constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Creates a success result containing a value.
|
|
10
|
+
*/
|
|
11
|
+
static ok<T>(value: T): Result<T, never>;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an error result containing an error object.
|
|
14
|
+
*/
|
|
15
|
+
static err<E>(error: E): Result<never, E>;
|
|
16
|
+
/**
|
|
17
|
+
* Returns true when this is a success result.
|
|
18
|
+
*/
|
|
19
|
+
abstract isOk(): this is Ok<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Returns true when this is an error result.
|
|
22
|
+
*/
|
|
23
|
+
abstract isErr(): this is Err<E>;
|
|
24
|
+
/**
|
|
25
|
+
* Runs a handler based on the result state (similar to pattern matching).
|
|
26
|
+
*/
|
|
27
|
+
abstract match<R>(handlers: {
|
|
28
|
+
ok: (value: T) => R;
|
|
29
|
+
err: (error: E) => R;
|
|
30
|
+
}): R;
|
|
31
|
+
/**
|
|
32
|
+
* Returns the value when this is success, or null when this is an error.
|
|
33
|
+
*/
|
|
34
|
+
getOrNull(): T | null;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the error when this is a failure, or null when this is a success.
|
|
37
|
+
*/
|
|
38
|
+
errorOrNull(): E | null;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the value when this is a success, or throws the error when this is a failure.
|
|
41
|
+
*/
|
|
42
|
+
getOrThrow(): T;
|
|
43
|
+
/**
|
|
44
|
+
* Transforms the success value using the provided function. Keeps the error when this is a failure.
|
|
45
|
+
*/
|
|
46
|
+
map<R>(transform: (value: T) => R): Result<R, E>;
|
|
47
|
+
/**
|
|
48
|
+
* Transforms the error using the provided function. Keeps the value when this is a success.
|
|
49
|
+
*/
|
|
50
|
+
mapError<F>(transform: (error: E) => F): Result<T, F>;
|
|
51
|
+
/**
|
|
52
|
+
* Chains another operation that returns a Result when this result is a success.
|
|
53
|
+
*/
|
|
54
|
+
flatMap<R>(transform: (value: T) => Result<R, E>): Result<R, E>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Success variant of Result.
|
|
58
|
+
*/
|
|
59
|
+
declare class Ok<T> extends Result<T, never> {
|
|
60
|
+
readonly value: T;
|
|
61
|
+
constructor(value: T);
|
|
62
|
+
isOk(): this is Ok<T>;
|
|
63
|
+
isErr(): this is Err<never>;
|
|
64
|
+
match<R>(handlers: {
|
|
65
|
+
ok: (value: T) => R;
|
|
66
|
+
err: (error: never) => R;
|
|
67
|
+
}): R;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Error variant of Result.
|
|
71
|
+
*/
|
|
72
|
+
declare class Err<E> extends Result<never, E> {
|
|
73
|
+
readonly error: E;
|
|
74
|
+
constructor(error: E);
|
|
75
|
+
isOk(): this is Ok<never>;
|
|
76
|
+
isErr(): this is Err<E>;
|
|
77
|
+
match<R>(handlers: {
|
|
78
|
+
ok: (value: never) => R;
|
|
79
|
+
err: (error: E) => R;
|
|
80
|
+
}): R;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/core/result/outcome.d.ts
|
|
84
|
+
/**
|
|
85
|
+
* Outcome — Business decision abstraction.
|
|
86
|
+
*
|
|
87
|
+
* Represents the semantic result of a domain decision, policy evaluation,
|
|
88
|
+
* or business rule. NOT for technical success/failure — use Result for that.
|
|
89
|
+
*
|
|
90
|
+
* Mental model:
|
|
91
|
+
* "What was the business decision?"
|
|
92
|
+
* NOT "Did the operation succeed?"
|
|
93
|
+
*
|
|
94
|
+
* The status axis represents valid business outcomes (APPROVED, REJECTED,
|
|
95
|
+
* ALLOW, DENY, NO_OP, etc.), never technical Ok/Err.
|
|
96
|
+
*
|
|
97
|
+
* @typeParam S - Union of valid status literals for this outcome
|
|
98
|
+
* @typeParam D - Shape of the decision data carried by this outcome
|
|
99
|
+
*/
|
|
100
|
+
declare class Outcome<S extends string, D = undefined> {
|
|
101
|
+
readonly status: S;
|
|
102
|
+
readonly data: D;
|
|
103
|
+
readonly reason: string | undefined;
|
|
104
|
+
readonly metadata: Readonly<Record<string, unknown>>;
|
|
105
|
+
private constructor();
|
|
106
|
+
static of<S extends string, D = undefined>(status: S, data: D, reason?: string, metadata?: Record<string, unknown>): Outcome<S, D>;
|
|
107
|
+
static approved<D = undefined>(data: D, reason?: string): Outcome<"APPROVED", D>;
|
|
108
|
+
static rejected<D = undefined>(data: D, reason: string): Outcome<"REJECTED", D>;
|
|
109
|
+
static noOp<D = undefined>(data: D, reason?: string): Outcome<"NO_OP", D>;
|
|
110
|
+
static deferred<D = undefined>(data: D, reason: string): Outcome<"DEFERRED", D>;
|
|
111
|
+
static requiresReview<D = undefined>(data: D, reason: string): Outcome<"REQUIRES_REVIEW", D>;
|
|
112
|
+
/**
|
|
113
|
+
* Narrows this Outcome to a specific status.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* if (outcome.is('DENY')) {
|
|
117
|
+
* outcome.data.violations // TS knows status is 'DENY'
|
|
118
|
+
* }
|
|
119
|
+
*/
|
|
120
|
+
is<T extends S>(status: T): this is Outcome<T, D>;
|
|
121
|
+
/**
|
|
122
|
+
* Exhaustive pattern match over all possible statuses.
|
|
123
|
+
* TypeScript enforces that every status has a handler.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* outcome.match({
|
|
127
|
+
* ALLOW: (o) => handleAllow(o.data),
|
|
128
|
+
* DENY: (o) => handleDeny(o.data.violations),
|
|
129
|
+
* })
|
|
130
|
+
*/
|
|
131
|
+
match<THandlers extends { [K in S]: (outcome: Outcome<K, D>) => unknown }>(handlers: THandlers): ReturnType<THandlers[S]>;
|
|
132
|
+
withMetadata(extra: Record<string, unknown>): Outcome<S, D>;
|
|
133
|
+
withReason(reason: string): Outcome<S, D>;
|
|
134
|
+
toString(): string;
|
|
135
|
+
}
|
|
136
|
+
/** Common business outcome statuses. Extend per domain as needed. */
|
|
137
|
+
type CommonOutcomeStatus = "APPROVED" | "REJECTED" | "NO_OP" | "DEFERRED" | "REQUIRES_REVIEW";
|
|
138
|
+
//#endregion
|
|
139
|
+
export { Result as a, Ok as i, Outcome as n, Err as r, CommonOutcomeStatus as t };
|
|
140
|
+
//# sourceMappingURL=outcome.d.ts.map
|
package/dist/outcome.js
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { a as Result } from "./outcome.cjs";
|
|
2
|
+
import { d as GatePayloadV1, s as PolicyContext, u as GatePayload } from "./gate-types.cjs";
|
|
3
|
+
import { a as ComputeOutcome, c as VersionedComputeEngine, i as ComputeEvaluatorRegistration, l as ComputePayload, p as ComputePayloadV1, r as ComputeEvaluator } from "./gate-engine-registry.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/core/policies/engines/compute-evaluator-registry.d.ts
|
|
6
|
+
declare class ComputeEvaluatorRegistry {
|
|
7
|
+
private readonly evaluators;
|
|
8
|
+
private static toRegistryKey;
|
|
9
|
+
register(registration: ComputeEvaluatorRegistration): void;
|
|
10
|
+
get(policyKey: string, version: number): ComputeEvaluator | undefined;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/core/policies/engines/compute-engine-registry.d.ts
|
|
14
|
+
declare class ComputeEngineRegistry {
|
|
15
|
+
private readonly engines;
|
|
16
|
+
register(engine: VersionedComputeEngine): void;
|
|
17
|
+
get(version: number): VersionedComputeEngine | undefined;
|
|
18
|
+
evaluate(version: number, params: {
|
|
19
|
+
readonly policyKey: string;
|
|
20
|
+
readonly payloadSchemaVersion: number;
|
|
21
|
+
readonly payload: unknown;
|
|
22
|
+
readonly context: PolicyContext;
|
|
23
|
+
readonly evaluators: ComputeEvaluatorRegistry;
|
|
24
|
+
}): Result<ComputeOutcome, string>;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/core/policies/engines/parse-compute-payload.d.ts
|
|
28
|
+
type ComputePayloadParser = (payload: unknown) => Result<ComputePayload, string>;
|
|
29
|
+
declare class ComputePayloadParserRegistry {
|
|
30
|
+
private readonly parsers;
|
|
31
|
+
register(payloadSchemaVersion: number, parser: ComputePayloadParser): void;
|
|
32
|
+
get(payloadSchemaVersion: number): ComputePayloadParser | undefined;
|
|
33
|
+
parse(payloadSchemaVersion: number, payload: unknown): Result<ComputePayloadV1, string>;
|
|
34
|
+
}
|
|
35
|
+
declare class ComputePayloadParsers {
|
|
36
|
+
private static readonly registry;
|
|
37
|
+
private static defaultsRegistered;
|
|
38
|
+
private static ensureDefaults;
|
|
39
|
+
static register(payloadSchemaVersion: number, parser: ComputePayloadParser): void;
|
|
40
|
+
static get(payloadSchemaVersion: number): ComputePayloadParser | undefined;
|
|
41
|
+
static parse(payloadSchemaVersion: number, payload: unknown): Result<ComputePayloadV1, string>;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/core/policies/engines/parse-gate-payload.d.ts
|
|
45
|
+
type GatePayloadParser = (payload: unknown) => Result<GatePayload, string>;
|
|
46
|
+
declare class GatePayloadParserRegistry {
|
|
47
|
+
private readonly parsers;
|
|
48
|
+
register(engineVersion: number, parser: GatePayloadParser): void;
|
|
49
|
+
get(engineVersion: number): GatePayloadParser | undefined;
|
|
50
|
+
parse(engineVersion: number, payload: unknown): Result<GatePayloadV1, string>;
|
|
51
|
+
}
|
|
52
|
+
declare class GatePayloadParsers {
|
|
53
|
+
private static readonly registry;
|
|
54
|
+
private static defaultsRegistered;
|
|
55
|
+
private static ensureDefaults;
|
|
56
|
+
static register(engineVersion: number, parser: GatePayloadParser): void;
|
|
57
|
+
static get(engineVersion: number): GatePayloadParser | undefined;
|
|
58
|
+
static parse(engineVersion: number, payload: unknown): Result<GatePayloadV1, string>;
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { ComputePayloadParserRegistry as a, ComputeEvaluatorRegistry as c, ComputePayloadParser as i, GatePayloadParserRegistry as n, ComputePayloadParsers as o, GatePayloadParsers as r, ComputeEngineRegistry as s, GatePayloadParser as t };
|
|
62
|
+
//# sourceMappingURL=parse-gate-payload.d.cts.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as Result } from "./outcome.js";
|
|
2
|
+
import { d as GatePayloadV1, s as PolicyContext, u as GatePayload } from "./gate-types.js";
|
|
2
3
|
import { a as ComputeOutcome, c as VersionedComputeEngine, i as ComputeEvaluatorRegistration, l as ComputePayload, p as ComputePayloadV1, r as ComputeEvaluator } from "./gate-engine-registry.js";
|
|
3
4
|
|
|
4
5
|
//#region src/core/policies/engines/compute-evaluator-registry.d.ts
|
package/dist/path.d.cts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { a as Result } from "./outcome.cjs";
|
|
2
|
+
import { m as CoreConfig } from "./gate-types.cjs";
|
|
3
|
+
import { D as PolicyDefinition, N as PolicyCatalog, P as PolicyCatalogEntryProps, S as PolicyDefinitionRepository, T as FindCandidatesParams, m as ContextResolverRegistry, y as ContextValueResolver } from "./policy-service.cjs";
|
|
4
|
+
import { i as ComputeEvaluatorRegistration } from "./gate-engine-registry.cjs";
|
|
5
|
+
|
|
6
|
+
//#region src/core/config/core-config.instance.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Shared instance for the application composition root.
|
|
10
|
+
* Use `new CoreConfig()` for isolated runtimes, tests, or multi-tenant hosts.
|
|
11
|
+
*
|
|
12
|
+
* The default reporter is silent so the core has no runtime logging dependency.
|
|
13
|
+
* Hosts can swap in their own `PolicyReporter` via
|
|
14
|
+
* `coreConfig.configure({ observability: { reporter } })`.
|
|
15
|
+
*/
|
|
16
|
+
declare const coreConfig: CoreConfig;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/core/policies/utils/hash.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* Produces a deterministic SHA-256 hex digest of a canonical JSON representation.
|
|
21
|
+
* Object keys are sorted recursively so key order does not affect the hash.
|
|
22
|
+
* Array item order is preserved on purpose; callers that treat arrays as sets
|
|
23
|
+
* must normalize or sort them before hashing.
|
|
24
|
+
*
|
|
25
|
+
* Values that JSON.stringify would silently drop or coerce (undefined,
|
|
26
|
+
* non-finite numbers, functions, symbols, bigint) are rejected up-front to
|
|
27
|
+
* prevent semantically different payloads from collapsing to the same hash.
|
|
28
|
+
*/
|
|
29
|
+
declare class PolicyHashing {
|
|
30
|
+
static sha256(input: string): string;
|
|
31
|
+
private static assertHashable;
|
|
32
|
+
static canonicalJson(value: unknown): string;
|
|
33
|
+
static computePayloadHash(payload: unknown, policyKey: string, policyVersion: string): string;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/core/policies/defs/in-memory-policy-definition-repo.d.ts
|
|
37
|
+
declare class InMemoryPolicyDefinitionRepository implements PolicyDefinitionRepository {
|
|
38
|
+
private readonly definitions;
|
|
39
|
+
private readonly definitionsByPolicyKey;
|
|
40
|
+
constructor(definitions: readonly PolicyDefinition[]);
|
|
41
|
+
findCandidates(params: FindCandidatesParams): PolicyDefinition[];
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/core/policies/package/policy-package.d.ts
|
|
45
|
+
/**
|
|
46
|
+
* Protocol of contribution: a module declares what it wants to add to the
|
|
47
|
+
* policy system without the core knowing which module it is.
|
|
48
|
+
*
|
|
49
|
+
* - catalogEntries: static policy descriptors the catalog will register.
|
|
50
|
+
* - definitions: default policy definitions (rules/parameters) to seed.
|
|
51
|
+
* - evaluators: compute evaluator registrations keyed by policy/version.
|
|
52
|
+
*/
|
|
53
|
+
interface PolicyPackage {
|
|
54
|
+
readonly catalogEntries: readonly PolicyCatalogEntryProps[];
|
|
55
|
+
readonly definitions: readonly PolicyDefinition[];
|
|
56
|
+
readonly evaluators: readonly ComputeEvaluatorRegistration[];
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/core/policies/catalog/catalog.instance.d.ts
|
|
60
|
+
/**
|
|
61
|
+
* Aggregates catalog entries contributed by each module package into a single
|
|
62
|
+
* PolicyCatalog. The core knows nothing about concrete modules — callers
|
|
63
|
+
* are responsible for passing all relevant packages at composition time.
|
|
64
|
+
*/
|
|
65
|
+
declare class PolicyCatalogFactory {
|
|
66
|
+
static build(packages: readonly PolicyPackage[]): PolicyCatalog;
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/core/policies/context/context-registry.instance.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Official instance of the ContextResolverRegistry.
|
|
72
|
+
* Use `new ContextResolverRegistry()` plus `registerNamespacedContextResolversIn`
|
|
73
|
+
* when each runtime/composition root needs its own isolated resolver graph.
|
|
74
|
+
*/
|
|
75
|
+
declare const contextResolverRegistry: ContextResolverRegistry;
|
|
76
|
+
declare function registerNamespacedContextResolvers(namespace: string, resolvers: readonly ContextValueResolver[]): Result<ContextResolverRegistry, string>;
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/core/policies/context/path.d.ts
|
|
79
|
+
declare class PolicyContextPath {
|
|
80
|
+
private static readonly FORBIDDEN_SEGMENTS;
|
|
81
|
+
private static resolve;
|
|
82
|
+
private static parseSegments;
|
|
83
|
+
private static isPlainRecord;
|
|
84
|
+
static getOrAbsent(obj: Record<string, unknown>, path: string): Result<unknown, "absent">;
|
|
85
|
+
static has(obj: Record<string, unknown>, path: string): boolean;
|
|
86
|
+
static set(obj: Record<string, unknown>, path: string, value: unknown): Result<void, string>;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
export { PolicyPackage as a, coreConfig as c, PolicyCatalogFactory as i, contextResolverRegistry as n, InMemoryPolicyDefinitionRepository as o, registerNamespacedContextResolvers as r, PolicyHashing as s, PolicyContextPath as t };
|
|
90
|
+
//# sourceMappingURL=path.d.cts.map
|
package/dist/path.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as Result } from "./outcome.js";
|
|
2
|
+
import { m as CoreConfig } from "./gate-types.js";
|
|
2
3
|
import { D as PolicyDefinition, N as PolicyCatalog, P as PolicyCatalogEntryProps, S as PolicyDefinitionRepository, T as FindCandidatesParams, m as ContextResolverRegistry, y as ContextValueResolver } from "./policy-service.js";
|
|
3
4
|
import { i as ComputeEvaluatorRegistration } from "./gate-engine-registry.js";
|
|
4
5
|
|
package/dist/plugin.cjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//#region src/core/plugins/plugin.ts
|
|
2
|
+
/**
|
|
3
|
+
* Registry that holds a set of plugins implementing a shared {@link PluginContract}
|
|
4
|
+
* and resolves which of them answer a given extension point.
|
|
5
|
+
*
|
|
6
|
+
* Plugins are kept sorted by priority (descending). For a given method,
|
|
7
|
+
* {@link invoke} either delegates to the highest-priority enabled plugin
|
|
8
|
+
* (`'first'`) or folds every enabled plugin into a single value (`'pipeline'`).
|
|
9
|
+
* When no plugin is enabled, the supplied `fallback` runs instead, so the host
|
|
10
|
+
* always has defined behaviour even with an empty registry.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam P - The plugin contract this manager coordinates.
|
|
13
|
+
*/
|
|
14
|
+
var PluginManager = class {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.plugins = [];
|
|
17
|
+
}
|
|
18
|
+
/** Registers one or more plugins and re-sorts by priority. */
|
|
19
|
+
register(...plugins) {
|
|
20
|
+
this.plugins.push(...plugins);
|
|
21
|
+
this.reorder();
|
|
22
|
+
}
|
|
23
|
+
/** Removes a previously registered plugin by name. */
|
|
24
|
+
unregister(name) {
|
|
25
|
+
this.plugins = this.plugins.filter((p) => p.name !== name);
|
|
26
|
+
}
|
|
27
|
+
/** Enables a registered plugin by name. */
|
|
28
|
+
enable(name) {
|
|
29
|
+
const plugin = this.plugins.find((p) => p.name === name);
|
|
30
|
+
if (plugin) plugin.enabled = true;
|
|
31
|
+
}
|
|
32
|
+
/** Disables a registered plugin by name (without removing it). */
|
|
33
|
+
disable(name) {
|
|
34
|
+
const plugin = this.plugins.find((p) => p.name === name);
|
|
35
|
+
if (plugin) plugin.enabled = false;
|
|
36
|
+
}
|
|
37
|
+
/** Lists enabled plugins only, already in priority order. */
|
|
38
|
+
list() {
|
|
39
|
+
return this.plugins.filter((p) => p.enabled !== false);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Resolves `method` against the registered plugins.
|
|
43
|
+
*
|
|
44
|
+
* In `'first'` mode the highest-priority enabled plugin that implements
|
|
45
|
+
* `method` answers. In `'pipeline'` mode every enabled plugin runs and the
|
|
46
|
+
* results are folded with `reducer` (defaults to "keep the last result").
|
|
47
|
+
* When no enabled plugin implements `method`, `fallback` is called.
|
|
48
|
+
*/
|
|
49
|
+
invoke(method, args, opts) {
|
|
50
|
+
const { mode = "first", fallback, reducer } = opts;
|
|
51
|
+
const enabled = this.list().filter((p) => method in p);
|
|
52
|
+
if (enabled.length === 0) return fallback(...args);
|
|
53
|
+
if (mode === "first") {
|
|
54
|
+
const fn = enabled[0][method];
|
|
55
|
+
return fn(...args);
|
|
56
|
+
}
|
|
57
|
+
const defaultReducer = (_, curr) => curr;
|
|
58
|
+
const red = reducer ?? defaultReducer;
|
|
59
|
+
let acc = enabled[0][method](...args);
|
|
60
|
+
enabled.slice(1).forEach((plugin, idx) => {
|
|
61
|
+
const res = plugin[method](...args);
|
|
62
|
+
acc = red(acc, res, plugin, idx + 1);
|
|
63
|
+
});
|
|
64
|
+
return acc;
|
|
65
|
+
}
|
|
66
|
+
/** Re-sorts the internal list by priority (desc; default priority = 0). */
|
|
67
|
+
reorder() {
|
|
68
|
+
this.plugins.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
//#endregion
|
|
72
|
+
Object.defineProperty(exports, "PluginManager", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function() {
|
|
75
|
+
return PluginManager;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
//# sourceMappingURL=plugin.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.cjs","names":[],"sources":["../src/core/plugins/plugin.ts"],"sourcesContent":["import {\n type BasePlugin,\n type InvokeOptions,\n type PipelineReducer,\n type PluginContract,\n} from \"./types.js\";\n\n/**\n * Registry that holds a set of plugins implementing a shared {@link PluginContract}\n * and resolves which of them answer a given extension point.\n *\n * Plugins are kept sorted by priority (descending). For a given method,\n * {@link invoke} either delegates to the highest-priority enabled plugin\n * (`'first'`) or folds every enabled plugin into a single value (`'pipeline'`).\n * When no plugin is enabled, the supplied `fallback` runs instead, so the host\n * always has defined behaviour even with an empty registry.\n *\n * @typeParam P - The plugin contract this manager coordinates.\n */\nclass PluginManager<P extends PluginContract> {\n /** Internal list, kept sorted by priority (desc). */\n private plugins: (P & BasePlugin)[] = [];\n\n /** Registers one or more plugins and re-sorts by priority. */\n public register(...plugins: (P & BasePlugin)[]): void {\n this.plugins.push(...plugins);\n this.reorder();\n }\n\n /** Removes a previously registered plugin by name. */\n public unregister(name: string): void {\n this.plugins = this.plugins.filter((p) => p.name !== name);\n }\n\n /** Enables a registered plugin by name. */\n public enable(name: string): void {\n const plugin = this.plugins.find((p) => p.name === name);\n if (plugin) plugin.enabled = true;\n }\n\n /** Disables a registered plugin by name (without removing it). */\n public disable(name: string): void {\n const plugin = this.plugins.find((p) => p.name === name);\n if (plugin) plugin.enabled = false;\n }\n\n /** Lists enabled plugins only, already in priority order. */\n public list(): readonly (P & BasePlugin)[] {\n return this.plugins.filter((p) => p.enabled !== false);\n }\n\n /**\n * Resolves `method` against the registered plugins.\n *\n * In `'first'` mode the highest-priority enabled plugin that implements\n * `method` answers. In `'pipeline'` mode every enabled plugin runs and the\n * results are folded with `reducer` (defaults to \"keep the last result\").\n * When no enabled plugin implements `method`, `fallback` is called.\n */\n public invoke<K extends keyof P>(\n method: K,\n args: Parameters<P[K]>,\n opts: InvokeOptions<P, K>,\n ): ReturnType<P[K]> {\n const { mode = \"first\", fallback, reducer } = opts;\n const enabled = this.list().filter((p) => method in p) as Array<\n P & BasePlugin\n >;\n\n if (enabled.length === 0) {\n return (fallback as P[K])(...args);\n }\n\n if (mode === \"first\") {\n const fn = enabled[0][method] as P[K];\n return fn(...args);\n }\n\n const defaultReducer: PipelineReducer<ReturnType<P[K]>, P> = (\n _,\n curr,\n ) => curr;\n const red = reducer ?? defaultReducer;\n\n let acc: ReturnType<P[K]> = enabled[0][method]!(...args);\n enabled.slice(1).forEach((plugin, idx) => {\n const res = plugin[method]!(...args);\n acc = red(acc, res, plugin, idx + 1);\n });\n return acc;\n }\n\n /** Re-sorts the internal list by priority (desc; default priority = 0). */\n private reorder(): void {\n this.plugins.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));\n }\n}\n\nexport { PluginManager };\n"],"mappings":";;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAA8C;;iBAEJ,CAAC;;;CAGvC,SAAgB,GAAG,SAAmC;EAClD,KAAK,QAAQ,KAAK,GAAG,OAAO;EAC5B,KAAK,QAAQ;CACjB;;CAGA,WAAkB,MAAoB;EAClC,KAAK,UAAU,KAAK,QAAQ,QAAQ,MAAM,EAAE,SAAS,IAAI;CAC7D;;CAGA,OAAc,MAAoB;EAC9B,MAAM,SAAS,KAAK,QAAQ,MAAM,MAAM,EAAE,SAAS,IAAI;EACvD,IAAI,QAAQ,OAAO,UAAU;CACjC;;CAGA,QAAe,MAAoB;EAC/B,MAAM,SAAS,KAAK,QAAQ,MAAM,MAAM,EAAE,SAAS,IAAI;EACvD,IAAI,QAAQ,OAAO,UAAU;CACjC;;CAGA,OAA2C;EACvC,OAAO,KAAK,QAAQ,QAAQ,MAAM,EAAE,YAAY,KAAK;CACzD;;;;;;;;;CAUA,OACI,QACA,MACA,MACgB;EAChB,MAAM,EAAE,OAAO,SAAS,UAAU,YAAY;EAC9C,MAAM,UAAU,KAAK,KAAK,EAAE,QAAQ,MAAM,UAAU,CAAC;EAIrD,IAAI,QAAQ,WAAW,GACnB,OAAQ,SAAkB,GAAG,IAAI;EAGrC,IAAI,SAAS,SAAS;GAClB,MAAM,KAAK,QAAQ,GAAG;GACtB,OAAO,GAAG,GAAG,IAAI;EACrB;EAEA,MAAM,kBACF,GACA,SACC;EACL,MAAM,MAAM,WAAW;EAEvB,IAAI,MAAwB,QAAQ,GAAG,QAAS,GAAG,IAAI;EACvD,QAAQ,MAAM,CAAC,EAAE,SAAS,QAAQ,QAAQ;GACtC,MAAM,MAAM,OAAO,QAAS,GAAG,IAAI;GACnC,MAAM,IAAI,KAAK,KAAK,QAAQ,MAAM,CAAC;EACvC,CAAC;EACD,OAAO;CACX;;CAGA,UAAwB;EACpB,KAAK,QAAQ,MAAM,GAAG,OAAO,EAAE,YAAY,MAAM,EAAE,YAAY,EAAE;CACrE;AACJ"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//#region src/core/plugins/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Common shape every plugin shares, regardless of the contract it implements.
|
|
4
|
+
* Carries the metadata the {@link PluginManager} uses to identify, order and
|
|
5
|
+
* toggle plugins.
|
|
6
|
+
*/
|
|
7
|
+
interface BasePlugin {
|
|
8
|
+
/** Unique identifier, used for logs / debugging and for enable/disable. */
|
|
9
|
+
readonly name: string;
|
|
10
|
+
/** Weight; the highest priority wins (default = 0). */
|
|
11
|
+
readonly priority?: number;
|
|
12
|
+
/** Shortcut to disable a plugin without removing it. */
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A plugin contract is a record whose values are the methods plugins may
|
|
17
|
+
* implement for a given extension point.
|
|
18
|
+
*
|
|
19
|
+
* `any` here is intentional: it is the only way to express the constraint
|
|
20
|
+
* "a record whose values are arbitrary functions". Using `unknown` for the
|
|
21
|
+
* parameters breaks assignment of concrete contracts by contravariance —
|
|
22
|
+
* TypeScript rejects functions with specific parameters as a subtype of
|
|
23
|
+
* `(...args: unknown[]) => unknown`.
|
|
24
|
+
*/
|
|
25
|
+
type PluginContract = Record<string, (...args: any[]) => any>;
|
|
26
|
+
/**
|
|
27
|
+
* Reducer used in `pipeline` mode to combine the result of each plugin into a
|
|
28
|
+
* single accumulated value.
|
|
29
|
+
*/
|
|
30
|
+
type PipelineReducer<R, P extends PluginContract> = (accumulated: R, current: R, plugin: P & BasePlugin, index: number) => R;
|
|
31
|
+
/** Options accepted by {@link PluginManager.invoke}. */
|
|
32
|
+
interface InvokeOptions<P extends PluginContract, K extends keyof P> {
|
|
33
|
+
/**
|
|
34
|
+
* `'first'` → returns the first enabled plugin with the highest priority.
|
|
35
|
+
* `'pipeline'` → runs every enabled plugin, combining results via `reducer`.
|
|
36
|
+
* default = `'first'`.
|
|
37
|
+
*/
|
|
38
|
+
mode?: "first" | "pipeline";
|
|
39
|
+
/** Executed when no plugin is enabled for the method. */
|
|
40
|
+
fallback: P[K];
|
|
41
|
+
/** Reducer used only when `mode = "pipeline"`. */
|
|
42
|
+
reducer?: PipelineReducer<ReturnType<P[K]>, P>;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/core/plugins/plugin.d.ts
|
|
46
|
+
/**
|
|
47
|
+
* Registry that holds a set of plugins implementing a shared {@link PluginContract}
|
|
48
|
+
* and resolves which of them answer a given extension point.
|
|
49
|
+
*
|
|
50
|
+
* Plugins are kept sorted by priority (descending). For a given method,
|
|
51
|
+
* {@link invoke} either delegates to the highest-priority enabled plugin
|
|
52
|
+
* (`'first'`) or folds every enabled plugin into a single value (`'pipeline'`).
|
|
53
|
+
* When no plugin is enabled, the supplied `fallback` runs instead, so the host
|
|
54
|
+
* always has defined behaviour even with an empty registry.
|
|
55
|
+
*
|
|
56
|
+
* @typeParam P - The plugin contract this manager coordinates.
|
|
57
|
+
*/
|
|
58
|
+
declare class PluginManager<P extends PluginContract> {
|
|
59
|
+
/** Internal list, kept sorted by priority (desc). */
|
|
60
|
+
private plugins;
|
|
61
|
+
/** Registers one or more plugins and re-sorts by priority. */
|
|
62
|
+
register(...plugins: (P & BasePlugin)[]): void;
|
|
63
|
+
/** Removes a previously registered plugin by name. */
|
|
64
|
+
unregister(name: string): void;
|
|
65
|
+
/** Enables a registered plugin by name. */
|
|
66
|
+
enable(name: string): void;
|
|
67
|
+
/** Disables a registered plugin by name (without removing it). */
|
|
68
|
+
disable(name: string): void;
|
|
69
|
+
/** Lists enabled plugins only, already in priority order. */
|
|
70
|
+
list(): readonly (P & BasePlugin)[];
|
|
71
|
+
/**
|
|
72
|
+
* Resolves `method` against the registered plugins.
|
|
73
|
+
*
|
|
74
|
+
* In `'first'` mode the highest-priority enabled plugin that implements
|
|
75
|
+
* `method` answers. In `'pipeline'` mode every enabled plugin runs and the
|
|
76
|
+
* results are folded with `reducer` (defaults to "keep the last result").
|
|
77
|
+
* When no enabled plugin implements `method`, `fallback` is called.
|
|
78
|
+
*/
|
|
79
|
+
invoke<K extends keyof P>(method: K, args: Parameters<P[K]>, opts: InvokeOptions<P, K>): ReturnType<P[K]>;
|
|
80
|
+
/** Re-sorts the internal list by priority (desc; default priority = 0). */
|
|
81
|
+
private reorder;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { PluginContract as a, PipelineReducer as i, BasePlugin as n, InvokeOptions as r, PluginManager as t };
|
|
85
|
+
//# sourceMappingURL=plugin.d.cts.map
|