@geonosis/document 0.9.1 → 0.9.2

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 (91) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/core/actor.cjs +48 -0
  3. package/dist/core/actor.d.cts +3 -0
  4. package/dist/core/actor.d.ts +3 -0
  5. package/dist/core/actor.js +8 -0
  6. package/dist/core/body-version.cjs +75 -0
  7. package/dist/core/body-version.d.cts +25 -0
  8. package/dist/core/body-version.d.ts +25 -0
  9. package/dist/core/body-version.js +35 -0
  10. package/dist/core/canonical.cjs +55 -0
  11. package/dist/core/canonical.d.cts +5 -0
  12. package/dist/core/canonical.d.ts +5 -0
  13. package/dist/core/canonical.js +15 -0
  14. package/dist/core/document.cjs +136 -0
  15. package/dist/core/document.d.cts +137 -0
  16. package/dist/core/document.d.ts +137 -0
  17. package/dist/core/document.js +96 -0
  18. package/dist/core/entities.cjs +68 -0
  19. package/dist/core/entities.d.cts +42 -0
  20. package/dist/core/entities.d.ts +42 -0
  21. package/dist/core/entities.js +28 -0
  22. package/dist/core/naming.cjs +56 -0
  23. package/dist/core/naming.d.cts +17 -0
  24. package/dist/core/naming.d.ts +17 -0
  25. package/dist/core/naming.js +16 -0
  26. package/dist/core/numbering.cjs +41 -0
  27. package/dist/core/numbering.d.cts +12 -0
  28. package/dist/core/numbering.d.ts +12 -0
  29. package/dist/core/numbering.js +0 -0
  30. package/dist/core/ports.cjs +41 -0
  31. package/dist/core/ports.d.cts +29 -0
  32. package/dist/core/ports.d.ts +29 -0
  33. package/dist/core/ports.js +0 -0
  34. package/dist/core/schema.cjs +173 -0
  35. package/dist/core/schema.d.cts +33 -0
  36. package/dist/core/schema.d.ts +33 -0
  37. package/dist/{chunk-NA3DLROL.js → core/schema.js} +34 -72
  38. package/dist/core/status.cjs +47 -0
  39. package/dist/core/status.d.cts +8 -0
  40. package/dist/core/status.d.ts +8 -0
  41. package/dist/core/status.js +7 -0
  42. package/dist/index.cjs +70 -398
  43. package/dist/index.d.cts +11 -32
  44. package/dist/index.d.ts +11 -32
  45. package/dist/index.js +13 -25
  46. package/dist/store/ddl.cjs +209 -0
  47. package/dist/store/ddl.d.cts +56 -0
  48. package/dist/store/ddl.d.ts +56 -0
  49. package/dist/store/ddl.js +175 -0
  50. package/dist/store/hash.cjs +50 -0
  51. package/dist/store/hash.d.cts +2 -0
  52. package/dist/store/hash.d.ts +2 -0
  53. package/dist/store/hash.js +10 -0
  54. package/dist/store/store.cjs +606 -0
  55. package/dist/store/store.d.cts +169 -0
  56. package/dist/store/store.d.ts +169 -0
  57. package/dist/store/store.js +578 -0
  58. package/dist/store.cjs +51 -1358
  59. package/dist/store.d.cts +4 -277
  60. package/dist/store.d.ts +4 -277
  61. package/dist/store.js +17 -1114
  62. package/dist/surface/table.cjs +326 -0
  63. package/dist/surface/table.d.cts +70 -0
  64. package/dist/surface/table.d.ts +70 -0
  65. package/dist/surface/table.js +296 -0
  66. package/dist/workflows/document-workflows.cjs +314 -0
  67. package/dist/workflows/document-workflows.d.cts +112 -0
  68. package/dist/workflows/document-workflows.d.ts +112 -0
  69. package/dist/workflows/document-workflows.js +281 -0
  70. package/dist/workflows/effects.cjs +41 -0
  71. package/dist/workflows/effects.d.cts +68 -0
  72. package/dist/workflows/effects.d.ts +68 -0
  73. package/dist/workflows/effects.js +0 -0
  74. package/dist/workflows/ports.cjs +50 -0
  75. package/dist/workflows/ports.d.cts +72 -0
  76. package/dist/workflows/ports.d.ts +72 -0
  77. package/dist/workflows/ports.js +10 -0
  78. package/dist/workflows/refusals.cjs +80 -0
  79. package/dist/workflows/refusals.d.cts +59 -0
  80. package/dist/workflows/refusals.d.ts +59 -0
  81. package/dist/workflows/refusals.js +40 -0
  82. package/dist/workflows.cjs +52 -504
  83. package/dist/workflows.d.cts +4 -310
  84. package/dist/workflows.d.ts +4 -310
  85. package/dist/workflows.js +21 -368
  86. package/package.json +8 -8
  87. package/dist/chunk-A6ZU4QU3.js +0 -171
  88. package/dist/entities-DTNJMeVf.d.cts +0 -205
  89. package/dist/entities-DTNJMeVf.d.ts +0 -205
  90. package/dist/ports-C_OfflMQ.d.cts +0 -66
  91. package/dist/ports-C_OfflMQ.d.ts +0 -66
package/dist/workflows.js CHANGED
@@ -1,372 +1,25 @@
1
+ // src/workflows.ts
1
2
  import {
2
- canTransition,
3
- enumOf,
4
- nonEmptyString,
5
- objectOf,
6
- optional,
7
- positiveInt,
8
- systemActor,
9
- validate
10
- } from "./chunk-NA3DLROL.js";
11
-
12
- // src/workflows/document-workflows.ts
13
- import { createStep, createWorkflow } from "@geonosis/workflows";
14
-
15
- // src/workflows/refusals.ts
16
- import { TaggedError as tagged } from "better-result";
17
- var DOCUMENT_NOT_FOUND_TAG = "DocumentNotFound";
18
- var DOCUMENT_TAKEN_TAG = "DocumentTaken";
19
- var NUMBERING_REFUSED_TAG = "NumberingRefused";
20
- var TRANSITION_REFUSED_TAG = "TransitionRefused";
21
- var DOCUMENT_NOT_REMOVABLE_TAG = "DocumentNotRemovable";
22
- var CONVERSION_REFUSED_TAG = "ConversionRefused";
23
- var DocumentNotFound = class extends (/* @__PURE__ */ tagged(DOCUMENT_NOT_FOUND_TAG)) {
24
- };
25
- var DocumentTaken = class extends (/* @__PURE__ */ tagged(DOCUMENT_TAKEN_TAG)) {
26
- };
27
- var NumberingRefused = class extends (/* @__PURE__ */ tagged(NUMBERING_REFUSED_TAG)) {
28
- };
29
- var TransitionRefused = class extends (/* @__PURE__ */ tagged(TRANSITION_REFUSED_TAG)) {
30
- };
31
- var DocumentNotRemovable = class extends (/* @__PURE__ */ tagged(DOCUMENT_NOT_REMOVABLE_TAG)) {
32
- };
33
- var ConversionRefused = class extends (/* @__PURE__ */ tagged(CONVERSION_REFUSED_TAG)) {
34
- };
35
-
36
- // src/workflows/document-workflows.ts
37
- var withArticle = (noun) => `${/^[aeiou]/i.test(noun) ? "an" : "a"} ${noun}`;
38
- var capitalised = (noun) => `${noun.charAt(0).toUpperCase()}${noun.slice(1)}`;
39
- var mailText = (opening, url, ttlDays) => `${opening}
40
-
41
- ${url}
42
-
43
- The link works for ${ttlDays} days.
44
- `;
45
- var typed = (schema) => schema;
46
- var defaultActor = (ctx, workflowName) => {
47
- const held = ctx?.actor;
48
- return typeof held === "string" ? held : systemActor(workflowName);
49
- };
50
- var documentWorkflows = (entity, options) => {
51
- const { effects, ports } = options;
52
- const name = entity.name;
53
- const article = withArticle(entity.noun);
54
- const noun = capitalised(entity.noun);
55
- const actorOf = ports.actor ?? defaultActor;
56
- const guard = ports.guard ?? (() => Promise.resolve());
57
- const now = ports.now ?? (() => (/* @__PURE__ */ new Date()).toISOString());
58
- const seed = ports.seed ?? ((_ctx, seeding) => {
59
- if (seeding.templateId !== void 0) {
60
- throw new Error(`${article} takes no template: this host declares no seed port`);
61
- }
62
- return Promise.resolve(seeding.input);
63
- });
64
- const beforeCreate = effects.beforeCreate ?? ((_ctx, body) => Promise.resolve(body));
65
- const mailWords = effects.mail ?? ((number) => ({
66
- subject: `${noun} ${number}`,
67
- opening: `${noun} ${number} is ready for you to read.`
68
- }));
69
- const announce = (fact) => effects.announce(fact);
70
- const write = async (ctx, written, workflowName) => {
71
- await ports.door(ctx, written.entity).commit({
72
- actor: actorOf(ctx, workflowName),
73
- body: written.body,
74
- ref: written.number
75
- });
76
- return written;
77
- };
78
- const stored = async (ctx, at) => {
79
- const found = await ports.door(ctx, name).get({ ref: at.ref });
80
- if (!found) {
81
- throw new DocumentNotFound({
82
- entity: name,
83
- message: `no ${entity.noun} is stored as ${at.ref}`,
84
- ref: at.ref
85
- });
86
- }
87
- return found;
88
- };
89
- const refuseTaken = async (ctx, at) => {
90
- const owner = at.entity ?? name;
91
- if (await ports.door(ctx, owner).get({ ref: at.ref })) {
92
- throw new DocumentTaken({
93
- entity: owner,
94
- message: `a ${owner} is already stored as ${at.ref}`,
95
- ref: at.ref
96
- });
97
- }
98
- };
99
- const refFor = async (ctx, given, draft) => {
100
- const numbering = entity.numbering;
101
- if (numbering.mode === "given") {
102
- if (given === void 0) {
103
- throw new NumberingRefused({
104
- entity: name,
105
- message: `${article} is numbered by whoever creates it`,
106
- mode: "given"
107
- });
108
- }
109
- await refuseTaken(ctx, { ref: given });
110
- return { ref: given, minted: null };
111
- }
112
- if (given !== void 0) {
113
- throw new NumberingRefused({
114
- entity: name,
115
- message: `${article} is numbered by the organisation`,
116
- mode: "counter"
117
- });
118
- }
119
- if (ports.counter === void 0) {
120
- throw new NumberingRefused({
121
- entity: name,
122
- message: `${article} is numbered from a counter and this host declares no counter port`,
123
- mode: "counter"
124
- });
125
- }
126
- const series = numbering.series(draft);
127
- const { number: sequence } = await ports.counter(ctx, numbering.counter);
128
- return { ref: numbering.format(series, sequence), minted: { sequence } };
129
- };
130
- const shareLink = createStep(
131
- { name: "create-share-link", noCompensation: true },
132
- (input, ctx) => ports.link(ctx, { entity: name, ...input })
133
- );
134
- const sendMail = createStep(
135
- { name: "send-email", noCompensation: true },
136
- async (input, ctx) => {
137
- const sent = await ports.mail(ctx, input);
138
- if (sent.isErr()) throw sent.error;
139
- return { to: input.to };
140
- }
141
- );
142
- const createName = `${name}.create`;
143
- const create = createWorkflow(
144
- createName,
145
- async (input, ctx) => {
146
- await guard(ctx);
147
- const draft = await validate(
148
- entity.draft,
149
- await seed(ctx, {
150
- entity: name,
151
- templateId: input.templateId,
152
- input: input.body
153
- })
154
- );
155
- const minted = await refFor(ctx, input.ref, draft);
156
- const body = entity.prepare(
157
- await beforeCreate(ctx, entity.fromDraft(draft, minted.ref))
158
- );
159
- const written = await write(ctx, { entity: name, number: minted.ref, body }, createName);
160
- return { ...written, minted: minted.minted };
161
- },
162
- {
163
- announce: ({ minted, ...written }) => announce({ kind: "created", written, minted }),
164
- input: typed(
165
- objectOf({
166
- body: entity.draft,
167
- ref: optional(nonEmptyString),
168
- templateId: optional(nonEmptyString)
169
- })
170
- )
171
- }
172
- );
173
- const saveName = `${name}.save`;
174
- const save = createWorkflow(
175
- saveName,
176
- async (input, ctx) => {
177
- const current = await stored(ctx, input);
178
- const body = entity.prepare(
179
- entity.merge(current.body, input.body, now())
180
- );
181
- return write(ctx, { entity: name, number: input.ref, body }, saveName);
182
- },
183
- {
184
- announce: (written) => announce({ kind: "saved", written }),
185
- input: typed(objectOf({ body: entity.incoming, ref: nonEmptyString }))
186
- }
187
- );
188
- const setStatusName = `${name}.set-status`;
189
- const setStatus = createWorkflow(
190
- setStatusName,
191
- async (input, ctx) => {
192
- const current = await stored(ctx, input);
193
- const from = entity.statusOf(current.body);
194
- if (!canTransition(entity.statuses, from, input.to)) {
195
- throw new TransitionRefused({
196
- entity: name,
197
- from,
198
- message: `${article} cannot go from ${from} to ${input.to}`,
199
- ref: input.ref,
200
- to: input.to
201
- });
202
- }
203
- const written = await write(
204
- ctx,
205
- {
206
- entity: name,
207
- number: input.ref,
208
- body: entity.withStatus(current.body, input.to, now())
209
- },
210
- setStatusName
211
- );
212
- return { ...written, to: input.to };
213
- },
214
- {
215
- announce: ({ to, ...written }) => announce({ kind: "moved", written, to }),
216
- input: typed(
217
- objectOf({ ref: nonEmptyString, to: enumOf(entity.statuses.statuses) })
218
- )
219
- }
220
- );
221
- const refInput = typed(objectOf({ ref: nonEmptyString }));
222
- const deleteName = `${name}.delete`;
223
- const remove = createWorkflow(
224
- deleteName,
225
- async (input, ctx) => {
226
- if (!entity.removable) {
227
- throw new DocumentNotRemovable({
228
- entity: name,
229
- message: `deleting is not something ${article} does`
230
- });
231
- }
232
- const door = ports.door(ctx, name);
233
- const found = await door.get({ ref: input.ref });
234
- if (!found) {
235
- throw new DocumentNotFound({
236
- entity: name,
237
- message: `no ${entity.noun} is stored as ${input.ref}`,
238
- ref: input.ref
239
- });
240
- }
241
- await door.retire({ actor: actorOf(ctx, deleteName), ref: input.ref });
242
- return { number: input.ref };
243
- },
244
- {
245
- announce: (removed) => announce({ kind: "deleted", number: removed.number }),
246
- input: refInput
247
- }
248
- );
249
- const sendName = `${name}.send`;
250
- const send = createWorkflow(
251
- sendName,
252
- async (input, ctx) => {
253
- const current = await stored(ctx, input);
254
- const link = await shareLink({
255
- ref: input.ref,
256
- now: input.now,
257
- ...input.ttlDays === void 0 ? {} : { ttlDays: input.ttlDays }
258
- });
259
- const marked = entity.onSent(current.body);
260
- const written = marked === current.body ? { entity: name, number: input.ref, body: current.body } : await write(ctx, { entity: name, number: input.ref, body: marked }, sendName);
261
- const words = mailWords(input.ref);
262
- await sendMail({
263
- to: input.to,
264
- subject: words.subject,
265
- text: mailText(words.opening, link.url, link.ttlDays)
266
- });
267
- return { ...written, to: input.to, url: link.url, expiresAt: link.expiresAt };
268
- },
269
- {
270
- announce: ({ to, url, expiresAt, ...written }) => announce({ kind: "sent", written, to, url, expiresAt }),
271
- execution: "durable",
272
- idempotent: (input) => `${sendName}:${input.ref}:${input.to}`,
273
- input: typed(
274
- objectOf({
275
- now: positiveInt,
276
- ref: nonEmptyString,
277
- to: nonEmptyString,
278
- ttlDays: optional(positiveInt)
279
- })
280
- )
281
- }
282
- );
283
- const convertName = `${name}.convert`;
284
- const convert = createWorkflow(
285
- convertName,
286
- async (input, ctx) => {
287
- const conversion = effects.conversion;
288
- if (conversion === void 0) {
289
- throw new ConversionRefused({
290
- entity: name,
291
- message: `converting is not something ${article} does`,
292
- ref: input.ref
293
- });
294
- }
295
- const source = await stored(ctx, input);
296
- const refusal = conversion.refuse(source.body);
297
- if (refusal !== null) {
298
- throw new ConversionRefused({
299
- entity: name,
300
- message: `${input.ref} ${refusal}`,
301
- ref: input.ref
302
- });
303
- }
304
- await refuseTaken(ctx, { ref: input.ref, entity: conversion.to.name });
305
- await ports.door(ctx, conversion.to.name).commit({
306
- actor: actorOf(ctx, convertName),
307
- body: conversion.bodyFor(source.body, input.ref),
308
- ref: input.ref
309
- });
310
- await write(
311
- ctx,
312
- {
313
- entity: name,
314
- number: input.ref,
315
- body: conversion.markSource(source.body, input.ref)
316
- },
317
- convertName
318
- );
319
- return { entity: conversion.to.name, number: input.ref };
320
- },
321
- {
322
- announce: (converted) => announce({ kind: "converted", number: converted.number, to: converted.entity }),
323
- input: refInput
324
- }
325
- );
326
- const duplicateName = `${name}.duplicate`;
327
- const duplicate = createWorkflow(
328
- duplicateName,
329
- async (input, ctx) => {
330
- await guard(ctx);
331
- const current = await stored(ctx, input);
332
- const body = entity.copy(current.body);
333
- const minted = await refFor(ctx, input.targetRef, body);
334
- const written = await write(ctx, { entity: name, number: minted.ref, body }, duplicateName);
335
- return { ...written, minted: minted.minted };
336
- },
337
- {
338
- announce: ({ minted, ...written }) => announce({ kind: "created", written, minted }),
339
- input: typed(
340
- objectOf({ ref: nonEmptyString, targetRef: optional(nonEmptyString) })
341
- )
342
- }
343
- );
344
- const share = createWorkflow(
345
- `${name}.share`,
346
- async (input, ctx) => {
347
- await stored(ctx, input);
348
- const link = await shareLink({
349
- ref: input.ref,
350
- now: input.now,
351
- ...input.ttlDays === void 0 ? {} : { ttlDays: input.ttlDays }
352
- });
353
- return { number: input.ref, token: link.token, url: link.url, expiresAt: link.expiresAt };
354
- },
355
- {
356
- announce: (link) => announce({ kind: "shared", number: link.number, url: link.url, expiresAt: link.expiresAt }),
357
- input: typed(
358
- objectOf({ now: positiveInt, ref: nonEmptyString, ttlDays: optional(positiveInt) })
359
- )
360
- }
361
- );
362
- return { entity, create, save, setStatus, remove, send, convert, duplicate, share };
363
- };
364
-
365
- // src/workflows/ports.ts
366
- import { TaggedError as tagged2 } from "better-result";
367
- var MAIL_UNDELIVERED_TAG = "DocumentMailUndelivered";
368
- var MailUndelivered = class extends (/* @__PURE__ */ tagged2(MAIL_UNDELIVERED_TAG)) {
369
- };
3
+ documentWorkflows
4
+ } from "./workflows/document-workflows.js";
5
+ import {
6
+ MAIL_UNDELIVERED_TAG,
7
+ MailUndelivered
8
+ } from "./workflows/ports.js";
9
+ import {
10
+ CONVERSION_REFUSED_TAG,
11
+ ConversionRefused,
12
+ DOCUMENT_NOT_FOUND_TAG,
13
+ DOCUMENT_NOT_REMOVABLE_TAG,
14
+ DOCUMENT_TAKEN_TAG,
15
+ DocumentNotFound,
16
+ DocumentNotRemovable,
17
+ DocumentTaken,
18
+ NUMBERING_REFUSED_TAG,
19
+ NumberingRefused,
20
+ TRANSITION_REFUSED_TAG,
21
+ TransitionRefused
22
+ } from "./workflows/refusals.js";
370
23
  export {
371
24
  CONVERSION_REFUSED_TAG,
372
25
  ConversionRefused,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geonosis/document",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "types": "./dist/index.d.ts",
5
5
  "description": "The abstract Document: an entity-first base every kind of paper-trail record extends, a derived entity registry whose name union keeps every consumer map total, and per-entity body versioning — engine-free, SQL-free, the pure core the store, the workflows and the wire contract build on.",
6
6
  "keywords": [
@@ -58,10 +58,10 @@
58
58
  "migrations"
59
59
  ],
60
60
  "dependencies": {
61
+ "@geonosis/db": "^0.14.2",
62
+ "@geonosis/workflows": "^0.6.0",
61
63
  "@standard-schema/spec": "^1.1.0",
62
- "better-result": "^3.0.0",
63
- "@geonosis/db": "^0.14.1",
64
- "@geonosis/workflows": "^0.6.0"
64
+ "better-result": "^3.0.1"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "drizzle-orm": ">=0.45.0"
@@ -72,11 +72,11 @@
72
72
  }
73
73
  },
74
74
  "devDependencies": {
75
- "drizzle-orm": "^0.45.2",
75
+ "drizzle-orm": "^0.45.3",
76
76
  "embedded-postgres-17": "npm:embedded-postgres@17.10.0-beta.17",
77
77
  "embedded-postgres-18": "npm:embedded-postgres@18.4.0-beta.17",
78
- "pg": "^8.16.3",
79
- "zod": "^4.5.2"
78
+ "pg": "^8.23.0",
79
+ "zod": "^4.6.5"
80
80
  },
81
81
  "engines": {
82
82
  "node": ">=22"
@@ -85,7 +85,7 @@
85
85
  "access": "public"
86
86
  },
87
87
  "scripts": {
88
- "build": "tsx ../../tooling/build-step.ts tsup",
88
+ "build": "bun ../../tooling/build-step.ts bun ../../tooling/bundle.ts",
89
89
  "typecheck": "tsc --noEmit",
90
90
  "lint": "oxlint --config ../../.oxlintrc.json .",
91
91
  "test:unit": "vitest run --project unit --root ../.. --passWithNoTests \"$PWD\"",
@@ -1,171 +0,0 @@
1
- // src/core/body-version.ts
2
- var BODY_VERSION_KEY = "_v";
3
- var versionOf = (body) => {
4
- const stamped = body[BODY_VERSION_KEY];
5
- return typeof stamped === "number" && Number.isInteger(stamped) ? stamped : 1;
6
- };
7
- var migrateBody = (definition, stored) => {
8
- if (stored === null || typeof stored !== "object" || Array.isArray(stored)) return stored;
9
- const target = definition.bodyVersion ?? 1;
10
- const steps = definition.migrations ?? {};
11
- let body = stored;
12
- for (let version = versionOf(body); version < target; version += 1) {
13
- const step = steps[version];
14
- if (step === void 0) break;
15
- body = step(body);
16
- }
17
- const { [BODY_VERSION_KEY]: _stamp, ...withoutStamp } = body;
18
- return withoutStamp;
19
- };
20
- var stampBody = (definition, body) => ({
21
- ...body,
22
- [BODY_VERSION_KEY]: definition.bodyVersion ?? 1
23
- });
24
- var documentBodyVersioning = (definition) => ({
25
- migrate: (stored) => migrateBody(definition, stored),
26
- stamp: (body) => stampBody(definition, body)
27
- });
28
-
29
- // src/core/document.ts
30
- var fieldPatterns = {
31
- calendarDate: /^\d{4}-\d{2}-\d{2}$/,
32
- decimal: /^-?\d+(?:\.\d+)?$/
33
- };
34
- var FIELD_CHECKS = {
35
- calendarDate: {
36
- message: "a calendar date is `YYYY-MM-DD`, never prose",
37
- test: (value) => typeof value === "string" && fieldPatterns.calendarDate.test(value)
38
- },
39
- decimal: {
40
- message: "an amount is a decimal string, never a float",
41
- test: (value) => typeof value === "string" && fieldPatterns.decimal.test(value)
42
- },
43
- int: {
44
- message: "an integer",
45
- test: (value) => typeof value === "number" && Number.isInteger(value)
46
- },
47
- positiveInt: {
48
- message: "a positive integer",
49
- test: (value) => typeof value === "number" && Number.isInteger(value) && value > 0
50
- },
51
- text: { message: "a string", test: (value) => typeof value === "string" }
52
- };
53
- var validatorOf = (fields, options) => {
54
- const nullable = options?.nullable ?? false;
55
- return {
56
- "~standard": {
57
- validate: (input) => {
58
- if (input === null || typeof input !== "object") {
59
- return { issues: [{ message: "expected an object" }] };
60
- }
61
- const record = input;
62
- const issues = [];
63
- const value = {};
64
- for (const [key, kind] of Object.entries(fields)) {
65
- const held = record[key];
66
- value[key] = held;
67
- if (nullable && held === null) continue;
68
- if (!FIELD_CHECKS[kind].test(held)) {
69
- issues.push({ message: FIELD_CHECKS[kind].message, path: [{ key }] });
70
- }
71
- }
72
- return issues.length > 0 ? { issues } : { value };
73
- },
74
- vendor: "geonosis-document",
75
- version: 1
76
- }
77
- };
78
- };
79
- var parseWith = (schema, input) => {
80
- const result = schema["~standard"].validate(input);
81
- if (result instanceof Promise) {
82
- throw new Error(
83
- "Document.head and Document.record parse synchronously; an async Standard Schema cannot back them"
84
- );
85
- }
86
- if (result.issues !== void 0) {
87
- throw new Error(result.issues.map((issue) => issue.message).join("; "));
88
- }
89
- return result.value;
90
- };
91
- var headFields = {
92
- status: "text",
93
- issueDate: "calendarDate",
94
- dueDate: "calendarDate",
95
- counterparty: "text",
96
- externalRef: "text",
97
- revision: "text",
98
- total: "decimal"
99
- };
100
- var recordFields = {
101
- version: "positiveInt",
102
- createdAt: "int",
103
- updatedAt: "int"
104
- };
105
- var headValidator = validatorOf(headFields, { nullable: true });
106
- var recordValidator = validatorOf(recordFields);
107
- var Document = {
108
- head: headValidator,
109
- headFields,
110
- record: recordValidator,
111
- recordFields,
112
- extend: (definition) => ({
113
- ...definition,
114
- headOf: (body) => parseWith(headValidator, definition.headOf(body)),
115
- is: (document) => document.entity === definition.name,
116
- view: definition.view ?? definition.body
117
- })
118
- };
119
-
120
- // src/core/entities.ts
121
- var documentEntities = (map) => {
122
- for (const [key, entity] of Object.entries(map)) {
123
- if (entity.name !== key) {
124
- throw new Error(
125
- `documentEntities was handed "${key}" for an entity whose own name is "${entity.name}": the map's key is what a row's \`entity\` column carries, and a mismatch here is a row nothing can find again`
126
- );
127
- }
128
- }
129
- const names = Object.keys(map);
130
- const values = Object.values(map);
131
- const fold = (pick) => values.map(pick);
132
- const distinctBy = (pick) => [
133
- ...new Set(fold(pick))
134
- ];
135
- return {
136
- bodies: distinctBy((entity) => entity.body),
137
- cards: distinctBy((entity) => entity.card.schema),
138
- entities: map,
139
- fold,
140
- instance: (name, parts) => ({ ...parts, entity: name, body: parts.body }),
141
- keyed: (pick) => Object.fromEntries(names.map((name) => [name, pick(map[name], name)])),
142
- named: (name) => map[name],
143
- names,
144
- views: distinctBy((entity) => entity.view)
145
- };
146
- };
147
-
148
- // src/core/canonical.ts
149
- var KEY_ORDER = new Intl.Collator("en-US");
150
- var canonicalOf = (input) => {
151
- if (input === null || typeof input !== "object") return input;
152
- const { toJSON } = input;
153
- if (typeof toJSON === "function") return canonicalOf(toJSON.call(input));
154
- if (Array.isArray(input)) return input.map(canonicalOf);
155
- return Object.fromEntries(
156
- Object.entries(input).toSorted(([a], [b]) => KEY_ORDER.compare(a, b)).map(([key, held]) => [key, canonicalOf(held)])
157
- );
158
- };
159
-
160
- export {
161
- BODY_VERSION_KEY,
162
- migrateBody,
163
- stampBody,
164
- documentBodyVersioning,
165
- fieldPatterns,
166
- validatorOf,
167
- headFields,
168
- Document,
169
- documentEntities,
170
- canonicalOf
171
- };