@composable-os/sdk 0.0.0-stage → 0.1.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +109 -0
- package/dist/allowance.d.ts +198 -0
- package/dist/allowance.js +374 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +146 -0
- package/dist/capacity.js +189 -0
- package/dist/contract.d.ts +851 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +211 -0
- package/dist/document.js +726 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +98 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +24 -0
- package/dist/manifest.d.ts +1708 -0
- package/dist/manifest.js +1494 -0
- package/dist/media.d.ts +41 -0
- package/dist/media.js +90 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/recurring.d.ts +114 -0
- package/dist/recurring.js +173 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +48 -0
- package/dist/rich-text.js +184 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +331 -0
- package/dist/sellable.js +746 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +292 -0
- package/dist/states.js +383 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +679 -0
- package/dist/unique.d.ts +20 -0
- package/dist/unique.js +58 -0
- package/package.json +34 -4
- package/src/allowance.ts +522 -0
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +303 -0
- package/src/contract.ts +909 -0
- package/src/document.ts +935 -0
- package/src/field-types.ts +111 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +36 -0
- package/src/manifest.ts +1785 -0
- package/src/media.ts +108 -0
- package/src/permissions.ts +60 -0
- package/src/recurring.ts +321 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +221 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +1138 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +598 -0
- package/src/testing.ts +847 -0
- package/src/unique.ts +81 -0
- package/README.md +0 -3
package/dist/capacity.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// The `capacity` behaviour (stage 26, ADR 0430, ADR 0431): an object declares that its records
|
|
2
|
+
// have a limited number of places, and the host never gives out more than there are, for every
|
|
3
|
+
// actor. Its declaration, its manifest schema and its contract checks live here; the host's side
|
|
4
|
+
// is packages/core/src/capacity/.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { moveOrigins, windowBoundSchema, windowChecks } from "./states.js";
|
|
7
|
+
/**
|
|
8
|
+
* What customers and the public are told about a record's places (staff always see the real
|
|
9
|
+
* numbers): `exact`, the number left; `low`, available, "only N left" at or below `lowAt`, or sold
|
|
10
|
+
* out; `availability`, available or sold out.
|
|
11
|
+
*/
|
|
12
|
+
export const CAPACITY_SHOW = ["exact", "low", "availability"];
|
|
13
|
+
/**
|
|
14
|
+
* The read-only values the host adds to every record of an object with a capacity: places held
|
|
15
|
+
* while someone pays, places taken, and places left (the record's and its pool's, whichever is
|
|
16
|
+
* fewer; null when unlimited). An object with a capacity cannot have fields of these names.
|
|
17
|
+
*/
|
|
18
|
+
export const CAPACITY_VALUES = ["held", "taken", "available"];
|
|
19
|
+
const fieldName = z.string().max(40);
|
|
20
|
+
export const capacityManifestSchema = z
|
|
21
|
+
.object({
|
|
22
|
+
places: fieldName,
|
|
23
|
+
pool: fieldName.optional(),
|
|
24
|
+
show: z.enum(CAPACITY_SHOW).optional(),
|
|
25
|
+
lowAt: z.number().int().min(1).max(1_000_000).optional(),
|
|
26
|
+
releaseOnRefund: z.boolean().optional(),
|
|
27
|
+
})
|
|
28
|
+
.strict();
|
|
29
|
+
/** Reports problems with `objects.<name>.capacity` through `contract`. */
|
|
30
|
+
export function checkObjectCapacity(m, name, object, contract) {
|
|
31
|
+
const capacity = object.capacity;
|
|
32
|
+
if (!capacity)
|
|
33
|
+
return;
|
|
34
|
+
const path = `objects.${name}.capacity`;
|
|
35
|
+
const fieldOf = (local) => Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
36
|
+
const places = fieldOf(capacity.places);
|
|
37
|
+
if (places?.type !== "number" || places.many) {
|
|
38
|
+
contract(`${path}.places`, `"${capacity.places}" must be a number field of ${name}`);
|
|
39
|
+
}
|
|
40
|
+
for (const reserved of CAPACITY_VALUES) {
|
|
41
|
+
if (Object.hasOwn(object.fields, reserved)) {
|
|
42
|
+
contract(`objects.${name}.fields.${reserved}`, `"${reserved}" is a value the host adds to an object with a capacity; rename the field`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (capacity.pool !== undefined) {
|
|
46
|
+
const pool = fieldOf(capacity.pool);
|
|
47
|
+
const target = pool?.type === "relation" && !pool.many ? pool.to : undefined;
|
|
48
|
+
const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
|
|
49
|
+
const targetObject = local !== undefined && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
50
|
+
if (!targetObject) {
|
|
51
|
+
contract(`${path}.pool`, `"${capacity.pool}" must be a relation field (one record) to another of this extension's objects`);
|
|
52
|
+
}
|
|
53
|
+
else if (local === name) {
|
|
54
|
+
contract(`${path}.pool`, "An object cannot be its own pool");
|
|
55
|
+
}
|
|
56
|
+
else if (!targetObject.capacity) {
|
|
57
|
+
contract(`${path}.pool`, `The pool's object ${target} must declare a capacity`);
|
|
58
|
+
}
|
|
59
|
+
else if (targetObject.capacity.pool !== undefined) {
|
|
60
|
+
contract(`${path}.pool`, `The pool's object ${target} has a pool of its own; capacity has one level of pool`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (capacity.lowAt !== undefined && capacity.show !== "low") {
|
|
64
|
+
contract(`${path}.lowAt`, 'lowAt is the threshold of show: "low"');
|
|
65
|
+
}
|
|
66
|
+
if (capacity.show === "low" && capacity.lowAt === undefined) {
|
|
67
|
+
contract(`${path}.lowAt`, 'show: "low" needs lowAt, the number at or below which it says so');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export const waitlistManifestSchema = z
|
|
71
|
+
.object({
|
|
72
|
+
state: z.string().min(1).max(100),
|
|
73
|
+
promote: z.string().min(1).max(100),
|
|
74
|
+
until: windowBoundSchema,
|
|
75
|
+
promotedAt: fieldName.optional(),
|
|
76
|
+
passedOverAt: fieldName.optional(),
|
|
77
|
+
passedOverReason: fieldName.optional(),
|
|
78
|
+
})
|
|
79
|
+
.strict();
|
|
80
|
+
export const claimsManifestSchema = z
|
|
81
|
+
.object({
|
|
82
|
+
on: fieldName,
|
|
83
|
+
held: z.array(z.string().min(1).max(100)).max(20).optional(),
|
|
84
|
+
confirmed: z.array(z.string().min(1).max(100)).max(20).optional(),
|
|
85
|
+
waitlist: waitlistManifestSchema.optional(),
|
|
86
|
+
})
|
|
87
|
+
.strict();
|
|
88
|
+
/** Reports problems with `objects.<name>.claims` through `contract`. */
|
|
89
|
+
export function checkObjectClaims(m, name, object, contract) {
|
|
90
|
+
const claims = object.claims;
|
|
91
|
+
if (!claims)
|
|
92
|
+
return;
|
|
93
|
+
const path = `objects.${name}.claims`;
|
|
94
|
+
const on = Object.hasOwn(object.fields, claims.on) ? object.fields[claims.on] : undefined;
|
|
95
|
+
const target = on?.type === "relation" && !on.many ? on.to : undefined;
|
|
96
|
+
const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
|
|
97
|
+
const targetObject = local !== undefined && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
98
|
+
if (!targetObject || local === name) {
|
|
99
|
+
contract(`${path}.on`, `"${claims.on}" must be a relation field (one record) to another of this extension's objects`);
|
|
100
|
+
}
|
|
101
|
+
else if (!targetObject.capacity) {
|
|
102
|
+
contract(`${path}.on`, `${target} must declare a capacity for its places to be claimed`);
|
|
103
|
+
}
|
|
104
|
+
const held = claims.held ?? [];
|
|
105
|
+
const confirmed = claims.confirmed ?? [];
|
|
106
|
+
if (!held.length && !confirmed.length) {
|
|
107
|
+
contract(path, "Name the states that hold a place (held) or take one (confirmed)");
|
|
108
|
+
}
|
|
109
|
+
const states = object.states;
|
|
110
|
+
if (!states) {
|
|
111
|
+
contract(path, `${name} must declare states: its states hold and release the claim`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const field = Object.hasOwn(object.fields, states.field)
|
|
115
|
+
? object.fields[states.field]
|
|
116
|
+
: undefined;
|
|
117
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
118
|
+
for (const [key, list] of [
|
|
119
|
+
["held", held],
|
|
120
|
+
["confirmed", confirmed],
|
|
121
|
+
]) {
|
|
122
|
+
if (new Set(list).size !== list.length)
|
|
123
|
+
contract(`${path}.${key}`, "List each state once");
|
|
124
|
+
for (const state of list) {
|
|
125
|
+
if (!values.has(state))
|
|
126
|
+
contract(`${path}.${key}`, `"${state}" is not a state of ${name}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const both = held.filter((state) => confirmed.includes(state));
|
|
130
|
+
if (both.length) {
|
|
131
|
+
contract(path, `"${both[0]}" cannot both hold and take a place`);
|
|
132
|
+
}
|
|
133
|
+
states.moves.forEach((move, i) => {
|
|
134
|
+
const from = typeof move.from === "string" ? [move.from] : move.from;
|
|
135
|
+
if (held.includes(move.to) && from.some((state) => confirmed.includes(state))) {
|
|
136
|
+
contract(`objects.${name}.states.moves.${i}`, `A move from a state that takes a place cannot go back to one that only holds it ("${move.to}")`);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
const waitlist = claims.waitlist;
|
|
140
|
+
if (!waitlist)
|
|
141
|
+
return;
|
|
142
|
+
// A waitlist (ADR 0520).
|
|
143
|
+
const wp = `${path}.waitlist`;
|
|
144
|
+
if (!values.has(waitlist.state)) {
|
|
145
|
+
contract(`${wp}.state`, `"${waitlist.state}" is not a state of ${name}`);
|
|
146
|
+
}
|
|
147
|
+
else if (held.includes(waitlist.state) || confirmed.includes(waitlist.state)) {
|
|
148
|
+
contract(`${wp}.state`, `"${waitlist.state}" waits: it can neither hold nor take a place`);
|
|
149
|
+
}
|
|
150
|
+
else if (states.moves.some((move) => move.final && move.to === waitlist.state)) {
|
|
151
|
+
contract(`${wp}.state`, `"${waitlist.state}" is final: nobody waits in it`);
|
|
152
|
+
}
|
|
153
|
+
else if (states.initial !== waitlist.state &&
|
|
154
|
+
!Object.hasOwn(states.create?.in ?? {}, waitlist.state)) {
|
|
155
|
+
contract(`${wp}.state`, `"${waitlist.state}" must be a state ${name} may be created in (states.create.in): a waiter joins by being made waiting`);
|
|
156
|
+
}
|
|
157
|
+
if (!confirmed.includes(waitlist.promote)) {
|
|
158
|
+
contract(`${wp}.promote`, `"${waitlist.promote}" must be one of claims.confirmed`);
|
|
159
|
+
}
|
|
160
|
+
const promotions = states.moves.filter((move) => move.to === waitlist.promote && moveOrigins(move).includes(waitlist.state));
|
|
161
|
+
if (!promotions.some((move) => move.by.includes("host"))) {
|
|
162
|
+
contract(`${wp}.promote`, `Declare a move from "${waitlist.state}" to "${waitlist.promote}" by the host: promotion is the host's`);
|
|
163
|
+
}
|
|
164
|
+
// A customer may move their own waiting record in, but only onto a free place: the host's
|
|
165
|
+
// promotion alone takes a place the waitlist holds, so nobody moves themselves ahead of it.
|
|
166
|
+
// Their move needs the create's window, so they cannot claim a place booking would refuse.
|
|
167
|
+
promotions.forEach((move) => {
|
|
168
|
+
if (move.by.includes("customer") && !move.window) {
|
|
169
|
+
contract(`${wp}.promote`, `A customer's move from "${waitlist.state}" to "${waitlist.promote}" needs a window (the create's): it books them`);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
windowChecks(m, object, name, contract).checkBound(waitlist.until, `${wp}.until`);
|
|
173
|
+
for (const [key, type] of [
|
|
174
|
+
["promotedAt", "datetime"],
|
|
175
|
+
["passedOverAt", "datetime"],
|
|
176
|
+
["passedOverReason", "text"],
|
|
177
|
+
]) {
|
|
178
|
+
const local = waitlist[key];
|
|
179
|
+
if (local === undefined)
|
|
180
|
+
continue;
|
|
181
|
+
const field = Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
182
|
+
if (field?.type !== type || field.many || !field.readOnly) {
|
|
183
|
+
contract(`${wp}.${key}`, `"${local}" must be a read-only ${type} field of ${name}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (targetObject?.capacity?.pool !== undefined) {
|
|
187
|
+
contract(`${wp}`, `A waitlist is on places with no pool; ${target} has one`);
|
|
188
|
+
}
|
|
189
|
+
}
|