@entifix/business 0.10.2 → 0.12.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/dist/compose/compose.d.ts +107 -0
- package/dist/compose/compose.d.ts.map +1 -0
- package/dist/compose/compose.js +146 -0
- package/dist/compose/compose.js.map +1 -0
- package/dist/compose/index.d.ts +2 -0
- package/dist/compose/index.d.ts.map +1 -0
- package/dist/compose/index.js +3 -0
- package/dist/compose/index.js.map +1 -0
- package/dist/entity-api/entity-api.d.ts +137 -0
- package/dist/entity-api/entity-api.d.ts.map +1 -0
- package/dist/entity-api/entity-api.js +17 -0
- package/dist/entity-api/entity-api.js.map +1 -0
- package/dist/entity-api/index.d.ts +2 -0
- package/dist/entity-api/index.d.ts.map +1 -0
- package/dist/entity-api/index.js +3 -0
- package/dist/entity-api/index.js.map +1 -0
- package/dist/flow/catalog.d.ts +51 -0
- package/dist/flow/catalog.d.ts.map +1 -0
- package/dist/flow/catalog.js +63 -0
- package/dist/flow/catalog.js.map +1 -0
- package/dist/flow/fragment.d.ts +105 -0
- package/dist/flow/fragment.d.ts.map +1 -0
- package/dist/flow/fragment.js +77 -0
- package/dist/flow/fragment.js.map +1 -0
- package/dist/flow/index.d.ts +5 -0
- package/dist/flow/index.d.ts.map +1 -0
- package/dist/flow/index.js +6 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/flow/slip.d.ts +141 -0
- package/dist/flow/slip.d.ts.map +1 -0
- package/dist/flow/slip.js +81 -0
- package/dist/flow/slip.js.map +1 -0
- package/dist/flow/validate.d.ts +70 -0
- package/dist/flow/validate.d.ts.map +1 -0
- package/dist/flow/validate.js +145 -0
- package/dist/flow/validate.js.map +1 -0
- package/dist/idempotency/idempotency-store.d.ts +53 -0
- package/dist/idempotency/idempotency-store.d.ts.map +1 -0
- package/dist/idempotency/idempotency-store.js +5 -0
- package/dist/idempotency/idempotency-store.js.map +1 -0
- package/dist/idempotency/index.d.ts +2 -0
- package/dist/idempotency/index.d.ts.map +1 -0
- package/dist/idempotency/index.js +3 -0
- package/dist/idempotency/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/request/index.d.ts +2 -0
- package/dist/request/index.d.ts.map +1 -0
- package/dist/request/index.js +3 -0
- package/dist/request/index.js.map +1 -0
- package/dist/request/request-context.d.ts +86 -0
- package/dist/request/request-context.d.ts.map +1 -0
- package/dist/request/request-context.js +23 -0
- package/dist/request/request-context.js.map +1 -0
- package/dist/resolve/index.d.ts +3 -0
- package/dist/resolve/index.d.ts.map +1 -0
- package/dist/resolve/index.js +4 -0
- package/dist/resolve/index.js.map +1 -0
- package/dist/resolve/resolve.d.ts +70 -0
- package/dist/resolve/resolve.d.ts.map +1 -0
- package/dist/resolve/resolve.js +69 -0
- package/dist/resolve/resolve.js.map +1 -0
- package/dist/resolve/verified-context.d.ts +45 -0
- package/dist/resolve/verified-context.d.ts.map +1 -0
- package/dist/resolve/verified-context.js +8 -0
- package/dist/resolve/verified-context.js.map +1 -0
- package/dist/verified/index.d.ts +10 -0
- package/dist/verified/index.d.ts.map +1 -0
- package/dist/verified/index.js +10 -0
- package/dist/verified/index.js.map +1 -0
- package/package.json +17 -2
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Invalid } from '@entifix/core';
|
|
2
|
+
import { Effect, Schema } from 'effect';
|
|
3
|
+
import type { FragmentCatalog } from '../flow/catalog.js';
|
|
4
|
+
import type { FragmentKeys } from '../flow/fragment.js';
|
|
5
|
+
import { type Slip, type SlipSegment } from '../flow/slip.js';
|
|
6
|
+
import { type CompositionRefused, type FlowRequirementsUnmet, type SlipViolation } from '../flow/validate.js';
|
|
7
|
+
import { type ResolutionRule, Unresolvable } from '../resolve/resolve.js';
|
|
8
|
+
import type { VerifiedContext } from '../resolve/verified-context.js';
|
|
9
|
+
/** A gate over what is known when the flow starts. Never over anything else. */
|
|
10
|
+
export type RecipeGate<I> = (context: VerifiedContext, input: I, segment?: SlipSegment) => boolean;
|
|
11
|
+
/** One step of a recipe: a fragment by its pinned reference, as a string. */
|
|
12
|
+
export interface RecipeStep<I> {
|
|
13
|
+
readonly ref: string;
|
|
14
|
+
/** The step's name in the slip; defaults to the fragment id. */
|
|
15
|
+
readonly step?: string;
|
|
16
|
+
readonly when?: RecipeGate<I>;
|
|
17
|
+
/** Parameters pinned at composition, read from the context's config. */
|
|
18
|
+
readonly parameters?: (context: VerifiedContext) => Readonly<Record<string, unknown>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* One stage of a recipe. A `perSegment` stage is expanded once per segment the
|
|
22
|
+
* start input names — one per vendor, say — each step tagged with its segment
|
|
23
|
+
* and named `<segment key>:<step>`.
|
|
24
|
+
*/
|
|
25
|
+
export interface RecipeStage<I> {
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly when?: RecipeGate<I>;
|
|
28
|
+
readonly perSegment?: boolean;
|
|
29
|
+
readonly steps: readonly RecipeStep<I>[];
|
|
30
|
+
}
|
|
31
|
+
/** A use case the recipe resolves, recorded on the slip as it was resolved. */
|
|
32
|
+
export interface RecipeResolution {
|
|
33
|
+
readonly useCase: string;
|
|
34
|
+
readonly rules: readonly ResolutionRule<unknown>[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A named template a slip is composed from. It references fragments by
|
|
38
|
+
* `id@n` strings, never by import, so no domain imports another to take part in
|
|
39
|
+
* a flow.
|
|
40
|
+
*/
|
|
41
|
+
export interface Recipe<I> {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
/** The start input, decoded before anything else is read from it. */
|
|
44
|
+
readonly input: Schema.Codec<I, unknown>;
|
|
45
|
+
/** Keys the start input provides to the first steps, with their schemas. */
|
|
46
|
+
readonly provides?: FragmentKeys;
|
|
47
|
+
/** A checkout needs a commit point even with nothing retriable in it. */
|
|
48
|
+
readonly requiresCommitPoint?: boolean;
|
|
49
|
+
/** The segments a `perSegment` stage expands over. */
|
|
50
|
+
readonly segments?: (input: I) => readonly SlipSegment[];
|
|
51
|
+
readonly resolve?: readonly RecipeResolution[];
|
|
52
|
+
readonly stages: readonly RecipeStage<I>[];
|
|
53
|
+
}
|
|
54
|
+
/** What a composition is stamped with: both are inputs, so composing is deterministic. */
|
|
55
|
+
export interface ComposeOptions {
|
|
56
|
+
readonly flowId: string;
|
|
57
|
+
readonly composedAt: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Composes a recipe into a pinned, validated slip for one caller: decodes the
|
|
61
|
+
* start input, resolves the recipe's use cases, expands per-segment stages,
|
|
62
|
+
* pins versions and parameters, stamps `slipVersion`, validates, and annotates
|
|
63
|
+
* the span with `entifix.slip.version` and `entifix.slip.fragments`.
|
|
64
|
+
*
|
|
65
|
+
* An unknown `id@n` is a `CompositionRefused` carrying `UnknownFragment`, never
|
|
66
|
+
* a throw; a caller who lacks only what steps require is `FlowRequirementsUnmet`. The same context, input and options always give the same slip.
|
|
67
|
+
*/
|
|
68
|
+
export declare const compose: <I>(recipe: Recipe<I>, catalog: FragmentCatalog, context: VerifiedContext, input: unknown, options: ComposeOptions) => Effect.Effect<Slip, CompositionRefused | FlowRequirementsUnmet | Unresolvable | Invalid>;
|
|
69
|
+
/** One axis value, labelled so a report can say which combination broke. */
|
|
70
|
+
export interface Labelled<T> {
|
|
71
|
+
readonly label: string;
|
|
72
|
+
readonly value: T;
|
|
73
|
+
}
|
|
74
|
+
/** The axes a recipe is enumerated over. */
|
|
75
|
+
export interface CompositionAxes<I> {
|
|
76
|
+
readonly contexts: readonly Labelled<VerifiedContext>[];
|
|
77
|
+
readonly inputs: readonly Labelled<I>[];
|
|
78
|
+
}
|
|
79
|
+
/** How one combination composed. */
|
|
80
|
+
export interface CompositionOutcome {
|
|
81
|
+
readonly context: string;
|
|
82
|
+
readonly input: string;
|
|
83
|
+
/**
|
|
84
|
+
* `composed`; `unresolvable`, the caller is offered no variant; `unmet`, the
|
|
85
|
+
* caller lacks what a step requires; or `refused`, the slip broke a rule
|
|
86
|
+
* that no caller could repair — a defect.
|
|
87
|
+
*/
|
|
88
|
+
readonly outcome: 'composed' | 'unresolvable' | 'unmet' | 'refused';
|
|
89
|
+
readonly violations: readonly SlipViolation[];
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Composes the recipe for every context × input and reports each outcome —
|
|
93
|
+
* what a CI job runs to prove that every combination a recipe can produce is a
|
|
94
|
+
* valid slip before it ships. An unresolvable combination is a caller who is
|
|
95
|
+
* offered nothing, and an unmet one a caller who lacks an entitlement, a role
|
|
96
|
+
* or a channel a step requires — neither is a defect, but a host asserts which
|
|
97
|
+
* combinations are which, since a caller wrongly left without an entitlement
|
|
98
|
+
* looks exactly like one rightly refused. A refused combination is a defect.
|
|
99
|
+
*/
|
|
100
|
+
export declare const enumerateCompositions: <I>(recipe: Recipe<I>, catalog: FragmentCatalog, axes: CompositionAxes<I>) => readonly CompositionOutcome[];
|
|
101
|
+
/**
|
|
102
|
+
* Throws, listing every refused combination, unless all of them composed,
|
|
103
|
+
* were unresolvable or were unmet. Framework-neutral on purpose: any test runner reports a
|
|
104
|
+
* thrown error.
|
|
105
|
+
*/
|
|
106
|
+
export declare const expectAllCompositionsValid: (outcomes: readonly CompositionOutcome[]) => void;
|
|
107
|
+
//# sourceMappingURL=compose.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose.d.ts","sourceRoot":"","sources":["../../src/compose/compose.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EACL,KAAK,IAAI,EAGT,KAAK,WAAW,EA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AC,eAAe,CAAC,EAAE,CAAC;IACxD,QAAQ,CAAC,MAAM,EAAE,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;CACzC;AAED,oCAAoC;AACpC,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,UAAU,GAAG,cAAc,GAAG,OAAO,GAAG,SAAS,CAAC;IACpE,QAAQ,CAAC,UAAU,EAAE,SAAS,aAAa,EAAE,CAAC;CAC/C;AAOD;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,GAAI,CAAC,EACrC,QAAQ,MAAM,CAAC,CAAC,CAAC,EACjB,SAAS,eAAe,EACxB,MAAM,eAAe,CAAC,CAAC,CAAC,KACvB,SAAS,kBAAkB,EAwC3B,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,GACrC,UAAU,SAAS,kBAAkB,EAAE,KACtC,IAeF,CAAC"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
import { FieldProblem, Invalid } from '@entifix/core';
|
|
3
|
+
import { Effect, Schema } from 'effect';
|
|
4
|
+
import { SLIP_VERSION, slipSteps } from '../flow/slip.js';
|
|
5
|
+
import { assertValidSlip, onlyUnmetRequirements, validateSlip } from '../flow/validate.js';
|
|
6
|
+
import { dryRun, Unresolvable } from '../resolve/resolve.js';
|
|
7
|
+
const segmentKey = (segment)=>[
|
|
8
|
+
segment.vendor,
|
|
9
|
+
segment.paymentMethod
|
|
10
|
+
].filter(Boolean).join('/');
|
|
11
|
+
const buildStep = (recipeStep, catalog, context, segment)=>{
|
|
12
|
+
var _ref, _recipeStep_step, _ref1;
|
|
13
|
+
const fragment = catalog.get(recipeStep.ref);
|
|
14
|
+
const name = (_ref = (_recipeStep_step = recipeStep.step) != null ? _recipeStep_step : fragment == null ? void 0 : fragment.id) != null ? _ref : recipeStep.ref;
|
|
15
|
+
const parameters = recipeStep.parameters == null ? void 0 : recipeStep.parameters.call(recipeStep, context);
|
|
16
|
+
return _extends({
|
|
17
|
+
ref: recipeStep.ref,
|
|
18
|
+
step: segment === undefined ? name : `${segmentKey(segment)}:${name}`,
|
|
19
|
+
cardinality: (_ref1 = fragment == null ? void 0 : fragment.cardinality) != null ? _ref1 : 'one'
|
|
20
|
+
}, segment === undefined ? {} : {
|
|
21
|
+
segment
|
|
22
|
+
}, (fragment == null ? void 0 : fragment.participant) === undefined ? {} : {
|
|
23
|
+
participant: fragment.participant
|
|
24
|
+
}, (fragment == null ? void 0 : fragment.call) === undefined ? {} : {
|
|
25
|
+
call: fragment.call
|
|
26
|
+
}, (fragment == null ? void 0 : fragment.compensation) === undefined ? {} : {
|
|
27
|
+
compensation: fragment.compensation
|
|
28
|
+
}, parameters === undefined ? {} : {
|
|
29
|
+
parameters
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const resolutionOf = (resolutions, context)=>Effect.forEach(resolutions, ({ useCase, rules })=>{
|
|
33
|
+
const chosen = dryRun(rules, context);
|
|
34
|
+
return chosen === undefined ? Effect.fail(new Unresolvable({
|
|
35
|
+
useCase
|
|
36
|
+
})) : Effect.succeed({
|
|
37
|
+
useCase,
|
|
38
|
+
variant: chosen.variant,
|
|
39
|
+
rule: chosen.rule
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
/** Expands the recipe into an unvalidated slip. */ const draft = (recipe, catalog, context, input, resolution, options)=>{
|
|
43
|
+
var _ref;
|
|
44
|
+
const segments = (_ref = recipe.segments == null ? void 0 : recipe.segments.call(recipe, input)) != null ? _ref : [];
|
|
45
|
+
const stages = recipe.stages.filter((stage)=>{
|
|
46
|
+
var _ref;
|
|
47
|
+
return (_ref = stage.when == null ? void 0 : stage.when.call(stage, context, input)) != null ? _ref : true;
|
|
48
|
+
}).map((stage)=>{
|
|
49
|
+
const each = stage.perSegment === true ? segments : [
|
|
50
|
+
undefined
|
|
51
|
+
];
|
|
52
|
+
return {
|
|
53
|
+
name: stage.name,
|
|
54
|
+
steps: each.flatMap((segment)=>stage.steps.filter((recipeStep)=>{
|
|
55
|
+
var _ref;
|
|
56
|
+
return (_ref = recipeStep.when == null ? void 0 : recipeStep.when.call(recipeStep, context, input, segment)) != null ? _ref : true;
|
|
57
|
+
}).map((recipeStep)=>buildStep(recipeStep, catalog, context, segment)))
|
|
58
|
+
};
|
|
59
|
+
}).filter((stage)=>stage.steps.length > 0);
|
|
60
|
+
return _extends({
|
|
61
|
+
slipVersion: SLIP_VERSION,
|
|
62
|
+
flowId: options.flowId,
|
|
63
|
+
recipe: recipe.name
|
|
64
|
+
}, recipe.requiresCommitPoint === undefined ? {} : {
|
|
65
|
+
requiresCommitPoint: recipe.requiresCommitPoint
|
|
66
|
+
}, {
|
|
67
|
+
stages,
|
|
68
|
+
resolution,
|
|
69
|
+
composedAt: options.composedAt
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const decodeInput = (recipe, input)=>Schema.decodeUnknownEffect(recipe.input)(input).pipe(Effect.mapError((error)=>new Invalid({
|
|
73
|
+
errors: [
|
|
74
|
+
FieldProblem.make({
|
|
75
|
+
pointer: '',
|
|
76
|
+
detail: error.message,
|
|
77
|
+
code: 'invalidFlowInput'
|
|
78
|
+
})
|
|
79
|
+
]
|
|
80
|
+
})));
|
|
81
|
+
/**
|
|
82
|
+
* Composes a recipe into a pinned, validated slip for one caller: decodes the
|
|
83
|
+
* start input, resolves the recipe's use cases, expands per-segment stages,
|
|
84
|
+
* pins versions and parameters, stamps `slipVersion`, validates, and annotates
|
|
85
|
+
* the span with `entifix.slip.version` and `entifix.slip.fragments`.
|
|
86
|
+
*
|
|
87
|
+
* An unknown `id@n` is a `CompositionRefused` carrying `UnknownFragment`, never
|
|
88
|
+
* a throw; a caller who lacks only what steps require is `FlowRequirementsUnmet`. The same context, input and options always give the same slip.
|
|
89
|
+
*/ export const compose = (recipe, catalog, context, input, options)=>Effect.gen(function*() {
|
|
90
|
+
var _recipe_resolve;
|
|
91
|
+
const decoded = yield* decodeInput(recipe, input);
|
|
92
|
+
const resolution = yield* resolutionOf((_recipe_resolve = recipe.resolve) != null ? _recipe_resolve : [], context);
|
|
93
|
+
const slip = yield* assertValidSlip(draft(recipe, catalog, context, decoded, resolution, options), catalog, _extends({
|
|
94
|
+
context
|
|
95
|
+
}, recipe.provides === undefined ? {} : {
|
|
96
|
+
start: recipe.provides
|
|
97
|
+
}));
|
|
98
|
+
yield* Effect.annotateCurrentSpan({
|
|
99
|
+
'entifix.slip.version': slip.slipVersion,
|
|
100
|
+
'entifix.slip.fragments': slipSteps(slip).map((step)=>step.ref).join(',')
|
|
101
|
+
});
|
|
102
|
+
return slip;
|
|
103
|
+
});
|
|
104
|
+
const ENUMERATED = {
|
|
105
|
+
flowId: 'enumerated',
|
|
106
|
+
composedAt: '1970-01-01T00:00:00.000Z'
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Composes the recipe for every context × input and reports each outcome —
|
|
110
|
+
* what a CI job runs to prove that every combination a recipe can produce is a
|
|
111
|
+
* valid slip before it ships. An unresolvable combination is a caller who is
|
|
112
|
+
* offered nothing, and an unmet one a caller who lacks an entitlement, a role
|
|
113
|
+
* or a channel a step requires — neither is a defect, but a host asserts which
|
|
114
|
+
* combinations are which, since a caller wrongly left without an entitlement
|
|
115
|
+
* looks exactly like one rightly refused. A refused combination is a defect.
|
|
116
|
+
*/ export const enumerateCompositions = (recipe, catalog, axes)=>axes.contexts.flatMap((context)=>axes.inputs.map((input)=>{
|
|
117
|
+
var _recipe_resolve;
|
|
118
|
+
const resolved = Effect.runSync(Effect.result(resolutionOf((_recipe_resolve = recipe.resolve) != null ? _recipe_resolve : [], context.value)));
|
|
119
|
+
if (resolved._tag === 'Failure') return {
|
|
120
|
+
context: context.label,
|
|
121
|
+
input: input.label,
|
|
122
|
+
outcome: 'unresolvable',
|
|
123
|
+
violations: []
|
|
124
|
+
};
|
|
125
|
+
const violations = validateSlip(draft(recipe, catalog, context.value, input.value, resolved.success, ENUMERATED), catalog, _extends({
|
|
126
|
+
context: context.value
|
|
127
|
+
}, recipe.provides === undefined ? {} : {
|
|
128
|
+
start: recipe.provides
|
|
129
|
+
}));
|
|
130
|
+
return {
|
|
131
|
+
context: context.label,
|
|
132
|
+
input: input.label,
|
|
133
|
+
outcome: violations.length === 0 ? 'composed' : onlyUnmetRequirements(violations) ? 'unmet' : 'refused',
|
|
134
|
+
violations
|
|
135
|
+
};
|
|
136
|
+
}));
|
|
137
|
+
/**
|
|
138
|
+
* Throws, listing every refused combination, unless all of them composed,
|
|
139
|
+
* were unresolvable or were unmet. Framework-neutral on purpose: any test runner reports a
|
|
140
|
+
* thrown error.
|
|
141
|
+
*/ export const expectAllCompositionsValid = (outcomes)=>{
|
|
142
|
+
const refused = outcomes.filter((outcome)=>outcome.outcome === 'refused');
|
|
143
|
+
if (refused.length > 0) throw new Error(`${String(refused.length)} composition(s) are not valid slips:\n` + refused.map((outcome)=>` ${outcome.context} × ${outcome.input}: ` + outcome.violations.map((violation)=>`${violation.tag} (${violation.detail})`).join('; ')).join('\n'));
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
//# sourceMappingURL=compose.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/compose/compose.ts"],"sourcesContent":["import { FieldProblem, Invalid } from '@entifix/core';\nimport { Effect, Schema } from 'effect';\n\nimport type { FragmentCatalog } from '../flow/catalog.js';\nimport type { FragmentKeys } from '../flow/fragment.js';\nimport {\n type Slip,\n SLIP_VERSION,\n type SlipResolution,\n type SlipSegment,\n type SlipStep,\n slipSteps,\n} from '../flow/slip.js';\nimport {\n assertValidSlip,\n type CompositionRefused,\n type FlowRequirementsUnmet,\n onlyUnmetRequirements,\n type SlipViolation,\n validateSlip,\n} from '../flow/validate.js';\nimport {\n dryRun,\n type ResolutionRule,\n Unresolvable,\n} from '../resolve/resolve.js';\nimport type { VerifiedContext } from '../resolve/verified-context.js';\n\n/** A gate over what is known when the flow starts. Never over anything else. */\nexport type RecipeGate<I> = (\n context: VerifiedContext,\n input: I,\n segment?: SlipSegment,\n) => boolean;\n\n/** One step of a recipe: a fragment by its pinned reference, as a string. */\nexport interface RecipeStep<I> {\n readonly ref: string;\n /** The step's name in the slip; defaults to the fragment id. */\n readonly step?: string;\n readonly when?: RecipeGate<I>;\n /** Parameters pinned at composition, read from the context's config. */\n readonly parameters?: (\n context: VerifiedContext,\n ) => Readonly<Record<string, unknown>>;\n}\n\n/**\n * One stage of a recipe. A `perSegment` stage is expanded once per segment the\n * start input names — one per vendor, say — each step tagged with its segment\n * and named `<segment key>:<step>`.\n */\nexport interface RecipeStage<I> {\n readonly name: string;\n readonly when?: RecipeGate<I>;\n readonly perSegment?: boolean;\n readonly steps: readonly RecipeStep<I>[];\n}\n\n/** A use case the recipe resolves, recorded on the slip as it was resolved. */\nexport interface RecipeResolution {\n readonly useCase: string;\n readonly rules: readonly ResolutionRule<unknown>[];\n}\n\n/**\n * A named template a slip is composed from. It references fragments by\n * `id@n` strings, never by import, so no domain imports another to take part in\n * a flow.\n */\nexport interface Recipe<I> {\n readonly name: string;\n /** The start input, decoded before anything else is read from it. */\n readonly input: Schema.Codec<I, unknown>;\n /** Keys the start input provides to the first steps, with their schemas. */\n readonly provides?: FragmentKeys;\n /** A checkout needs a commit point even with nothing retriable in it. */\n readonly requiresCommitPoint?: boolean;\n /** The segments a `perSegment` stage expands over. */\n readonly segments?: (input: I) => readonly SlipSegment[];\n readonly resolve?: readonly RecipeResolution[];\n readonly stages: readonly RecipeStage<I>[];\n}\n\n/** What a composition is stamped with: both are inputs, so composing is deterministic. */\nexport interface ComposeOptions {\n readonly flowId: string;\n readonly composedAt: string;\n}\n\nconst segmentKey = (segment: SlipSegment): string =>\n [segment.vendor, segment.paymentMethod].filter(Boolean).join('/');\n\nconst buildStep = <I>(\n recipeStep: RecipeStep<I>,\n catalog: FragmentCatalog,\n context: VerifiedContext,\n segment: SlipSegment | undefined,\n): SlipStep => {\n const fragment = catalog.get(recipeStep.ref);\n const name = recipeStep.step ?? fragment?.id ?? recipeStep.ref;\n const parameters = recipeStep.parameters?.(context);\n return {\n ref: recipeStep.ref,\n step: segment === undefined ? name : `${segmentKey(segment)}:${name}`,\n cardinality: fragment?.cardinality ?? 'one',\n ...(segment === undefined ? {} : { segment }),\n ...(fragment?.participant === undefined\n ? {}\n : { participant: fragment.participant }),\n ...(fragment?.call === undefined ? {} : { call: fragment.call }),\n ...(fragment?.compensation === undefined\n ? {}\n : { compensation: fragment.compensation }),\n ...(parameters === undefined ? {} : { parameters }),\n };\n};\n\nconst resolutionOf = (\n resolutions: readonly RecipeResolution[],\n context: VerifiedContext,\n): Effect.Effect<readonly SlipResolution[], Unresolvable> =>\n Effect.forEach(resolutions, ({ useCase, rules }) => {\n const chosen = dryRun(rules, context);\n return chosen === undefined\n ? Effect.fail(new Unresolvable({ useCase }))\n : Effect.succeed({ useCase, variant: chosen.variant, rule: chosen.rule });\n });\n\n/** Expands the recipe into an unvalidated slip. */\nconst draft = <I>(\n recipe: Recipe<I>,\n catalog: FragmentCatalog,\n context: VerifiedContext,\n input: I,\n resolution: readonly SlipResolution[],\n options: ComposeOptions,\n): Slip => {\n const segments = recipe.segments?.(input) ?? [];\n const stages = recipe.stages\n .filter(stage => stage.when?.(context, input) ?? true)\n .map(stage => {\n const each: readonly (SlipSegment | undefined)[] =\n stage.perSegment === true ? segments : [undefined];\n return {\n name: stage.name,\n steps: each.flatMap(segment =>\n stage.steps\n .filter(\n recipeStep => recipeStep.when?.(context, input, segment) ?? true,\n )\n .map(recipeStep =>\n buildStep(recipeStep, catalog, context, segment),\n ),\n ),\n };\n })\n .filter(stage => stage.steps.length > 0);\n return {\n slipVersion: SLIP_VERSION,\n flowId: options.flowId,\n recipe: recipe.name,\n ...(recipe.requiresCommitPoint === undefined\n ? {}\n : { requiresCommitPoint: recipe.requiresCommitPoint }),\n stages,\n resolution,\n composedAt: options.composedAt,\n };\n};\n\nconst decodeInput = <I>(recipe: Recipe<I>, input: unknown) =>\n Schema.decodeUnknownEffect(recipe.input)(input).pipe(\n Effect.mapError(\n error =>\n new Invalid({\n errors: [\n FieldProblem.make({\n pointer: '',\n detail: error.message,\n code: 'invalidFlowInput',\n }),\n ],\n }),\n ),\n );\n\n/**\n * Composes a recipe into a pinned, validated slip for one caller: decodes the\n * start input, resolves the recipe's use cases, expands per-segment stages,\n * pins versions and parameters, stamps `slipVersion`, validates, and annotates\n * the span with `entifix.slip.version` and `entifix.slip.fragments`.\n *\n * An unknown `id@n` is a `CompositionRefused` carrying `UnknownFragment`, never\n * a throw; a caller who lacks only what steps require is `FlowRequirementsUnmet`. The same context, input and options always give the same slip.\n */\nexport const compose = <I>(\n recipe: Recipe<I>,\n catalog: FragmentCatalog,\n context: VerifiedContext,\n input: unknown,\n options: ComposeOptions,\n): Effect.Effect<\n Slip,\n CompositionRefused | FlowRequirementsUnmet | Unresolvable | Invalid\n> =>\n Effect.gen(function* () {\n const decoded = yield* decodeInput(recipe, input);\n const resolution = yield* resolutionOf(recipe.resolve ?? [], context);\n const slip = yield* assertValidSlip(\n draft(recipe, catalog, context, decoded, resolution, options),\n catalog,\n {\n context,\n ...(recipe.provides === undefined ? {} : { start: recipe.provides }),\n },\n );\n yield* Effect.annotateCurrentSpan({\n 'entifix.slip.version': slip.slipVersion,\n 'entifix.slip.fragments': slipSteps(slip)\n .map(step => step.ref)\n .join(','),\n });\n return slip;\n });\n\n/** One axis value, labelled so a report can say which combination broke. */\nexport interface Labelled<T> {\n readonly label: string;\n readonly value: T;\n}\n\n/** The axes a recipe is enumerated over. */\nexport interface CompositionAxes<I> {\n readonly contexts: readonly Labelled<VerifiedContext>[];\n readonly inputs: readonly Labelled<I>[];\n}\n\n/** How one combination composed. */\nexport interface CompositionOutcome {\n readonly context: string;\n readonly input: string;\n /**\n * `composed`; `unresolvable`, the caller is offered no variant; `unmet`, the\n * caller lacks what a step requires; or `refused`, the slip broke a rule\n * that no caller could repair — a defect.\n */\n readonly outcome: 'composed' | 'unresolvable' | 'unmet' | 'refused';\n readonly violations: readonly SlipViolation[];\n}\n\nconst ENUMERATED: ComposeOptions = {\n flowId: 'enumerated',\n composedAt: '1970-01-01T00:00:00.000Z',\n};\n\n/**\n * Composes the recipe for every context × input and reports each outcome —\n * what a CI job runs to prove that every combination a recipe can produce is a\n * valid slip before it ships. An unresolvable combination is a caller who is\n * offered nothing, and an unmet one a caller who lacks an entitlement, a role\n * or a channel a step requires — neither is a defect, but a host asserts which\n * combinations are which, since a caller wrongly left without an entitlement\n * looks exactly like one rightly refused. A refused combination is a defect.\n */\nexport const enumerateCompositions = <I>(\n recipe: Recipe<I>,\n catalog: FragmentCatalog,\n axes: CompositionAxes<I>,\n): readonly CompositionOutcome[] =>\n axes.contexts.flatMap(context =>\n axes.inputs.map(input => {\n const resolved = Effect.runSync(\n Effect.result(resolutionOf(recipe.resolve ?? [], context.value)),\n );\n if (resolved._tag === 'Failure')\n return {\n context: context.label,\n input: input.label,\n outcome: 'unresolvable' as const,\n violations: [],\n };\n const violations = validateSlip(\n draft(\n recipe,\n catalog,\n context.value,\n input.value,\n resolved.success,\n ENUMERATED,\n ),\n catalog,\n {\n context: context.value,\n ...(recipe.provides === undefined ? {} : { start: recipe.provides }),\n },\n );\n return {\n context: context.label,\n input: input.label,\n outcome:\n violations.length === 0\n ? ('composed' as const)\n : onlyUnmetRequirements(violations)\n ? ('unmet' as const)\n : ('refused' as const),\n violations,\n };\n }),\n );\n\n/**\n * Throws, listing every refused combination, unless all of them composed,\n * were unresolvable or were unmet. Framework-neutral on purpose: any test runner reports a\n * thrown error.\n */\nexport const expectAllCompositionsValid = (\n outcomes: readonly CompositionOutcome[],\n): void => {\n const refused = outcomes.filter(outcome => outcome.outcome === 'refused');\n if (refused.length > 0)\n throw new Error(\n `${String(refused.length)} composition(s) are not valid slips:\\n` +\n refused\n .map(\n outcome =>\n ` ${outcome.context} × ${outcome.input}: ` +\n outcome.violations\n .map(violation => `${violation.tag} (${violation.detail})`)\n .join('; '),\n )\n .join('\\n'),\n );\n};\n"],"names":["FieldProblem","Invalid","Effect","Schema","SLIP_VERSION","slipSteps","assertValidSlip","onlyUnmetRequirements","validateSlip","dryRun","Unresolvable","segmentKey","segment","vendor","paymentMethod","filter","Boolean","join","buildStep","recipeStep","catalog","context","fragment","get","ref","name","step","id","parameters","undefined","cardinality","participant","call","compensation","resolutionOf","resolutions","forEach","useCase","rules","chosen","fail","succeed","variant","rule","draft","recipe","input","resolution","options","segments","stages","stage","when","map","each","perSegment","steps","flatMap","length","slipVersion","flowId","requiresCommitPoint","composedAt","decodeInput","decodeUnknownEffect","pipe","mapError","error","errors","make","pointer","detail","message","code","compose","gen","decoded","resolve","slip","provides","start","annotateCurrentSpan","ENUMERATED","enumerateCompositions","axes","contexts","inputs","resolved","runSync","result","value","_tag","label","outcome","violations","success","expectAllCompositionsValid","outcomes","refused","Error","String","violation","tag"],"mappings":";AAAA,SAASA,YAAY,EAAEC,OAAO,QAAQ,gBAAgB;AACtD,SAASC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAIxC,SAEEC,YAAY,EAIZC,SAAS,QACJ,kBAAkB;AACzB,SACEC,eAAe,EAGfC,qBAAqB,EAErBC,YAAY,QACP,sBAAsB;AAC7B,SACEC,MAAM,EAENC,YAAY,QACP,wBAAwB;AAiE/B,MAAMC,aAAa,CAACC,UAClB;QAACA,QAAQC,MAAM;QAAED,QAAQE,aAAa;KAAC,CAACC,MAAM,CAACC,SAASC,IAAI,CAAC;AAE/D,MAAMC,YAAY,CAChBC,YACAC,SACAC,SACAT;QAGaO,MAAAA;IADb,MAAMG,WAAWF,QAAQG,GAAG,CAACJ,WAAWK,GAAG;IAC3C,MAAMC,QAAON,QAAAA,mBAAAA,WAAWO,IAAI,YAAfP,mBAAmBG,4BAAAA,SAAUK,EAAE,YAA/BR,OAAm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BC,SACEC,WAAWpC,MAAM,KAAK,IACjB,aACDnD,sBAAsBuF,cACnB,UACA;gBACTA;YACF;QACF,IACA;AAEJ;;;;CAIC,GACD,OAAO,MAAME,6BAA6B,CACxCC;IAEA,MAAMC,UAAUD,SAASlF,MAAM,CAAC8E,CAAAA,UAAWA,QAAQA,OAAO,KAAK;IAC/D,IAAIK,QAAQxC,MAAM,GAAG,GACnB,MAAM,IAAIyC,MACR,GAAGC,OAAOF,QAAQxC,MAAM,EAAE,sCAAsC,CAAC,GAC/DwC,QACG7C,GAAG,CACFwC,CAAAA,UACE,CAAC,EAAE,EAAEA,QAAQxE,OAAO,CAAC,GAAG,EAAEwE,QAAQ/C,KAAK,CAAC,EAAE,CAAC,GAC3C+C,QAAQC,UAAU,CACfzC,GAAG,CAACgD,CAAAA,YAAa,GAAGA,UAAUC,GAAG,CAAC,EAAE,EAAED,UAAU9B,MAAM,CAAC,CAAC,CAAC,EACzDtD,IAAI,CAAC,OAEXA,IAAI,CAAC;AAEhB,EAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compose/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/compose/index.ts"],"sourcesContent":["export * from './compose.js';\n"],"names":[],"mappings":"AAAA,cAAc,eAAe"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { type EndpointIdempotency, type EndpointSchema, type EntifixEnvelopeLink, type EntifixFailure, type Entity, type EntityConstructor, type EntityId, type FailureClass, type FilterGroup, type MetaUseCase, type UseCaseConstructor } from '@entifix/core';
|
|
2
|
+
import type { Effect } from 'effect';
|
|
3
|
+
import type { RequestPrincipal } from '../request/request-context.js';
|
|
4
|
+
import type { ResolutionRule } from '../resolve/resolve.js';
|
|
5
|
+
import type { VerifiedContext } from '../resolve/verified-context.js';
|
|
6
|
+
/** The five generic verbs an entity's endpoints can serve. */
|
|
7
|
+
export type EntityApiVerb = 'list' | 'get' | 'create' | 'update' | 'delete';
|
|
8
|
+
/**
|
|
9
|
+
* Who may reach an entity's endpoints. The permission is derived from the
|
|
10
|
+
* entity and the verb, so it is never written here.
|
|
11
|
+
*/
|
|
12
|
+
export type EntityApiGuard = {
|
|
13
|
+
readonly kind: 'public';
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: 'permission';
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: 'organization';
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: 'crossing';
|
|
20
|
+
readonly organization: boolean;
|
|
21
|
+
};
|
|
22
|
+
/** The affordances an entity's endpoints derive from the verbs they serve. */
|
|
23
|
+
export interface EntityApiLinks {
|
|
24
|
+
readonly record: (id: EntityId) => EntifixEnvelopeLink[];
|
|
25
|
+
readonly collection: () => EntifixEnvelopeLink[];
|
|
26
|
+
}
|
|
27
|
+
/** What a `prepare`, an override or a verb knows about the caller. */
|
|
28
|
+
export interface EntityApiContext {
|
|
29
|
+
readonly organizationId?: string;
|
|
30
|
+
readonly principal?: RequestPrincipal;
|
|
31
|
+
readonly verified?: VerifiedContext;
|
|
32
|
+
readonly links: EntityApiLinks;
|
|
33
|
+
}
|
|
34
|
+
/** What an override's handler receives: the decoded parts of its request. */
|
|
35
|
+
export interface EntityApiRequest {
|
|
36
|
+
readonly params?: {
|
|
37
|
+
readonly id?: string;
|
|
38
|
+
};
|
|
39
|
+
readonly payload?: unknown;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Which records of an entity a caller may see: every one, none, or those a
|
|
43
|
+
* filter keeps ([ADR 0015](../../../../../docs/adr/0015-composed-flows-and-use-case-resolution.md)).
|
|
44
|
+
* A filter is conjoined onto the load request, so a query string can only
|
|
45
|
+
* narrow it further and no `or` in it can escape.
|
|
46
|
+
*/
|
|
47
|
+
export type Narrowing<T extends Entity> = 'unscoped' | 'nothing' | FilterGroup<T>;
|
|
48
|
+
/**
|
|
49
|
+
* A read's narrowing as ordered resolver rules over the verified context: the
|
|
50
|
+
* first variant the caller is entitled to and whose predicate holds builds the
|
|
51
|
+
* narrowing. A caller no rule matches sees **nothing** — the direction a
|
|
52
|
+
* missing rule must fail in.
|
|
53
|
+
*/
|
|
54
|
+
export type NarrowingRules<T extends Entity> = readonly ResolutionRule<(context: VerifiedContext) => Narrowing<T>>[];
|
|
55
|
+
/**
|
|
56
|
+
* One verb served by a handler of the host's instead of the generic one. It
|
|
57
|
+
* keeps the group's path and links, and may change the guard, the payload,
|
|
58
|
+
* the failures and the idempotency the verb declares.
|
|
59
|
+
*/
|
|
60
|
+
export interface EntityVerbOverride<R> {
|
|
61
|
+
readonly guard?: EntityApiGuard;
|
|
62
|
+
readonly idempotent?: EndpointIdempotency;
|
|
63
|
+
readonly errors?: readonly FailureClass[];
|
|
64
|
+
/** The body the verb takes instead of an entity envelope. */
|
|
65
|
+
readonly payload?: EndpointSchema;
|
|
66
|
+
readonly handler: (request: EntityApiRequest, context: EntityApiContext) => Effect.Effect<unknown, EntifixFailure, R>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A `@useCase()` verb's endpoint: its path, method, permission and contract
|
|
70
|
+
* come from the declaration, and only who may call it and what it does are
|
|
71
|
+
* written here.
|
|
72
|
+
*/
|
|
73
|
+
export interface VerbEndpoint<R> {
|
|
74
|
+
readonly useCase: MetaUseCase;
|
|
75
|
+
/** Defaults to the group's write guard. */
|
|
76
|
+
readonly guard?: EntityApiGuard;
|
|
77
|
+
readonly idempotent?: EndpointIdempotency;
|
|
78
|
+
readonly handler: (request: EntityApiRequest, context: EntityApiContext) => Effect.Effect<unknown, EntifixFailure, R>;
|
|
79
|
+
/**
|
|
80
|
+
* The same verb over a selection, for a record-bound verb that also sits in
|
|
81
|
+
* a collection cell (`alsoAt`): one permission for one act, whether it is
|
|
82
|
+
* asked of a record or of twenty. Required exactly when that cell is
|
|
83
|
+
* declared; its request carries the selection as its payload.
|
|
84
|
+
*/
|
|
85
|
+
readonly bulk?: (request: EntityApiRequest, context: EntityApiContext) => Effect.Effect<unknown, EntifixFailure, R>;
|
|
86
|
+
}
|
|
87
|
+
/** Serve a `@useCase()` class's verb on its entity's endpoints. */
|
|
88
|
+
export declare const verbEndpoint: <const O extends Omit<VerbEndpoint<any>, "useCase">>(useCaseClass: UseCaseConstructor, options: O) => O & {
|
|
89
|
+
readonly useCase: MetaUseCase;
|
|
90
|
+
};
|
|
91
|
+
/** What an entity's endpoints serve, and under which rules. */
|
|
92
|
+
export interface EntityApiOptions<T extends Entity, R> {
|
|
93
|
+
readonly verbs: readonly EntityApiVerb[];
|
|
94
|
+
/** One guard for every verb, or one for the reads and one for the writes. */
|
|
95
|
+
readonly guard: EntityApiGuard | {
|
|
96
|
+
readonly read: EntityApiGuard;
|
|
97
|
+
readonly write: EntityApiGuard;
|
|
98
|
+
};
|
|
99
|
+
/** Narrows the reads, and the facet counts, to what this caller may see. */
|
|
100
|
+
readonly narrowing?: NarrowingRules<T>;
|
|
101
|
+
/** Serve `$facets` beside the list, under the list's guard and narrowing. */
|
|
102
|
+
readonly facets?: boolean;
|
|
103
|
+
/** Runs on every generic create and update, before the save. */
|
|
104
|
+
readonly prepare?: (entity: T, context: EntityApiContext & {
|
|
105
|
+
readonly verb: 'create' | 'update';
|
|
106
|
+
}) => Effect.Effect<T, EntifixFailure, R>;
|
|
107
|
+
/** The business failures `prepare` may answer with, for the contract. */
|
|
108
|
+
readonly errors?: readonly FailureClass[];
|
|
109
|
+
readonly override?: Partial<Record<EntityApiVerb, EntityVerbOverride<R>>>;
|
|
110
|
+
/**
|
|
111
|
+
* The entity's `@useCase()` verbs, one endpoint each. Every verb the entity
|
|
112
|
+
* declares must be here and none another entity declares, or serving the
|
|
113
|
+
* declaration throws.
|
|
114
|
+
*/
|
|
115
|
+
readonly useCases?: readonly VerbEndpoint<R>[];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* An entity's endpoints as a value: which verbs, behind which guards, with
|
|
119
|
+
* which handlers. Declaring them needs no HTTP — a domain's cell declares, and
|
|
120
|
+
* the shell serves the declaration with `entityApi`
|
|
121
|
+
* ([ADR 0020](../../../../../docs/adr/0020-endpoints-are-descriptors-and-the-shell-builds-an-httpapi.md)).
|
|
122
|
+
*/
|
|
123
|
+
export interface EntityApiDeclaration<T extends Entity, K extends string, O extends EntityApiOptions<T, any>> {
|
|
124
|
+
readonly entity: EntityConstructor<T>;
|
|
125
|
+
readonly key: K;
|
|
126
|
+
readonly options: O;
|
|
127
|
+
}
|
|
128
|
+
/** `O`, with every key `Shape` does not name typed `never`. */
|
|
129
|
+
type Exact<O, Shape> = O & Record<Exclude<keyof O, keyof Shape>, never>;
|
|
130
|
+
/**
|
|
131
|
+
* Declare an entity's endpoints, to be served by the shell. An option the
|
|
132
|
+
* declaration does not know is a compile error rather than a value nobody
|
|
133
|
+
* reads — `compositions` included, which the shell adds when it serves.
|
|
134
|
+
*/
|
|
135
|
+
export declare const defineEntityApi: <T extends Entity, const K extends string, const O extends EntityApiOptions<T, any>>(entity: EntityConstructor<T>, key: K, options: Exact<O, EntityApiOptions<T, any>>) => EntityApiDeclaration<T, K, O>;
|
|
136
|
+
export {};
|
|
137
|
+
//# sourceMappingURL=entity-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-api.d.ts","sourceRoot":"","sources":["../../src/entity-api/entity-api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,MAAM,EACX,KAAK,iBAAiB,EACtB,KAAK,QAAQ,EAEb,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACxB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,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AChD;AAED;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB,CACnC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,EAEhB,CAAC,SAAS,gBAAgB,CAAC,CAAC,EAAE,GAAG,CAAC;IAElC,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;IACtC,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;CACrB;AAED,+DAA+D;AAC/D,KAAK,KAAK,CAAC,CAAC,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC;AAExE;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAC1B,CAAC,SAAS,MAAM,EAChB,KAAK,CAAC,CAAC,SAAS,MAAM,EAEtB,KAAK,CAAC,CAAC,SAAS,gBAAgB,CAAC,CAAC,EAAE,GAAG,CAAC,EAExC,QAAQ,iBAAiB,CAAC,CAAC,CAAC,EAC5B,KAAK,CAAC,EAEN,SAAS,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,KAC1C,oBAAoB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAA+B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
import { extractUseCase } from '@entifix/core';
|
|
3
|
+
/** Serve a `@useCase()` class's verb on its entity's endpoints. */ export const verbEndpoint = (useCaseClass, options)=>_extends({}, options, {
|
|
4
|
+
useCase: extractUseCase(useCaseClass)
|
|
5
|
+
});
|
|
6
|
+
/**
|
|
7
|
+
* Declare an entity's endpoints, to be served by the shell. An option the
|
|
8
|
+
* declaration does not know is a compile error rather than a value nobody
|
|
9
|
+
* reads — `compositions` included, which the shell adds when it serves.
|
|
10
|
+
*/ export const defineEntityApi = (entity, key, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- inferred from the options
|
|
11
|
+
options)=>({
|
|
12
|
+
entity,
|
|
13
|
+
key,
|
|
14
|
+
options
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=entity-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/entity-api/entity-api.ts"],"sourcesContent":["import {\n type EndpointIdempotency,\n type EndpointSchema,\n type EntifixEnvelopeLink,\n type EntifixFailure,\n type Entity,\n type EntityConstructor,\n type EntityId,\n extractUseCase,\n type FailureClass,\n type FilterGroup,\n type MetaUseCase,\n type UseCaseConstructor,\n} from '@entifix/core';\nimport type { Effect } from 'effect';\n\nimport type { RequestPrincipal } from '../request/request-context.js';\nimport type { ResolutionRule } from '../resolve/resolve.js';\nimport type { VerifiedContext } from '../resolve/verified-context.js';\n\n/** The five generic verbs an entity's endpoints can serve. */\nexport type EntityApiVerb = 'list' | 'get' | 'create' | 'update' | 'delete';\n\n/**\n * Who may reach an entity's endpoints. The permission is derived from the\n * entity and the verb, so it is never written here.\n */\nexport type EntityApiGuard =\n | { readonly kind: 'public' }\n | { readonly kind: 'permission' }\n | { readonly kind: 'organization' }\n | { readonly kind: 'crossing'; readonly organization: boolean };\n\n/** The affordances an entity's endpoints derive from the verbs they serve. */\nexport interface EntityApiLinks {\n readonly record: (id: EntityId) => EntifixEnvelopeLink[];\n readonly collection: () => EntifixEnvelopeLink[];\n}\n\n/** What a `prepare`, an override or a verb knows about the caller. */\nexport interface EntityApiContext {\n readonly organizationId?: string;\n readonly principal?: RequestPrincipal;\n readonly verified?: VerifiedContext;\n readonly links: EntityApiLinks;\n}\n\n/** What an override's handler receives: the decoded parts of its request. */\nexport interface EntityApiRequest {\n readonly params?: { readonly id?: string };\n readonly payload?: unknown;\n}\n\n/**\n * Which records of an entity a caller may see: every one, none, or those a\n * filter keeps ([ADR 0015](../../../../../docs/adr/0015-composed-flows-and-use-case-resolution.md)).\n * A filter is conjoined onto the load request, so a query string can only\n * narrow it further and no `or` in it can escape.\n */\nexport type Narrowing<T extends Entity> =\n 'unscoped' | 'nothing' | FilterGroup<T>;\n\n/**\n * A read's narrowing as ordered resolver rules over the verified context: the\n * first variant the caller is entitled to and whose predicate holds builds the\n * narrowing. A caller no rule matches sees **nothing** — the direction a\n * missing rule must fail in.\n */\nexport type NarrowingRules<T extends Entity> = readonly ResolutionRule<\n (context: VerifiedContext) => Narrowing<T>\n>[];\n\n/**\n * One verb served by a handler of the host's instead of the generic one. It\n * keeps the group's path and links, and may change the guard, the payload,\n * the failures and the idempotency the verb declares.\n */\nexport interface EntityVerbOverride<R> {\n readonly guard?: EntityApiGuard;\n readonly idempotent?: EndpointIdempotency;\n readonly errors?: readonly FailureClass[];\n /** The body the verb takes instead of an entity envelope. */\n readonly payload?: EndpointSchema;\n readonly handler: (\n request: EntityApiRequest,\n context: EntityApiContext,\n ) => Effect.Effect<unknown, EntifixFailure, R>;\n}\n\n/**\n * A `@useCase()` verb's endpoint: its path, method, permission and contract\n * come from the declaration, and only who may call it and what it does are\n * written here.\n */\nexport interface VerbEndpoint<R> {\n readonly useCase: MetaUseCase;\n /** Defaults to the group's write guard. */\n readonly guard?: EntityApiGuard;\n readonly idempotent?: EndpointIdempotency;\n readonly handler: (\n request: EntityApiRequest,\n context: EntityApiContext,\n ) => Effect.Effect<unknown, EntifixFailure, R>;\n /**\n * The same verb over a selection, for a record-bound verb that also sits in\n * a collection cell (`alsoAt`): one permission for one act, whether it is\n * asked of a record or of twenty. Required exactly when that cell is\n * declared; its request carries the selection as its payload.\n */\n readonly bulk?: (\n request: EntityApiRequest,\n context: EntityApiContext,\n ) => Effect.Effect<unknown, EntifixFailure, R>;\n}\n\n/** Serve a `@useCase()` class's verb on its entity's endpoints. */\nexport const verbEndpoint = <\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- inferred from the options\n const O extends Omit<VerbEndpoint<any>, 'useCase'>,\n>(\n useCaseClass: UseCaseConstructor,\n options: O,\n): O & { readonly useCase: MetaUseCase } => ({\n ...options,\n useCase: extractUseCase(useCaseClass),\n});\n\n/** What an entity's endpoints serve, and under which rules. */\nexport interface EntityApiOptions<T extends Entity, R> {\n readonly verbs: readonly EntityApiVerb[];\n /** One guard for every verb, or one for the reads and one for the writes. */\n readonly guard:\n | EntityApiGuard\n | { readonly read: EntityApiGuard; readonly write: EntityApiGuard };\n /** Narrows the reads, and the facet counts, to what this caller may see. */\n readonly narrowing?: NarrowingRules<T>;\n /** Serve `$facets` beside the list, under the list's guard and narrowing. */\n readonly facets?: boolean;\n /** Runs on every generic create and update, before the save. */\n readonly prepare?: (\n entity: T,\n context: EntityApiContext & { readonly verb: 'create' | 'update' },\n ) => Effect.Effect<T, EntifixFailure, R>;\n /** The business failures `prepare` may answer with, for the contract. */\n readonly errors?: readonly FailureClass[];\n readonly override?: Partial<Record<EntityApiVerb, EntityVerbOverride<R>>>;\n /**\n * The entity's `@useCase()` verbs, one endpoint each. Every verb the entity\n * declares must be here and none another entity declares, or serving the\n * declaration throws.\n */\n readonly useCases?: readonly VerbEndpoint<R>[];\n}\n\n/**\n * An entity's endpoints as a value: which verbs, behind which guards, with\n * which handlers. Declaring them needs no HTTP — a domain's cell declares, and\n * the shell serves the declaration with `entityApi`\n * ([ADR 0020](../../../../../docs/adr/0020-endpoints-are-descriptors-and-the-shell-builds-an-httpapi.md)).\n */\nexport interface EntityApiDeclaration<\n T extends Entity,\n K extends string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- inferred from the options\n O extends EntityApiOptions<T, any>,\n> {\n readonly entity: EntityConstructor<T>;\n readonly key: K;\n readonly options: O;\n}\n\n/** `O`, with every key `Shape` does not name typed `never`. */\ntype Exact<O, Shape> = O & Record<Exclude<keyof O, keyof Shape>, never>;\n\n/**\n * Declare an entity's endpoints, to be served by the shell. An option the\n * declaration does not know is a compile error rather than a value nobody\n * reads — `compositions` included, which the shell adds when it serves.\n */\nexport const defineEntityApi = <\n T extends Entity,\n const K extends string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- inferred from the options\n const O extends EntityApiOptions<T, any>,\n>(\n entity: EntityConstructor<T>,\n key: K,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- inferred from the options\n options: Exact<O, EntityApiOptions<T, any>>,\n): EntityApiDeclaration<T, K, O> => ({ entity, key, options });\n"],"names":["extractUseCase","verbEndpoint","useCaseClass","options","useCase","defineEntityApi","entity","key"],"mappings":";AAAA,SAQEA,cAAc,QAKT,gBAAgB;AAsGvB,iEAAiE,GACjE,OAAO,MAAMC,eAAe,CAI1BC,cACAC,UAC2C,aACxCA;QACHC,SAASJ,eAAeE;OACvB;AAiDH;;;;CAIC,GACD,OAAO,MAAMG,kBAAkB,CAM7BC,QACAC,KACA,2FAA2F;AAC3FJ,UACmC,CAAA;QAAEG;QAAQC;QAAKJ;IAAQ,CAAA,EAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-api/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/entity-api/index.ts"],"sourcesContent":["export * from './entity-api.js';\n"],"names":[],"mappings":"AAAA,cAAc,kBAAkB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type FragmentDescriptor } from './fragment.js';
|
|
2
|
+
/** Every fragment a host composes flows from, by pinned reference. */
|
|
3
|
+
export interface FragmentCatalog {
|
|
4
|
+
readonly get: (ref: string) => FragmentDescriptor | undefined;
|
|
5
|
+
readonly refs: readonly string[];
|
|
6
|
+
readonly fragments: readonly FragmentDescriptor[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Assembles a catalog. Two fragments claiming one `id@version` throw: a pinned
|
|
10
|
+
* reference must name exactly one contract, or a slip pinned to it means
|
|
11
|
+
* whichever module happened to load last.
|
|
12
|
+
*/
|
|
13
|
+
export declare const defineCatalog: (fragments: readonly FragmentDescriptor[]) => FragmentCatalog;
|
|
14
|
+
/**
|
|
15
|
+
* The catalog as a JSON document: every fragment with its contract as JSON
|
|
16
|
+
* Schema and its fingerprint, ordered by reference so it is snapshot-stable. It
|
|
17
|
+
* is what a CI check diffs, and what a register check reads without importing
|
|
18
|
+
* the packages that declare the fragments.
|
|
19
|
+
*/
|
|
20
|
+
export declare const catalogDocument: (catalog: FragmentCatalog) => {
|
|
21
|
+
fragments: {
|
|
22
|
+
readOnly: boolean;
|
|
23
|
+
irreversible: boolean;
|
|
24
|
+
isolation: import("./fragment.js").FragmentIsolation;
|
|
25
|
+
requires: import("./fragment.js").FragmentRequirements;
|
|
26
|
+
excludedFrom: Readonly<Record<string, readonly string[]>>;
|
|
27
|
+
consumes: {
|
|
28
|
+
[k: string]: unknown;
|
|
29
|
+
};
|
|
30
|
+
provides: {
|
|
31
|
+
[k: string]: unknown;
|
|
32
|
+
};
|
|
33
|
+
contract: {
|
|
34
|
+
input: unknown;
|
|
35
|
+
output: unknown;
|
|
36
|
+
failure: unknown;
|
|
37
|
+
};
|
|
38
|
+
fingerprint: string;
|
|
39
|
+
compensation?: import("./fragment.js").FragmentCall | undefined;
|
|
40
|
+
call?: import("./fragment.js").FragmentCall | undefined;
|
|
41
|
+
participant?: string | undefined;
|
|
42
|
+
ref: string;
|
|
43
|
+
id: string;
|
|
44
|
+
version: number;
|
|
45
|
+
module: string;
|
|
46
|
+
kind: import("./fragment.js").FragmentKind;
|
|
47
|
+
cardinality: import("./fragment.js").FragmentCardinality;
|
|
48
|
+
}[];
|
|
49
|
+
};
|
|
50
|
+
export type CatalogDocument = ReturnType<typeof catalogDocument>;
|
|
51
|
+
//# sourceMappingURL=catalog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/flow/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,EAKxB,MAAM,eAAe,CAAC;AAEvB,sEAAsE;AACtE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,kBAAkB,GAAG,SAAS,CAAC;IAC9D,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,QAAQ,CAAC,SAAS,EAAE,SAAS,kBAAkB,EAAE,CAAC;CACnD;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,GACxB,WAAW,SAAS,kBAAkB,EAAE,KACvC,eAkBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6BtD,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC"}
|