@adhd/apigen-base-logical 0.1.2 → 0.1.3

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 (4) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/index.js +1 -1
  3. package/index.mjs +856 -1024
  4. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1,1031 +1,863 @@
1
- const D = "$apigen";
2
- class N extends Error {
3
- constructor(t) {
4
- super(t), this.code = "E_DUP_CODEC", this.name = "CodecRegistryError";
5
- }
6
- }
7
- function de(e) {
8
- e == null || e.wellKnown;
9
- const t = /* @__PURE__ */ new Map();
10
- return {
11
- register(n, o) {
12
- if (t.has(n.id) && !(o != null && o.override))
13
- throw new N(
14
- `E_DUP_CODEC: a codec is already registered for id "${n.id}"`
15
- );
16
- t.set(n.id, n);
17
- },
18
- resolve(n) {
19
- for (const o of t.values())
20
- if (o.matches(n))
21
- return o;
22
- },
23
- get(n) {
24
- return t.get(n);
25
- },
26
- ids() {
27
- return [...t.keys()];
28
- },
29
- freeze() {
30
- const n = new Map(t), o = {
31
- register() {
32
- throw new N(
33
- "E_DUP_CODEC: registry is frozen and cannot accept new codecs"
34
- );
35
- },
36
- resolve(i) {
37
- for (const a of n.values())
38
- if (a.matches(i))
39
- return a;
40
- },
41
- get(i) {
42
- return n.get(i);
43
- },
44
- ids() {
45
- return [...n.keys()];
46
- },
47
- freeze() {
48
- return o;
49
- }
50
- };
51
- return o;
52
- }
53
- };
54
- }
55
- const z = "x-apigen-logical", J = "x-apigen-codec", ae = "x-apigen-ctor", fe = "x-apigen-tojson", ue = "0.1.0", V = [
56
- "scalar",
57
- "nominal",
58
- "union",
59
- "map",
60
- "set"
61
- ];
62
- function pe(e) {
63
- const t = e[z];
64
- return typeof t == "string" && V.includes(t) ? t : void 0;
65
- }
66
- function me(e) {
67
- const t = e[J];
68
- return typeof t == "string" ? t : void 0;
69
- }
70
- function v(e, t) {
71
- return {
72
- registry: e,
73
- resolve: (r) => {
74
- throw new Error(
75
- `[apigen-logical] $ref "${r}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`
76
- );
77
- },
78
- seen: /* @__PURE__ */ new WeakSet(),
79
- path: "",
80
- mode: "strict",
81
- ...t
82
- };
83
- }
84
- function l(e, t, r) {
85
- const n = r.registry.resolve(t);
86
- if (n)
87
- return n.encode(e, t, r);
88
- const o = t.$ref;
89
- if (typeof o == "string") {
90
- const c = r.resolve(o);
91
- return l(e, c, r);
92
- }
93
- const i = t.oneOf;
94
- if (Array.isArray(i)) {
95
- const c = L(e, i, t, r);
96
- return l(e, c, r);
97
- }
98
- const a = t.type;
99
- if (a === "array") {
100
- if (!Array.isArray(e))
101
- return b(e, t, r);
102
- const c = t.items;
103
- if (!c)
104
- return e.map((f) => _(f));
105
- const u = r.path;
106
- return Array.isArray(c) ? e.map((f, s) => {
107
- const d = c[s];
108
- return d === void 0 ? _(f) : l(f, d, { ...r, path: `${u}/${s}` });
109
- }) : e.map(
110
- (f, s) => l(f, c, { ...r, path: `${u}/${s}` })
111
- );
112
- }
113
- if (a === "object") {
114
- if (e === null || typeof e != "object" || Array.isArray(e))
115
- return b(e, t, r);
116
- const c = t.properties;
117
- if (!c)
118
- return _(e);
119
- const u = r.path, f = {};
120
- for (const [s, d] of Object.entries(c)) {
121
- const p = e[s];
122
- p !== void 0 && (f[s] = l(p, d, {
123
- ...r,
124
- path: `${u}/${s}`
125
- }));
126
- }
127
- return f;
128
- }
129
- return a == null ? b(e, t, r) : _(e);
130
- }
131
- function y(e, t, r) {
132
- const n = r.registry.resolve(t);
133
- if (n)
134
- return n.decode(e, t, r);
135
- const o = t.$ref;
136
- if (typeof o == "string") {
137
- const c = r.resolve(o);
138
- return y(e, c, r);
139
- }
140
- const i = t.oneOf;
141
- if (Array.isArray(i)) {
142
- const c = L(e, i, t, r);
143
- return y(e, c, r);
144
- }
145
- const a = t.type;
146
- if (a === "array") {
147
- if (!Array.isArray(e))
148
- return e;
149
- const c = t.items;
150
- if (!c)
151
- return e;
152
- const u = r.path;
153
- return Array.isArray(c) ? e.map((f, s) => {
154
- const d = c[s];
155
- return d === void 0 ? f : y(f, d, { ...r, path: `${u}/${s}` });
156
- }) : e.map(
157
- (f, s) => y(f, c, { ...r, path: `${u}/${s}` })
158
- );
159
- }
160
- if (a === "object") {
161
- if (e === null || typeof e != "object" || Array.isArray(e))
162
- return e;
163
- const c = t.properties;
164
- if (!c)
165
- return e;
166
- const u = r.path, f = {};
167
- for (const [s, d] of Object.entries(c)) {
168
- const p = e[s];
169
- p !== void 0 && (f[s] = y(p, d, {
170
- ...r,
171
- path: `${u}/${s}`
172
- }));
173
- }
174
- for (const [s, d] of Object.entries(e))
175
- !(s in c) && d !== void 0 && (f[s] = d);
176
- return f;
177
- }
178
- return a == null ? S(e, r) : e;
179
- }
180
- function L(e, t, r, n) {
181
- var u;
182
- const o = r.discriminator;
183
- if (o != null && o.propertyName) {
184
- const f = o.propertyName, s = e !== null && typeof e == "object" ? e[f] : void 0;
185
- if (typeof s == "string") {
186
- if (o.mapping) {
187
- const d = o.mapping[s];
188
- if (d) {
189
- const p = t.find((m) => m.$ref === d);
190
- if (p)
191
- return p;
192
- }
193
- }
194
- for (const d of t) {
195
- const m = (u = O(d, n).properties) == null ? void 0 : u[f];
196
- if (!m)
197
- continue;
198
- const g = m.enum;
199
- if (m.const === s || Array.isArray(g) && g.includes(s))
200
- return d;
201
- }
202
- }
203
- }
204
- const i = Y(e, t, n);
205
- if (i)
206
- return i;
207
- let a, c = -1;
208
- for (const f of t) {
209
- const s = O(f, n), d = U(e, s, n);
210
- d !== null && d > c && (c = d, a = f);
211
- }
212
- return a !== void 0 ? a : t[0] ?? {};
213
- }
214
- function O(e, t) {
215
- const r = e.$ref;
216
- return typeof r == "string" ? t.resolve(r) : e;
217
- }
218
- function I(e) {
219
- if ("const" in e)
220
- return e.const;
221
- const t = e.enum;
222
- if (Array.isArray(t) && t.length === 1)
223
- return t[0];
224
- }
225
- function Y(e, t, r) {
226
- if (e === null || typeof e != "object" || Array.isArray(e))
227
- return;
228
- const n = e, o = t.map((a) => O(a, r)), i = /* @__PURE__ */ new Set();
229
- for (const a of o) {
230
- const c = a.properties;
231
- if (c)
232
- for (const [u, f] of Object.entries(c))
233
- I(f) !== void 0 && i.add(u);
234
- }
235
- for (const a of i) {
236
- const c = o.map((p) => {
237
- const m = p.properties, g = m == null ? void 0 : m[a];
238
- return g ? I(g) : void 0;
239
- }), u = c.filter((p) => p !== void 0), f = new Set(u);
240
- if (u.length < 2 || f.size !== u.length)
241
- continue;
242
- const s = n[a];
243
- if (s === void 0)
244
- continue;
245
- const d = c.findIndex((p) => p === s);
246
- if (d !== -1)
247
- return t[d];
248
- }
249
- }
250
- function W(e) {
251
- if (e === null)
252
- return "null";
253
- if (Array.isArray(e))
254
- return "array";
255
- const t = typeof e;
256
- return t === "number" ? Number.isInteger(e) ? "integer" : "number" : t;
257
- }
258
- function U(e, t, r) {
259
- const n = t.oneOf;
260
- if (Array.isArray(n)) {
261
- let s = null;
262
- for (const d of n) {
263
- const p = U(e, O(d, r), r);
264
- p !== null && (s === null || p > s) && (s = p);
265
- }
266
- return s;
267
- }
268
- const o = t.type, i = W(e);
269
- if (typeof o == "string") {
270
- if (!(o === i || o === "number" && i === "integer"))
271
- return null;
272
- } else if (Array.isArray(o) && !o.some(
273
- (d) => d === i || d === "number" && i === "integer"
274
- ))
275
- return null;
276
- if (e === null || typeof e != "object" || Array.isArray(e))
277
- return typeof o == "string" ? 1 : 0;
278
- const a = e, c = t.required;
279
- let u = 0;
280
- if (Array.isArray(c)) {
281
- for (const s of c)
282
- if (typeof s == "string") {
283
- if (a[s] === void 0)
284
- return null;
285
- u += 2;
286
- }
287
- }
288
- const f = t.properties;
289
- if (f) {
290
- for (const s of Object.keys(f))
291
- a[s] !== void 0 && (u += 1);
292
- for (const s of Object.keys(a))
293
- f[s] === void 0 && (u -= 1);
294
- }
295
- return u;
296
- }
297
- function b(e, t, r) {
298
- var n;
299
- for (const o of r.registry.ids()) {
300
- const i = r.registry.get(o);
301
- if ((n = i == null ? void 0 : i.ownsValue) != null && n.call(i, e))
302
- return {
303
- [D]: o,
304
- v: i.encode(e, i.schema, r)
305
- };
306
- }
307
- if (Array.isArray(e))
308
- return e.map((o) => b(o, {}, r));
309
- if (j(e)) {
310
- const o = {};
311
- for (const [i, a] of Object.entries(e))
312
- a !== void 0 && (o[i] = b(a, {}, r));
313
- return o;
314
- }
315
- return _(e);
316
- }
317
- function j(e) {
318
- if (e === null || typeof e != "object" || Array.isArray(e))
319
- return !1;
320
- const t = Object.getPrototypeOf(e);
321
- return t === Object.prototype || t === null;
322
- }
323
- function S(e, t) {
324
- if (e !== null && typeof e == "object" && !Array.isArray(e) && D in e) {
325
- const r = e, n = r[D], o = t.registry.get(n);
326
- if (o)
327
- return o.decode(r.v, o.schema, t);
328
- }
329
- if (Array.isArray(e))
330
- return e.map((r) => S(r, t));
331
- if (j(e)) {
332
- const r = {};
333
- for (const [n, o] of Object.entries(e))
334
- r[n] = S(o, t);
335
- return r;
336
- }
337
- return e;
338
- }
339
- function _(e) {
340
- if (e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean")
341
- return e;
342
- if (Array.isArray(e))
343
- return e.map(_);
344
- if (typeof e == "object") {
345
- const t = {};
346
- for (const [r, n] of Object.entries(e))
347
- n !== void 0 && (t[r] = _(n));
348
- return t;
349
- }
350
- return null;
351
- }
352
- function _e(e, t) {
353
- const r = /* @__PURE__ */ new Set(), n = [e];
354
- for (; n.length > 0; ) {
355
- const o = n.pop();
356
- if (!o || r.has(o))
357
- continue;
358
- r.add(o);
359
- const i = o.$ref;
360
- if (typeof i == "string") {
361
- if (t) {
362
- const d = t[i];
363
- if (!d) {
364
- const p = Object.keys(t).join(", ") || "(none)";
365
- throw new Error(
366
- `[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${p}`
367
- );
368
- }
369
- n.push(d);
370
- }
371
- continue;
372
- }
373
- const a = o.oneOf;
374
- if (Array.isArray(a))
375
- for (const d of a)
376
- typeof d == "object" && d !== null && n.push(d);
377
- const c = o.properties;
378
- if (c)
379
- for (const d of Object.values(c))
380
- n.push(d);
381
- const u = o.items;
382
- if (Array.isArray(u))
383
- for (const d of u)
384
- typeof d == "object" && d !== null && n.push(d);
385
- else
386
- typeof u == "object" && u !== null && n.push(u);
387
- const f = o.additionalProperties;
388
- typeof f == "object" && f !== null && n.push(f);
389
- const s = o.propertyNames;
390
- typeof s == "object" && s !== null && n.push(s);
391
- }
392
- }
393
- function le(e) {
394
- return {
395
- encode(t, r, n) {
396
- const o = v(e, n);
397
- return l(t, r, o);
398
- },
399
- decode(t, r, n) {
400
- const o = v(e, n);
401
- return y(t, r, o);
402
- }
403
- };
404
- }
405
- function ye(e, t, r) {
406
- try {
407
- const n = r();
408
- e.register(n);
409
- } catch (n) {
410
- if (q(n))
411
- return;
412
- throw n;
413
- }
414
- }
415
- function q(e) {
416
- return e === null || typeof e != "object" ? !1 : e.code === "MODULE_NOT_FOUND";
417
- }
418
- const k = {
419
- id: "date-time",
420
- kind: "scalar",
421
- schema: { type: "string", format: "date-time" },
422
- matches(e) {
423
- return e.type === "string" && e.format === "date-time";
424
- },
425
- ownsValue(e) {
426
- return e instanceof Date;
427
- },
428
- encode(e, t, r) {
429
- return e.toISOString();
430
- },
431
- decode(e, t, r) {
432
- if (typeof e != "string") {
433
- if (r.mode === "strict")
434
- throw new TypeError(
435
- `[date-time] expected a string on the wire at "${r.path}", got ${typeof e}`
436
- );
437
- return new Date(String(e));
438
- }
439
- if (r.mode === "strict" && Number.isNaN(new Date(e).getTime()))
440
- throw new TypeError(
441
- `[date-time] invalid date-time string at "${r.path}": ${JSON.stringify(e)}`
442
- );
443
- return new Date(e);
444
- }
445
- }, R = {
446
- id: "int64",
447
- kind: "scalar",
448
- schema: { type: "string", format: "int64" },
449
- matches(e) {
450
- return e.type === "string" && e.format === "int64";
451
- },
452
- ownsValue(e) {
453
- return typeof e == "bigint";
454
- },
455
- encode(e, t, r) {
456
- return String(e);
457
- },
458
- decode(e, t, r) {
459
- if (typeof e != "string") {
460
- if (r.mode === "strict")
461
- throw new TypeError(
462
- `[int64] expected a string on the wire at "${r.path}", got ${typeof e}`
463
- );
464
- try {
465
- return BigInt(Math.trunc(Number(e)));
466
- } catch {
467
- return BigInt(0);
468
- }
469
- }
470
- if (r.mode === "strict")
471
- return BigInt(e);
472
- if (/^-?\d+$/.test(e))
473
- try {
474
- return BigInt(e);
475
- } catch {
476
- return BigInt(0);
477
- }
478
- return BigInt(0);
479
- }
1
+ //#region src/lib/contracts.ts
2
+ var e = "$apigen", t = class extends Error {
3
+ constructor(e) {
4
+ super(e), this.code = "E_DUP_CODEC", this.name = "CodecRegistryError";
5
+ }
480
6
  };
481
- function ge(e) {
482
- return e;
483
- }
484
- const P = {
485
- id: "decimal",
486
- kind: "scalar",
487
- schema: { type: "string", format: "decimal" },
488
- matches(e) {
489
- return e.type === "string" && e.format === "decimal";
490
- },
491
- encode(e, t, r) {
492
- return e;
493
- },
494
- decode(e, t, r) {
495
- if (typeof e != "string") {
496
- if (r.mode === "strict")
497
- throw new TypeError(
498
- `[decimal] expected a string on the wire at "${r.path}", got ${typeof e}`
499
- );
500
- return String(e);
501
- }
502
- if (r.mode === "strict" && !/^-?\d+(\.\d+)?$/.test(e))
503
- throw new TypeError(
504
- `[decimal] wire value "${e}" at "${r.path}" is not a valid decimal string`
505
- );
506
- return e;
507
- }
508
- }, B = {
509
- id: "byte",
510
- kind: "scalar",
511
- schema: { type: "string", format: "byte" },
512
- matches(e) {
513
- return e.type === "string" && e.format === "byte";
514
- },
515
- ownsValue(e) {
516
- return e instanceof Uint8Array;
517
- },
518
- encode(e, t, r) {
519
- return Buffer.from(e).toString("base64");
520
- },
521
- decode(e, t, r) {
522
- if (typeof e != "string") {
523
- if (r.mode === "strict")
524
- throw new TypeError(
525
- `[byte] expected a base64 string on the wire at "${r.path}", got ${typeof e}`
526
- );
527
- return new Uint8Array(0);
528
- }
529
- if (r.mode === "strict" && !/^[A-Za-z0-9+/]*={0,2}$/.test(e))
530
- throw new TypeError(
531
- `[byte] wire value at "${r.path}" is not standard base64 (format:byte): "${e}"`
532
- );
533
- return new Uint8Array(Buffer.from(e, "base64"));
534
- }
535
- }, K = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, w = {
536
- id: "uuid",
537
- kind: "scalar",
538
- schema: { type: "string", format: "uuid" },
539
- matches(e) {
540
- return e.type === "string" && e.format === "uuid";
541
- },
542
- encode(e, t, r) {
543
- return e.toLowerCase();
544
- },
545
- decode(e, t, r) {
546
- if (typeof e != "string") {
547
- if (r.mode === "strict")
548
- throw new TypeError(
549
- `[uuid] expected a string on the wire at "${r.path}", got ${typeof e}`
550
- );
551
- return String(e).toLowerCase();
552
- }
553
- if (r.mode === "strict" && !K.test(e))
554
- throw new TypeError(
555
- `[uuid] wire value "${e}" at "${r.path}" is not a lowercase-hyphenated RFC 4122 UUID`
556
- );
557
- return e;
558
- }
559
- }, h = "NaN", C = "Infinity", $ = "-Infinity", X = /* @__PURE__ */ new Set([
560
- h,
561
- C,
562
- $
563
- ]), M = {
564
- id: "number-special",
565
- kind: "scalar",
566
- schema: { type: "number" },
567
- matches(e) {
568
- return e.type === "number" && e.format === void 0;
569
- },
570
- ownsValue(e) {
571
- return typeof e == "number" && !Number.isFinite(e);
572
- },
573
- encode(e, t, r) {
574
- return Number.isNaN(e) ? h : e === 1 / 0 ? C : e === -1 / 0 ? $ : e;
575
- },
576
- decode(e, t, r) {
577
- if (typeof e == "number")
578
- return e;
579
- if (typeof e == "string") {
580
- if (e === h)
581
- return NaN;
582
- if (e === C)
583
- return 1 / 0;
584
- if (e === $)
585
- return -1 / 0;
586
- const n = Number(e);
587
- if (!Number.isNaN(n))
588
- return n;
589
- }
590
- if (r.mode === "strict")
591
- throw new TypeError(
592
- `[number-special] unrecognized wire value at "${r.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...X].join(", ")}.`
593
- );
594
- return NaN;
595
- }
596
- }, Z = [
597
- k,
598
- R,
599
- P,
600
- B,
601
- w,
602
- M
7
+ function n(e) {
8
+ e?.wellKnown;
9
+ let n = /* @__PURE__ */ new Map();
10
+ return {
11
+ register(e, r) {
12
+ if (n.has(e.id) && !r?.override) throw new t(`E_DUP_CODEC: a codec is already registered for id "${e.id}"`);
13
+ n.set(e.id, e);
14
+ },
15
+ resolve(e) {
16
+ for (let t of n.values()) if (t.matches(e)) return t;
17
+ },
18
+ get(e) {
19
+ return n.get(e);
20
+ },
21
+ ids() {
22
+ return [...n.keys()];
23
+ },
24
+ freeze() {
25
+ let e = new Map(n), r = {
26
+ register() {
27
+ throw new t("E_DUP_CODEC: registry is frozen and cannot accept new codecs");
28
+ },
29
+ resolve(t) {
30
+ for (let n of e.values()) if (n.matches(t)) return n;
31
+ },
32
+ get(t) {
33
+ return e.get(t);
34
+ },
35
+ ids() {
36
+ return [...e.keys()];
37
+ },
38
+ freeze() {
39
+ return r;
40
+ }
41
+ };
42
+ return r;
43
+ }
44
+ };
45
+ }
46
+ //#endregion
47
+ //#region src/lib/descriptor-ext.ts
48
+ var r = "x-apigen-logical", i = "x-apigen-codec", a = "x-apigen-ctor", o = "x-apigen-tojson", s = "0.1.0", c = [
49
+ "scalar",
50
+ "nominal",
51
+ "union",
52
+ "map",
53
+ "set"
603
54
  ];
604
- function be(e, t = {}) {
605
- for (const r of Z)
606
- e.register(r, { override: t.override ?? !1 });
607
- }
608
- const H = 12;
609
- function Q(e, t) {
610
- const r = /^#\/(definitions|\$defs)\/(.+)$/.exec(e);
611
- if (!r)
612
- return;
613
- const [, n, o] = r, i = n === "$defs" ? t.$defs : t.definitions;
614
- return i == null ? void 0 : i[decodeURIComponent(o)];
615
- }
616
- function T(e) {
617
- return Array.isArray(e) && e.length > 0 ? e[0] : void 0;
55
+ function l(e) {
56
+ let t = e[r];
57
+ return typeof t == "string" && c.includes(t) ? t : void 0;
58
+ }
59
+ function u(e) {
60
+ let t = e[i];
61
+ return typeof t == "string" ? t : void 0;
62
+ }
63
+ //#endregion
64
+ //#region src/lib/runmode.ts
65
+ function d(e, t) {
66
+ return {
67
+ registry: e,
68
+ resolve: (e) => {
69
+ throw Error(`[apigen-logical] $ref "${e}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`);
70
+ },
71
+ seen: /* @__PURE__ */ new WeakSet(),
72
+ path: "",
73
+ mode: "strict",
74
+ ...t
75
+ };
76
+ }
77
+ function f(e, t, n) {
78
+ let r = n.registry.resolve(t);
79
+ if (r) return r.encode(e, t, n);
80
+ let i = t.$ref;
81
+ if (typeof i == "string") return f(e, n.resolve(i), n);
82
+ let a = t.oneOf;
83
+ if (Array.isArray(a)) return f(e, m(e, a, t, n), n);
84
+ let o = t.type;
85
+ if (o === "array") {
86
+ if (!Array.isArray(e)) return b(e, t, n);
87
+ let r = t.items;
88
+ if (!r) return e.map((e) => C(e));
89
+ let i = n.path;
90
+ return Array.isArray(r) ? e.map((e, t) => {
91
+ let a = r[t];
92
+ return a === void 0 ? C(e) : f(e, a, {
93
+ ...n,
94
+ path: `${i}/${t}`
95
+ });
96
+ }) : e.map((e, t) => f(e, r, {
97
+ ...n,
98
+ path: `${i}/${t}`
99
+ }));
100
+ }
101
+ if (o === "object") {
102
+ if (typeof e != "object" || !e || Array.isArray(e)) return b(e, t, n);
103
+ let r = t.properties;
104
+ if (!r) return C(e);
105
+ let i = n.path, a = {};
106
+ for (let [t, o] of Object.entries(r)) {
107
+ let r = e[t];
108
+ r !== void 0 && (a[t] = f(r, o, {
109
+ ...n,
110
+ path: `${i}/${t}`
111
+ }));
112
+ }
113
+ return a;
114
+ }
115
+ return o == null ? b(e, t, n) : C(e);
116
+ }
117
+ function p(e, t, n) {
118
+ let r = n.registry.resolve(t);
119
+ if (r) return r.decode(e, t, n);
120
+ let i = t.$ref;
121
+ if (typeof i == "string") return p(e, n.resolve(i), n);
122
+ let a = t.oneOf;
123
+ if (Array.isArray(a)) return p(e, m(e, a, t, n), n);
124
+ let o = t.type;
125
+ if (o === "array") {
126
+ if (!Array.isArray(e)) return e;
127
+ let r = t.items;
128
+ if (!r) return e;
129
+ let i = n.path;
130
+ return Array.isArray(r) ? e.map((e, t) => {
131
+ let a = r[t];
132
+ return a === void 0 ? e : p(e, a, {
133
+ ...n,
134
+ path: `${i}/${t}`
135
+ });
136
+ }) : e.map((e, t) => p(e, r, {
137
+ ...n,
138
+ path: `${i}/${t}`
139
+ }));
140
+ }
141
+ if (o === "object") {
142
+ if (typeof e != "object" || !e || Array.isArray(e)) return e;
143
+ let r = t.properties;
144
+ if (!r) return e;
145
+ let i = n.path, a = {};
146
+ for (let [t, o] of Object.entries(r)) {
147
+ let r = e[t];
148
+ r !== void 0 && (a[t] = p(r, o, {
149
+ ...n,
150
+ path: `${i}/${t}`
151
+ }));
152
+ }
153
+ for (let [t, n] of Object.entries(e)) !(t in r) && n !== void 0 && (a[t] = n);
154
+ return a;
155
+ }
156
+ return o == null ? S(e, n) : e;
157
+ }
158
+ function m(e, t, n, r) {
159
+ let i = n.discriminator;
160
+ if (i?.propertyName) {
161
+ let n = i.propertyName, a = typeof e == "object" && e ? e[n] : void 0;
162
+ if (typeof a == "string") {
163
+ if (i.mapping) {
164
+ let e = i.mapping[a];
165
+ if (e) {
166
+ let n = t.find((t) => t.$ref === e);
167
+ if (n) return n;
168
+ }
169
+ }
170
+ for (let e of t) {
171
+ let t = h(e, r).properties?.[n];
172
+ if (!t) continue;
173
+ let i = t.enum;
174
+ if (t.const === a || Array.isArray(i) && i.includes(a)) return e;
175
+ }
176
+ }
177
+ }
178
+ let a = _(e, t, r);
179
+ if (a) return a;
180
+ let o, s = -1;
181
+ for (let n of t) {
182
+ let t = y(e, h(n, r), r);
183
+ t !== null && t > s && (s = t, o = n);
184
+ }
185
+ return o === void 0 ? t[0] ?? {} : o;
186
+ }
187
+ function h(e, t) {
188
+ let n = e.$ref;
189
+ return typeof n == "string" ? t.resolve(n) : e;
190
+ }
191
+ function g(e) {
192
+ if ("const" in e) return e.const;
193
+ let t = e.enum;
194
+ if (Array.isArray(t) && t.length === 1) return t[0];
195
+ }
196
+ function _(e, t, n) {
197
+ if (typeof e != "object" || !e || Array.isArray(e)) return;
198
+ let r = e, i = t.map((e) => h(e, n)), a = /* @__PURE__ */ new Set();
199
+ for (let e of i) {
200
+ let t = e.properties;
201
+ if (t) for (let [e, n] of Object.entries(t)) g(n) !== void 0 && a.add(e);
202
+ }
203
+ for (let o of a) {
204
+ let a = i.map((e) => {
205
+ let t = e.properties?.[o];
206
+ return t ? g(t) : void 0;
207
+ }), s = a.filter((e) => e !== void 0), c = new Set(s);
208
+ if (s.length < 2 || c.size !== s.length) continue;
209
+ let l = r[o];
210
+ if (l === void 0) continue;
211
+ let u = a.findIndex((e) => e === l);
212
+ if (u !== -1 && y(e, i[u], n) !== null) return t[u];
213
+ }
214
+ }
215
+ function v(e) {
216
+ if (e === null) return "null";
217
+ if (Array.isArray(e)) return "array";
218
+ let t = typeof e;
219
+ return t === "number" ? Number.isInteger(e) ? "integer" : "number" : t;
220
+ }
221
+ function y(e, t, n) {
222
+ let r = t.oneOf;
223
+ if (Array.isArray(r)) {
224
+ let t = null;
225
+ for (let i of r) {
226
+ let r = y(e, h(i, n), n);
227
+ r !== null && (t === null || r > t) && (t = r);
228
+ }
229
+ return t;
230
+ }
231
+ let i = t.type, a = v(e);
232
+ if (typeof i == "string") {
233
+ if (!(i === a || i === "number" && a === "integer")) return null;
234
+ } else if (Array.isArray(i) && !i.some((e) => e === a || e === "number" && a === "integer")) return null;
235
+ if (typeof e != "object" || !e || Array.isArray(e)) return +(typeof i == "string");
236
+ let o = e, s = t.required, c = 0;
237
+ if (Array.isArray(s)) {
238
+ for (let e of s) if (typeof e == "string") {
239
+ if (o[e] === void 0) return null;
240
+ c += 2;
241
+ }
242
+ }
243
+ let l = t.properties;
244
+ if (l) {
245
+ for (let e of Object.keys(l)) o[e] !== void 0 && (c += 1);
246
+ for (let e of Object.keys(o)) l[e] === void 0 && --c;
247
+ }
248
+ return c;
249
+ }
250
+ function b(t, n, r) {
251
+ for (let n of r.registry.ids()) {
252
+ let i = r.registry.get(n);
253
+ if (i?.ownsValue?.(t)) return {
254
+ [e]: n,
255
+ v: i.encode(t, i.schema, r)
256
+ };
257
+ }
258
+ if (Array.isArray(t)) return t.map((e) => b(e, {}, r));
259
+ if (x(t)) {
260
+ let e = {};
261
+ for (let [n, i] of Object.entries(t)) i !== void 0 && (e[n] = b(i, {}, r));
262
+ return e;
263
+ }
264
+ return C(t);
618
265
  }
619
266
  function x(e) {
620
- if (Array.isArray(e.type))
621
- return e.type.find((t) => t !== "null") ?? e.type[0];
622
- if (typeof e.type == "string")
623
- return e.type;
624
- if (e.properties)
625
- return "object";
626
- }
627
- function ee(e) {
628
- switch (e.format) {
629
- case "date-time":
630
- return "1970-01-01T00:00:00.000Z";
631
- case "date":
632
- return "1970-01-01";
633
- case "time":
634
- return "00:00:00Z";
635
- case "uuid":
636
- return "00000000-0000-0000-0000-000000000000";
637
- case "email":
638
- return "user@example.com";
639
- case "uri":
640
- case "url":
641
- case "uri-reference":
642
- return "https://example.com";
643
- case "hostname":
644
- return "example.com";
645
- case "ipv4":
646
- return "127.0.0.1";
647
- case "ipv6":
648
- return "::1";
649
- case "decimal":
650
- return "0";
651
- case "int64":
652
- return "0";
653
- case "byte":
654
- return "";
655
- default:
656
- return "<string>";
657
- }
658
- }
659
- function F(e, t, r) {
660
- const n = e.properties ?? {}, o = e.required ?? [], i = {};
661
- for (const a of o) {
662
- const c = n[a];
663
- i[a] = c !== void 0 ? E(c, t, r + 1) : `<${a}>`;
664
- }
665
- return i;
666
- }
667
- function te(e, t, r) {
668
- const n = Array.isArray(e.items) ? e.items[0] : e.items;
669
- return n ? [E(n, t, r + 1)] : [];
267
+ if (typeof e != "object" || !e || Array.isArray(e)) return !1;
268
+ let t = Object.getPrototypeOf(e);
269
+ return t === Object.prototype || t === null;
270
+ }
271
+ function S(t, n) {
272
+ if (typeof t == "object" && t && !Array.isArray(t) && "$apigen" in t) {
273
+ let r = t, i = r[e], a = n.registry.get(i);
274
+ if (a) return a.decode(r.v, a.schema, n);
275
+ }
276
+ if (Array.isArray(t)) return t.map((e) => S(e, n));
277
+ if (x(t)) {
278
+ let e = {};
279
+ for (let [r, i] of Object.entries(t)) e[r] = S(i, n);
280
+ return e;
281
+ }
282
+ return t;
283
+ }
284
+ function C(e) {
285
+ if (e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean") return e;
286
+ if (Array.isArray(e)) return e.map(C);
287
+ if (typeof e == "object") {
288
+ let t = {};
289
+ for (let [n, r] of Object.entries(e)) r !== void 0 && (t[n] = C(r));
290
+ return t;
291
+ }
292
+ return null;
293
+ }
294
+ function w(e, t) {
295
+ let n = /* @__PURE__ */ new Set(), r = [e];
296
+ for (; r.length > 0;) {
297
+ let e = r.pop();
298
+ if (!e || n.has(e)) continue;
299
+ n.add(e);
300
+ let i = e.$ref;
301
+ if (typeof i == "string") {
302
+ if (t) {
303
+ let e = t[i];
304
+ if (!e) {
305
+ let e = Object.keys(t).join(", ") || "(none)";
306
+ throw Error(`[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${e}`);
307
+ }
308
+ r.push(e);
309
+ }
310
+ continue;
311
+ }
312
+ let a = e.oneOf;
313
+ if (Array.isArray(a)) for (let e of a) typeof e == "object" && e && r.push(e);
314
+ let o = e.properties;
315
+ if (o) for (let e of Object.values(o)) r.push(e);
316
+ let s = e.items;
317
+ if (Array.isArray(s)) for (let e of s) typeof e == "object" && e && r.push(e);
318
+ else typeof s == "object" && s && r.push(s);
319
+ let c = e.additionalProperties;
320
+ typeof c == "object" && c && r.push(c);
321
+ let l = e.propertyNames;
322
+ typeof l == "object" && l && r.push(l);
323
+ }
670
324
  }
671
- function E(e, t = e ?? {}, r = 0) {
672
- if (!e || r > H)
673
- return null;
674
- if (typeof e.$ref == "string") {
675
- const i = Q(e.$ref, t);
676
- return i !== void 0 ? E(i, t, r + 1) : null;
677
- }
678
- if (e.const !== void 0)
679
- return e.const;
680
- if (Array.isArray(e.enum) && e.enum.length > 0)
681
- return e.enum[0];
682
- if (Array.isArray(e.allOf) && e.allOf.length > 0) {
683
- const i = {}, a = [];
684
- for (const c of e.allOf) {
685
- Object.assign(i, c.properties ?? {});
686
- for (const u of c.required ?? [])
687
- a.includes(u) || a.push(u);
688
- }
689
- return F(
690
- { ...e, properties: i, required: a },
691
- t,
692
- r
693
- );
694
- }
695
- const n = T(e.oneOf) ?? T(e.anyOf);
696
- if (n)
697
- return E(n, t, r + 1);
698
- switch (x(e)) {
699
- case "object":
700
- return F(e, t, r);
701
- case "array":
702
- return te(e, t, r);
703
- case "string":
704
- return ee(e);
705
- case "integer":
706
- case "number":
707
- return 0;
708
- case "boolean":
709
- return !1;
710
- case "null":
711
- return null;
712
- default:
713
- return null;
714
- }
715
- }
716
- function Ee(e) {
717
- if (!e)
718
- return;
719
- const t = E(e);
720
- return `Example: ${JSON.stringify(t)}`;
325
+ function T(e) {
326
+ return {
327
+ encode(t, n, r) {
328
+ return f(t, n, d(e, r));
329
+ },
330
+ decode(t, n, r) {
331
+ return p(t, n, d(e, r));
332
+ }
333
+ };
334
+ }
335
+ function E(e, t, n) {
336
+ try {
337
+ let t = n();
338
+ e.register(t);
339
+ } catch (e) {
340
+ if (D(e)) return;
341
+ throw e;
342
+ }
343
+ }
344
+ function D(e) {
345
+ return typeof e != "object" || !e ? !1 : e.code === "MODULE_NOT_FOUND";
346
+ }
347
+ //#endregion
348
+ //#region src/lib/codecs/date-time.ts
349
+ var O = {
350
+ id: "date-time",
351
+ kind: "scalar",
352
+ schema: {
353
+ type: "string",
354
+ format: "date-time"
355
+ },
356
+ matches(e) {
357
+ return e.type === "string" && e.format === "date-time";
358
+ },
359
+ ownsValue(e) {
360
+ return e instanceof Date;
361
+ },
362
+ encode(e, t, n) {
363
+ return e.toISOString();
364
+ },
365
+ decode(e, t, n) {
366
+ if (typeof e != "string") {
367
+ if (n.mode === "strict") throw TypeError(`[date-time] expected a string on the wire at "${n.path}", got ${typeof e}`);
368
+ return new Date(String(e));
369
+ }
370
+ if (n.mode === "strict" && Number.isNaN(new Date(e).getTime())) throw TypeError(`[date-time] invalid date-time string at "${n.path}": ${JSON.stringify(e)}`);
371
+ return new Date(e);
372
+ }
373
+ }, k = {
374
+ id: "int64",
375
+ kind: "scalar",
376
+ schema: {
377
+ type: "string",
378
+ format: "int64"
379
+ },
380
+ matches(e) {
381
+ return e.type === "string" && e.format === "int64";
382
+ },
383
+ ownsValue(e) {
384
+ return typeof e == "bigint";
385
+ },
386
+ encode(e, t, n) {
387
+ return String(e);
388
+ },
389
+ decode(e, t, n) {
390
+ if (typeof e != "string") {
391
+ if (n.mode === "strict") throw TypeError(`[int64] expected a string on the wire at "${n.path}", got ${typeof e}`);
392
+ try {
393
+ return BigInt(Math.trunc(Number(e)));
394
+ } catch {
395
+ return BigInt(0);
396
+ }
397
+ }
398
+ if (n.mode === "strict") return BigInt(e);
399
+ if (/^-?\d+$/.test(e)) try {
400
+ return BigInt(e);
401
+ } catch {
402
+ return BigInt(0);
403
+ }
404
+ return BigInt(0);
405
+ }
406
+ };
407
+ //#endregion
408
+ //#region src/lib/codecs/decimal.ts
409
+ function A(e) {
410
+ return e;
411
+ }
412
+ var j = {
413
+ id: "decimal",
414
+ kind: "scalar",
415
+ schema: {
416
+ type: "string",
417
+ format: "decimal"
418
+ },
419
+ matches(e) {
420
+ return e.type === "string" && e.format === "decimal";
421
+ },
422
+ encode(e, t, n) {
423
+ return e;
424
+ },
425
+ decode(e, t, n) {
426
+ if (typeof e != "string") {
427
+ if (n.mode === "strict") throw TypeError(`[decimal] expected a string on the wire at "${n.path}", got ${typeof e}`);
428
+ return A(String(e));
429
+ }
430
+ if (n.mode === "strict" && !/^-?\d+(\.\d+)?$/.test(e)) throw TypeError(`[decimal] wire value "${e}" at "${n.path}" is not a valid decimal string`);
431
+ return A(e);
432
+ }
433
+ }, M = {
434
+ id: "byte",
435
+ kind: "scalar",
436
+ schema: {
437
+ type: "string",
438
+ format: "byte"
439
+ },
440
+ matches(e) {
441
+ return e.type === "string" && e.format === "byte";
442
+ },
443
+ ownsValue(e) {
444
+ return e instanceof Uint8Array;
445
+ },
446
+ encode(e, t, n) {
447
+ return Buffer.from(e).toString("base64");
448
+ },
449
+ decode(e, t, n) {
450
+ if (typeof e != "string") {
451
+ if (n.mode === "strict") throw TypeError(`[byte] expected a base64 string on the wire at "${n.path}", got ${typeof e}`);
452
+ return /* @__PURE__ */ new Uint8Array();
453
+ }
454
+ if (n.mode === "strict" && !/^[A-Za-z0-9+/]*={0,2}$/.test(e)) throw TypeError(`[byte] wire value at "${n.path}" is not standard base64 (format:byte): "${e}"`);
455
+ return new Uint8Array(Buffer.from(e, "base64"));
456
+ }
457
+ }, N = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, P = {
458
+ id: "uuid",
459
+ kind: "scalar",
460
+ schema: {
461
+ type: "string",
462
+ format: "uuid"
463
+ },
464
+ matches(e) {
465
+ return e.type === "string" && e.format === "uuid";
466
+ },
467
+ encode(e, t, n) {
468
+ return e.toLowerCase();
469
+ },
470
+ decode(e, t, n) {
471
+ if (typeof e != "string") {
472
+ if (n.mode === "strict") throw TypeError(`[uuid] expected a string on the wire at "${n.path}", got ${typeof e}`);
473
+ return String(e).toLowerCase();
474
+ }
475
+ if (n.mode === "strict" && !N.test(e)) throw TypeError(`[uuid] wire value "${e}" at "${n.path}" is not a lowercase-hyphenated RFC 4122 UUID`);
476
+ return e;
477
+ }
478
+ }, F = "NaN", I = "Infinity", L = "-Infinity", R = /* @__PURE__ */ new Set([
479
+ F,
480
+ I,
481
+ L
482
+ ]), z = {
483
+ id: "number-special",
484
+ kind: "scalar",
485
+ schema: { type: "number" },
486
+ matches(e) {
487
+ return e.type === "number" && e.format === void 0;
488
+ },
489
+ ownsValue(e) {
490
+ return typeof e == "number" && !Number.isFinite(e);
491
+ },
492
+ encode(e, t, n) {
493
+ return Number.isNaN(e) ? F : e === Infinity ? I : e === -Infinity ? L : e;
494
+ },
495
+ decode(e, t, n) {
496
+ if (typeof e == "number") return e;
497
+ if (typeof e == "string") {
498
+ if (e === F) return NaN;
499
+ if (e === I) return Infinity;
500
+ if (e === L) return -Infinity;
501
+ let t = Number(e);
502
+ if (!Number.isNaN(t)) return t;
503
+ }
504
+ if (n.mode === "strict") throw TypeError(`[number-special] unrecognized wire value at "${n.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...R].join(", ")}.`);
505
+ return NaN;
506
+ }
507
+ }, B = [
508
+ O,
509
+ k,
510
+ j,
511
+ M,
512
+ P,
513
+ z
514
+ ];
515
+ function V(e, t = {}) {
516
+ for (let n of B) e.register(n, { override: t.override ?? !1 });
517
+ }
518
+ //#endregion
519
+ //#region src/lib/example.ts
520
+ var H = 12;
521
+ function U(e, t) {
522
+ let n = /^#\/(definitions|\$defs)\/(.+)$/.exec(e);
523
+ if (!n) return;
524
+ let [, r, i] = n;
525
+ return (r === "$defs" ? t.$defs : t.definitions)?.[decodeURIComponent(i)];
721
526
  }
722
- const G = [
723
- k.id,
724
- // 'date-time'
725
- R.id,
726
- // 'int64'
727
- P.id,
728
- // 'decimal'
729
- B.id,
730
- // 'byte'
731
- w.id,
732
- // 'uuid'
733
- M.id
734
- // 'number-special'
735
- ], re = {
736
- "date-time": {
737
- encode: "$.toISOString()",
738
- decode: "new Date($)",
739
- mode: "native"
740
- },
741
- int64: {
742
- encode: "String($)",
743
- decode: "BigInt($)",
744
- mode: "native"
745
- },
746
- decimal: {
747
- // Default: branded string (zero-dep); opt-in to decimal.js for arithmetic.
748
- // DESIGN §13.2 branded row / §18 resolved: mode:'branded', dep declared so
749
- // generators can optionally inject it; consumers that never use Decimal
750
- // never install it (§14.2).
751
- encode: "$.toString()",
752
- decode: "new Decimal($)",
753
- imports: [],
754
- dep: { name: "decimal.js", version: "^10" },
755
- mode: "branded"
756
- },
757
- byte: {
758
- encode: "Buffer.from($).toString('base64')",
759
- decode: "new Uint8Array(Buffer.from($, 'base64'))",
760
- mode: "native"
761
- },
762
- uuid: {
763
- // UUID is a plain string in TS; encode normalises to lowercase.
764
- encode: "$.toLowerCase()",
765
- decode: "$",
766
- mode: "native"
767
- },
768
- "number-special": {
769
- // numToWire / wireToNum are stdlib helpers emitted inline by the engine.
770
- encode: "numToWire($)",
771
- decode: "wireToNum($)",
772
- mode: "native"
773
- }
774
- }, ne = {
775
- "date-time": {
776
- encode: "$.isoformat()",
777
- decode: "datetime.fromisoformat($)",
778
- imports: ["from datetime import datetime"],
779
- mode: "native"
780
- },
781
- int64: {
782
- encode: "str($)",
783
- decode: "int($)",
784
- mode: "native"
785
- },
786
- decimal: {
787
- encode: "str($)",
788
- decode: "Decimal($)",
789
- imports: ["from decimal import Decimal"],
790
- mode: "native"
791
- },
792
- byte: {
793
- encode: "b64encode($).decode()",
794
- decode: "b64decode($)",
795
- imports: ["from base64 import b64encode, b64decode"],
796
- mode: "native"
797
- },
798
- uuid: {
799
- encode: "str($)",
800
- decode: "UUID($)",
801
- imports: ["from uuid import UUID"],
802
- mode: "native"
803
- },
804
- "number-special": {
805
- // Python json.dumps maps float('nan')/float('inf') to null by default;
806
- // the codec overrides with string sentinels (same as TS).
807
- encode: "num_to_wire($)",
808
- decode: "wire_to_num($)",
809
- mode: "native"
810
- }
811
- }, oe = {
812
- "date-time": {
813
- // chrono::DateTime<Utc> serialises as RFC3339 via serde.
814
- encode: "__SCAFFOLD_RUST_DATETIME_ENCODE__",
815
- decode: "__SCAFFOLD_RUST_DATETIME_DECODE__",
816
- imports: ["use chrono::{DateTime, Utc};"],
817
- dep: { name: "chrono", version: "^0.4" },
818
- mode: "lib"
819
- },
820
- int64: {
821
- // serde_with DisplayFromStr for i64/u64.
822
- encode: "__SCAFFOLD_RUST_INT64_ENCODE__",
823
- decode: "__SCAFFOLD_RUST_INT64_DECODE__",
824
- imports: ["use serde_with::DisplayFromStr;"],
825
- dep: { name: "serde_with", version: "^3" },
826
- mode: "lib"
827
- },
828
- decimal: {
829
- encode: "__SCAFFOLD_RUST_DECIMAL_ENCODE__",
830
- decode: "__SCAFFOLD_RUST_DECIMAL_DECODE__",
831
- imports: ["use rust_decimal::Decimal;"],
832
- dep: { name: "rust_decimal", version: "^1" },
833
- mode: "lib"
834
- },
835
- byte: {
836
- // serde_with Base64 attribute.
837
- encode: "__SCAFFOLD_RUST_BYTE_ENCODE__",
838
- decode: "__SCAFFOLD_RUST_BYTE_DECODE__",
839
- imports: ["use serde_with::base64::Base64;"],
840
- dep: { name: "serde_with", version: "^3" },
841
- mode: "lib"
842
- },
843
- uuid: {
844
- encode: "__SCAFFOLD_RUST_UUID_ENCODE__",
845
- decode: "__SCAFFOLD_RUST_UUID_DECODE__",
846
- imports: ["use uuid::Uuid;"],
847
- dep: { name: "uuid", version: "^1" },
848
- mode: "lib"
849
- },
850
- "number-special": {
851
- encode: "__SCAFFOLD_RUST_NUMSPECIAL_ENCODE__",
852
- decode: "__SCAFFOLD_RUST_NUMSPECIAL_DECODE__",
853
- mode: "native"
854
- }
855
- }, ie = {
856
- "date-time": {
857
- // time.Time serialises as RFC3339Nano via MarshalJSON.
858
- encode: "__SCAFFOLD_GO_DATETIME_ENCODE__",
859
- decode: "__SCAFFOLD_GO_DATETIME_DECODE__",
860
- imports: ['"time"'],
861
- mode: "native"
862
- },
863
- int64: {
864
- // struct tag `json:"x,string"` or math/big for arbitrary precision.
865
- encode: "__SCAFFOLD_GO_INT64_ENCODE__",
866
- decode: "__SCAFFOLD_GO_INT64_DECODE__",
867
- imports: ['"strconv"'],
868
- mode: "native"
869
- },
870
- decimal: {
871
- encode: "__SCAFFOLD_GO_DECIMAL_ENCODE__",
872
- decode: "__SCAFFOLD_GO_DECIMAL_DECODE__",
873
- imports: ['"github.com/shopspring/decimal"'],
874
- dep: { name: "github.com/shopspring/decimal", version: "v1" },
875
- mode: "lib"
876
- },
877
- byte: {
878
- // encoding/base64 — stdlib.
879
- encode: "__SCAFFOLD_GO_BYTE_ENCODE__",
880
- decode: "__SCAFFOLD_GO_BYTE_DECODE__",
881
- imports: ['"encoding/base64"'],
882
- mode: "native"
883
- },
884
- uuid: {
885
- encode: "__SCAFFOLD_GO_UUID_ENCODE__",
886
- decode: "__SCAFFOLD_GO_UUID_DECODE__",
887
- imports: ['"github.com/google/uuid"'],
888
- dep: { name: "github.com/google/uuid", version: "v1" },
889
- mode: "lib"
890
- },
891
- "number-special": {
892
- encode: "__SCAFFOLD_GO_NUMSPECIAL_ENCODE__",
893
- decode: "__SCAFFOLD_GO_NUMSPECIAL_DECODE__",
894
- mode: "native"
895
- }
896
- }, se = {
897
- "date-time": {
898
- // Instant.toString() (RFC-3339/ISO-8601, UTC) / Instant.parse($) — the
899
- // jackson-datatype-jsr310 module is registered on the shared ObjectMapper
900
- // so java.time.Instant fields round-trip automatically, but the
901
- // dispatcher-woven glue (which builds/reads JsonNode by hand rather than
902
- // going through a field-annotated POJO) uses these expressions directly.
903
- encode: "$.toString()",
904
- decode: "Instant.parse($)",
905
- imports: ["import java.time.Instant;"],
906
- dep: { name: "jackson-datatype-jsr310", version: "2.x" },
907
- mode: "lib"
908
- },
909
- int64: {
910
- // Declarative, not expression-shaped: Jackson field annotation forces
911
- // Long/long to serialise as a JSON string (avoids precision loss in
912
- // JS/JSON-number consumers). mode:'native' — the "encode"/"decode"
913
- // values below ARE the annotation text (identical on both sides; Jackson
914
- // applies it symmetrically), not a `$`-substituted call expression.
915
- encode: "@JsonFormat(shape = JsonFormat.Shape.STRING)",
916
- decode: "@JsonFormat(shape = JsonFormat.Shape.STRING)",
917
- imports: ["import com.fasterxml.jackson.annotation.JsonFormat;"],
918
- mode: "native"
919
- },
920
- decimal: {
921
- // Encode is declarative (field annotation forces BigDecimal -> JSON
922
- // string via Jackson's stdlib ToStringSerializer, so scale/precision
923
- // survive the wire); decode is a plain expression since BigDecimal has
924
- // no matching stdlib deserializer-by-annotation for "parse from string".
925
- encode: "@JsonSerialize(using = ToStringSerializer.class)",
926
- decode: "new BigDecimal($)",
927
- imports: [
928
- "import java.math.BigDecimal;",
929
- "import com.fasterxml.jackson.databind.annotation.JsonSerialize;",
930
- "import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;"
931
- ],
932
- mode: "native"
933
- },
934
- byte: {
935
- // byte[] <-> base64 String is Jackson's built-in default codec — no
936
- // annotation, no wrapping expression. The dispatcher glue passes the
937
- // value through unchanged; Jackson's ObjectMapper performs the base64
938
- // transcoding when the JsonNode tree is built/read.
939
- encode: "$",
940
- decode: "$",
941
- mode: "native"
942
- },
943
- uuid: {
944
- // UUID.toString() / UUID.fromString($) — stdlib Java, no Jackson module.
945
- encode: "$.toString()",
946
- decode: "UUID.fromString($)",
947
- imports: ["import java.util.UUID;"],
948
- mode: "native"
949
- },
950
- "number-special": {
951
- // NaN/Infinity/-Infinity have no native JSON representation; mirrors the
952
- // TS numToWire/wireToNum inline helpers with a small custom Jackson
953
- // StdSerializer/StdDeserializer pair, generated inline by
954
- // renderDispatcherJava (class names below are the fixed contract the
955
- // generator emits — apigen-plugin-java-javalin/src/lib/dispatcher-template.ts).
956
- encode: "@JsonSerialize(using = NumberSpecialSerializer.class)",
957
- decode: "@JsonDeserialize(using = NumberSpecialDeserializer.class)",
958
- imports: [
959
- "import com.fasterxml.jackson.databind.annotation.JsonSerialize;",
960
- "import com.fasterxml.jackson.databind.annotation.JsonDeserialize;"
961
- ],
962
- mode: "native"
963
- }
964
- }, A = Object.freeze({
965
- typescript: re,
966
- python: ne,
967
- rust: oe,
968
- go: ie,
969
- java: se
527
+ function W(e) {
528
+ return Array.isArray(e) && e.length > 0 ? e[0] : void 0;
529
+ }
530
+ function G(e) {
531
+ if (Array.isArray(e.type)) return e.type.find((e) => e !== "null") ?? e.type[0];
532
+ if (typeof e.type == "string") return e.type;
533
+ if (e.properties) return "object";
534
+ }
535
+ function K(e) {
536
+ switch (e.format) {
537
+ case "date-time": return "1970-01-01T00:00:00.000Z";
538
+ case "date": return "1970-01-01";
539
+ case "time": return "00:00:00Z";
540
+ case "uuid": return "00000000-0000-0000-0000-000000000000";
541
+ case "email": return "user@example.com";
542
+ case "uri":
543
+ case "url":
544
+ case "uri-reference": return "https://example.com";
545
+ case "hostname": return "example.com";
546
+ case "ipv4": return "127.0.0.1";
547
+ case "ipv6": return "::1";
548
+ case "decimal": return "0";
549
+ case "int64": return "0";
550
+ case "byte": return "";
551
+ default: return "<string>";
552
+ }
553
+ }
554
+ function q(e, t, n) {
555
+ let r = e.properties ?? {}, i = e.required ?? [], a = {};
556
+ for (let e of i) {
557
+ let i = r[e];
558
+ a[e] = i === void 0 ? `<${e}>` : Y(i, t, n + 1);
559
+ }
560
+ return a;
561
+ }
562
+ function J(e, t, n) {
563
+ let r = Array.isArray(e.items) ? e.items[0] : e.items;
564
+ return r ? [Y(r, t, n + 1)] : [];
565
+ }
566
+ function Y(e, t = e ?? {}, n = 0) {
567
+ if (!e || n > H) return null;
568
+ if (typeof e.$ref == "string") {
569
+ let r = U(e.$ref, t);
570
+ return r === void 0 ? null : Y(r, t, n + 1);
571
+ }
572
+ if (e.const !== void 0) return e.const;
573
+ if (Array.isArray(e.enum) && e.enum.length > 0) return e.enum[0];
574
+ if (Array.isArray(e.allOf) && e.allOf.length > 0) {
575
+ let r = {}, i = [];
576
+ for (let t of e.allOf) {
577
+ Object.assign(r, t.properties ?? {});
578
+ for (let e of t.required ?? []) i.includes(e) || i.push(e);
579
+ }
580
+ return q({
581
+ ...e,
582
+ properties: r,
583
+ required: i
584
+ }, t, n);
585
+ }
586
+ let r = W(e.oneOf) ?? W(e.anyOf);
587
+ if (r) return Y(r, t, n + 1);
588
+ switch (G(e)) {
589
+ case "object": return q(e, t, n);
590
+ case "array": return J(e, t, n);
591
+ case "string": return K(e);
592
+ case "integer":
593
+ case "number": return 0;
594
+ case "boolean": return !1;
595
+ case "null": return null;
596
+ default: return null;
597
+ }
598
+ }
599
+ function X(e) {
600
+ if (!e) return;
601
+ let t = Y(e);
602
+ return `Example: ${JSON.stringify(t)}`;
603
+ }
604
+ //#endregion
605
+ //#region src/lib/hints.ts
606
+ var Z = [
607
+ O.id,
608
+ k.id,
609
+ j.id,
610
+ M.id,
611
+ P.id,
612
+ z.id
613
+ ], Q = Object.freeze({
614
+ typescript: {
615
+ "date-time": {
616
+ encode: "$.toISOString()",
617
+ decode: "new Date($)",
618
+ mode: "native"
619
+ },
620
+ int64: {
621
+ encode: "String($)",
622
+ decode: "BigInt($)",
623
+ mode: "native"
624
+ },
625
+ decimal: {
626
+ encode: "$.toString()",
627
+ decode: "new Decimal($)",
628
+ imports: [],
629
+ dep: {
630
+ name: "decimal.js",
631
+ version: "^10"
632
+ },
633
+ mode: "branded"
634
+ },
635
+ byte: {
636
+ encode: "Buffer.from($).toString('base64')",
637
+ decode: "new Uint8Array(Buffer.from($, 'base64'))",
638
+ mode: "native"
639
+ },
640
+ uuid: {
641
+ encode: "$.toLowerCase()",
642
+ decode: "$",
643
+ mode: "native"
644
+ },
645
+ "number-special": {
646
+ encode: "numToWire($)",
647
+ decode: "wireToNum($)",
648
+ mode: "native"
649
+ }
650
+ },
651
+ python: {
652
+ "date-time": {
653
+ encode: "$.isoformat()",
654
+ decode: "datetime.fromisoformat($)",
655
+ imports: ["from datetime import datetime"],
656
+ mode: "native"
657
+ },
658
+ int64: {
659
+ encode: "str($)",
660
+ decode: "int($)",
661
+ mode: "native"
662
+ },
663
+ decimal: {
664
+ encode: "str($)",
665
+ decode: "Decimal($)",
666
+ imports: ["from decimal import Decimal"],
667
+ mode: "native"
668
+ },
669
+ byte: {
670
+ encode: "b64encode($).decode()",
671
+ decode: "b64decode($)",
672
+ imports: ["from base64 import b64encode, b64decode"],
673
+ mode: "native"
674
+ },
675
+ uuid: {
676
+ encode: "str($)",
677
+ decode: "UUID($)",
678
+ imports: ["from uuid import UUID"],
679
+ mode: "native"
680
+ },
681
+ "number-special": {
682
+ encode: "num_to_wire($)",
683
+ decode: "wire_to_num($)",
684
+ mode: "native"
685
+ }
686
+ },
687
+ rust: {
688
+ "date-time": {
689
+ encode: "__SCAFFOLD_RUST_DATETIME_ENCODE__",
690
+ decode: "__SCAFFOLD_RUST_DATETIME_DECODE__",
691
+ imports: ["use chrono::{DateTime, Utc};"],
692
+ dep: {
693
+ name: "chrono",
694
+ version: "^0.4"
695
+ },
696
+ mode: "lib"
697
+ },
698
+ int64: {
699
+ encode: "__SCAFFOLD_RUST_INT64_ENCODE__",
700
+ decode: "__SCAFFOLD_RUST_INT64_DECODE__",
701
+ imports: ["use serde_with::DisplayFromStr;"],
702
+ dep: {
703
+ name: "serde_with",
704
+ version: "^3"
705
+ },
706
+ mode: "lib"
707
+ },
708
+ decimal: {
709
+ encode: "__SCAFFOLD_RUST_DECIMAL_ENCODE__",
710
+ decode: "__SCAFFOLD_RUST_DECIMAL_DECODE__",
711
+ imports: ["use rust_decimal::Decimal;"],
712
+ dep: {
713
+ name: "rust_decimal",
714
+ version: "^1"
715
+ },
716
+ mode: "lib"
717
+ },
718
+ byte: {
719
+ encode: "__SCAFFOLD_RUST_BYTE_ENCODE__",
720
+ decode: "__SCAFFOLD_RUST_BYTE_DECODE__",
721
+ imports: ["use serde_with::base64::Base64;"],
722
+ dep: {
723
+ name: "serde_with",
724
+ version: "^3"
725
+ },
726
+ mode: "lib"
727
+ },
728
+ uuid: {
729
+ encode: "__SCAFFOLD_RUST_UUID_ENCODE__",
730
+ decode: "__SCAFFOLD_RUST_UUID_DECODE__",
731
+ imports: ["use uuid::Uuid;"],
732
+ dep: {
733
+ name: "uuid",
734
+ version: "^1"
735
+ },
736
+ mode: "lib"
737
+ },
738
+ "number-special": {
739
+ encode: "__SCAFFOLD_RUST_NUMSPECIAL_ENCODE__",
740
+ decode: "__SCAFFOLD_RUST_NUMSPECIAL_DECODE__",
741
+ mode: "native"
742
+ }
743
+ },
744
+ go: {
745
+ "date-time": {
746
+ encode: "__SCAFFOLD_GO_DATETIME_ENCODE__",
747
+ decode: "__SCAFFOLD_GO_DATETIME_DECODE__",
748
+ imports: ["\"time\""],
749
+ mode: "native"
750
+ },
751
+ int64: {
752
+ encode: "__SCAFFOLD_GO_INT64_ENCODE__",
753
+ decode: "__SCAFFOLD_GO_INT64_DECODE__",
754
+ imports: ["\"strconv\""],
755
+ mode: "native"
756
+ },
757
+ decimal: {
758
+ encode: "__SCAFFOLD_GO_DECIMAL_ENCODE__",
759
+ decode: "__SCAFFOLD_GO_DECIMAL_DECODE__",
760
+ imports: ["\"github.com/shopspring/decimal\""],
761
+ dep: {
762
+ name: "github.com/shopspring/decimal",
763
+ version: "v1"
764
+ },
765
+ mode: "lib"
766
+ },
767
+ byte: {
768
+ encode: "__SCAFFOLD_GO_BYTE_ENCODE__",
769
+ decode: "__SCAFFOLD_GO_BYTE_DECODE__",
770
+ imports: ["\"encoding/base64\""],
771
+ mode: "native"
772
+ },
773
+ uuid: {
774
+ encode: "__SCAFFOLD_GO_UUID_ENCODE__",
775
+ decode: "__SCAFFOLD_GO_UUID_DECODE__",
776
+ imports: ["\"github.com/google/uuid\""],
777
+ dep: {
778
+ name: "github.com/google/uuid",
779
+ version: "v1"
780
+ },
781
+ mode: "lib"
782
+ },
783
+ "number-special": {
784
+ encode: "__SCAFFOLD_GO_NUMSPECIAL_ENCODE__",
785
+ decode: "__SCAFFOLD_GO_NUMSPECIAL_DECODE__",
786
+ mode: "native"
787
+ }
788
+ },
789
+ java: {
790
+ "date-time": {
791
+ encode: "$.toString()",
792
+ decode: "Instant.parse($)",
793
+ imports: ["import java.time.Instant;"],
794
+ dep: {
795
+ name: "jackson-datatype-jsr310",
796
+ version: "2.x"
797
+ },
798
+ mode: "lib"
799
+ },
800
+ int64: {
801
+ encode: "@JsonFormat(shape = JsonFormat.Shape.STRING)",
802
+ decode: "@JsonFormat(shape = JsonFormat.Shape.STRING)",
803
+ imports: ["import com.fasterxml.jackson.annotation.JsonFormat;"],
804
+ mode: "native"
805
+ },
806
+ decimal: {
807
+ encode: "@JsonSerialize(using = ToStringSerializer.class)",
808
+ decode: "new BigDecimal($)",
809
+ imports: [
810
+ "import java.math.BigDecimal;",
811
+ "import com.fasterxml.jackson.databind.annotation.JsonSerialize;",
812
+ "import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;"
813
+ ],
814
+ mode: "native"
815
+ },
816
+ byte: {
817
+ encode: "$",
818
+ decode: "$",
819
+ mode: "native"
820
+ },
821
+ uuid: {
822
+ encode: "$.toString()",
823
+ decode: "UUID.fromString($)",
824
+ imports: ["import java.util.UUID;"],
825
+ mode: "native"
826
+ },
827
+ "number-special": {
828
+ encode: "@JsonSerialize(using = NumberSpecialSerializer.class)",
829
+ decode: "@JsonDeserialize(using = NumberSpecialDeserializer.class)",
830
+ imports: ["import com.fasterxml.jackson.databind.annotation.JsonSerialize;", "import com.fasterxml.jackson.databind.annotation.JsonDeserialize;"],
831
+ mode: "native"
832
+ }
833
+ }
970
834
  });
971
- function Oe(e) {
972
- return A[e];
973
- }
974
- function Ae(e, t) {
975
- const r = A[t], n = [];
976
- for (const o of e) {
977
- const i = r[o];
978
- i != null && i.dep && n.push({ id: o, dep: i.dep });
979
- }
980
- return n;
981
- }
982
- function De() {
983
- const e = A.typescript, t = {};
984
- for (const r of G) {
985
- const n = e[r].dep;
986
- n && (t[r] = n);
987
- }
988
- return Object.freeze(t);
989
- }
990
- function Se(e, t) {
991
- const r = t ?? A[e], n = [];
992
- for (const o of G)
993
- r[o] || n.push(o);
994
- if (n.length > 0)
995
- throw new Error(
996
- `[hints] assertNoEmptyCells: language "${e}" is missing cells for: ${n.join(
997
- ", "
998
- )}`
999
- );
1000
- }
1001
- export {
1002
- G as CANONICAL_LOGICAL_TYPE_IDS,
1003
- N as CodecRegistryError,
1004
- D as ENVELOPE_KEY,
1005
- ue as LOGICAL_TYPE_VERSION,
1006
- A as TEMPLATE_CELLS,
1007
- J as X_APIGEN_CODEC,
1008
- ae as X_APIGEN_CTOR,
1009
- z as X_APIGEN_LOGICAL,
1010
- fe as X_APIGEN_TOJSON,
1011
- Se as assertNoEmptyCells,
1012
- le as buildTranscoder,
1013
- B as byteCodec,
1014
- Oe as cellsFor,
1015
- me as codecIdOf,
1016
- de as createRegistry,
1017
- k as dateTimeCodec,
1018
- P as decimalCodec,
1019
- Ae as depsForLogicalTypes,
1020
- R as int64Codec,
1021
- pe as logicalKindOf,
1022
- ge as makeDecimal,
1023
- M as numberSpecialCodec,
1024
- be as registerWellKnown,
1025
- Ee as renderExampleNote,
1026
- E as synthesizeExample,
1027
- ye as tryRegister,
1028
- De as tsDepMap,
1029
- w as uuidCodec,
1030
- _e as validateSchemaRefs
1031
- };
835
+ function $(e) {
836
+ return Q[e];
837
+ }
838
+ function ee(e, t) {
839
+ let n = Q[t], r = [];
840
+ for (let t of e) {
841
+ let e = n[t];
842
+ e?.dep && r.push({
843
+ id: t,
844
+ dep: e.dep
845
+ });
846
+ }
847
+ return r;
848
+ }
849
+ function te() {
850
+ let e = Q.typescript, t = {};
851
+ for (let n of Z) {
852
+ let r = e[n].dep;
853
+ r && (t[n] = r);
854
+ }
855
+ return Object.freeze(t);
856
+ }
857
+ function ne(e, t) {
858
+ let n = t ?? Q[e], r = [];
859
+ for (let e of Z) n[e] || r.push(e);
860
+ if (r.length > 0) throw Error(`[hints] assertNoEmptyCells: language "${e}" is missing cells for: ${r.join(", ")}`);
861
+ }
862
+ //#endregion
863
+ export { Z as CANONICAL_LOGICAL_TYPE_IDS, t as CodecRegistryError, e as ENVELOPE_KEY, s as LOGICAL_TYPE_VERSION, Q as TEMPLATE_CELLS, i as X_APIGEN_CODEC, a as X_APIGEN_CTOR, r as X_APIGEN_LOGICAL, o as X_APIGEN_TOJSON, ne as assertNoEmptyCells, T as buildTranscoder, M as byteCodec, $ as cellsFor, u as codecIdOf, n as createRegistry, O as dateTimeCodec, j as decimalCodec, ee as depsForLogicalTypes, k as int64Codec, l as logicalKindOf, A as makeDecimal, z as numberSpecialCodec, V as registerWellKnown, X as renderExampleNote, Y as synthesizeExample, E as tryRegister, te as tsDepMap, P as uuidCodec, w as validateSchemaRefs };