@fabrika/app 0.9.0-dev.14 → 0.9.0-dev.15

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.
Files changed (64) hide show
  1. package/package.json +14 -9
  2. package/skill/SKILL.md +2 -2
  3. package/skill/references/app-setup.md +10 -9
  4. package/skill/references/endpoint.md +8 -1
  5. package/src/build-contract-child.ts +16 -0
  6. package/src/build.ts +48 -2
  7. package/src/client/contract.ts +6 -4
  8. package/src/cloudflare/application.ts +3 -0
  9. package/src/contract/artifact-schema.ts +355 -301
  10. package/src/contract/attachment.ts +4 -3
  11. package/src/contract/compile.ts +3 -2
  12. package/src/contract/context.ts +2 -3
  13. package/src/contract/define.ts +17 -5
  14. package/src/contract/group.ts +8 -7
  15. package/src/contract/index.ts +1 -1
  16. package/src/contract/kit.ts +2 -2
  17. package/src/contract/problems.ts +4 -0
  18. package/src/contract/schema-shape.ts +14 -2
  19. package/src/contract/token.ts +4 -3
  20. package/src/from-row.ts +12 -6
  21. package/src/implement/approved.ts +57 -0
  22. package/src/implement/attachment.ts +2 -1
  23. package/src/implement/boot.ts +90 -12
  24. package/src/implement/dispatch.ts +14 -10
  25. package/src/implement/egress.ts +7 -6
  26. package/src/implement/faults.ts +1 -0
  27. package/src/implement/project.ts +3 -1
  28. package/src/implement/tokens.ts +3 -2
  29. package/src/implement/types.ts +18 -6
  30. package/src/react/hooks.ts +12 -11
  31. package/src/react/provider.tsx +2 -1
  32. package/src/react/types.ts +2 -2
  33. package/src/schema/builder.ts +146 -0
  34. package/src/schema/data-schema.ts +365 -0
  35. package/src/schema/index.ts +18 -0
  36. package/src/schema/replay.ts +623 -0
  37. package/src/testing/kit.ts +3 -3
  38. package/types/src/build-contract-child.d.ts +1 -0
  39. package/types/src/build.d.ts +16 -1
  40. package/types/src/client/contract.d.ts +3 -4
  41. package/types/src/cloudflare/application.d.ts +2 -0
  42. package/types/src/contract/artifact-schema.d.ts +8 -0
  43. package/types/src/contract/attachment.d.ts +3 -2
  44. package/types/src/contract/context.d.ts +2 -3
  45. package/types/src/contract/define.d.ts +14 -5
  46. package/types/src/contract/group.d.ts +5 -6
  47. package/types/src/contract/index.d.ts +1 -1
  48. package/types/src/contract/kit.d.ts +2 -3
  49. package/types/src/contract/problems.d.ts +1 -0
  50. package/types/src/contract/schema-shape.d.ts +3 -2
  51. package/types/src/contract/token.d.ts +3 -3
  52. package/types/src/from-row.d.ts +9 -5
  53. package/types/src/implement/approved.d.ts +11 -0
  54. package/types/src/implement/boot.d.ts +6 -0
  55. package/types/src/implement/faults.d.ts +1 -0
  56. package/types/src/implement/project.d.ts +2 -1
  57. package/types/src/implement/types.d.ts +4 -5
  58. package/types/src/react/hooks.d.ts +4 -5
  59. package/types/src/react/types.d.ts +2 -2
  60. package/types/src/schema/builder.d.ts +45 -0
  61. package/types/src/schema/data-schema.d.ts +124 -0
  62. package/types/src/schema/index.d.ts +3 -0
  63. package/types/src/schema/replay.d.ts +27 -0
  64. package/types/src/testing/kit.d.ts +2 -3
@@ -0,0 +1,623 @@
1
+ // One step of a `z` chain of the contract subset, replayed against zod itself: a factory, or a method on a receiver that
2
+ // an earlier step built. `DataSchema` and the static contract reader both build zod only through here, so a recorded
3
+ // chain and the evaluated module hold the same schema.
4
+
5
+ import { z } from 'zod/v4'
6
+
7
+ /** A plain argument of a step, with every schema in it already zod. */
8
+ export type ReplayArg =
9
+ | string
10
+ | number
11
+ | boolean
12
+ | null
13
+ | RegExp
14
+ | z.ZodType
15
+ | Unreadable
16
+ | readonly ReplayArg[]
17
+ | { readonly [key: string]: ReplayArg }
18
+
19
+ /** An argument a reader of source read that is no value of the subset (a builder call, say): refused by its kind. */
20
+ export class Unreadable {
21
+ readonly kind: string
22
+
23
+ constructor(kind: string) {
24
+ this.kind = kind
25
+ Object.freeze(this)
26
+ }
27
+ }
28
+
29
+ /** `z.<factory>(…)`, `z.iso.<factory>(…)` or `.<method>(…)` on a receiver. */
30
+ export type ReplayStep = { readonly factory: string; readonly iso: boolean } | { readonly method: string; readonly receiver: z.ZodType }
31
+
32
+ export const SCHEMA_SUBSET = 'a schema of the supported z subset (packages/check/CLAUDE.md)'
33
+
34
+ /** A step zod would refuse, or one outside the contract subset: what is wrong and what is allowed there. */
35
+ export class SchemaRefusal extends Error {
36
+ override readonly name = 'SchemaRefusal'
37
+ readonly allowed: string
38
+
39
+ constructor(message: string, allowed: string) {
40
+ super(message)
41
+ this.allowed = allowed
42
+ }
43
+ }
44
+
45
+ /** The zod schema `step` builds from `args`; a step outside the subset, or one zod refuses, throws a `SchemaRefusal`. */
46
+ export function replayStep(step: ReplayStep, args: readonly ReplayArg[]): z.ZodType {
47
+ try {
48
+ return 'factory' in step ? (step.iso ? isoFormat(step.factory, args) : factory(step.factory, args)) : method(step.method, step.receiver, args)
49
+ } catch (error) {
50
+ if (error instanceof SchemaRefusal) {
51
+ throw error
52
+ }
53
+ // zod refuses some arguments while building, e.g. `.pick()` of a key the object does not have.
54
+ const message = error instanceof Error ? (error.message.split('\n')[0] ?? error.message) : 'zod refused the schema'
55
+ throw new SchemaRefusal(`zod refuses this schema: ${message}`, SCHEMA_SUBSET)
56
+ }
57
+ }
58
+
59
+ function factory(name: string, args: readonly ReplayArg[]): z.ZodType {
60
+ const call = `z.${name}()`
61
+ switch (name) {
62
+ case 'string':
63
+ return z.string(messageParams(call, only(call, args)))
64
+ case 'number':
65
+ return z.number(messageParams(call, only(call, args)))
66
+ case 'int':
67
+ return z.int(messageParams(call, only(call, args)))
68
+ case 'boolean':
69
+ return z.boolean(messageParams(call, only(call, args)))
70
+ case 'null':
71
+ return z.null(messageParams(call, only(call, args)))
72
+ case 'uuid':
73
+ return z.uuid(uuidParams(call, only(call, args)))
74
+ case 'email':
75
+ return z.email(messageParams(call, only(call, args)))
76
+ case 'url':
77
+ return z.url(messageParams(call, only(call, args)))
78
+ case 'literal': {
79
+ const [value, params] = between(call, args, 1, 2)
80
+ const literal = literalValues(call, value)
81
+ return Array.isArray(literal) ? z.literal(literal, messageParams(call, params)) : z.literal(literal, messageParams(call, params))
82
+ }
83
+ case 'enum': {
84
+ const [values, params] = between(call, args, 1, 2)
85
+ const entries = enumEntries(call, values)
86
+ return Array.isArray(entries) ? z.enum(entries, messageParams(call, params)) : z.enum(entries, messageParams(call, params))
87
+ }
88
+ case 'strictObject': {
89
+ const [shape, params] = between(call, args, 1, 2)
90
+ return z.strictObject(schemaShape(call, shape), messageParams(call, params))
91
+ }
92
+ case 'array': {
93
+ const [element, params] = between(call, args, 1, 2)
94
+ return z.array(schema(call, element), messageParams(call, params))
95
+ }
96
+ case 'tuple':
97
+ return tuple(call, args)
98
+ case 'union': {
99
+ const [options, params] = between(call, args, 1, 2)
100
+ return z.union(schemaList(call, options), messageParams(call, params))
101
+ }
102
+ case 'discriminatedUnion': {
103
+ const [discriminator, options, params] = between(call, args, 2, 3)
104
+ const [first, ...rest] = schemaList(call, options).map((option) => objectSchema(call, option))
105
+ if (first === undefined) {
106
+ throw new SchemaRefusal(`${call} has no option`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
107
+ }
108
+ return z.discriminatedUnion(text(call, discriminator), [first, ...rest], messageParams(call, params))
109
+ }
110
+ case 'record': {
111
+ const [key, value, params] = between(call, args, 2, 3)
112
+ return z.record(recordKey(call, schema(call, key)), schema(call, value), messageParams(call, params))
113
+ }
114
+ case 'optional':
115
+ return z.optional(schema(call, exactly(call, args, 1)[0]))
116
+ case 'nullable':
117
+ return z.nullable(schema(call, exactly(call, args, 1)[0]))
118
+ default:
119
+ throw new SchemaRefusal(`'${name}' is not in the supported schema subset`, SCHEMA_SUBSET)
120
+ }
121
+ }
122
+
123
+ function isoFormat(name: string, args: readonly ReplayArg[]): z.ZodType {
124
+ const call = `z.iso.${name}()`
125
+ switch (name) {
126
+ case 'date':
127
+ return z.iso.date(messageParams(call, only(call, args)))
128
+ case 'datetime':
129
+ return z.iso.datetime(datetimeParams(call, only(call, args)))
130
+ case 'time':
131
+ return z.iso.time(timeParams(call, only(call, args)))
132
+ case 'duration':
133
+ return z.iso.duration(messageParams(call, only(call, args)))
134
+ default:
135
+ throw new SchemaRefusal(`'iso.${name}' is not in the supported schema subset`, SCHEMA_SUBSET)
136
+ }
137
+ }
138
+
139
+ function method(name: string, receiver: z.ZodType, args: readonly ReplayArg[]): z.ZodType {
140
+ const call = `.${name}()`
141
+ switch (name) {
142
+ case 'min':
143
+ case 'max':
144
+ case 'length': {
145
+ const [bound, params] = between(call, args, 1, 2)
146
+ const value = number(call, bound)
147
+ const message = messageParams(call, params)
148
+ if (receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat) {
149
+ return name === 'min' ? receiver.min(value, message) : name === 'max' ? receiver.max(value, message) : receiver.length(value, message)
150
+ }
151
+ if (receiver instanceof z.ZodArray) {
152
+ return name === 'min' ? receiver.min(value, message) : name === 'max' ? receiver.max(value, message) : receiver.length(value, message)
153
+ }
154
+ if (receiver instanceof z.ZodNumber && name !== 'length') {
155
+ return name === 'min' ? receiver.min(value, message) : receiver.max(value, message)
156
+ }
157
+ throw unsupported(call, receiver)
158
+ }
159
+ case 'nonempty': {
160
+ const message = messageParams(call, only(call, args))
161
+ if (receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat || receiver instanceof z.ZodArray) {
162
+ return receiver.nonempty(message)
163
+ }
164
+ throw unsupported(call, receiver)
165
+ }
166
+ case 'email':
167
+ case 'url':
168
+ case 'uuid': {
169
+ const params = only(call, args)
170
+ if (!(receiver instanceof z.ZodString)) {
171
+ throw unsupported(call, receiver)
172
+ }
173
+ return name === 'email'
174
+ ? receiver.email(messageParams(call, params))
175
+ : name === 'url'
176
+ ? receiver.url(messageParams(call, params))
177
+ : receiver.uuid(uuidParams(call, params))
178
+ }
179
+ case 'startsWith':
180
+ case 'endsWith':
181
+ case 'includes':
182
+ case 'regex': {
183
+ const [argument, params] = between(call, args, 1, 2)
184
+ if (!(receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat)) {
185
+ throw unsupported(call, receiver)
186
+ }
187
+ switch (name) {
188
+ case 'startsWith':
189
+ return receiver.startsWith(text(call, argument), messageParams(call, params))
190
+ case 'endsWith':
191
+ return receiver.endsWith(text(call, argument), messageParams(call, params))
192
+ case 'includes':
193
+ return receiver.includes(text(call, argument), includesParams(call, params))
194
+ default:
195
+ return receiver.regex(regex(call, argument), messageParams(call, params))
196
+ }
197
+ }
198
+ case 'int':
199
+ case 'positive':
200
+ case 'negative':
201
+ case 'nonnegative':
202
+ case 'nonpositive': {
203
+ const message = messageParams(call, only(call, args))
204
+ if (!(receiver instanceof z.ZodNumber)) {
205
+ throw unsupported(call, receiver)
206
+ }
207
+ switch (name) {
208
+ case 'int':
209
+ return receiver.int(message)
210
+ case 'positive':
211
+ return receiver.positive(message)
212
+ case 'negative':
213
+ return receiver.negative(message)
214
+ case 'nonnegative':
215
+ return receiver.nonnegative(message)
216
+ default:
217
+ return receiver.nonpositive(message)
218
+ }
219
+ }
220
+ case 'gt':
221
+ case 'gte':
222
+ case 'lt':
223
+ case 'lte':
224
+ case 'multipleOf': {
225
+ const [bound, params] = between(call, args, 1, 2)
226
+ const value = number(call, bound)
227
+ const message = messageParams(call, params)
228
+ if (!(receiver instanceof z.ZodNumber)) {
229
+ throw unsupported(call, receiver)
230
+ }
231
+ switch (name) {
232
+ case 'gt':
233
+ return receiver.gt(value, message)
234
+ case 'gte':
235
+ return receiver.gte(value, message)
236
+ case 'lt':
237
+ return receiver.lt(value, message)
238
+ case 'lte':
239
+ return receiver.lte(value, message)
240
+ default:
241
+ return receiver.multipleOf(value, message)
242
+ }
243
+ }
244
+ case 'optional':
245
+ exactly(call, args, 0)
246
+ return receiver.optional()
247
+ case 'nullable':
248
+ exactly(call, args, 0)
249
+ return receiver.nullable()
250
+ case 'nullish':
251
+ exactly(call, args, 0)
252
+ return receiver.nullish()
253
+ case 'array':
254
+ exactly(call, args, 0)
255
+ return receiver.array()
256
+ case 'default':
257
+ return receiver.default(plain(call, exactly(call, args, 1)[0]))
258
+ case 'describe':
259
+ return receiver.describe(text(call, exactly(call, args, 1)[0]))
260
+ case 'or':
261
+ return receiver.or(schema(call, exactly(call, args, 1)[0]))
262
+ case 'extend':
263
+ case 'pick':
264
+ case 'omit':
265
+ case 'partial': {
266
+ if (!(receiver instanceof z.ZodObject)) {
267
+ throw unsupported(call, receiver)
268
+ }
269
+ if (name === 'extend') {
270
+ return receiver.extend(schemaShape(call, exactly(call, args, 1)[0]))
271
+ }
272
+ if (name === 'partial') {
273
+ const [mask] = between(call, args, 0, 1)
274
+ return mask === undefined ? receiver.partial() : receiver.partial(keyMask(call, mask))
275
+ }
276
+ const mask = keyMask(call, exactly(call, args, 1)[0])
277
+ return name === 'pick' ? receiver.pick(mask) : receiver.omit(mask)
278
+ }
279
+ case 'exclude':
280
+ case 'extract': {
281
+ // Outside the contract subset, which no contract module calls: application code narrows an enum with them.
282
+ const [keys, params] = between(call, args, 1, 2)
283
+ if (!(receiver instanceof z.ZodEnum)) {
284
+ throw unsupported(call, receiver)
285
+ }
286
+ const names = textList(call, keys)
287
+ return name === 'exclude' ? receiver.exclude(names, messageParams(call, params)) : receiver.extract(names, messageParams(call, params))
288
+ }
289
+ default:
290
+ throw new SchemaRefusal(`'${name}' is not in the supported schema subset`, SCHEMA_SUBSET)
291
+ }
292
+ }
293
+
294
+ function tuple(call: string, args: readonly ReplayArg[]): z.ZodType {
295
+ const [items, second, third] = between(call, args, 1, 3)
296
+ const [first, ...rest] = schemaList(call, items)
297
+ const restSchema = second instanceof z.ZodType ? second : undefined
298
+ const params = messageParams(call, restSchema === undefined ? second : third)
299
+ if (restSchema === undefined && third !== undefined) {
300
+ throw new SchemaRefusal(`${call} takes its items, an optional rest schema and options`, 'z.tuple([<schemas>], <rest schema>)')
301
+ }
302
+ if (first === undefined) {
303
+ if (restSchema !== undefined) {
304
+ throw new SchemaRefusal(`${call} with a rest and no item`, 'z.array(<schema>)')
305
+ }
306
+ return z.tuple([], params)
307
+ }
308
+ return restSchema === undefined ? z.tuple([first, ...rest], params) : z.tuple([first, ...rest], restSchema, params)
309
+ }
310
+
311
+ // ── Arguments ───────────────────────────────────────────────────────────────────────────────────
312
+
313
+ type Entries = { readonly [key: string]: ReplayArg }
314
+
315
+ /** What the literal subset calls an argument's kind, for a refusal that names it. */
316
+ function kindOf(value: ReplayArg): string {
317
+ if (value === null) {
318
+ return 'null'
319
+ }
320
+ if (value instanceof RegExp) {
321
+ return 'regex'
322
+ }
323
+ if (value instanceof z.ZodType) {
324
+ return 'schema'
325
+ }
326
+ if (value instanceof Unreadable) {
327
+ return value.kind
328
+ }
329
+ if (Array.isArray(value)) {
330
+ return 'array'
331
+ }
332
+ return typeof value
333
+ }
334
+
335
+ function entriesOf(value: ReplayArg | undefined): Entries | undefined {
336
+ if (
337
+ typeof value !== 'object' || value === null || value instanceof RegExp || value instanceof z.ZodType || value instanceof Unreadable || isList(value)
338
+ ) {
339
+ return undefined
340
+ }
341
+ const prototype: unknown = Object.getPrototypeOf(value)
342
+ return prototype === Object.prototype || prototype === null ? value : undefined
343
+ }
344
+
345
+ function isList(value: ReplayArg): value is readonly ReplayArg[] {
346
+ return Array.isArray(value)
347
+ }
348
+
349
+ function listOf(value: ReplayArg | undefined): readonly ReplayArg[] | undefined {
350
+ return value !== undefined && isList(value) ? value : undefined
351
+ }
352
+
353
+ function between(call: string, args: readonly ReplayArg[], min: number, max: number): readonly (ReplayArg | undefined)[] {
354
+ if (args.length < min || args.length > max) {
355
+ const count = min === max ? `${min}` : `${min} to ${max}`
356
+ throw new SchemaRefusal(`${call} takes ${count} arguments, not ${args.length}`, SCHEMA_SUBSET)
357
+ }
358
+ return Array.from({ length: max }, (_, index) => args[index])
359
+ }
360
+
361
+ function exactly(call: string, args: readonly ReplayArg[], count: number): readonly (ReplayArg | undefined)[] {
362
+ return between(call, args, count, count)
363
+ }
364
+
365
+ /** The one optional argument of a call that takes only options. */
366
+ function only(call: string, args: readonly ReplayArg[]): ReplayArg | undefined {
367
+ return between(call, args, 0, 1)[0]
368
+ }
369
+
370
+ function expected(call: string, what: string, value: ReplayArg | undefined): SchemaRefusal {
371
+ return new SchemaRefusal(`${call} takes ${what}${value === undefined ? '' : `, not a ${kindOf(value)}`}`, SCHEMA_SUBSET)
372
+ }
373
+
374
+ function schema(call: string, value: ReplayArg | undefined): z.ZodType {
375
+ if (!(value instanceof z.ZodType)) {
376
+ throw expected(call, 'a schema', value)
377
+ }
378
+ return value
379
+ }
380
+
381
+ function number(call: string, value: ReplayArg | undefined): number {
382
+ if (typeof value !== 'number') {
383
+ throw expected(call, 'a number', value)
384
+ }
385
+ return value
386
+ }
387
+
388
+ function text(call: string, value: ReplayArg | undefined): string {
389
+ if (typeof value !== 'string') {
390
+ throw expected(call, 'a string', value)
391
+ }
392
+ return value
393
+ }
394
+
395
+ function textList(call: string, value: ReplayArg | undefined): string[] {
396
+ const items = listOf(value)
397
+ if (items === undefined) {
398
+ throw expected(call, 'an array of strings', value)
399
+ }
400
+ return items.map((item) => text(call, item))
401
+ }
402
+
403
+ function regex(call: string, value: ReplayArg | undefined): RegExp {
404
+ if (!(value instanceof RegExp)) {
405
+ throw expected(call, 'a regular expression literal', value)
406
+ }
407
+ return value
408
+ }
409
+
410
+ function schemaList(call: string, value: ReplayArg | undefined): z.ZodType[] {
411
+ const items = listOf(value)
412
+ if (items === undefined) {
413
+ throw expected(call, 'an array of schemas', value)
414
+ }
415
+ return items.map((item) => schema(call, item))
416
+ }
417
+
418
+ function schemaShape(call: string, value: ReplayArg | undefined): Record<string, z.ZodType> {
419
+ const entries = entriesOf(value)
420
+ if (entries === undefined) {
421
+ throw expected(call, 'an object of schemas', value)
422
+ }
423
+ return Object.fromEntries(Object.entries(entries).map(([key, field]) => [key, schema(call, field)]))
424
+ }
425
+
426
+ function keyMask(call: string, value: ReplayArg | undefined): Record<string, true> {
427
+ const entries = entriesOf(value)
428
+ if (entries === undefined || Object.values(entries).some((entry) => entry !== true)) {
429
+ throw expected(call, 'an object of keys set to true', value)
430
+ }
431
+ return Object.fromEntries(Object.keys(entries).map((key): [string, true] => [key, true]))
432
+ }
433
+
434
+ function objectSchema(call: string, option: z.ZodType): z.ZodObject {
435
+ if (!(option instanceof z.ZodObject)) {
436
+ throw new SchemaRefusal(`${call} takes object options`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
437
+ }
438
+ return option
439
+ }
440
+
441
+ function recordKey(call: string, key: z.ZodType): z.ZodString | z.ZodStringFormat | z.ZodEnum {
442
+ if (key instanceof z.ZodString || key instanceof z.ZodStringFormat || key instanceof z.ZodEnum) {
443
+ return key
444
+ }
445
+ throw new SchemaRefusal(`${call} takes a string or enum key`, 'z.record(z.string(), <schema>)')
446
+ }
447
+
448
+ type Primitive = string | number | boolean | null
449
+
450
+ function primitive(call: string, value: ReplayArg | undefined): Primitive {
451
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null) {
452
+ return value
453
+ }
454
+ throw expected(call, 'a string, number, boolean or null', value)
455
+ }
456
+
457
+ function literalValues(call: string, value: ReplayArg | undefined): Primitive | Primitive[] {
458
+ const items = listOf(value)
459
+ return items === undefined ? primitive(call, value) : items.map((item) => primitive(call, item))
460
+ }
461
+
462
+ function enumEntries(call: string, value: ReplayArg | undefined): string[] | Record<string, string | number> {
463
+ const items = listOf(value)
464
+ if (items !== undefined) {
465
+ return items.map((item) => text(call, item))
466
+ }
467
+ const entries = entriesOf(value)
468
+ if (entries !== undefined) {
469
+ return Object.fromEntries(Object.entries(entries).map(([key, entry]) => [key, typeof entry === 'number' ? entry : text(call, entry)]))
470
+ }
471
+ throw expected(call, 'an array of strings or an object', value)
472
+ }
473
+
474
+ /** A value with no schema in it, as the evaluated literal would be. */
475
+ function plain(call: string, value: ReplayArg | undefined): unknown {
476
+ const items = listOf(value)
477
+ if (items !== undefined) {
478
+ return items.map((item) => plain(call, item))
479
+ }
480
+ const entries = entriesOf(value)
481
+ if (entries !== undefined) {
482
+ return Object.fromEntries(Object.entries(entries).map(([key, entry]) => [key, plain(call, entry)]))
483
+ }
484
+ return primitive(call, value)
485
+ }
486
+
487
+ // ── Options ─────────────────────────────────────────────────────────────────────────────────────
488
+
489
+ /** An error message changes no shape; `error` and `message` are the only options every step takes. */
490
+ interface MessageParams {
491
+ message?: string
492
+ error?: string
493
+ }
494
+
495
+ const MESSAGE_KEYS = ['message', 'error'] as const
496
+
497
+ function options(call: string, value: ReplayArg | undefined, keys: readonly string[]): Entries | string | undefined {
498
+ if (value === undefined || typeof value === 'string') {
499
+ return value
500
+ }
501
+ const entries = entriesOf(value)
502
+ if (entries === undefined) {
503
+ throw expected(call, 'an error message or an options object', value)
504
+ }
505
+ for (const key of Object.keys(entries)) {
506
+ if (!keys.includes(key)) {
507
+ throw new SchemaRefusal(`'${key}' is not an option the contract reads for ${call}`, `options among ${keys.join(', ')}`)
508
+ }
509
+ }
510
+ return entries
511
+ }
512
+
513
+ function messageFields(call: string, entries: Entries): MessageParams {
514
+ const params: MessageParams = {}
515
+ const message = entries['message']
516
+ const error = entries['error']
517
+ if (message !== undefined) {
518
+ params.message = text(call, message)
519
+ }
520
+ if (error !== undefined) {
521
+ params.error = text(call, error)
522
+ }
523
+ return params
524
+ }
525
+
526
+ function messageParams(call: string, value: ReplayArg | undefined): string | MessageParams | undefined {
527
+ const read = options(call, value, MESSAGE_KEYS)
528
+ return read === undefined || typeof read === 'string' ? read : messageFields(call, read)
529
+ }
530
+
531
+ const UUID_VERSIONS = ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8'] as const
532
+
533
+ type UuidVersion = (typeof UUID_VERSIONS)[number]
534
+
535
+ function isUuidVersion(version: string): version is UuidVersion {
536
+ return UUID_VERSIONS.some((known) => known === version)
537
+ }
538
+
539
+ function uuidParams(call: string, value: ReplayArg | undefined): string | (MessageParams & { version?: UuidVersion }) | undefined {
540
+ const read = options(call, value, [...MESSAGE_KEYS, 'version'])
541
+ if (read === undefined || typeof read === 'string') {
542
+ return read
543
+ }
544
+ const params: MessageParams & { version?: UuidVersion } = messageFields(call, read)
545
+ const version = read['version']
546
+ if (version !== undefined) {
547
+ const name = text(call, version)
548
+ if (!isUuidVersion(name)) {
549
+ throw new SchemaRefusal(`'${name}' is not a UUID version`, `one of ${UUID_VERSIONS.join(', ')}`)
550
+ }
551
+ params.version = name
552
+ }
553
+ return params
554
+ }
555
+
556
+ function precision(call: string, value: ReplayArg): number | null {
557
+ return value === null ? null : number(call, value)
558
+ }
559
+
560
+ function datetimeParams(
561
+ call: string,
562
+ value: ReplayArg | undefined,
563
+ ): string | (MessageParams & { offset?: boolean; local?: boolean; precision?: number | null }) | undefined {
564
+ const read = options(call, value, [...MESSAGE_KEYS, 'offset', 'local', 'precision'])
565
+ if (read === undefined || typeof read === 'string') {
566
+ return read
567
+ }
568
+ const params: MessageParams & { offset?: boolean; local?: boolean; precision?: number | null } = messageFields(call, read)
569
+ const offset = read['offset']
570
+ const local = read['local']
571
+ const digits = read['precision']
572
+ if (offset !== undefined) {
573
+ params.offset = flag(call, offset)
574
+ }
575
+ if (local !== undefined) {
576
+ params.local = flag(call, local)
577
+ }
578
+ if (digits !== undefined) {
579
+ params.precision = precision(call, digits)
580
+ }
581
+ return params
582
+ }
583
+
584
+ function timeParams(call: string, value: ReplayArg | undefined): string | (MessageParams & { precision?: number | null }) | undefined {
585
+ const read = options(call, value, [...MESSAGE_KEYS, 'precision'])
586
+ if (read === undefined || typeof read === 'string') {
587
+ return read
588
+ }
589
+ const params: MessageParams & { precision?: number | null } = messageFields(call, read)
590
+ const digits = read['precision']
591
+ if (digits !== undefined) {
592
+ params.precision = precision(call, digits)
593
+ }
594
+ return params
595
+ }
596
+
597
+ /** `.includes()` takes its options only as an object. */
598
+ function includesParams(call: string, value: ReplayArg | undefined): (MessageParams & { position?: number }) | undefined {
599
+ const read = options(call, value, [...MESSAGE_KEYS, 'position'])
600
+ if (typeof read === 'string') {
601
+ throw new SchemaRefusal(`${call} takes its options as an object`, "{ message: '…' }")
602
+ }
603
+ if (read === undefined) {
604
+ return undefined
605
+ }
606
+ const params: MessageParams & { position?: number } = messageFields(call, read)
607
+ const position = read['position']
608
+ if (position !== undefined) {
609
+ params.position = number(call, position)
610
+ }
611
+ return params
612
+ }
613
+
614
+ function flag(call: string, value: ReplayArg): boolean {
615
+ if (typeof value !== 'boolean') {
616
+ throw expected(call, 'a boolean option', value)
617
+ }
618
+ return value
619
+ }
620
+
621
+ function unsupported(call: string, receiver: z.ZodType): SchemaRefusal {
622
+ return new SchemaRefusal(`${call} is not a method of a ${receiver._zod.def.type} schema in the contract subset`, SCHEMA_SUBSET)
623
+ }
@@ -39,11 +39,10 @@ import { type IamGateway, REQUEST_ID_HEADER, stripFabrikaCookies } from '@fabrik
39
39
  import { exportJWK, generateKeyPair, SignJWT } from 'jose'
40
40
  import { existsSync } from 'node:fs'
41
41
  import { resolve } from 'node:path'
42
- import type { z } from 'zod/v4'
43
42
  import { challengeUrl } from '../client/contract'
44
43
  import type { RpcFetch } from '../client/transport'
45
44
  import { compileContract } from '../contract/compile'
46
- import type { AnyContract, DimensionOf } from '../contract/define'
45
+ import type { AnyContract, DimensionOf, SchemaInput } from '../contract/define'
47
46
  import type { TokenPurpose } from '../contract/token'
48
47
  import { diskAssets } from '../disk-assets'
49
48
  import type { RequestExecutionContext } from '../fetch'
@@ -247,7 +246,8 @@ type TokensOf<C> = C extends { readonly surface: { readonly tokens: infer T } }
247
246
  export type TokenPurposeName<C> = keyof TokensOf<C> & string
248
247
 
249
248
  /** What a token of purpose `P` carries, as a site passes it to `sign`. */
250
- export type TokenClaimsInput<C, P extends string> = TokensOf<C> extends { readonly [K in P]: TokenPurpose<infer Claims> } ? z.input<Claims> : never
249
+ export type TokenClaimsInput<C, P extends string> = TokensOf<C> extends { readonly [K in P]: TokenPurpose<infer Claims> } ? SchemaInput<Claims>
250
+ : never
251
251
 
252
252
  /** Each SPA mount from its built directory under `root` when one exists, as the deploy serves it; an empty page otherwise. */
253
253
  function builtAssets(root: string): SpaAssets {
@@ -0,0 +1 @@
1
+ export {};
@@ -6,6 +6,11 @@ export declare const GENERATED_ENTRY_FILE = "fabrika/entry.generated.ts";
6
6
  export declare const IMPLEMENTATION_FILE = "src/app.ts";
7
7
  /** The application's migrations as a module, relative to the application root: a Worker reads no file system. */
8
8
  export declare const GENERATED_MIGRATIONS_FILE = "fabrika/migrations.generated.ts";
9
+ /**
10
+ * The compiled contract as a module, relative to the application root: the Cloudflare entrypoint hands it to boot, which
11
+ * checks its digest and reads it instead of compiling the contract inside the isolate.
12
+ */
13
+ export declare const GENERATED_CONTRACT_FILE = "fabrika/contract.generated.ts";
9
14
  /** Where an application keeps its migrations, relative to its root; `fabrika db new` writes there. */
10
15
  export declare const MIGRATIONS_DIRECTORY = "db/migrations";
11
16
  export type EntryRuntime = 'bun' | 'cloudflare';
@@ -13,8 +18,18 @@ export * from './build-recipe.js';
13
18
  export { CLOUDFLARE_APPLICATION_BINDING, CLOUDFLARE_APPLICATION_CLASS, CLOUDFLARE_ASSETS_BINDING, CLOUDFLARE_FILES_BINDING, } from './cloudflare/names.js';
14
19
  /** The entrypoint source for one runtime. */
15
20
  export declare function generatedEntry(runtime: EntryRuntime): string;
16
- /** Writes the entrypoint under `root` and returns its path; the Cloudflare one also writes the migrations module it imports. */
21
+ /**
22
+ * Writes the entrypoint under `root` and returns its path; the Cloudflare one also writes the migrations and the compiled
23
+ * contract modules it imports.
24
+ */
17
25
  export declare function writeGeneratedEntry(root: string, runtime: EntryRuntime): Promise<string>;
26
+ /** The compiled contract module source: the artifact's shared encoding (`encodeArtifact`) as one string, verbatim. */
27
+ export declare function generatedContract(encoded: string): string;
28
+ /**
29
+ * Compiles the application's contract in a child process, which loads `src/app.ts` with no variable but `PATH` and
30
+ * `HOME`, writes the module under `root` and returns its path. A contract that does not compile fails the build.
31
+ */
32
+ export declare function writeGeneratedContract(root: string): Promise<string>;
18
33
  /** The application's migrations under `root`, every `.sql` file of `db/migrations` by name; none without the directory. */
19
34
  export declare function applicationMigrations(root: string): Promise<readonly {
20
35
  readonly name: string;