@mdbase-dev/connect 0.1.0-beta.31 → 0.1.0-beta.33

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 (68) hide show
  1. package/README.md +112 -34
  2. package/dist/advanced.d.ts +9 -0
  3. package/dist/advanced.js +6 -0
  4. package/dist/application-session.d.ts +39 -14
  5. package/dist/application-session.js +201 -70
  6. package/dist/async.js +11 -5
  7. package/dist/authority-proof.d.ts +3 -0
  8. package/dist/authority-proof.js +24 -0
  9. package/dist/browser/integrity.json +1 -1
  10. package/dist/browser/mdbase-connect.min.js +3 -3
  11. package/dist/browser/mdbase-connect.min.js.sha384 +1 -1
  12. package/dist/browser.d.ts +3 -0
  13. package/dist/browser.js +5 -0
  14. package/dist/capabilities.js +2 -1
  15. package/dist/collection-client.d.ts +36 -33
  16. package/dist/collection-client.js +549 -104
  17. package/dist/connect-authorization-helpers.d.ts +3 -0
  18. package/dist/connect-authorization-helpers.js +13 -0
  19. package/dist/connect-options.d.ts +12 -4
  20. package/dist/connect.d.ts +12 -4
  21. package/dist/connect.js +102 -103
  22. package/dist/connection-file-transport.d.ts +2 -2
  23. package/dist/connection-file-transport.js +4 -4
  24. package/dist/connection-notifications.d.ts +10 -4
  25. package/dist/connection-notifications.js +41 -23
  26. package/dist/connection-transport.d.ts +19 -15
  27. package/dist/connection-transport.js +202 -202
  28. package/dist/connection-types.d.ts +5 -4
  29. package/dist/connection.d.ts +47 -46
  30. package/dist/connection.js +320 -162
  31. package/dist/crypto-entry.d.ts +7 -0
  32. package/dist/crypto-entry.js +4 -0
  33. package/dist/crypto.js +4 -4
  34. package/dist/external-store.d.ts +10 -0
  35. package/dist/external-store.js +10 -0
  36. package/dist/file-transfer-internals.d.ts +23 -0
  37. package/dist/file-transfer-internals.js +224 -0
  38. package/dist/files.d.ts +36 -14
  39. package/dist/files.js +92 -222
  40. package/dist/hosted-file-request.js +3 -3
  41. package/dist/index.d.ts +24 -19
  42. package/dist/index.js +10 -18
  43. package/dist/internal-types.d.ts +9 -1
  44. package/dist/local-file-transport.d.ts +2 -2
  45. package/dist/local-file-transport.js +13 -13
  46. package/dist/mdbase-connect.d.ts +6 -4
  47. package/dist/mdbase-connect.js +9 -8
  48. package/dist/notifications.d.ts +3 -2
  49. package/dist/operation-helpers.d.ts +3 -3
  50. package/dist/operation-helpers.js +17 -34
  51. package/dist/operation-types.d.ts +435 -45
  52. package/dist/outcomes.d.ts +15 -15
  53. package/dist/outcomes.js +13 -1
  54. package/dist/pending-mutation-store.d.ts +13 -0
  55. package/dist/pending-mutation-store.js +64 -0
  56. package/dist/request-budget.d.ts +27 -0
  57. package/dist/request-budget.js +95 -0
  58. package/dist/runtime-utils.d.ts +2 -0
  59. package/dist/runtime-utils.js +19 -0
  60. package/dist/session.d.ts +5 -4
  61. package/dist/session.js +6 -5
  62. package/dist/stored-token.d.ts +10 -0
  63. package/dist/stored-token.js +60 -0
  64. package/dist/token-response.d.ts +11 -0
  65. package/dist/token-response.js +77 -0
  66. package/dist/watch-policy.d.ts +8 -0
  67. package/dist/watch-policy.js +12 -0
  68. package/package.json +12 -2
@@ -1,6 +1,8 @@
1
1
  import { abortableDelay } from "./async.js";
2
- import { connectError, connectProblem, operationProblem } from "./errors.js";
2
+ import { MdbaseConnectError, connectError, connectProblem, operationProblem } from "./errors.js";
3
3
  import { COLLECTION_CHANGES_PROBLEM_CODES, COLLECTION_DESCRIPTION_PROBLEM_CODES, COLLECTION_MUTATION_PROBLEM_CODES, COLLECTION_QUERY_PROBLEM_CODES, COLLECTION_READ_PROBLEM_CODES, COLLECTION_TYPE_PROBLEM_CODES, ALL_CONNECT_PROBLEM_CODES, captureConnectOutcome, connectFailure, connectSuccess } from "./outcomes.js";
4
+ import { createRequestBudget, DEFAULT_REQUEST_TIMEOUT_MS, requestAbortReason } from "./request-budget.js";
5
+ import { watchRetryPolicy } from "./watch-policy.js";
4
6
  /**
5
7
  * Typed collection operations independent of OAuth, HTTP, or storage.
6
8
  *
@@ -9,23 +11,38 @@ import { COLLECTION_CHANGES_PROBLEM_CODES, COLLECTION_DESCRIPTION_PROBLEM_CODES,
9
11
  */
10
12
  export class MdbaseCollectionClient {
11
13
  transport;
12
- constructor(transport) {
14
+ requestTimeoutMs;
15
+ constructor(transport, requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
13
16
  this.transport = transport;
17
+ this.requestTimeoutMs = requestTimeoutMs;
14
18
  }
15
19
  operation(operation, input, options) {
16
20
  return captureConnectOutcome(() => this.transport.operation(operation, input, options), ALL_CONNECT_PROBLEM_CODES);
17
21
  }
18
- describe() {
19
- return this.rawOperation("describe", {}, COLLECTION_DESCRIPTION_PROBLEM_CODES);
20
- }
21
- changes(input = {}, options) {
22
- return this.rawOperation("changes", input, COLLECTION_CHANGES_PROBLEM_CODES, options);
23
- }
24
- read(input) {
25
- return this.envelopeOperation("read", input, COLLECTION_READ_PROBLEM_CODES);
26
- }
27
- query(input = {}, options) {
28
- return this.envelopeOperation("query", input, COLLECTION_QUERY_PROBLEM_CODES, options);
22
+ async describe(options) {
23
+ const outcome = await this.rawOperation("describe", {}, COLLECTION_DESCRIPTION_PROBLEM_CODES, options);
24
+ return mapOutcome(outcome, wireCollectionDescription);
25
+ }
26
+ async changes(input = {}, options) {
27
+ const outcome = await this.rawOperation("changes", input, COLLECTION_CHANGES_PROBLEM_CODES, options);
28
+ return mapOutcome(outcome, wireChangesPage);
29
+ }
30
+ async read(input, options) {
31
+ const outcome = await this.envelopeOperation("read", wireReadInput(input), COLLECTION_READ_PROBLEM_CODES, options);
32
+ return mapOutcome(outcome, wireRecordDocument);
33
+ }
34
+ async query(input = {}, options) {
35
+ const outcome = await this.envelopeOperation("query", wireQueryInput(input), COLLECTION_QUERY_PROBLEM_CODES, options);
36
+ return mapOutcome(outcome, ({ results, meta }) => ({
37
+ results: results.map(wireQueryRecord),
38
+ ...(meta ? {
39
+ meta: {
40
+ totalCount: meta.total_count,
41
+ hasMore: meta.has_more,
42
+ ...(meta.snapshot ? { snapshot: meta.snapshot } : {})
43
+ }
44
+ } : {})
45
+ }));
29
46
  }
30
47
  async *queryPages(input = {}, options = {}) {
31
48
  const { offset: requestedOffset, limit: requestedLimit, snapshot: requestedSnapshot, ...criteria } = input;
@@ -41,7 +58,7 @@ export class MdbaseCollectionClient {
41
58
  offset,
42
59
  limit: pageNumber === 0 ? firstPageSize : pageSize,
43
60
  ...(snapshot ? { snapshot } : {})
44
- }, { signal: options.signal });
61
+ }, { signal: options.signal, timeoutMs: options.pageTimeoutMs });
45
62
  if (!queried.ok) {
46
63
  yield queried;
47
64
  return;
@@ -55,7 +72,7 @@ export class MdbaseCollectionClient {
55
72
  if (!snapshot && returnedSnapshot)
56
73
  snapshot = returnedSnapshot;
57
74
  loaded += result.results.length;
58
- const complete = !result.meta?.has_more || result.results.length === 0;
75
+ const complete = !result.meta?.hasMore || result.results.length === 0;
59
76
  const page = {
60
77
  results: result.results,
61
78
  ...(result.meta ? { meta: result.meta } : {}),
@@ -74,92 +91,158 @@ export class MdbaseCollectionClient {
74
91
  }
75
92
  }
76
93
  async queryAll(input = {}, options = {}) {
94
+ const budget = createRequestBudget(options, this.requestTimeoutMs);
77
95
  const results = [];
78
96
  let finalPage;
79
97
  const diagnostics = [];
80
- for await (const outcome of this.queryPages(input, options)) {
81
- if (!outcome.ok)
82
- return outcome;
83
- const page = outcome.value;
84
- results.push(...page.results);
85
- finalPage = page;
86
- diagnostics.push(...outcome.diagnostics);
98
+ try {
99
+ for await (const outcome of this.queryPages(input, {
100
+ firstPageSize: options.firstPageSize,
101
+ pageSize: options.pageSize,
102
+ signal: budget.signal,
103
+ pageTimeoutMs: null,
104
+ onProgress: options.onProgress
105
+ })) {
106
+ if (!outcome.ok)
107
+ return outcome;
108
+ const page = outcome.value;
109
+ results.push(...page.results);
110
+ finalPage = page;
111
+ diagnostics.push(...outcome.diagnostics);
112
+ }
113
+ if (budget.signal.aborted)
114
+ throw requestAbortReason(budget.signal);
115
+ return connectSuccess({
116
+ results,
117
+ meta: {
118
+ ...(finalPage?.meta ?? {}),
119
+ totalCount: finalPage?.meta?.totalCount ?? results.length,
120
+ hasMore: finalPage ? !finalPage.complete : false,
121
+ ...(finalPage?.snapshot ? { snapshot: finalPage.snapshot } : {})
122
+ }
123
+ }, diagnostics);
87
124
  }
88
- return connectSuccess({
89
- results,
90
- meta: {
91
- ...(finalPage?.meta ?? {}),
92
- total_count: finalPage?.meta?.total_count ?? results.length,
93
- has_more: finalPage ? !finalPage.complete : false,
94
- ...(finalPage?.snapshot ? { snapshot: finalPage.snapshot } : {})
125
+ catch (error) {
126
+ if (error instanceof MdbaseConnectError) {
127
+ return connectFailure(error.problem);
95
128
  }
96
- }, diagnostics);
97
- }
98
- listViews() {
99
- return this.envelopeOperation("list_views", {}, COLLECTION_READ_PROBLEM_CODES);
100
- }
101
- executeView(input) {
102
- return this.envelopeOperation("execute_view", input, COLLECTION_READ_PROBLEM_CODES);
103
- }
104
- readViewSource(input) {
105
- return this.envelopeOperation("read_view_source", input, COLLECTION_READ_PROBLEM_CODES);
106
- }
107
- createViewSource(input) {
108
- return this.envelopeOperation("create_view_source", input, COLLECTION_MUTATION_PROBLEM_CODES);
109
- }
110
- updateViewSource(input) {
111
- return this.envelopeOperation("update_view_source", input, COLLECTION_MUTATION_PROBLEM_CODES);
112
- }
113
- deleteViewSource(input) {
114
- return this.envelopeOperation("delete_view_source", input, COLLECTION_MUTATION_PROBLEM_CODES);
115
- }
116
- create(input) {
117
- return this.envelopeOperation("create", input, COLLECTION_MUTATION_PROBLEM_CODES);
118
- }
119
- update(input) {
120
- return this.envelopeOperation("update", input, COLLECTION_MUTATION_PROBLEM_CODES);
121
- }
122
- delete(input, options) {
123
- return this.envelopeOperation("delete", input, COLLECTION_MUTATION_PROBLEM_CODES, options);
124
- }
125
- preflightDelete(input, options) {
126
- return this.envelopeOperation("delete", { ...input, check_backlinks: true, dry_run: true }, COLLECTION_MUTATION_PROBLEM_CODES, options);
127
- }
128
- rename(input, options) {
129
- return this.envelopeOperation("rename", input, COLLECTION_MUTATION_PROBLEM_CODES, options);
130
- }
131
- preflightRename(input, options) {
132
- return this.envelopeOperation("rename", { ...input, dry_run: true }, COLLECTION_MUTATION_PROBLEM_CODES, options);
133
- }
134
- validate(input = {}) {
135
- return this.envelopeOperation("validate", input, COLLECTION_READ_PROBLEM_CODES);
136
- }
137
- readType(input) {
138
- return this.envelopeOperation("read_type", input, COLLECTION_TYPE_PROBLEM_CODES);
139
- }
140
- createType(input) {
141
- return this.envelopeOperation("create_type", input, COLLECTION_TYPE_PROBLEM_CODES);
142
- }
143
- updateType(input) {
144
- return this.envelopeOperation("update_type", input, COLLECTION_TYPE_PROBLEM_CODES);
145
- }
146
- assessTypePack(input) {
147
- return this.envelopeOperation("assess_type_pack", input, COLLECTION_TYPE_PROBLEM_CODES);
148
- }
149
- applyTypePack(input) {
150
- return this.envelopeOperation("apply_type_pack", input, COLLECTION_TYPE_PROBLEM_CODES);
151
- }
152
- listTimers(namespace) {
153
- return this.rawOperation("list_timers", { namespace }, COLLECTION_READ_PROBLEM_CODES);
154
- }
155
- putTimer(input) {
156
- return this.rawOperation("put_timer", input, COLLECTION_MUTATION_PROBLEM_CODES);
157
- }
158
- cancelTimer(input) {
159
- return this.rawOperation("cancel_timer", input, COLLECTION_MUTATION_PROBLEM_CODES);
129
+ throw error;
130
+ }
131
+ finally {
132
+ budget.dispose();
133
+ }
160
134
  }
161
- reconcileTimers(input) {
162
- return this.rawOperation("reconcile_timers", input, COLLECTION_MUTATION_PROBLEM_CODES);
135
+ async listViews(options) {
136
+ const outcome = await this.envelopeOperation("list_views", {}, COLLECTION_READ_PROBLEM_CODES, options);
137
+ return mapOutcome(outcome, (value) => ({ views: value.views, meta: { totalCount: value.meta.total_count } }));
138
+ }
139
+ async executeView(input, options) {
140
+ const outcome = await this.envelopeOperation("execute_view", input, COLLECTION_READ_PROBLEM_CODES, options);
141
+ return mapOutcome(outcome, wireSavedViewExecution);
142
+ }
143
+ readViewSource(input, options) {
144
+ return this.envelopeOperation("read_view_source", input, COLLECTION_READ_PROBLEM_CODES, options);
145
+ }
146
+ createViewSource(input, options) {
147
+ return this.envelopeOperation("create_view_source", input, COLLECTION_MUTATION_PROBLEM_CODES, options);
148
+ }
149
+ updateViewSource(input, options) {
150
+ return this.envelopeOperation("update_view_source", wireRevisionInput(input), COLLECTION_MUTATION_PROBLEM_CODES, options);
151
+ }
152
+ deleteViewSource(input, options) {
153
+ return this.envelopeOperation("delete_view_source", wireRevisionInput(input), COLLECTION_MUTATION_PROBLEM_CODES, options);
154
+ }
155
+ async create(input, options) {
156
+ const outcome = await this.envelopeOperation("create", wireCreateInput(input), COLLECTION_MUTATION_PROBLEM_CODES, options);
157
+ return mapOutcome(outcome, wireRecordDocument);
158
+ }
159
+ async update(input, options) {
160
+ const outcome = await this.envelopeOperation("update", wireUpdateInput(input), COLLECTION_MUTATION_PROBLEM_CODES, options);
161
+ return mapOutcome(outcome, wireRecordDocument);
162
+ }
163
+ async delete(input, options) {
164
+ const outcome = await this.envelopeOperation("delete", wireDeleteInput(input), COLLECTION_MUTATION_PROBLEM_CODES, options);
165
+ return mapOutcome(outcome, wireDeleteResult);
166
+ }
167
+ async preflightDelete(input, options) {
168
+ const outcome = await this.envelopeOperation("delete", {
169
+ ...wireDeleteInput(input),
170
+ check_backlinks: true,
171
+ dry_run: true
172
+ }, COLLECTION_MUTATION_PROBLEM_CODES, options);
173
+ return mapOutcome(outcome, wireDeletePreflightResult);
174
+ }
175
+ async rename(input, options) {
176
+ const outcome = await this.envelopeOperation("rename", wireRenameInput(input), COLLECTION_MUTATION_PROBLEM_CODES, options);
177
+ return mapOutcome(outcome, wireRenameResult);
178
+ }
179
+ async preflightRename(input, options) {
180
+ const outcome = await this.envelopeOperation("rename", {
181
+ ...wireRenameInput(input),
182
+ dry_run: true
183
+ }, COLLECTION_MUTATION_PROBLEM_CODES, options);
184
+ return mapOutcome(outcome, wireRenamePreflightResult);
185
+ }
186
+ validate(input = {}, options) {
187
+ return this.envelopeOperation("validate", input, COLLECTION_READ_PROBLEM_CODES, options);
188
+ }
189
+ readType(input, options) {
190
+ return this.envelopeOperation("read_type", input, COLLECTION_TYPE_PROBLEM_CODES, options);
191
+ }
192
+ createType(input, options) {
193
+ return this.envelopeOperation("create_type", input, COLLECTION_TYPE_PROBLEM_CODES, options);
194
+ }
195
+ updateType(input, options) {
196
+ return this.envelopeOperation("update_type", wireRevisionInput(input), COLLECTION_TYPE_PROBLEM_CODES, options);
197
+ }
198
+ async assessTypePack(input, options) {
199
+ const outcome = await this.envelopeOperation("assess_type_pack", wireAssessTypePackInput(input), COLLECTION_TYPE_PROBLEM_CODES, options);
200
+ return mapOutcome(outcome, wireTypePackAssessment);
201
+ }
202
+ async applyTypePack(input, options) {
203
+ const outcome = await this.envelopeOperation("apply_type_pack", wireApplyTypePackInput(input), COLLECTION_TYPE_PROBLEM_CODES, options);
204
+ return mapOutcome(outcome, wireTypePackApplyResult);
205
+ }
206
+ async assessCollectionSetup(input, options) {
207
+ const outcome = await this.envelopeOperation("assess_collection_setup", wireAssessCollectionSetupInput(input), COLLECTION_TYPE_PROBLEM_CODES, options);
208
+ return mapOutcome(outcome, wireCollectionSetupAssessment);
209
+ }
210
+ async applyCollectionSetup(input, options) {
211
+ const outcome = await this.envelopeOperation("apply_collection_setup", wireApplyCollectionSetupInput(input), COLLECTION_TYPE_PROBLEM_CODES, options);
212
+ return mapOutcome(outcome, (value) => ({
213
+ assessment: wireCollectionSetupAssessment(value.assessment),
214
+ receipt: wireCollectionSetupReceipt(value.receipt)
215
+ }));
216
+ }
217
+ async listTimers(namespace, options) {
218
+ const outcome = await this.rawOperation("list_timers", { namespace }, COLLECTION_READ_PROBLEM_CODES, options);
219
+ return mapOutcome(outcome, (value) => ({
220
+ namespace: value.namespace,
221
+ timers: value.timers.map(wireTimer)
222
+ }));
223
+ }
224
+ async putTimer(input, options) {
225
+ const outcome = await this.rawOperation("put_timer", {
226
+ namespace: input.namespace,
227
+ criterion_id: input.criterionId,
228
+ timer: wireDesiredTimer(input.timer)
229
+ }, COLLECTION_MUTATION_PROBLEM_CODES, options);
230
+ return mapOutcome(outcome, wireTimer);
231
+ }
232
+ cancelTimer(input, options) {
233
+ return this.rawOperation("cancel_timer", input, COLLECTION_MUTATION_PROBLEM_CODES, options);
234
+ }
235
+ async reconcileTimers(input, options) {
236
+ const outcome = await this.rawOperation("reconcile_timers", {
237
+ namespace: input.namespace,
238
+ criterion_id: input.criterionId,
239
+ timers: input.timers.map(wireDesiredTimer)
240
+ }, COLLECTION_MUTATION_PROBLEM_CODES, options);
241
+ return mapOutcome(outcome, (value) => ({
242
+ namespace: value.namespace,
243
+ timers: value.timers.map(wireTimer),
244
+ cancelledIds: value.cancelled_ids
245
+ }));
163
246
  }
164
247
  async *watch(options = {}) {
165
248
  let cursor = options.cursor;
@@ -169,7 +252,7 @@ export class MdbaseCollectionClient {
169
252
  let connected = false;
170
253
  options.onStatus?.({ state: "connecting", ...(cursor === undefined ? {} : { cursor }) });
171
254
  while (!options.signal?.aborted) {
172
- const changed = await this.changes(cursor === undefined ? {} : { after: cursor, limit: 200 }, { signal: options.signal });
255
+ const changed = await this.changes(cursor === undefined ? {} : { after: cursor, limit: 200 }, { signal: options.signal, timeoutMs: options.timeoutMs });
173
256
  if (!changed.ok) {
174
257
  const problem = changed.problem;
175
258
  if (options.signal?.aborted)
@@ -220,7 +303,7 @@ export class MdbaseCollectionClient {
220
303
  for (const event of page.events)
221
304
  yield connectSuccess(event);
222
305
  cursor = page.cursor;
223
- if (!page.has_more) {
306
+ if (!page.hasMore) {
224
307
  try {
225
308
  await abortableDelay(pollInterval, options.signal);
226
309
  }
@@ -245,14 +328,376 @@ export class MdbaseCollectionClient {
245
328
  return connectSuccess(envelope.result, envelope.diagnostics);
246
329
  }
247
330
  }
248
- function watchRetryPolicy(options) {
249
- if (options === false)
250
- return undefined;
331
+ function mapOutcome(outcome, map) {
332
+ return outcome.ok ? connectSuccess(map(outcome.value), outcome.diagnostics) : outcome;
333
+ }
334
+ function wireReadInput(input) {
335
+ return {
336
+ path: input.path,
337
+ ...(input.contract ? { contract: input.contract } : {}),
338
+ ...(input.includeDocument === undefined ? {} : { include_document: input.includeDocument })
339
+ };
340
+ }
341
+ function wireQueryInput(input) {
342
+ return {
343
+ ...(input.types ? { types: input.types } : {}),
344
+ ...(input.context ? { context: input.context } : {}),
345
+ ...(input.projections ? {
346
+ projections: Object.fromEntries(Object.entries(input.projections).map(([name, projection]) => [
347
+ name,
348
+ { expr: projection.expression, ...(projection.description ? { description: projection.description } : {}) }
349
+ ]))
350
+ } : {}),
351
+ ...(input.where ? { where: input.where } : {}),
352
+ ...(input.select ? {
353
+ select: input.select.map((selection) => typeof selection === "string"
354
+ ? selection
355
+ : {
356
+ name: selection.name,
357
+ expr: selection.expression,
358
+ ...(selection.label ? { label: selection.label } : {}),
359
+ ...(selection.description ? { description: selection.description } : {})
360
+ })
361
+ } : {}),
362
+ ...(input.orderBy ? { order_by: input.orderBy } : {}),
363
+ ...(input.groupBy ? { group_by: input.groupBy } : {}),
364
+ ...(input.summaryFunctions ? {
365
+ summary_functions: Object.fromEntries(Object.entries(input.summaryFunctions).map(([name, projection]) => [
366
+ name,
367
+ { expr: projection.expression, ...(projection.description ? { description: projection.description } : {}) }
368
+ ]))
369
+ } : {}),
370
+ ...(input.summaries ? { summaries: input.summaries } : {}),
371
+ ...(input.limit === undefined ? {} : { limit: input.limit }),
372
+ ...(input.offset === undefined ? {} : { offset: input.offset }),
373
+ ...(input.snapshot ? { snapshot: input.snapshot } : {}),
374
+ ...(input.includeBody === undefined ? {} : { include_body: input.includeBody }),
375
+ ...(input.frontmatterMode ? { frontmatter_mode: input.frontmatterMode } : {}),
376
+ ...(input.contract ? { contract: input.contract } : {})
377
+ };
378
+ }
379
+ function wireCreateInput(input) {
380
+ return {
381
+ ...(input.path ? { path: input.path } : {}),
382
+ ...(input.type ? { type: input.type } : {}),
383
+ ...(input.contract ? { contract: input.contract } : {}),
384
+ ...(input.frontmatter ? { frontmatter: input.frontmatter } : {}),
385
+ ...(input.body === undefined ? {} : { body: input.body }),
386
+ ...(input.ifRevision ? { if_revision: input.ifRevision } : {}),
387
+ ...(input.includeDocument === undefined ? {} : { include_document: input.includeDocument })
388
+ };
389
+ }
390
+ function wireUpdateInput(input) {
391
+ return {
392
+ path: input.path,
393
+ ...(input.contract ? { contract: input.contract } : {}),
394
+ ...(input.ifRevision ? { if_revision: input.ifRevision } : {}),
395
+ ...(input.includeDocument === undefined ? {} : { include_document: input.includeDocument }),
396
+ ...(input.document === undefined
397
+ ? { patch: input.patch, ...(input.body === undefined ? {} : { body: input.body }) }
398
+ : { document: input.document })
399
+ };
400
+ }
401
+ function wireDeleteInput(input) {
402
+ return {
403
+ path: input.path,
404
+ ...(input.contract ? { contract: input.contract } : {}),
405
+ ...(input.checkBacklinks === undefined ? {} : { check_backlinks: input.checkBacklinks }),
406
+ ...(input.ifRevision ? { if_revision: input.ifRevision } : {})
407
+ };
408
+ }
409
+ function wireRenameInput(input) {
410
+ return {
411
+ from: input.from,
412
+ to: input.to,
413
+ ...(input.contract ? { contract: input.contract } : {}),
414
+ ...(input.updateRefs === undefined ? {} : { update_refs: input.updateRefs }),
415
+ ...(input.ifRevision ? { if_revision: input.ifRevision } : {}),
416
+ ...(input.includeDocument === undefined ? {} : { include_document: input.includeDocument })
417
+ };
418
+ }
419
+ function wireRevisionInput(input) {
420
+ const { ifRevision, ...rest } = input;
421
+ return { ...rest, ...(ifRevision ? { if_revision: ifRevision } : {}) };
422
+ }
423
+ function wireAssessTypePackInput(input) {
424
+ return {
425
+ provision: input.provision,
426
+ installed_by: input.installedBy,
427
+ ...(input.adoptResources ? { adopt_resources: input.adoptResources } : {}),
428
+ ...(input.preserveSeedTargets ? { preserve_seed_targets: input.preserveSeedTargets } : {}),
429
+ ...(input.targetOverrides ? { target_overrides: input.targetOverrides } : {}),
430
+ ...(input.contractSetups ? { contract_setups: input.contractSetups.map(wireContractSetupChoice) } : {})
431
+ };
432
+ }
433
+ function wireApplyTypePackInput(input) {
434
+ return {
435
+ ...wireAssessTypePackInput(input),
436
+ expected_assessment_digest: input.expectedAssessmentDigest,
437
+ ...(input.allowDowngrade === undefined ? {} : { allow_downgrade: input.allowDowngrade })
438
+ };
439
+ }
440
+ function wireAssessCollectionSetupInput(input) {
441
+ return {
442
+ application_id: input.applicationId,
443
+ declaration_digest: input.declarationDigest,
444
+ requirements: input.requirements,
445
+ provisions: {
446
+ configuration: input.provisions.configuration,
447
+ type_packs: input.provisions.typePacks
448
+ },
449
+ ...(input.contractSetups ? { contract_setups: input.contractSetups.map(wireContractSetupChoice) } : {})
450
+ };
451
+ }
452
+ function wireApplyCollectionSetupInput(input) {
453
+ return {
454
+ ...wireAssessCollectionSetupInput(input),
455
+ expected_assessment_digest: input.expectedAssessmentDigest,
456
+ expected_collection_revision: input.expectedCollectionRevision,
457
+ expected_provision_digest: input.expectedProvisionDigest,
458
+ ...(input.allowTypePackDowngrades
459
+ ? { allow_type_pack_downgrades: input.allowTypePackDowngrades }
460
+ : {})
461
+ };
462
+ }
463
+ function wireDeleteResult(value) {
464
+ return {
465
+ path: value.path,
466
+ deleted: value.deleted,
467
+ ...(value.broken_links ? { brokenLinks: value.broken_links } : {})
468
+ };
469
+ }
470
+ function wireDeletePreflightResult(value) {
471
+ return {
472
+ ...wireDeleteResult(value),
473
+ deleted: false,
474
+ dryRun: true,
475
+ wouldDelete: true
476
+ };
477
+ }
478
+ function wireRenameResult(value) {
479
+ const { references_updated, ...record } = value;
480
+ return {
481
+ ...wireRecordDocument(record),
482
+ from: value.from,
483
+ to: value.to,
484
+ ...(references_updated ? { referencesUpdated: references_updated } : {})
485
+ };
486
+ }
487
+ function wireRenamePreflightResult(value) {
488
+ return {
489
+ from: value.from,
490
+ to: value.to,
491
+ dryRun: true,
492
+ wouldRename: true,
493
+ ...(value.references_affected ? { referencesAffected: value.references_affected } : {}),
494
+ ...(value.warnings ? { warnings: value.warnings } : {})
495
+ };
496
+ }
497
+ function wireDesiredTimer(timer) {
498
+ return { id: timer.id, fire_at: timer.fireAt, ...(timer.data === undefined ? {} : { data: timer.data }) };
499
+ }
500
+ function wireTimer(timer) {
501
+ return {
502
+ id: timer.id,
503
+ fireAt: timer.fire_at,
504
+ ...(timer.data === undefined ? {} : { data: timer.data }),
505
+ criterionId: timer.criterion_id,
506
+ generation: timer.generation,
507
+ status: timer.status,
508
+ createdAt: timer.created_at,
509
+ updatedAt: timer.updated_at,
510
+ firedAt: timer.fired_at
511
+ };
512
+ }
513
+ function wireContractSetupChoice(choice) {
514
+ return choice.mode === "starter"
515
+ ? choice
516
+ : {
517
+ contract: choice.contract,
518
+ mode: "existing",
519
+ type_name: choice.typeName,
520
+ type_revision: choice.typeRevision,
521
+ fields: choice.fields,
522
+ ...(choice.binding ? { binding: choice.binding } : {})
523
+ };
524
+ }
525
+ function wireDataContractIdentity(value) {
526
+ return {
527
+ id: value.id,
528
+ version: value.version,
529
+ digest: value.digest,
530
+ type: value.type,
531
+ implementationDigest: value.implementation_digest
532
+ };
533
+ }
534
+ function wireQueryRecord(value) {
535
+ const { effective_frontmatter, contract, ...record } = value;
536
+ return {
537
+ ...record,
538
+ ...(effective_frontmatter === undefined ? {} : { effectiveFrontmatter: effective_frontmatter }),
539
+ ...(contract ? { contract: wireDataContractIdentity(contract) } : {})
540
+ };
541
+ }
542
+ function wireRecordDocument(value) {
543
+ const { effective_frontmatter, contract, ...record } = value;
544
+ return {
545
+ ...record,
546
+ effectiveFrontmatter: effective_frontmatter,
547
+ ...(contract ? { contract: wireDataContractIdentity(contract) } : {})
548
+ };
549
+ }
550
+ function wireSavedViewExecution(value) {
551
+ return {
552
+ results: value.results.map((record) => {
553
+ const mapped = wireQueryRecord(record);
554
+ if (!mapped.effectiveFrontmatter) {
555
+ throw new Error("Saved view result omitted canonical effective frontmatter.");
556
+ }
557
+ return { ...mapped, effectiveFrontmatter: mapped.effectiveFrontmatter };
558
+ }),
559
+ meta: {
560
+ totalCount: value.meta.total_count,
561
+ hasMore: value.meta.has_more,
562
+ view: value.meta.view,
563
+ ...(value.meta.context ? { context: value.meta.context } : {}),
564
+ ...(value.meta.groups ? { groups: value.meta.groups } : {})
565
+ }
566
+ };
567
+ }
568
+ function wireTypePackResource(value) {
569
+ return {
570
+ source: value.source,
571
+ target: value.target,
572
+ kind: value.kind,
573
+ mode: value.mode,
574
+ action: value.action,
575
+ digest: value.digest,
576
+ ...(value.current_digest ? { currentDigest: value.current_digest } : {}),
577
+ ...(value.installed_digest ? { installedDigest: value.installed_digest } : {}),
578
+ ...(value.adopted_from_digest ? { adoptedFromDigest: value.adopted_from_digest } : {}),
579
+ ...(value.reason ? { reason: value.reason } : {})
580
+ };
581
+ }
582
+ function wireTypePackReceipt(value) {
583
+ return {
584
+ id: value.id,
585
+ version: value.version,
586
+ digest: value.digest,
587
+ installedBy: value.installed_by,
588
+ resources: value.resources.map((resource) => ({
589
+ source: resource.source,
590
+ target: resource.target,
591
+ kind: resource.kind,
592
+ mode: resource.mode,
593
+ digest: resource.digest,
594
+ ...(resource.adopted_from_digest ? { adoptedFromDigest: resource.adopted_from_digest } : {})
595
+ }))
596
+ };
597
+ }
598
+ function clientContractSetupChoice(choice) {
599
+ return choice.mode === "starter"
600
+ ? choice
601
+ : {
602
+ contract: choice.contract,
603
+ mode: "existing",
604
+ typeName: choice.type_name,
605
+ typeRevision: choice.type_revision,
606
+ fields: choice.fields,
607
+ ...(choice.binding ? { binding: choice.binding } : {})
608
+ };
609
+ }
610
+ function wireTypePackAssessment(value) {
611
+ return {
612
+ status: value.status,
613
+ applicable: value.applicable,
614
+ assessmentDigest: value.assessment_digest,
615
+ ...(value.current ? { current: wireTypePackReceipt(value.current) } : {}),
616
+ desired: wireTypePackReceipt(value.desired),
617
+ resources: value.resources.map(wireTypePackResource),
618
+ lock: value.lock,
619
+ contractSetups: {
620
+ choices: value.contract_setups.choices.map(clientContractSetupChoice),
621
+ resources: value.contract_setups.resources.map(wireTypePackResource)
622
+ }
623
+ };
624
+ }
625
+ function wireTypePackApplyResult(value) {
626
+ return {
627
+ ...wireTypePackAssessment(value),
628
+ receipt: wireTypePackReceipt(value.receipt),
629
+ cleanupDeferred: value.cleanup_deferred
630
+ };
631
+ }
632
+ function wireCollectionSetupAssessment(value) {
633
+ return {
634
+ status: value.status,
635
+ applicable: value.applicable,
636
+ applicationId: value.application_id,
637
+ declarationDigest: value.declaration_digest,
638
+ provisionDigest: value.provision_digest,
639
+ collectionRevision: value.collection_revision,
640
+ finalCollectionRevision: value.final_collection_revision,
641
+ configuration: value.configuration,
642
+ typePacks: value.type_packs.map(wireTypePackAssessment),
643
+ finalResourceRevisions: value.final_resource_revisions,
644
+ assessmentDigest: value.assessment_digest
645
+ };
646
+ }
647
+ function wireCollectionSetupReceipt(value) {
648
+ return {
649
+ applicationId: value.application_id,
650
+ declarationDigest: value.declaration_digest,
651
+ provisionDigest: value.provision_digest,
652
+ assessmentDigest: value.assessment_digest,
653
+ collectionRevision: value.collection_revision,
654
+ configuration: value.configuration,
655
+ typePacks: value.type_packs.map(wireTypePackReceipt),
656
+ cleanupDeferred: value.cleanup_deferred
657
+ };
658
+ }
659
+ function wireChange(value) {
660
+ return {
661
+ cursor: value.cursor,
662
+ type: value.type,
663
+ occurredAt: value.occurred_at,
664
+ payload: value.payload
665
+ };
666
+ }
667
+ function wireChangesPage(value) {
668
+ return {
669
+ events: value.events.map(wireChange),
670
+ cursor: value.cursor,
671
+ hasMore: value.has_more,
672
+ reset: value.reset
673
+ };
674
+ }
675
+ function wireCollectionDescription(value) {
251
676
  return {
252
- initialDelayMs: Math.max(0, options?.initialDelayMs ?? 500),
253
- maxDelayMs: Math.max(0, options?.maxDelayMs ?? 15_000),
254
- multiplier: Math.max(1, options?.multiplier ?? 2),
255
- ...(options?.maxAttempts === undefined ? {} : { maxAttempts: Math.max(0, options.maxAttempts) })
677
+ protocolVersion: value.protocol_version,
678
+ collectionId: value.collection_id,
679
+ displayName: value.display_name,
680
+ specVersion: value.spec_version,
681
+ operations: value.operations,
682
+ changeCursor: value.change_cursor,
683
+ types: value.types,
684
+ contracts: value.contracts.map((contract) => ({
685
+ contractType: contract.contract_type,
686
+ id: contract.id,
687
+ version: contract.version,
688
+ digest: contract.digest,
689
+ schema: contract.schema,
690
+ ...(contract.binding_schema ? { bindingSchema: contract.binding_schema } : {}),
691
+ implementations: contract.implementations.map((implementation) => ({
692
+ typeName: implementation.type_name,
693
+ typeVersion: implementation.type_version,
694
+ ...(implementation.type_path ? { typePath: implementation.type_path } : {}),
695
+ digest: implementation.digest,
696
+ fields: implementation.fields,
697
+ ...(implementation.binding ? { binding: implementation.binding } : {})
698
+ }))
699
+ })),
700
+ ...(value.configuration ? { configuration: value.configuration } : {})
256
701
  };
257
702
  }
258
703
  function nonNegativeInteger(value, fallback) {