@opensaas/stack-core 0.44.0-alpha.6 → 0.44.0-alpha.7
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/CHANGELOG.md +655 -0
- package/dist/.build-hash +1 -1
- package/dist/access/actor.d.ts +56 -0
- package/dist/access/actor.d.ts.map +1 -0
- package/dist/access/actor.js +66 -0
- package/dist/access/actor.js.map +1 -0
- package/dist/access/decide-advertisement.d.ts +55 -0
- package/dist/access/decide-advertisement.d.ts.map +1 -0
- package/dist/access/decide-advertisement.js +125 -0
- package/dist/access/decide-advertisement.js.map +1 -0
- package/dist/access/declared-dependencies.d.ts +4 -89
- package/dist/access/declared-dependencies.d.ts.map +1 -1
- package/dist/access/declared-dependencies.js +14 -84
- package/dist/access/declared-dependencies.js.map +1 -1
- package/dist/access/depth-limits.d.ts +2 -0
- package/dist/access/depth-limits.d.ts.map +1 -1
- package/dist/access/depth-limits.js +2 -0
- package/dist/access/depth-limits.js.map +1 -1
- package/dist/access/engine.d.ts +1 -24
- package/dist/access/engine.d.ts.map +1 -1
- package/dist/access/engine.js +0 -35
- package/dist/access/engine.js.map +1 -1
- package/dist/access/field-access.d.ts +21 -19
- package/dist/access/field-access.d.ts.map +1 -1
- package/dist/access/field-access.js +100 -122
- package/dist/access/field-access.js.map +1 -1
- package/dist/access/index.d.ts +3 -6
- package/dist/access/index.d.ts.map +1 -1
- package/dist/access/index.js +3 -6
- package/dist/access/index.js.map +1 -1
- package/dist/access/rule-item.d.ts +7 -7
- package/dist/access/rule-item.d.ts.map +1 -1
- package/dist/access/rule-item.js +38 -38
- package/dist/access/rule-item.js.map +1 -1
- package/dist/access/session-vocabulary.d.ts +124 -0
- package/dist/access/session-vocabulary.d.ts.map +1 -0
- package/dist/access/session-vocabulary.js +366 -0
- package/dist/access/session-vocabulary.js.map +1 -0
- package/dist/access/transaction-registry.d.ts +6 -0
- package/dist/access/transaction-registry.d.ts.map +1 -1
- package/dist/access/transaction-registry.js +19 -2
- package/dist/access/transaction-registry.js.map +1 -1
- package/dist/access/types.d.ts +55 -6
- package/dist/access/types.d.ts.map +1 -1
- package/dist/access/vocabulary-filter-specs.d.ts +17 -0
- package/dist/access/vocabulary-filter-specs.d.ts.map +1 -0
- package/dist/access/vocabulary-filter-specs.js +27 -0
- package/dist/access/vocabulary-filter-specs.js.map +1 -0
- package/dist/client-safe.d.ts +8 -0
- package/dist/client-safe.d.ts.map +1 -0
- package/dist/client-safe.js +8 -0
- package/dist/client-safe.js.map +1 -0
- package/dist/config/field-kind.d.ts +7 -21
- package/dist/config/field-kind.d.ts.map +1 -1
- package/dist/config/field-kind.js +8 -5
- package/dist/config/field-kind.js.map +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +3 -2
- package/dist/config/index.js.map +1 -1
- package/dist/config/mcp-default-tools.d.ts +3 -0
- package/dist/config/mcp-default-tools.d.ts.map +1 -0
- package/dist/config/mcp-default-tools.js +14 -0
- package/dist/config/mcp-default-tools.js.map +1 -0
- package/dist/config/mcp-tool-names.d.ts +2 -0
- package/dist/config/mcp-tool-names.d.ts.map +1 -1
- package/dist/config/mcp-tool-names.js +17 -5
- package/dist/config/mcp-tool-names.js.map +1 -1
- package/dist/config/nav-count.d.ts +8 -16
- package/dist/config/nav-count.d.ts.map +1 -1
- package/dist/config/nav-count.js +20 -32
- package/dist/config/nav-count.js.map +1 -1
- package/dist/config/plugin-engine.d.ts.map +1 -1
- package/dist/config/plugin-engine.js +6 -0
- package/dist/config/plugin-engine.js.map +1 -1
- package/dist/config/types.d.ts +141 -28
- package/dist/config/types.d.ts.map +1 -1
- package/dist/context/engine-context.d.ts +2 -0
- package/dist/context/engine-context.d.ts.map +1 -1
- package/dist/context/engine-context.js +8 -0
- package/dist/context/engine-context.js.map +1 -1
- package/dist/context/hook-pipeline.d.ts +2 -1
- package/dist/context/hook-pipeline.d.ts.map +1 -1
- package/dist/context/hook-pipeline.js +4 -2
- package/dist/context/hook-pipeline.js.map +1 -1
- package/dist/context/index.d.ts +5 -1
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +156 -84
- package/dist/context/index.js.map +1 -1
- package/dist/context/junction.d.ts +28 -21
- package/dist/context/junction.d.ts.map +1 -1
- package/dist/context/junction.js +74 -69
- package/dist/context/junction.js.map +1 -1
- package/dist/context/plugin-field-write.d.ts.map +1 -1
- package/dist/context/plugin-field-write.js +19 -25
- package/dist/context/plugin-field-write.js.map +1 -1
- package/dist/context/process-lifetime.d.ts +8 -0
- package/dist/context/process-lifetime.d.ts.map +1 -0
- package/dist/context/process-lifetime.js +14 -0
- package/dist/context/process-lifetime.js.map +1 -0
- package/dist/context/relationship-input.d.ts +2 -15
- package/dist/context/relationship-input.d.ts.map +1 -1
- package/dist/context/relationship-input.js +44 -85
- package/dist/context/relationship-input.js.map +1 -1
- package/dist/context/transaction-boundary.d.ts +3 -2
- package/dist/context/transaction-boundary.d.ts.map +1 -1
- package/dist/context/transaction-boundary.js +17 -2
- package/dist/context/transaction-boundary.js.map +1 -1
- package/dist/context/write-pipeline.d.ts +5 -19
- package/dist/context/write-pipeline.d.ts.map +1 -1
- package/dist/context/write-pipeline.js +40 -98
- package/dist/context/write-pipeline.js.map +1 -1
- package/dist/context/write-target.d.ts +52 -0
- package/dist/context/write-target.d.ts.map +1 -0
- package/dist/context/write-target.js +118 -0
- package/dist/context/write-target.js.map +1 -0
- package/dist/contract/dependencies.d.ts +24 -10
- package/dist/contract/dependencies.d.ts.map +1 -1
- package/dist/contract/dependencies.js +120 -80
- package/dist/contract/dependencies.js.map +1 -1
- package/dist/contract/derive.d.ts +6 -11
- package/dist/contract/derive.d.ts.map +1 -1
- package/dist/contract/derive.js +256 -211
- package/dist/contract/derive.js.map +1 -1
- package/dist/contract/id-boundary.js +1 -1
- package/dist/contract/id-boundary.js.map +1 -1
- package/dist/contract/strategy.d.ts +13 -0
- package/dist/contract/strategy.d.ts.map +1 -0
- package/dist/contract/strategy.js +38 -0
- package/dist/contract/strategy.js.map +1 -0
- package/dist/db/client-alias.d.ts +12 -0
- package/dist/db/client-alias.d.ts.map +1 -0
- package/dist/db/client-alias.js +8 -0
- package/dist/db/client-alias.js.map +1 -0
- package/dist/extend.d.ts +1 -1
- package/dist/extend.d.ts.map +1 -1
- package/dist/extend.js.map +1 -1
- package/dist/fields/index.d.ts +0 -3
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +25 -10
- package/dist/fields/index.js.map +1 -1
- package/dist/filter/collect.d.ts +28 -39
- package/dist/filter/collect.d.ts.map +1 -1
- package/dist/filter/collect.js +46 -68
- package/dist/filter/collect.js.map +1 -1
- package/dist/filter/index.d.ts +1 -1
- package/dist/filter/index.d.ts.map +1 -1
- package/dist/filter/index.js +1 -1
- package/dist/filter/index.js.map +1 -1
- package/dist/filter/map.d.ts +4 -0
- package/dist/filter/map.d.ts.map +1 -1
- package/dist/filter/map.js +36 -3
- package/dist/filter/map.js.map +1 -1
- package/dist/hooks/index.d.ts +4 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +34 -5
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +5 -2
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +4 -5
- package/dist/internal.js.map +1 -1
- package/dist/mcp/advertise.d.ts +5 -13
- package/dist/mcp/advertise.d.ts.map +1 -1
- package/dist/mcp/advertise.js +7 -22
- package/dist/mcp/advertise.js.map +1 -1
- package/dist/mcp/arguments.d.ts.map +1 -1
- package/dist/mcp/arguments.js +8 -11
- package/dist/mcp/arguments.js.map +1 -1
- package/dist/mcp/field-schema.d.ts +4 -15
- package/dist/mcp/field-schema.d.ts.map +1 -1
- package/dist/mcp/field-schema.js +41 -71
- package/dist/mcp/field-schema.js.map +1 -1
- package/dist/mcp/handler.d.ts +111 -18
- package/dist/mcp/handler.d.ts.map +1 -1
- package/dist/mcp/handler.js +168 -73
- package/dist/mcp/handler.js.map +1 -1
- package/dist/mcp/index.d.ts +2 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/procedure-tools.d.ts +63 -0
- package/dist/mcp/procedure-tools.d.ts.map +1 -0
- package/dist/mcp/procedure-tools.js +139 -0
- package/dist/mcp/procedure-tools.js.map +1 -0
- package/dist/mcp/projection.d.ts.map +1 -1
- package/dist/mcp/projection.js +28 -36
- package/dist/mcp/projection.js.map +1 -1
- package/dist/mcp/reachability.d.ts +5 -1
- package/dist/mcp/reachability.d.ts.map +1 -1
- package/dist/mcp/reachability.js +24 -5
- package/dist/mcp/reachability.js.map +1 -1
- package/dist/mcp/relation-member.d.ts +15 -0
- package/dist/mcp/relation-member.d.ts.map +1 -0
- package/dist/mcp/relation-member.js +50 -0
- package/dist/mcp/relation-member.js.map +1 -0
- package/dist/mcp/types.d.ts +12 -0
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/mcp/where-id-boundary.d.ts.map +1 -1
- package/dist/mcp/where-id-boundary.js +12 -24
- package/dist/mcp/where-id-boundary.js.map +1 -1
- package/dist/mcp/where-reachability.d.ts +2 -0
- package/dist/mcp/where-reachability.d.ts.map +1 -1
- package/dist/mcp/where-reachability.js +39 -23
- package/dist/mcp/where-reachability.js.map +1 -1
- package/dist/members/claims.d.ts +71 -0
- package/dist/members/claims.d.ts.map +1 -0
- package/dist/members/claims.js +199 -0
- package/dist/members/claims.js.map +1 -0
- package/dist/members/list-members.d.ts +87 -0
- package/dist/members/list-members.d.ts.map +1 -0
- package/dist/members/list-members.js +250 -0
- package/dist/members/list-members.js.map +1 -0
- package/dist/procedures/client.d.ts +163 -0
- package/dist/procedures/client.d.ts.map +1 -0
- package/dist/procedures/client.js +152 -0
- package/dist/procedures/client.js.map +1 -0
- package/dist/procedures/codes.d.ts +23 -0
- package/dist/procedures/codes.d.ts.map +1 -0
- package/dist/procedures/codes.js +46 -0
- package/dist/procedures/codes.js.map +1 -0
- package/dist/procedures/context-face.d.ts +4 -0
- package/dist/procedures/context-face.d.ts.map +1 -0
- package/dist/procedures/context-face.js +31 -0
- package/dist/procedures/context-face.js.map +1 -0
- package/dist/procedures/failures.d.ts +31 -0
- package/dist/procedures/failures.d.ts.map +1 -0
- package/dist/procedures/failures.js +51 -0
- package/dist/procedures/failures.js.map +1 -0
- package/dist/procedures/http.d.ts +114 -0
- package/dist/procedures/http.d.ts.map +1 -0
- package/dist/procedures/http.js +224 -0
- package/dist/procedures/http.js.map +1 -0
- package/dist/procedures/index.d.ts +7 -0
- package/dist/procedures/index.d.ts.map +1 -0
- package/dist/procedures/index.js +11 -0
- package/dist/procedures/index.js.map +1 -0
- package/dist/procedures/procedure.d.ts +204 -0
- package/dist/procedures/procedure.d.ts.map +1 -0
- package/dist/procedures/procedure.js +123 -0
- package/dist/procedures/procedure.js.map +1 -0
- package/dist/procedures/result.d.ts +42 -0
- package/dist/procedures/result.d.ts.map +1 -0
- package/dist/procedures/result.js +51 -0
- package/dist/procedures/result.js.map +1 -0
- package/dist/procedures/run.d.ts +36 -0
- package/dist/procedures/run.d.ts.map +1 -0
- package/dist/procedures/run.js +76 -0
- package/dist/procedures/run.js.map +1 -0
- package/dist/procedures/server.d.ts +131 -0
- package/dist/procedures/server.d.ts.map +1 -0
- package/dist/procedures/server.js +115 -0
- package/dist/procedures/server.js.map +1 -0
- package/dist/procedures/validate.d.ts +60 -0
- package/dist/procedures/validate.d.ts.map +1 -0
- package/dist/procedures/validate.js +254 -0
- package/dist/procedures/validate.js.map +1 -0
- package/dist/procedures/web-session.d.ts +12 -0
- package/dist/procedures/web-session.d.ts.map +1 -0
- package/dist/procedures/web-session.js +35 -0
- package/dist/procedures/web-session.js.map +1 -0
- package/dist/query/relationship-options.d.ts +2 -0
- package/dist/query/relationship-options.d.ts.map +1 -1
- package/dist/query/relationship-options.js +3 -2
- package/dist/query/relationship-options.js.map +1 -1
- package/dist/secured/binding.d.ts +84 -0
- package/dist/secured/binding.d.ts.map +1 -0
- package/dist/secured/binding.js +52 -0
- package/dist/secured/binding.js.map +1 -0
- package/dist/secured/declared-branch.d.ts +11 -0
- package/dist/secured/declared-branch.d.ts.map +1 -0
- package/dist/secured/declared-branch.js +17 -0
- package/dist/secured/declared-branch.js.map +1 -0
- package/dist/secured/field-visibility.d.ts +56 -0
- package/dist/secured/field-visibility.d.ts.map +1 -0
- package/dist/secured/field-visibility.js +624 -0
- package/dist/secured/field-visibility.js.map +1 -0
- package/dist/secured/include.d.ts +68 -41
- package/dist/secured/include.d.ts.map +1 -1
- package/dist/secured/include.js +217 -115
- package/dist/secured/include.js.map +1 -1
- package/dist/secured/lower.d.ts.map +1 -1
- package/dist/secured/lower.js +1 -3
- package/dist/secured/lower.js.map +1 -1
- package/dist/secured/read.d.ts +11 -108
- package/dist/secured/read.d.ts.map +1 -1
- package/dist/secured/read.js +46 -633
- package/dist/secured/read.js.map +1 -1
- package/dist/secured/select.d.ts +0 -24
- package/dist/secured/select.d.ts.map +1 -1
- package/dist/secured/select.js +60 -153
- package/dist/secured/select.js.map +1 -1
- package/dist/secured/vocabulary.d.ts +13 -1
- package/dist/secured/vocabulary.d.ts.map +1 -1
- package/dist/secured/vocabulary.js +173 -83
- package/dist/secured/vocabulary.js.map +1 -1
- package/dist/testing/untyped-rule.d.ts +8 -0
- package/dist/testing/untyped-rule.d.ts.map +1 -0
- package/dist/testing/untyped-rule.js +4 -0
- package/dist/testing/untyped-rule.js.map +1 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/rows.d.ts +18 -1
- package/dist/types/rows.d.ts.map +1 -1
- package/dist/validation/config-refusal.d.ts +3 -1
- package/dist/validation/config-refusal.d.ts.map +1 -1
- package/dist/validation/database-config.d.ts +1 -0
- package/dist/validation/database-config.d.ts.map +1 -1
- package/dist/validation/database-config.js +1 -1
- package/dist/validation/database-config.js.map +1 -1
- package/dist/validation/default-value.d.ts.map +1 -1
- package/dist/validation/default-value.js +3 -8
- package/dist/validation/default-value.js.map +1 -1
- package/dist/validation/extension-packs.d.ts.map +1 -1
- package/dist/validation/extension-packs.js +1 -0
- package/dist/validation/extension-packs.js.map +1 -1
- package/dist/validation/field-config.d.ts +3 -2
- package/dist/validation/field-config.d.ts.map +1 -1
- package/dist/validation/field-config.js +3 -1
- package/dist/validation/field-config.js.map +1 -1
- package/dist/validation/field-names.d.ts +2 -1
- package/dist/validation/field-names.d.ts.map +1 -1
- package/dist/validation/field-names.js +76 -63
- package/dist/validation/field-names.js.map +1 -1
- package/dist/validation/identifier-names.d.ts +1 -0
- package/dist/validation/identifier-names.d.ts.map +1 -1
- package/dist/validation/identifier-names.js +17 -9
- package/dist/validation/identifier-names.js.map +1 -1
- package/dist/validation/needs-closure.d.ts +30 -14
- package/dist/validation/needs-closure.d.ts.map +1 -1
- package/dist/validation/needs-closure.js +184 -31
- package/dist/validation/needs-closure.js.map +1 -1
- package/dist/validation/schema.d.ts +1 -0
- package/dist/validation/schema.d.ts.map +1 -1
- package/dist/validation/schema.js +4 -2
- package/dist/validation/schema.js.map +1 -1
- package/package.json +20 -4
- package/dist/access/field-visibility.d.ts +0 -11
- package/dist/access/field-visibility.d.ts.map +0 -1
- package/dist/access/field-visibility.js +0 -366
- package/dist/access/field-visibility.js.map +0 -1
- package/dist/access/query-validation.d.ts +0 -30
- package/dist/access/query-validation.d.ts.map +0 -1
- package/dist/access/query-validation.js +0 -49
- package/dist/access/query-validation.js.map +0 -1
- package/dist/access/system-fields.d.ts +0 -2
- package/dist/access/system-fields.d.ts.map +0 -1
- package/dist/access/system-fields.js +0 -7
- package/dist/access/system-fields.js.map +0 -1
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// What each usable name on a list is: its List members.
|
|
2
|
+
import { isComputedField } from '../config/field-kind.js';
|
|
3
|
+
import { resolveIdStrategy, resolveListTimestamps } from '../contract/strategy.js';
|
|
4
|
+
import { refuseNonPascalListKeys } from '../validation/database-config.js';
|
|
5
|
+
import { validateExtensionPacks } from '../validation/extension-packs.js';
|
|
6
|
+
import { validateConfigFields } from '../validation/field-config.js';
|
|
7
|
+
import { validateFieldNames } from '../validation/field-names.js';
|
|
8
|
+
import { refuseNonIdentifierListKeys } from '../validation/identifier-names.js';
|
|
9
|
+
import { validateRelations } from '../validation/relations.js';
|
|
10
|
+
import { MemberClaimRefusal, systemFieldsOf, walkContractMembers, } from './claims.js';
|
|
11
|
+
const NAME_RELATION_REFUSALS = new Set([
|
|
12
|
+
'unknown-relationship-target',
|
|
13
|
+
'inverse-mismatch',
|
|
14
|
+
'many-to-many',
|
|
15
|
+
'self-referencing-field',
|
|
16
|
+
'foreign-key-on-both-sides',
|
|
17
|
+
'non-owning-side-nullability',
|
|
18
|
+
]);
|
|
19
|
+
function collectSynthetics(described) {
|
|
20
|
+
const synthetics = { entries: new Map(), claims: new Map() };
|
|
21
|
+
for (const [sourceList, sourceField, descriptor] of described) {
|
|
22
|
+
if (descriptor.kind !== 'relation' || !descriptor.inverse.synthetic)
|
|
23
|
+
continue;
|
|
24
|
+
const name = descriptor.inverse.field;
|
|
25
|
+
const entries = synthetics.entries.get(descriptor.target) ?? [];
|
|
26
|
+
entries.push({ name, sourceList, sourceField });
|
|
27
|
+
synthetics.entries.set(descriptor.target, entries);
|
|
28
|
+
if (descriptor.foreignKey === undefined)
|
|
29
|
+
continue;
|
|
30
|
+
const claims = synthetics.claims.get(descriptor.target) ?? [];
|
|
31
|
+
claims.push({ name, sourceList, sourceField, foreignKey: descriptor.foreignKey.name });
|
|
32
|
+
synthetics.claims.set(descriptor.target, claims);
|
|
33
|
+
}
|
|
34
|
+
return synthetics;
|
|
35
|
+
}
|
|
36
|
+
class DescriptorFailure extends Error {
|
|
37
|
+
original;
|
|
38
|
+
constructor(original) {
|
|
39
|
+
super('getContractField failed');
|
|
40
|
+
this.original = original;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const states = new WeakMap();
|
|
44
|
+
function refusalError(refusal) {
|
|
45
|
+
return new Error(refusal.message, { cause: refusal.cause ?? refusal });
|
|
46
|
+
}
|
|
47
|
+
function validationRefusal(config) {
|
|
48
|
+
const [fieldError] = validateConfigFields(config).filter((error) => error.missingMember === 'getContractField' || error.missingMember === 'assembleColumns');
|
|
49
|
+
if (fieldError !== undefined)
|
|
50
|
+
return new Error(fieldError.message, { cause: fieldError });
|
|
51
|
+
const [refusal] = [
|
|
52
|
+
...refuseNonPascalListKeys(config),
|
|
53
|
+
...refuseNonIdentifierListKeys(config),
|
|
54
|
+
...validateFieldNames(config),
|
|
55
|
+
...validateExtensionPacks(config).filter((entry) => entry.reason === 'field-descriptor-error'),
|
|
56
|
+
...validateRelations(config).filter((entry) => NAME_RELATION_REFUSALS.has(entry.reason)),
|
|
57
|
+
];
|
|
58
|
+
return refusal === undefined ? undefined : refusalError(refusal);
|
|
59
|
+
}
|
|
60
|
+
function listConfigOf(config, listKey) {
|
|
61
|
+
if (!Object.prototype.hasOwnProperty.call(config.lists, listKey)) {
|
|
62
|
+
throw new Error(`List "${listKey}" is not in the config`);
|
|
63
|
+
}
|
|
64
|
+
return config.lists[listKey];
|
|
65
|
+
}
|
|
66
|
+
function describe(config, state, listKey, fieldKey, field) {
|
|
67
|
+
const key = `${listKey}.${fieldKey}`;
|
|
68
|
+
const known = state.descriptors.get(field)?.get(key);
|
|
69
|
+
if (known !== undefined)
|
|
70
|
+
return known;
|
|
71
|
+
let descriptor;
|
|
72
|
+
if (isComputedField(field, fieldKey, undefined, undefined) || !field.getContractField) {
|
|
73
|
+
descriptor = { kind: 'computed' };
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
try {
|
|
77
|
+
descriptor = field.getContractField(fieldKey, listKey, config);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
throw new DescriptorFailure(error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const byKey = state.descriptors.get(field) ?? new Map();
|
|
84
|
+
byKey.set(key, descriptor);
|
|
85
|
+
state.descriptors.set(field, byKey);
|
|
86
|
+
return descriptor;
|
|
87
|
+
}
|
|
88
|
+
function* describeAll(config, state) {
|
|
89
|
+
for (const [listKey, listConfig] of Object.entries(config.lists)) {
|
|
90
|
+
for (const [fieldKey, field] of Object.entries(listConfig.fields ?? {})) {
|
|
91
|
+
yield [listKey, fieldKey, describe(config, state, listKey, fieldKey, field)];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function syntheticsOf(config, state) {
|
|
96
|
+
state.synthetics ??= collectSynthetics(describeAll(config, state)).entries;
|
|
97
|
+
return state.synthetics;
|
|
98
|
+
}
|
|
99
|
+
function claimRefusal(config, state) {
|
|
100
|
+
try {
|
|
101
|
+
walkContractMembers(config, {
|
|
102
|
+
describe: (listKey, fieldKey, field) => describe(config, state, listKey, fieldKey, field),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error instanceof MemberClaimRefusal)
|
|
107
|
+
return error;
|
|
108
|
+
if (error instanceof DescriptorFailure) {
|
|
109
|
+
return error.original instanceof Error
|
|
110
|
+
? error.original
|
|
111
|
+
: new Error(String(error.original), { cause: error.original });
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
function stateOf(config) {
|
|
118
|
+
const known = states.get(config);
|
|
119
|
+
if (known !== undefined)
|
|
120
|
+
return known;
|
|
121
|
+
const state = {
|
|
122
|
+
refusal: validationRefusal(config),
|
|
123
|
+
descriptors: new WeakMap(),
|
|
124
|
+
synthetics: undefined,
|
|
125
|
+
lists: new Map(),
|
|
126
|
+
};
|
|
127
|
+
state.refusal ??= claimRefusal(config, state);
|
|
128
|
+
states.set(config, state);
|
|
129
|
+
return state;
|
|
130
|
+
}
|
|
131
|
+
function memberColumn(column) {
|
|
132
|
+
return { name: column.name, column: column.map ?? column.name };
|
|
133
|
+
}
|
|
134
|
+
function storageOf(descriptor) {
|
|
135
|
+
switch (descriptor.kind) {
|
|
136
|
+
case 'column':
|
|
137
|
+
return { kind: 'column', column: descriptor.map ?? descriptor.name };
|
|
138
|
+
case 'columns': {
|
|
139
|
+
const parts = descriptor.columns.map(memberColumn);
|
|
140
|
+
return { kind: 'columns', parts, partNames: parts.map((part) => part.name) };
|
|
141
|
+
}
|
|
142
|
+
case 'relation':
|
|
143
|
+
return {
|
|
144
|
+
kind: 'relation',
|
|
145
|
+
relatedList: descriptor.target,
|
|
146
|
+
many: descriptor.many,
|
|
147
|
+
foreignKey: descriptor.foreignKey === undefined ? undefined : memberColumn(descriptor.foreignKey),
|
|
148
|
+
inverse: descriptor.inverse,
|
|
149
|
+
};
|
|
150
|
+
case 'computed':
|
|
151
|
+
return { kind: 'computed' };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function buildListMembers(config, listKey, describeField, synthetics) {
|
|
155
|
+
const listConfig = listConfigOf(config, listKey);
|
|
156
|
+
const fields = Object.entries(listConfig.fields ?? {}).map(([fieldKey, field]) => ({
|
|
157
|
+
fieldKey,
|
|
158
|
+
field,
|
|
159
|
+
descriptor: describeField(listKey, fieldKey, field),
|
|
160
|
+
}));
|
|
161
|
+
const members = new Map();
|
|
162
|
+
const add = (member) => {
|
|
163
|
+
if (!members.has(member.name))
|
|
164
|
+
members.set(member.name, member);
|
|
165
|
+
};
|
|
166
|
+
for (const { fieldKey, field, descriptor } of fields) {
|
|
167
|
+
add({ kind: 'field', name: fieldKey, field, storage: storageOf(descriptor) });
|
|
168
|
+
}
|
|
169
|
+
const systemFields = systemFieldsOf(resolveListTimestamps(listConfig, config.db));
|
|
170
|
+
for (const name of systemFields)
|
|
171
|
+
add({ kind: 'systemField', name });
|
|
172
|
+
for (const { fieldKey, descriptor } of fields) {
|
|
173
|
+
if (descriptor.kind === 'relation' && descriptor.foreignKey !== undefined) {
|
|
174
|
+
add({
|
|
175
|
+
kind: 'foreignKey',
|
|
176
|
+
field: fieldKey,
|
|
177
|
+
relatedList: descriptor.target,
|
|
178
|
+
...memberColumn(descriptor.foreignKey),
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
if (descriptor.kind === 'columns') {
|
|
182
|
+
for (const part of descriptor.columns) {
|
|
183
|
+
add({ kind: 'partColumn', field: fieldKey, ...memberColumn(part) });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const entry of synthetics.get(listKey) ?? []) {
|
|
188
|
+
add({ kind: 'syntheticBackRelation', ...entry, many: true });
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
listKey,
|
|
192
|
+
singleton: resolveIdStrategy(listConfig, config) === 'singleton',
|
|
193
|
+
systemFields,
|
|
194
|
+
members,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Every List member of `listKey`. Throws generate's own error for any refusal
|
|
199
|
+
* that changes which names a list has or what they are.
|
|
200
|
+
*/
|
|
201
|
+
export function resolveListMembers(config, listKey) {
|
|
202
|
+
const state = stateOf(config);
|
|
203
|
+
if (state.refusal !== undefined)
|
|
204
|
+
throw state.refusal;
|
|
205
|
+
const known = state.lists.get(listKey);
|
|
206
|
+
if (known !== undefined)
|
|
207
|
+
return known;
|
|
208
|
+
const built = buildListMembers(config, listKey, (list, fieldKey, field) => describe(config, state, list, fieldKey, field), syntheticsOf(config, state));
|
|
209
|
+
state.lists.set(listKey, built);
|
|
210
|
+
return built;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Every list's members, keyed by list key. Throws generate's own error for a
|
|
214
|
+
* config `generate` refuses.
|
|
215
|
+
*/
|
|
216
|
+
export function assertListMembersResolve(config) {
|
|
217
|
+
return new Map(Object.keys(config.lists).map((listKey) => [listKey, resolveListMembers(config, listKey)]));
|
|
218
|
+
}
|
|
219
|
+
/** Every list's List members, built ungated from descriptors already read. A field with none stores nothing. */
|
|
220
|
+
export function listMembersFromDescriptors(config, descriptors) {
|
|
221
|
+
const describeField = (listKey, fieldKey) => descriptors.get(listKey)?.get(fieldKey) ?? { kind: 'computed' };
|
|
222
|
+
const synthetics = collectSynthetics([...descriptors].flatMap(([listKey, fields]) => [...fields].map(([fieldKey, descriptor]) => [
|
|
223
|
+
listKey,
|
|
224
|
+
fieldKey,
|
|
225
|
+
descriptor,
|
|
226
|
+
])));
|
|
227
|
+
return {
|
|
228
|
+
lists: new Map(Object.keys(config.lists).map((listKey) => [
|
|
229
|
+
listKey,
|
|
230
|
+
buildListMembers(config, listKey, describeField, synthetics.entries),
|
|
231
|
+
])),
|
|
232
|
+
syntheticClaims: synthetics.claims,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/** The model field names `fieldKey` stores its value across, or `undefined` unless it is a `columns` field. */
|
|
236
|
+
export function partColumnsOf(members, fieldKey) {
|
|
237
|
+
const member = members.members.get(fieldKey);
|
|
238
|
+
if (member?.kind !== 'field' || member.storage.kind !== 'columns')
|
|
239
|
+
return undefined;
|
|
240
|
+
return member.storage.partNames;
|
|
241
|
+
}
|
|
242
|
+
/** The `default` branch of a position's exhaustive choice over a member's kind. */
|
|
243
|
+
export function unhandledMember(member) {
|
|
244
|
+
throw new Error(`Unhandled List member: ${JSON.stringify(member)}`);
|
|
245
|
+
}
|
|
246
|
+
/** The List member `name` on `listKey`, or `undefined` when the name is not one. */
|
|
247
|
+
export function resolveListMember(config, listKey, name) {
|
|
248
|
+
return resolveListMembers(config, listKey).members.get(name);
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=list-members.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list-members.js","sourceRoot":"","sources":["../../src/members/list-members.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAEzD,OAAO,EAAE,iBAAiB,EAAE,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,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAC5C,IAAI,MAAM,EAAE,IAAI,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IACnF,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAA;AACjC,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,eAAe,CAAC,MAAa;IAC3C,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;AACrE,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,iBAAiB,CAC/B,MAAsB,EACtB,OAAe,EACf,IAAY;IAEZ,OAAO,kBAAkB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AAC9D,CAAC"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type * as z from 'zod';
|
|
2
|
+
import type { Procedure, ProcedureHostContext, ProcedureRegistry } from './procedure.js';
|
|
3
|
+
import type { ProcedureFailure } from './result.js';
|
|
4
|
+
/**
|
|
5
|
+
* A call that produced no result from the procedure pipeline. Its `code` is
|
|
6
|
+
* the lower-case `'transport'`, which no procedure failure code can be: those
|
|
7
|
+
* are upper-case.
|
|
8
|
+
*
|
|
9
|
+
* - `'network'`: the request was not answered, or its answer could not be
|
|
10
|
+
* read. The call may or may not have run.
|
|
11
|
+
* - `'refused'`: the host refused the request before the pipeline ran, and
|
|
12
|
+
* `message` is its own: an unknown or unexposed name (404), a method other
|
|
13
|
+
* than `POST` (405), a body over the host's limit (413) or a content type
|
|
14
|
+
* it does not take (415).
|
|
15
|
+
* - `'malformed'`: an answer arrived that is not the host's: not JSON, or
|
|
16
|
+
* JSON in neither of its shapes under its status, as a proxy's error page
|
|
17
|
+
* or a gateway's `{ "error": … }` under a 502 or 504 is. The call may or
|
|
18
|
+
* may not have run.
|
|
19
|
+
* - `'unsent'`: the client sent nothing, because the input cannot be
|
|
20
|
+
* serialised as JSON (a circular value, a `bigint`, a throwing `toJSON`)
|
|
21
|
+
* or the name is not one a host can serve. The call did not run.
|
|
22
|
+
*/
|
|
23
|
+
export interface ProcedureTransportFailure {
|
|
24
|
+
code: 'transport';
|
|
25
|
+
reason: 'network' | 'refused' | 'malformed' | 'unsent';
|
|
26
|
+
/** The HTTP status, when a response arrived. */
|
|
27
|
+
status?: number;
|
|
28
|
+
message: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* What a call through the client resolves to: the procedure's data, a failure
|
|
32
|
+
* from the pipeline, or a {@link ProcedureTransportFailure}. Switching on
|
|
33
|
+
* `error.code` is exhaustive over the reserved codes, the codes the procedure
|
|
34
|
+
* declares and `'transport'`.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam TData - the procedure's parsed output.
|
|
37
|
+
* @typeParam TCode - the failure codes the procedure declares.
|
|
38
|
+
*/
|
|
39
|
+
export type ProcedureClientResult<TData, TCode extends string = never> = {
|
|
40
|
+
success: true;
|
|
41
|
+
data: TData;
|
|
42
|
+
} | {
|
|
43
|
+
success: false;
|
|
44
|
+
error: ProcedureFailure<TCode> | ProcedureTransportFailure;
|
|
45
|
+
};
|
|
46
|
+
/** The input a caller of `TProcedure` sends: what its input schema accepts. */
|
|
47
|
+
export type ProcedureInputOf<TProcedure> = TProcedure extends Procedure<infer _C, infer TIn, infer _TOut, infer _TCode> ? z.input<TIn> : never;
|
|
48
|
+
/** The data a successful call of `TProcedure` carries. */
|
|
49
|
+
export type ProcedureDataOf<TProcedure> = TProcedure extends Procedure<infer _C, infer _TIn, infer TOut, infer _TCode> ? z.output<TOut> : never;
|
|
50
|
+
/** The failure codes `TProcedure` declares. */
|
|
51
|
+
export type ProcedureCodeOf<TProcedure> = TProcedure extends Procedure<infer _C, infer _TIn, infer _TOut, infer TCode> ? TCode : never;
|
|
52
|
+
/** What the client resolves a call of `TProcedure` to. */
|
|
53
|
+
export type ProcedureClientResultOf<TProcedure> = ProcedureClientResult<ProcedureDataOf<TProcedure>, ProcedureCodeOf<TProcedure>>;
|
|
54
|
+
/**
|
|
55
|
+
* The arguments after the name: the input, which may be left out only when
|
|
56
|
+
* the procedure's input schema accepts `undefined`.
|
|
57
|
+
*/
|
|
58
|
+
export type ProcedureCallArgs<TProcedure> = undefined extends ProcedureInputOf<TProcedure> ? [input?: ProcedureInputOf<TProcedure>] : [input: ProcedureInputOf<TProcedure>];
|
|
59
|
+
type IsUnion<T, TAll = T> = T extends unknown ? ([TAll] extends [T] ? false : true) : never;
|
|
60
|
+
/**
|
|
61
|
+
* The arguments after `TName`. A name typed as a union of names takes no
|
|
62
|
+
* input that type-checks: the input one procedure requires cannot be typed
|
|
63
|
+
* for another, so narrow the name to one procedure first.
|
|
64
|
+
*/
|
|
65
|
+
export type ProcedureArgsFor<TRegistry, TName extends keyof TRegistry> = true extends IsUnion<TName> ? [input: never] : ProcedureCallArgs<TRegistry[TName]>;
|
|
66
|
+
/**
|
|
67
|
+
* The names a client of `TRegistry` may call. A registry type that admits any
|
|
68
|
+
* string as a name, as one annotated `ProcedureRegistry<Context>` or a client
|
|
69
|
+
* created without a type argument does, admits none.
|
|
70
|
+
*/
|
|
71
|
+
export type ProcedureNameOf<TRegistry> = string extends keyof TRegistry ? never : keyof TRegistry & string;
|
|
72
|
+
/**
|
|
73
|
+
* Calls a procedure of `TRegistry` by name. The name, the input and the result
|
|
74
|
+
* are typed from the registry, so an unknown name or a renamed input field is
|
|
75
|
+
* a compile error.
|
|
76
|
+
*/
|
|
77
|
+
export type ProcedureClient<TRegistry extends ProcedureRegistry<ProcedureHostContext>> = <TName extends ProcedureNameOf<TRegistry>>(name: TName, ...args: ProcedureArgsFor<TRegistry, TName>) => Promise<ProcedureClientResultOf<TRegistry[TName]>>;
|
|
78
|
+
/** Options for {@link createProcedureClient}. */
|
|
79
|
+
export interface ProcedureClientOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Where the host is mounted; each call is `POST <baseUrl>/<name>`. Default
|
|
82
|
+
* `/api/procedures`, the host's own default, and a host on the page's own
|
|
83
|
+
* origin needs nothing more.
|
|
84
|
+
*
|
|
85
|
+
* A host on another origin is not reachable with the host alone. Every call
|
|
86
|
+
* is a JSON `POST`, so the browser sends a CORS preflight first, and
|
|
87
|
+
* `createProcedureHandler` answers `OPTIONS` with 405 and sends no
|
|
88
|
+
* `Access-Control-*` headers: the browser blocks the call and it resolves to
|
|
89
|
+
* `'network'`. The application must answer the preflight and add the CORS
|
|
90
|
+
* headers itself (`Access-Control-Allow-Origin` naming this page's origin,
|
|
91
|
+
* `Access-Control-Allow-Credentials: true`, and for the preflight the
|
|
92
|
+
* method and `Content-Type`), in addition to listing this page's origin in
|
|
93
|
+
* the host's `allowedOrigins` and passing `credentials: 'include'`.
|
|
94
|
+
*/
|
|
95
|
+
baseUrl?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Whether the session cookie is sent. Default `'same-origin'`. `'include'`
|
|
98
|
+
* sends it to another origin, which also needs the CORS headers described
|
|
99
|
+
* under `baseUrl`.
|
|
100
|
+
*/
|
|
101
|
+
credentials?: 'same-origin' | 'include';
|
|
102
|
+
/** Sends the request. Default the global `fetch`. */
|
|
103
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* A typed client for an application's procedures, for browser code. Give it
|
|
107
|
+
* the **type** of the registry: the import is erased, so the registry, its
|
|
108
|
+
* schemas and everything `run` reaches stay on the server.
|
|
109
|
+
*
|
|
110
|
+
* A call POSTs JSON to the HTTP host and resolves, without throwing, to one of
|
|
111
|
+
* three outcomes: `{ success: true, data }`, a failure from the pipeline
|
|
112
|
+
* (`error.code` a reserved code or one the procedure declares), or a
|
|
113
|
+
* {@link ProcedureTransportFailure} (`error.code` `'transport'`) when the
|
|
114
|
+
* request was not sent or not answered, the host refused it before the
|
|
115
|
+
* pipeline ran, or the answer is not the host's. The status and the shape are
|
|
116
|
+
* checked together: an answer is read as a result only when it has the
|
|
117
|
+
* result's shape under the status the host gives that result, and a refusal
|
|
118
|
+
* only under one of the host's own refusal statuses (404, 405, 413, 415).
|
|
119
|
+
*
|
|
120
|
+
* The platform `fetch` follows redirects: a 307 or 308 sends the `POST` again
|
|
121
|
+
* to the new location, while a 301, 302 or 303 turns it into a `GET`, which
|
|
122
|
+
* the host answers as a `'refused'` 405.
|
|
123
|
+
*
|
|
124
|
+
* Identity is the session cookie alone. The client sends no `Authorization`
|
|
125
|
+
* header, which the host refuses, and takes no session argument. It sends the
|
|
126
|
+
* input exactly as given: no input is an empty body, which the host reads as
|
|
127
|
+
* `undefined`, so a procedure whose schema requires an object is called with
|
|
128
|
+
* `{}`.
|
|
129
|
+
*
|
|
130
|
+
* The client never retries, `RETRY` included: that code says the transaction
|
|
131
|
+
* rolled back, not that work outside it was undone, and after a `'network'`
|
|
132
|
+
* failure the call may have run. Whether running it again is safe is the
|
|
133
|
+
* caller's decision.
|
|
134
|
+
*
|
|
135
|
+
* The data is typed from the output schema and is not parsed again in the
|
|
136
|
+
* browser; the host has already parsed it. A procedure not exposed to `'web'`
|
|
137
|
+
* is callable in the type and answers as a `'refused'` 404.
|
|
138
|
+
*
|
|
139
|
+
* @typeParam TRegistry - `typeof` the registry object. Declare the registry
|
|
140
|
+
* with `satisfies ProcedureRegistry<Context>` rather than annotating it, so
|
|
141
|
+
* each name keeps its own procedure type. A registry type whose names are
|
|
142
|
+
* any string, an annotated one or none given, leaves no name callable.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* 'use client'
|
|
147
|
+
*
|
|
148
|
+
* import { createProcedureClient } from '@opensaas/stack-core/procedures/client'
|
|
149
|
+
* import type { procedures } from '@/lib/procedures'
|
|
150
|
+
*
|
|
151
|
+
* const call = createProcedureClient<typeof procedures>()
|
|
152
|
+
*
|
|
153
|
+
* export async function createPost(title: string): Promise<string> {
|
|
154
|
+
* const result = await call('createPost', { title })
|
|
155
|
+
* if (result.success) return result.data.id
|
|
156
|
+
* if (result.error.code === 'transport') throw new Error('Could not reach the server')
|
|
157
|
+
* throw new Error(result.error.message)
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
export declare function createProcedureClient<TRegistry extends ProcedureRegistry<ProcedureHostContext>>(options?: ProcedureClientOptions): ProcedureClient<TRegistry>;
|
|
162
|
+
export {};
|
|
163
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/procedures/client.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,KAAK,CAAC,MAAM,KAAK,CAAA;AAQ7B,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AACxF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,yBAAyB;IACxC,IAAI,EAAE,WAAW,CAAA;IACjB,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,QAAQ,CAAA;IACtD,gDAAgD;IAChD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,CAAC,KAAK,EAAE,KAAK,SAAS,MAAM,GAAG,KAAK,IACjE;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,KAAK,CAAA;CAAE,GAC9B;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,GAAG,yBAAyB,CAAA;CAAE,CAAA;AAElF,+EAA+E;AAC/E,MAAM,MAAM,gBAAgB,CAAC,UAAU,IACrC,UAAU,SAAS,SAAS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E,KAAK,CAAC,GAAG,iBAAiB,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AAEpF;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,SAAS,IAAI,MAAM,SAAS,MAAM,SAAS,GACnE,KAAK,GACL,MAAM,SAAS,GAAG,MAAM,CAAA;AAE5B;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,SAAS,SAAS,iBAAiB,CAAC,oBAAoB,CAAC,IAAI,CACvF,KAAK,SAAS,eAAe,CAAC,SAAS,CAAC,EAExC,IAAI,EAAE,KAAK,EACX,GAAG,IAAI,EAAE,gBAAgB,CAAC,SAAS,EAAE,KAAK,CAAC,KACxC,OAAO,CAAC,uBAAuB,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AAEvD,iDAAiD;AACjD,MAAM,WAAW,sBAAsB;IACrC;;;;;;;;;;;;;;OAcG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;OAIG;IACH,WAAW,CAAC,EAAE,aAAa,GAAG,SAAS,CAAA;IACvC,qDAAqD;IACrD,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAChD;AAuGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,SAAS,iBAAiB,CAAC,oBAAoB,CAAC,EAC7F,OAAO,GAAE,sBAA2B,GACnC,eAAe,CAAC,SAAS,CAAC,CAgB5B"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// @opensaas/stack-core/procedures/client
|
|
2
|
+
//
|
|
3
|
+
// The browser client for procedures, importable from a `'use client'` module.
|
|
4
|
+
// Its only runtime import is `codes.ts`, which imports nothing. See ADR-0086.
|
|
5
|
+
import { DECLARED_FAILURE_CODE_PATTERN, FAILURE_STATUS, isRefusalStatus, isReservedFailureCode, PROCEDURE_NAME_PATTERN, } from './codes.js';
|
|
6
|
+
const DEFAULT_BASE_URL = '/api/procedures';
|
|
7
|
+
function isRecord(value) {
|
|
8
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
function isFieldErrors(value) {
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
return true;
|
|
13
|
+
return isRecord(value) && Object.values(value).every((message) => typeof message === 'string');
|
|
14
|
+
}
|
|
15
|
+
/** A success needs no `data` key: an output accepting `undefined` serialises without it. */
|
|
16
|
+
function isSuccess(status, body) {
|
|
17
|
+
return status === 200 && isRecord(body) && body.success === true;
|
|
18
|
+
}
|
|
19
|
+
function isFailure(status, body) {
|
|
20
|
+
if (!isRecord(body) || body.success !== false)
|
|
21
|
+
return false;
|
|
22
|
+
const { error } = body;
|
|
23
|
+
if (!isRecord(error) || typeof error.code !== 'string' || typeof error.message !== 'string') {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (!isFieldErrors(error.fieldErrors))
|
|
27
|
+
return false;
|
|
28
|
+
if (isReservedFailureCode(error.code))
|
|
29
|
+
return status === FAILURE_STATUS[error.code];
|
|
30
|
+
return status === 200 && DECLARED_FAILURE_CODE_PATTERN.test(error.code);
|
|
31
|
+
}
|
|
32
|
+
function isRefusal(status, body) {
|
|
33
|
+
return isRefusalStatus(status) && isRecord(body) && typeof body.error === 'string';
|
|
34
|
+
}
|
|
35
|
+
function transport(reason, message, status) {
|
|
36
|
+
const error = { code: 'transport', reason, message };
|
|
37
|
+
if (status !== undefined)
|
|
38
|
+
error.status = status;
|
|
39
|
+
return { success: false, error };
|
|
40
|
+
}
|
|
41
|
+
function messageOf(error, fallback) {
|
|
42
|
+
return error instanceof Error && error.message !== '' ? error.message : fallback;
|
|
43
|
+
}
|
|
44
|
+
async function exchange(send, baseUrl, name, credentials, input) {
|
|
45
|
+
if (!PROCEDURE_NAME_PATTERN.test(name)) {
|
|
46
|
+
return transport('unsent', `${JSON.stringify(name)} is not a procedure name`);
|
|
47
|
+
}
|
|
48
|
+
let body;
|
|
49
|
+
try {
|
|
50
|
+
body = input === undefined ? undefined : JSON.stringify(input);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return transport('unsent', messageOf(error, 'The input cannot be serialised as JSON'));
|
|
54
|
+
}
|
|
55
|
+
let response;
|
|
56
|
+
try {
|
|
57
|
+
response = await send(new Request(`${baseUrl}/${name}`, {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
headers: { 'content-type': 'application/json' },
|
|
60
|
+
credentials,
|
|
61
|
+
body,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return transport('network', messageOf(error, 'The request was not answered'));
|
|
66
|
+
}
|
|
67
|
+
const { status } = response;
|
|
68
|
+
let text;
|
|
69
|
+
try {
|
|
70
|
+
text = await response.text();
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
return transport('network', messageOf(error, 'The response could not be read'), status);
|
|
74
|
+
}
|
|
75
|
+
let answer;
|
|
76
|
+
try {
|
|
77
|
+
answer = JSON.parse(text);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return transport('malformed', 'The response is not JSON', status);
|
|
81
|
+
}
|
|
82
|
+
if (isSuccess(status, answer))
|
|
83
|
+
return { success: true, data: answer.data };
|
|
84
|
+
if (isFailure(status, answer))
|
|
85
|
+
return { success: false, error: answer.error };
|
|
86
|
+
if (isRefusal(status, answer))
|
|
87
|
+
return transport('refused', answer.error, status);
|
|
88
|
+
return transport('malformed', 'The response is not a procedure result', status);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A typed client for an application's procedures, for browser code. Give it
|
|
92
|
+
* the **type** of the registry: the import is erased, so the registry, its
|
|
93
|
+
* schemas and everything `run` reaches stay on the server.
|
|
94
|
+
*
|
|
95
|
+
* A call POSTs JSON to the HTTP host and resolves, without throwing, to one of
|
|
96
|
+
* three outcomes: `{ success: true, data }`, a failure from the pipeline
|
|
97
|
+
* (`error.code` a reserved code or one the procedure declares), or a
|
|
98
|
+
* {@link ProcedureTransportFailure} (`error.code` `'transport'`) when the
|
|
99
|
+
* request was not sent or not answered, the host refused it before the
|
|
100
|
+
* pipeline ran, or the answer is not the host's. The status and the shape are
|
|
101
|
+
* checked together: an answer is read as a result only when it has the
|
|
102
|
+
* result's shape under the status the host gives that result, and a refusal
|
|
103
|
+
* only under one of the host's own refusal statuses (404, 405, 413, 415).
|
|
104
|
+
*
|
|
105
|
+
* The platform `fetch` follows redirects: a 307 or 308 sends the `POST` again
|
|
106
|
+
* to the new location, while a 301, 302 or 303 turns it into a `GET`, which
|
|
107
|
+
* the host answers as a `'refused'` 405.
|
|
108
|
+
*
|
|
109
|
+
* Identity is the session cookie alone. The client sends no `Authorization`
|
|
110
|
+
* header, which the host refuses, and takes no session argument. It sends the
|
|
111
|
+
* input exactly as given: no input is an empty body, which the host reads as
|
|
112
|
+
* `undefined`, so a procedure whose schema requires an object is called with
|
|
113
|
+
* `{}`.
|
|
114
|
+
*
|
|
115
|
+
* The client never retries, `RETRY` included: that code says the transaction
|
|
116
|
+
* rolled back, not that work outside it was undone, and after a `'network'`
|
|
117
|
+
* failure the call may have run. Whether running it again is safe is the
|
|
118
|
+
* caller's decision.
|
|
119
|
+
*
|
|
120
|
+
* The data is typed from the output schema and is not parsed again in the
|
|
121
|
+
* browser; the host has already parsed it. A procedure not exposed to `'web'`
|
|
122
|
+
* is callable in the type and answers as a `'refused'` 404.
|
|
123
|
+
*
|
|
124
|
+
* @typeParam TRegistry - `typeof` the registry object. Declare the registry
|
|
125
|
+
* with `satisfies ProcedureRegistry<Context>` rather than annotating it, so
|
|
126
|
+
* each name keeps its own procedure type. A registry type whose names are
|
|
127
|
+
* any string, an annotated one or none given, leaves no name callable.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* 'use client'
|
|
132
|
+
*
|
|
133
|
+
* import { createProcedureClient } from '@opensaas/stack-core/procedures/client'
|
|
134
|
+
* import type { procedures } from '@/lib/procedures'
|
|
135
|
+
*
|
|
136
|
+
* const call = createProcedureClient<typeof procedures>()
|
|
137
|
+
*
|
|
138
|
+
* export async function createPost(title: string): Promise<string> {
|
|
139
|
+
* const result = await call('createPost', { title })
|
|
140
|
+
* if (result.success) return result.data.id
|
|
141
|
+
* if (result.error.code === 'transport') throw new Error('Could not reach the server')
|
|
142
|
+
* throw new Error(result.error.message)
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function createProcedureClient(options = {}) {
|
|
147
|
+
const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '');
|
|
148
|
+
const credentials = options.credentials ?? 'same-origin';
|
|
149
|
+
const send = options.fetch ?? ((request) => globalThis.fetch(request));
|
|
150
|
+
return (name, ...args) => exchange(send, baseUrl, name, credentials, args[0]);
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/procedures/client.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAG9E,OAAO,EACL,6BAA6B,EAC7B,cAAc,EACd,eAAe,EACf,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,YAAY,CAAA;AAsInB,MAAM,gBAAgB,GAAG,iBAAiB,CAAA;AAE1C,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC7E,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAA;IACpC,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM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AAM,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,gBAAgB,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;IACzE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,aAAa,CAAA;IACxD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,OAAgB,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAA;IAE/E,OAAO,CACL,IAAW,EACX,GAAG,IAAwC,EAC3C,EAAE,CACF,QAAQ,CACN,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,WAAW,EACX,IAAI,CAAC,CAAC,CAAC,CACR,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The failure codes the stack owns. A procedure cannot declare one of these. */
|
|
2
|
+
export declare const RESERVED_FAILURE_CODES: readonly ['UNAUTHENTICATED', 'DENIED', 'INVALID_INPUT', 'VALIDATION', 'RETRY', 'CONFLICT', 'INTERNAL'];
|
|
3
|
+
export type ReservedFailureCode = (typeof RESERVED_FAILURE_CODES)[number];
|
|
4
|
+
/** Whether `code` is one of the codes the stack owns. */
|
|
5
|
+
export declare function isReservedFailureCode(code: string): code is ReservedFailureCode;
|
|
6
|
+
export declare const FAILURE_STATUS: Readonly<Record<ReservedFailureCode, number>>;
|
|
7
|
+
/** What a procedure name is. A host serves no other. */
|
|
8
|
+
export declare const PROCEDURE_NAME_PATTERN: RegExp;
|
|
9
|
+
/** What a declared failure code is. */
|
|
10
|
+
export declare const DECLARED_FAILURE_CODE_PATTERN: RegExp;
|
|
11
|
+
/**
|
|
12
|
+
* The statuses the HTTP host refuses a request under before the pipeline
|
|
13
|
+
* runs, each answered `{ error: "<message>" }` rather than with the envelope.
|
|
14
|
+
*/
|
|
15
|
+
export declare const REFUSAL_STATUS: {
|
|
16
|
+
readonly NOT_FOUND: 404;
|
|
17
|
+
readonly METHOD_NOT_ALLOWED: 405;
|
|
18
|
+
readonly BODY_TOO_LARGE: 413;
|
|
19
|
+
readonly UNSUPPORTED_MEDIA_TYPE: 415;
|
|
20
|
+
};
|
|
21
|
+
/** Whether `status` is one the HTTP host refuses a request under. */
|
|
22
|
+
export declare function isRefusalStatus(status: number): boolean;
|
|
23
|
+
//# sourceMappingURL=codes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codes.d.ts","sourceRoot":"","sources":["../../src/procedures/codes.ts"],"names":[],"mappings":"AAGA,iFAAiF;AACjF,eAAO,MAAM,sBAAsB,YACjC,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,YAAY,EACZ,OAAO,EACP,UAAU,EACV,UAAU,CACF,CAAA;AAEV,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAA;AAIzE,yDAAyD;AACzD,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,mBAAmB,CAE/E;AAED,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,MAAM,CAAC,CAQxE,CAAA;AAED,wDAAwD;AACxD,eAAO,MAAM,sBAAsB,QAAkC,CAAA;AAErE,uCAAuC;AACvC,eAAO,MAAM,6BAA6B,QAA2B,CAAA;AAErE;;;GAGG;AACH,eAAO,MAAM,cAAc;aACzB,SAAS,EAAE,GAAG;aACd,kBAAkB,EAAE,GAAG;aACvB,cAAc,EAAE,GAAG;aACnB,sBAAsB,EAAE,GAAG;CACnB,CAAA;AAIV,qEAAqE;AACrE,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEvD"}
|