@adhd/apigen-base-logical 0.1.0 → 0.1.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.
- package/CHANGELOG.md +49 -0
- package/README.md +36 -0
- package/index.d.ts +2 -0
- package/index.js +1 -1
- package/index.mjs +590 -310
- package/lib/contracts.d.ts +19 -0
- package/lib/example.d.ts +35 -0
- package/lib/hints.d.ts +11 -7
- package/package.json +20 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
+
## 0.1.2 (2026-09-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### 🚀 Features
|
|
5
|
+
|
|
6
|
+
- **backlog:** INTERFACE_v2 consolidation — six-verb surface, web UI (search/stats/batch/edit), stats API (citationCount, closedAt, summary scope + window)
|
|
7
|
+
|
|
8
|
+
- **backlog:** 1.0.0 — one surface, one identity, no predecessor left behind
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### 🩹 Fixes
|
|
12
|
+
|
|
13
|
+
- **apigen-base-logical:** pick union branches structurally and stop codecs claiming foreign values
|
|
14
|
+
|
|
15
|
+
- **apigen-base-logical:** discriminated-union branch selection is inert after $ref inlining
|
|
16
|
+
|
|
17
|
+
- **apigen-cli:** batch nested-input CLI/HTTP wiring, sandbox isolation leaks, morph-walk index-signature regression
|
|
18
|
+
|
|
19
|
+
- **apigen:** audit fixes — S-18/S-19/C-20/C-21/S-20, java mvn race, union-encoder envelope, lazy heavy-dep loading
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### ❤️ Thank You
|
|
23
|
+
|
|
24
|
+
- parity-harness-self-test
|
|
25
|
+
- pseudosky
|
|
26
|
+
- Sky
|
|
27
|
+
|
|
28
|
+
## 0.1.1 (2026-08-07)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### 🚀 Features
|
|
32
|
+
|
|
33
|
+
- **apigen:** schema-driven worked examples for MCP tool descriptions + validation errors
|
|
34
|
+
|
|
35
|
+
- **apigen-base-logical:** fill JAVA_COLUMN codec expressions (FEAT-APIGEN-001 1/3)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### 🩹 Fixes
|
|
39
|
+
|
|
40
|
+
- **apigen-java:** mirror PYTHON_MATRIX_SCRIPT's decode+invariant-diff negative-control algorithm exactly (FEAT-APIGEN-001 review)
|
|
41
|
+
|
|
42
|
+
- **apigen-cli:** restore 2768 files mass-deleted by 0117eb22 (BUG-APIGEN-052)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
### ❤️ Thank You
|
|
46
|
+
|
|
47
|
+
- parity-harness-self-test
|
|
48
|
+
- pseudosky
|
|
49
|
+
|
|
1
50
|
## 0.0.6 (2026-07-25)
|
|
2
51
|
|
|
3
52
|
|
package/README.md
CHANGED
|
@@ -34,3 +34,39 @@ The `x-apigen-*` descriptor keys are **optional** dispatch accelerators —
|
|
|
34
34
|
structure (`format`/`$ref`/`oneOf`) is authoritative. `logicalKindOf` and
|
|
35
35
|
`codecIdOf` therefore return `undefined` (never throw) when a key is absent or
|
|
36
36
|
malformed; correctness must never depend on a hint being present.
|
|
37
|
+
|
|
38
|
+
## Schema example synthesis (shipped, unlike the contract-spine surface above)
|
|
39
|
+
|
|
40
|
+
`synthesizeExample(schema)` / `renderExampleNote(schema)` — generates a
|
|
41
|
+
concrete, plausible example value from any JSON Schema: walks `properties`/
|
|
42
|
+
`required`, resolves `$ref` against the schema's own `definitions`/`$defs`,
|
|
43
|
+
picks a `oneOf`/`anyOf` branch, merges `allOf`, and produces format-aware
|
|
44
|
+
placeholders (`date-time`, `uuid`, `email`, `decimal`, `int64`, `byte`, etc.)
|
|
45
|
+
that satisfy `ajv-formats`. Every synthesized shape round-trips through real
|
|
46
|
+
AJV validation in this package's own tests.
|
|
47
|
+
|
|
48
|
+
This exists to solve a concrete discoverability problem: an apigen-mounted
|
|
49
|
+
MCP tool's description/error used to only describe the *shape* of a valid
|
|
50
|
+
call ("all domain parameters go inside a `data` envelope"), never a *worked
|
|
51
|
+
example* with real field names — so callers repeatedly got the call shape
|
|
52
|
+
wrong on the first try. `renderExampleNote` is consumed by:
|
|
53
|
+
|
|
54
|
+
- `@adhd/apigen-engine-runtime`'s `buildToolDescription` — appends a
|
|
55
|
+
synthesized example to every apigen-mounted tool's description.
|
|
56
|
+
- `@adhd/apigen-engine-runtime`'s validate-Layer — appends the same
|
|
57
|
+
synthesized example to AJV validation-failure error messages, so a
|
|
58
|
+
rejected call comes back with both what was wrong *and* a working example.
|
|
59
|
+
|
|
60
|
+
Because synthesis is schema-driven (not hand-written per tool), it covers
|
|
61
|
+
every current and future apigen-mounted MCP tool automatically, including
|
|
62
|
+
mount-derived tools (e.g. `apigen-plugin-batch`'s `_batch/<kind>` mounts)
|
|
63
|
+
whose top-level shape differs from the `{data:{...}}` envelope regular
|
|
64
|
+
extracted operations use — the synthesized example always reflects each
|
|
65
|
+
tool's own real, advertised schema, never an assumed universal convention.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { renderExampleNote } from '@adhd/apigen-base-logical';
|
|
69
|
+
|
|
70
|
+
renderExampleNote(someOperation.input);
|
|
71
|
+
// → 'Example: {"data":{"input":{"family":"<string>","title":"<string>","body":"<string>","repo":"<string>"}}}'
|
|
72
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -6,5 +6,7 @@ export { X_APIGEN_LOGICAL, X_APIGEN_CODEC, X_APIGEN_CTOR, X_APIGEN_TOJSON, LOGIC
|
|
|
6
6
|
export { buildTranscoder, tryRegister, validateSchemaRefs } from './lib/runmode';
|
|
7
7
|
export { dateTimeCodec, int64Codec, decimalCodec, makeDecimal, byteCodec, uuidCodec, numberSpecialCodec, registerWellKnown, } from './lib/codecs/index';
|
|
8
8
|
export type { DecimalString } from './lib/codecs/index';
|
|
9
|
+
export { synthesizeExample, renderExampleNote } from './lib/example';
|
|
10
|
+
export type { JsonSchemaLike } from './lib/example';
|
|
9
11
|
export { CANONICAL_LOGICAL_TYPE_IDS, TEMPLATE_CELLS, cellsFor, depsForLogicalTypes, tsDepMap, assertNoEmptyCells, } from './lib/hints';
|
|
10
12
|
export type { CanonicalLogicalTypeId, HostLanguage, LanguageTable, } from './lib/hints';
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l="$apigen";class C extends Error{constructor(o){super(o),this.code="E_DUP_CODEC",this.name="CodecRegistryError"}}function P(e){e==null||e.wellKnown;const o=new Map;return{register(n,r){if(o.has(n.id)&&!(r!=null&&r.override))throw new C(`E_DUP_CODEC: a codec is already registered for id "${n.id}"`);o.set(n.id,n)},resolve(n){for(const r of o.values())if(r.matches(n))return r},get(n){return o.get(n)},ids(){return[...o.keys()]},freeze(){const n=new Map(o),r={register(){throw new C("E_DUP_CODEC: registry is frozen and cannot accept new codecs")},resolve(d){for(const a of n.values())if(a.matches(d))return a},get(d){return n.get(d)},ids(){return[...n.keys()]},freeze(){return r}};return r}}}const $="x-apigen-logical",v="x-apigen-codec",j="x-apigen-ctor",M="x-apigen-tojson",R="0.1.0",G=["scalar","nominal","union","map","set"];function w(e){const o=e[$];return typeof o=="string"&&G.includes(o)?o:void 0}function B(e){const o=e[v];return typeof o=="string"?o:void 0}function T(e,o){return{registry:e,resolve:t=>{throw new Error(`[apigen-logical] $ref "${t}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`)},seen:new WeakSet,path:"",mode:"strict",...o}}function p(e,o,t){const n=t.registry.resolve(o);if(n)return n.encode(e,o,t);const r=o.$ref;if(typeof r=="string"){const i=t.resolve(r);return p(e,i,t)}const d=o.oneOf;if(Array.isArray(d)){const i=U(e,d,o);return p(e,i,t)}const a=o.type;if(a==="array"){if(!Array.isArray(e))return D(e,o,t);const i=o.items;if(!i)return e.map(c=>u(c));const f=t.path;return Array.isArray(i)?e.map((c,_)=>{const s=i[_];return s===void 0?u(c):p(c,s,{...t,path:`${f}/${_}`})}):e.map((c,_)=>p(c,i,{...t,path:`${f}/${_}`}))}if(a==="object"){if(e===null||typeof e!="object"||Array.isArray(e))return D(e,o,t);const i=o.properties;if(!i)return u(e);const f=t.path,c={};for(const[_,s]of Object.entries(i)){const m=e[_];m!==void 0&&(c[_]=p(m,s,{...t,path:`${f}/${_}`}))}return c}return a==null?D(e,o,t):u(e)}function E(e,o,t){const n=t.registry.resolve(o);if(n)return n.decode(e,o,t);const r=o.$ref;if(typeof r=="string"){const i=t.resolve(r);return E(e,i,t)}const d=o.oneOf;if(Array.isArray(d)){const i=U(e,d,o);return E(e,i,t)}const a=o.type;if(a==="array"){if(!Array.isArray(e))return e;const i=o.items;if(!i)return e;const f=t.path;return Array.isArray(i)?e.map((c,_)=>{const s=i[_];return s===void 0?c:E(c,s,{...t,path:`${f}/${_}`})}):e.map((c,_)=>E(c,i,{...t,path:`${f}/${_}`}))}if(a==="object"){if(e===null||typeof e!="object"||Array.isArray(e))return e;const i=o.properties;if(!i)return e;const f=t.path,c={};for(const[_,s]of Object.entries(i)){const m=e[_];m!==void 0&&(c[_]=E(m,s,{...t,path:`${f}/${_}`}))}return c}return a==null?k(e,t):e}function U(e,o,t,n){const r=t.discriminator;if(r!=null&&r.propertyName){const d=e!==null&&typeof e=="object"?e[r.propertyName]:void 0;if(typeof d=="string"&&r.mapping){const a=r.mapping[d];if(a){const i=o.find(f=>f.$ref===a);if(i)return i}}}return o[0]??{}}function D(e,o,t){for(const n of t.registry.ids()){const r=t.registry.get(n);if(r)try{const d=r.encode(e,r.schema,t);return{[l]:n,v:d}}catch{}}return u(e)}function k(e,o){if(e!==null&&typeof e=="object"&&!Array.isArray(e)&&l in e){const t=e,n=t[l],r=o.registry.get(n);if(r)return r.decode(t.v,r.schema,o)}return e}function u(e){if(e===null||typeof e=="string"||typeof e=="number"||typeof e=="boolean")return e;if(Array.isArray(e))return e.map(u);if(typeof e=="object"){const o={};for(const[t,n]of Object.entries(e))n!==void 0&&(o[t]=u(n));return o}return null}function J(e,o){const t=new Set,n=[e];for(;n.length>0;){const r=n.pop();if(!r||t.has(r))continue;t.add(r);const d=r.$ref;if(typeof d=="string"){if(o){const s=o[d];if(!s){const m=Object.keys(o).join(", ")||"(none)";throw new Error(`[apigen-logical] $ref "${d}" cannot be resolved. Available $defs: ${m}`)}n.push(s)}continue}const a=r.oneOf;if(Array.isArray(a))for(const s of a)typeof s=="object"&&s!==null&&n.push(s);const i=r.properties;if(i)for(const s of Object.values(i))n.push(s);const f=r.items;if(Array.isArray(f))for(const s of f)typeof s=="object"&&s!==null&&n.push(s);else typeof f=="object"&&f!==null&&n.push(f);const c=r.additionalProperties;typeof c=="object"&&c!==null&&n.push(c);const _=r.propertyNames;typeof _=="object"&&_!==null&&n.push(_)}}function V(e){return{encode(o,t,n){const r=T(e,n);return p(o,t,r)},decode(o,t,n){const r=T(e,n);return E(o,t,r)}}}function Y(e,o,t){try{const n=t();e.register(n)}catch(n){if(K(n))return;throw n}}function K(e){return e===null||typeof e!="object"?!1:e.code==="MODULE_NOT_FOUND"}const h={id:"date-time",kind:"scalar",schema:{type:"string",format:"date-time"},matches(e){return e.type==="string"&&e.format==="date-time"},encode(e,o,t){return e.toISOString()},decode(e,o,t){if(typeof e!="string"){if(t.mode==="strict")throw new TypeError(`[date-time] expected a string on the wire at "${t.path}", got ${typeof e}`);return new Date(String(e))}if(t.mode==="strict"&&Number.isNaN(new Date(e).getTime()))throw new TypeError(`[date-time] invalid date-time string at "${t.path}": ${JSON.stringify(e)}`);return new Date(e)}},S={id:"int64",kind:"scalar",schema:{type:"string",format:"int64"},matches(e){return e.type==="string"&&e.format==="int64"},encode(e,o,t){return String(e)},decode(e,o,t){if(typeof e!="string"){if(t.mode==="strict")throw new TypeError(`[int64] expected a string on the wire at "${t.path}", got ${typeof e}`);try{return BigInt(Math.trunc(Number(e)))}catch{return BigInt(0)}}if(t.mode==="strict")return BigInt(e);if(/^-?\d+$/.test(e))try{return BigInt(e)}catch{return BigInt(0)}return BigInt(0)}};function W(e){return e}const N={id:"decimal",kind:"scalar",schema:{type:"string",format:"decimal"},matches(e){return e.type==="string"&&e.format==="decimal"},encode(e,o,t){return e},decode(e,o,t){if(typeof e!="string"){if(t.mode==="strict")throw new TypeError(`[decimal] expected a string on the wire at "${t.path}", got ${typeof e}`);return String(e)}if(t.mode==="strict"&&!/^-?\d+(\.\d+)?$/.test(e))throw new TypeError(`[decimal] wire value "${e}" at "${t.path}" is not a valid decimal string`);return e}},b={id:"byte",kind:"scalar",schema:{type:"string",format:"byte"},matches(e){return e.type==="string"&&e.format==="byte"},encode(e,o,t){return Buffer.from(e).toString("base64")},decode(e,o,t){if(typeof e!="string"){if(t.mode==="strict")throw new TypeError(`[byte] expected a base64 string on the wire at "${t.path}", got ${typeof e}`);return new Uint8Array(0)}if(t.mode==="strict"&&!/^[A-Za-z0-9+/]*={0,2}$/.test(e))throw new TypeError(`[byte] wire value at "${t.path}" is not standard base64 (format:byte): "${e}"`);return new Uint8Array(Buffer.from(e,"base64"))}},z=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,L={id:"uuid",kind:"scalar",schema:{type:"string",format:"uuid"},matches(e){return e.type==="string"&&e.format==="uuid"},encode(e,o,t){return e.toLowerCase()},decode(e,o,t){if(typeof e!="string"){if(t.mode==="strict")throw new TypeError(`[uuid] expected a string on the wire at "${t.path}", got ${typeof e}`);return String(e).toLowerCase()}if(t.mode==="strict"&&!z.test(e))throw new TypeError(`[uuid] wire value "${e}" at "${t.path}" is not a lowercase-hyphenated RFC 4122 UUID`);return e}},O="NaN",A="Infinity",g="-Infinity",X=new Set([O,A,g]),F={id:"number-special",kind:"scalar",schema:{type:"number"},matches(e){return e.type==="number"&&e.format===void 0},encode(e,o,t){return Number.isNaN(e)?O:e===1/0?A:e===-1/0?g:e},decode(e,o,t){if(typeof e=="number")return e;if(typeof e=="string"){if(e===O)return NaN;if(e===A)return 1/0;if(e===g)return-1/0;const n=Number(e);if(!Number.isNaN(n))return n}if(t.mode==="strict")throw new TypeError(`[number-special] unrecognized wire value at "${t.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...X].join(", ")}.`);return NaN}},H=[h,S,N,b,L,F];function Z(e,o={}){for(const t of H)e.register(t,{override:o.override??!1})}const I=[h.id,S.id,N.id,b.id,L.id,F.id],q={"date-time":{encode:"$.toISOString()",decode:"new Date($)",mode:"native"},int64:{encode:"String($)",decode:"BigInt($)",mode:"native"},decimal:{encode:"$.toString()",decode:"new Decimal($)",imports:[],dep:{name:"decimal.js",version:"^10"},mode:"branded"},byte:{encode:"Buffer.from($).toString('base64')",decode:"new Uint8Array(Buffer.from($, 'base64'))",mode:"native"},uuid:{encode:"$.toLowerCase()",decode:"$",mode:"native"},"number-special":{encode:"numToWire($)",decode:"wireToNum($)",mode:"native"}},Q={"date-time":{encode:"$.isoformat()",decode:"datetime.fromisoformat($)",imports:["from datetime import datetime"],mode:"native"},int64:{encode:"str($)",decode:"int($)",mode:"native"},decimal:{encode:"str($)",decode:"Decimal($)",imports:["from decimal import Decimal"],mode:"native"},byte:{encode:"b64encode($).decode()",decode:"b64decode($)",imports:["from base64 import b64encode, b64decode"],mode:"native"},uuid:{encode:"str($)",decode:"UUID($)",imports:["from uuid import UUID"],mode:"native"},"number-special":{encode:"num_to_wire($)",decode:"wire_to_num($)",mode:"native"}},x={"date-time":{encode:"__SCAFFOLD_RUST_DATETIME_ENCODE__",decode:"__SCAFFOLD_RUST_DATETIME_DECODE__",imports:["use chrono::{DateTime, Utc};"],dep:{name:"chrono",version:"^0.4"},mode:"lib"},int64:{encode:"__SCAFFOLD_RUST_INT64_ENCODE__",decode:"__SCAFFOLD_RUST_INT64_DECODE__",imports:["use serde_with::DisplayFromStr;"],dep:{name:"serde_with",version:"^3"},mode:"lib"},decimal:{encode:"__SCAFFOLD_RUST_DECIMAL_ENCODE__",decode:"__SCAFFOLD_RUST_DECIMAL_DECODE__",imports:["use rust_decimal::Decimal;"],dep:{name:"rust_decimal",version:"^1"},mode:"lib"},byte:{encode:"__SCAFFOLD_RUST_BYTE_ENCODE__",decode:"__SCAFFOLD_RUST_BYTE_DECODE__",imports:["use serde_with::base64::Base64;"],dep:{name:"serde_with",version:"^3"},mode:"lib"},uuid:{encode:"__SCAFFOLD_RUST_UUID_ENCODE__",decode:"__SCAFFOLD_RUST_UUID_DECODE__",imports:["use uuid::Uuid;"],dep:{name:"uuid",version:"^1"},mode:"lib"},"number-special":{encode:"__SCAFFOLD_RUST_NUMSPECIAL_ENCODE__",decode:"__SCAFFOLD_RUST_NUMSPECIAL_DECODE__",mode:"native"}},ee={"date-time":{encode:"__SCAFFOLD_GO_DATETIME_ENCODE__",decode:"__SCAFFOLD_GO_DATETIME_DECODE__",imports:['"time"'],mode:"native"},int64:{encode:"__SCAFFOLD_GO_INT64_ENCODE__",decode:"__SCAFFOLD_GO_INT64_DECODE__",imports:['"strconv"'],mode:"native"},decimal:{encode:"__SCAFFOLD_GO_DECIMAL_ENCODE__",decode:"__SCAFFOLD_GO_DECIMAL_DECODE__",imports:['"github.com/shopspring/decimal"'],dep:{name:"github.com/shopspring/decimal",version:"v1"},mode:"lib"},byte:{encode:"__SCAFFOLD_GO_BYTE_ENCODE__",decode:"__SCAFFOLD_GO_BYTE_DECODE__",imports:['"encoding/base64"'],mode:"native"},uuid:{encode:"__SCAFFOLD_GO_UUID_ENCODE__",decode:"__SCAFFOLD_GO_UUID_DECODE__",imports:['"github.com/google/uuid"'],dep:{name:"github.com/google/uuid",version:"v1"},mode:"lib"},"number-special":{encode:"__SCAFFOLD_GO_NUMSPECIAL_ENCODE__",decode:"__SCAFFOLD_GO_NUMSPECIAL_DECODE__",mode:"native"}},te={"date-time":{encode:"__SCAFFOLD_JAVA_DATETIME_ENCODE__",decode:"__SCAFFOLD_JAVA_DATETIME_DECODE__",imports:["import java.time.Instant;"],dep:{name:"jackson-datatype-jsr310",version:"2.x"},mode:"lib"},int64:{encode:"__SCAFFOLD_JAVA_INT64_ENCODE__",decode:"__SCAFFOLD_JAVA_INT64_DECODE__",imports:["import com.fasterxml.jackson.annotation.JsonFormat;"],mode:"native"},decimal:{encode:"__SCAFFOLD_JAVA_DECIMAL_ENCODE__",decode:"__SCAFFOLD_JAVA_DECIMAL_DECODE__",imports:["import java.math.BigDecimal;"],mode:"native"},byte:{encode:"__SCAFFOLD_JAVA_BYTE_ENCODE__",decode:"__SCAFFOLD_JAVA_BYTE_DECODE__",mode:"native"},uuid:{encode:"__SCAFFOLD_JAVA_UUID_ENCODE__",decode:"__SCAFFOLD_JAVA_UUID_DECODE__",imports:["import java.util.UUID;"],mode:"native"},"number-special":{encode:"__SCAFFOLD_JAVA_NUMSPECIAL_ENCODE__",decode:"__SCAFFOLD_JAVA_NUMSPECIAL_DECODE__",mode:"native"}},y=Object.freeze({typescript:q,python:Q,rust:x,go:ee,java:te});function oe(e){return y[e]}function ne(e,o){const t=y[o],n=[];for(const r of e){const d=t[r];d!=null&&d.dep&&n.push({id:r,dep:d.dep})}return n}function re(){const e=y.typescript,o={};for(const t of I){const n=e[t].dep;n&&(o[t]=n)}return Object.freeze(o)}function ie(e,o){const t=o??y[e],n=[];for(const r of I)t[r]||n.push(r);if(n.length>0)throw new Error(`[hints] assertNoEmptyCells: language "${e}" is missing cells for: ${n.join(", ")}`)}exports.CANONICAL_LOGICAL_TYPE_IDS=I;exports.CodecRegistryError=C;exports.ENVELOPE_KEY=l;exports.LOGICAL_TYPE_VERSION=R;exports.TEMPLATE_CELLS=y;exports.X_APIGEN_CODEC=v;exports.X_APIGEN_CTOR=j;exports.X_APIGEN_LOGICAL=$;exports.X_APIGEN_TOJSON=M;exports.assertNoEmptyCells=ie;exports.buildTranscoder=V;exports.byteCodec=b;exports.cellsFor=oe;exports.codecIdOf=B;exports.createRegistry=P;exports.dateTimeCodec=h;exports.decimalCodec=N;exports.depsForLogicalTypes=ne;exports.int64Codec=S;exports.logicalKindOf=w;exports.makeDecimal=W;exports.numberSpecialCodec=F;exports.registerWellKnown=Z;exports.tryRegister=Y;exports.tsDepMap=re;exports.uuidCodec=L;exports.validateSchemaRefs=J;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A="$apigen";class C extends Error{constructor(t){super(t),this.code="E_DUP_CODEC",this.name="CodecRegistryError"}}function V(e){e==null||e.wellKnown;const t=new Map;return{register(n,o){if(t.has(n.id)&&!(o!=null&&o.override))throw new C(`E_DUP_CODEC: a codec is already registered for id "${n.id}"`);t.set(n.id,n)},resolve(n){for(const o of t.values())if(o.matches(n))return o},get(n){return t.get(n)},ids(){return[...t.keys()]},freeze(){const n=new Map(t),o={register(){throw new C("E_DUP_CODEC: registry is frozen and cannot accept new codecs")},resolve(i){for(const a of n.values())if(a.matches(i))return a},get(i){return n.get(i)},ids(){return[...n.keys()]},freeze(){return o}};return o}}}const M="x-apigen-logical",w="x-apigen-codec",Y="x-apigen-ctor",K="x-apigen-tojson",W="0.1.0",X=["scalar","nominal","union","map","set"];function q(e){const t=e[M];return typeof t=="string"&&X.includes(t)?t:void 0}function Z(e){const t=e[w];return typeof t=="string"?t:void 0}function P(e,t){return{registry:e,resolve:r=>{throw new Error(`[apigen-logical] $ref "${r}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`)},seen:new WeakSet,path:"",mode:"strict",...t}}function l(e,t,r){const n=r.registry.resolve(t);if(n)return n.encode(e,t,r);const o=t.$ref;if(typeof o=="string"){const c=r.resolve(o);return l(e,c,r)}const i=t.oneOf;if(Array.isArray(i)){const c=B(e,i,t,r);return l(e,c,r)}const a=t.type;if(a==="array"){if(!Array.isArray(e))return b(e,t,r);const c=t.items;if(!c)return e.map(f=>_(f));const u=r.path;return Array.isArray(c)?e.map((f,s)=>{const d=c[s];return d===void 0?_(f):l(f,d,{...r,path:`${u}/${s}`})}):e.map((f,s)=>l(f,c,{...r,path:`${u}/${s}`}))}if(a==="object"){if(e===null||typeof e!="object"||Array.isArray(e))return b(e,t,r);const c=t.properties;if(!c)return _(e);const u=r.path,f={};for(const[s,d]of Object.entries(c)){const p=e[s];p!==void 0&&(f[s]=l(p,d,{...r,path:`${u}/${s}`}))}return f}return a==null?b(e,t,r):_(e)}function y(e,t,r){const n=r.registry.resolve(t);if(n)return n.decode(e,t,r);const o=t.$ref;if(typeof o=="string"){const c=r.resolve(o);return y(e,c,r)}const i=t.oneOf;if(Array.isArray(i)){const c=B(e,i,t,r);return y(e,c,r)}const a=t.type;if(a==="array"){if(!Array.isArray(e))return e;const c=t.items;if(!c)return e;const u=r.path;return Array.isArray(c)?e.map((f,s)=>{const d=c[s];return d===void 0?f:y(f,d,{...r,path:`${u}/${s}`})}):e.map((f,s)=>y(f,c,{...r,path:`${u}/${s}`}))}if(a==="object"){if(e===null||typeof e!="object"||Array.isArray(e))return e;const c=t.properties;if(!c)return e;const u=r.path,f={};for(const[s,d]of Object.entries(c)){const p=e[s];p!==void 0&&(f[s]=y(p,d,{...r,path:`${u}/${s}`}))}for(const[s,d]of Object.entries(e))!(s in c)&&d!==void 0&&(f[s]=d);return f}return a==null?S(e,r):e}function B(e,t,r,n){var u;const o=r.discriminator;if(o!=null&&o.propertyName){const f=o.propertyName,s=e!==null&&typeof e=="object"?e[f]:void 0;if(typeof s=="string"){if(o.mapping){const d=o.mapping[s];if(d){const p=t.find(m=>m.$ref===d);if(p)return p}}for(const d of t){const m=(u=D(d,n).properties)==null?void 0:u[f];if(!m)continue;const E=m.enum;if(m.const===s||Array.isArray(E)&&E.includes(s))return d}}}const i=H(e,t,n);if(i)return i;let a,c=-1;for(const f of t){const s=D(f,n),d=z(e,s,n);d!==null&&d>c&&(c=d,a=f)}return a!==void 0?a:t[0]??{}}function D(e,t){const r=e.$ref;return typeof r=="string"?t.resolve(r):e}function k(e){if("const"in e)return e.const;const t=e.enum;if(Array.isArray(t)&&t.length===1)return t[0]}function H(e,t,r){if(e===null||typeof e!="object"||Array.isArray(e))return;const n=e,o=t.map(a=>D(a,r)),i=new Set;for(const a of o){const c=a.properties;if(c)for(const[u,f]of Object.entries(c))k(f)!==void 0&&i.add(u)}for(const a of i){const c=o.map(p=>{const m=p.properties,E=m==null?void 0:m[a];return E?k(E):void 0}),u=c.filter(p=>p!==void 0),f=new Set(u);if(u.length<2||f.size!==u.length)continue;const s=n[a];if(s===void 0)continue;const d=c.findIndex(p=>p===s);if(d!==-1)return t[d]}}function Q(e){if(e===null)return"null";if(Array.isArray(e))return"array";const t=typeof e;return t==="number"?Number.isInteger(e)?"integer":"number":t}function z(e,t,r){const n=t.oneOf;if(Array.isArray(n)){let s=null;for(const d of n){const p=z(e,D(d,r),r);p!==null&&(s===null||p>s)&&(s=p)}return s}const o=t.type,i=Q(e);if(typeof o=="string"){if(!(o===i||o==="number"&&i==="integer"))return null}else if(Array.isArray(o)&&!o.some(d=>d===i||d==="number"&&i==="integer"))return null;if(e===null||typeof e!="object"||Array.isArray(e))return typeof o=="string"?1:0;const a=e,c=t.required;let u=0;if(Array.isArray(c)){for(const s of c)if(typeof s=="string"){if(a[s]===void 0)return null;u+=2}}const f=t.properties;if(f){for(const s of Object.keys(f))a[s]!==void 0&&(u+=1);for(const s of Object.keys(a))f[s]===void 0&&(u-=1)}return u}function b(e,t,r){var n;for(const o of r.registry.ids()){const i=r.registry.get(o);if((n=i==null?void 0:i.ownsValue)!=null&&n.call(i,e))return{[A]:o,v:i.encode(e,i.schema,r)}}if(Array.isArray(e))return e.map(o=>b(o,{},r));if(J(e)){const o={};for(const[i,a]of Object.entries(e))a!==void 0&&(o[i]=b(a,{},r));return o}return _(e)}function J(e){if(e===null||typeof e!="object"||Array.isArray(e))return!1;const t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function S(e,t){if(e!==null&&typeof e=="object"&&!Array.isArray(e)&&A in e){const r=e,n=r[A],o=t.registry.get(n);if(o)return o.decode(r.v,o.schema,t)}if(Array.isArray(e))return e.map(r=>S(r,t));if(J(e)){const r={};for(const[n,o]of Object.entries(e))r[n]=S(o,t);return r}return e}function _(e){if(e===null||typeof e=="string"||typeof e=="number"||typeof e=="boolean")return e;if(Array.isArray(e))return e.map(_);if(typeof e=="object"){const t={};for(const[r,n]of Object.entries(e))n!==void 0&&(t[r]=_(n));return t}return null}function x(e,t){const r=new Set,n=[e];for(;n.length>0;){const o=n.pop();if(!o||r.has(o))continue;r.add(o);const i=o.$ref;if(typeof i=="string"){if(t){const d=t[i];if(!d){const p=Object.keys(t).join(", ")||"(none)";throw new Error(`[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${p}`)}n.push(d)}continue}const a=o.oneOf;if(Array.isArray(a))for(const d of a)typeof d=="object"&&d!==null&&n.push(d);const c=o.properties;if(c)for(const d of Object.values(c))n.push(d);const u=o.items;if(Array.isArray(u))for(const d of u)typeof d=="object"&&d!==null&&n.push(d);else typeof u=="object"&&u!==null&&n.push(u);const f=o.additionalProperties;typeof f=="object"&&f!==null&&n.push(f);const s=o.propertyNames;typeof s=="object"&&s!==null&&n.push(s)}}function ee(e){return{encode(t,r,n){const o=P(e,n);return l(t,r,o)},decode(t,r,n){const o=P(e,n);return y(t,r,o)}}}function te(e,t,r){try{const n=r();e.register(n)}catch(n){if(re(n))return;throw n}}function re(e){return e===null||typeof e!="object"?!1:e.code==="MODULE_NOT_FOUND"}const I={id:"date-time",kind:"scalar",schema:{type:"string",format:"date-time"},matches(e){return e.type==="string"&&e.format==="date-time"},ownsValue(e){return e instanceof Date},encode(e,t,r){return e.toISOString()},decode(e,t,r){if(typeof e!="string"){if(r.mode==="strict")throw new TypeError(`[date-time] expected a string on the wire at "${r.path}", got ${typeof e}`);return new Date(String(e))}if(r.mode==="strict"&&Number.isNaN(new Date(e).getTime()))throw new TypeError(`[date-time] invalid date-time string at "${r.path}": ${JSON.stringify(e)}`);return new Date(e)}},T={id:"int64",kind:"scalar",schema:{type:"string",format:"int64"},matches(e){return e.type==="string"&&e.format==="int64"},ownsValue(e){return typeof e=="bigint"},encode(e,t,r){return String(e)},decode(e,t,r){if(typeof e!="string"){if(r.mode==="strict")throw new TypeError(`[int64] expected a string on the wire at "${r.path}", got ${typeof e}`);try{return BigInt(Math.trunc(Number(e)))}catch{return BigInt(0)}}if(r.mode==="strict")return BigInt(e);if(/^-?\d+$/.test(e))try{return BigInt(e)}catch{return BigInt(0)}return BigInt(0)}};function ne(e){return e}const v={id:"decimal",kind:"scalar",schema:{type:"string",format:"decimal"},matches(e){return e.type==="string"&&e.format==="decimal"},encode(e,t,r){return e},decode(e,t,r){if(typeof e!="string"){if(r.mode==="strict")throw new TypeError(`[decimal] expected a string on the wire at "${r.path}", got ${typeof e}`);return String(e)}if(r.mode==="strict"&&!/^-?\d+(\.\d+)?$/.test(e))throw new TypeError(`[decimal] wire value "${e}" at "${r.path}" is not a valid decimal string`);return e}},L={id:"byte",kind:"scalar",schema:{type:"string",format:"byte"},matches(e){return e.type==="string"&&e.format==="byte"},ownsValue(e){return e instanceof Uint8Array},encode(e,t,r){return Buffer.from(e).toString("base64")},decode(e,t,r){if(typeof e!="string"){if(r.mode==="strict")throw new TypeError(`[byte] expected a base64 string on the wire at "${r.path}", got ${typeof e}`);return new Uint8Array(0)}if(r.mode==="strict"&&!/^[A-Za-z0-9+/]*={0,2}$/.test(e))throw new TypeError(`[byte] wire value at "${r.path}" is not standard base64 (format:byte): "${e}"`);return new Uint8Array(Buffer.from(e,"base64"))}},oe=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,F={id:"uuid",kind:"scalar",schema:{type:"string",format:"uuid"},matches(e){return e.type==="string"&&e.format==="uuid"},encode(e,t,r){return e.toLowerCase()},decode(e,t,r){if(typeof e!="string"){if(r.mode==="strict")throw new TypeError(`[uuid] expected a string on the wire at "${r.path}", got ${typeof e}`);return String(e).toLowerCase()}if(r.mode==="strict"&&!oe.test(e))throw new TypeError(`[uuid] wire value "${e}" at "${r.path}" is not a lowercase-hyphenated RFC 4122 UUID`);return e}},h="NaN",N="Infinity",$="-Infinity",ie=new Set([h,N,$]),U={id:"number-special",kind:"scalar",schema:{type:"number"},matches(e){return e.type==="number"&&e.format===void 0},ownsValue(e){return typeof e=="number"&&!Number.isFinite(e)},encode(e,t,r){return Number.isNaN(e)?h:e===1/0?N:e===-1/0?$:e},decode(e,t,r){if(typeof e=="number")return e;if(typeof e=="string"){if(e===h)return NaN;if(e===N)return 1/0;if(e===$)return-1/0;const n=Number(e);if(!Number.isNaN(n))return n}if(r.mode==="strict")throw new TypeError(`[number-special] unrecognized wire value at "${r.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...ie].join(", ")}.`);return NaN}},se=[I,T,v,L,F,U];function ce(e,t={}){for(const r of se)e.register(r,{override:t.override??!1})}const de=12;function ae(e,t){const r=/^#\/(definitions|\$defs)\/(.+)$/.exec(e);if(!r)return;const[,n,o]=r,i=n==="$defs"?t.$defs:t.definitions;return i==null?void 0:i[decodeURIComponent(o)]}function R(e){return Array.isArray(e)&&e.length>0?e[0]:void 0}function fe(e){if(Array.isArray(e.type))return e.type.find(t=>t!=="null")??e.type[0];if(typeof e.type=="string")return e.type;if(e.properties)return"object"}function ue(e){switch(e.format){case"date-time":return"1970-01-01T00:00:00.000Z";case"date":return"1970-01-01";case"time":return"00:00:00Z";case"uuid":return"00000000-0000-0000-0000-000000000000";case"email":return"user@example.com";case"uri":case"url":case"uri-reference":return"https://example.com";case"hostname":return"example.com";case"ipv4":return"127.0.0.1";case"ipv6":return"::1";case"decimal":return"0";case"int64":return"0";case"byte":return"";default:return"<string>"}}function G(e,t,r){const n=e.properties??{},o=e.required??[],i={};for(const a of o){const c=n[a];i[a]=c!==void 0?g(c,t,r+1):`<${a}>`}return i}function pe(e,t,r){const n=Array.isArray(e.items)?e.items[0]:e.items;return n?[g(n,t,r+1)]:[]}function g(e,t=e??{},r=0){if(!e||r>de)return null;if(typeof e.$ref=="string"){const i=ae(e.$ref,t);return i!==void 0?g(i,t,r+1):null}if(e.const!==void 0)return e.const;if(Array.isArray(e.enum)&&e.enum.length>0)return e.enum[0];if(Array.isArray(e.allOf)&&e.allOf.length>0){const i={},a=[];for(const c of e.allOf){Object.assign(i,c.properties??{});for(const u of c.required??[])a.includes(u)||a.push(u)}return G({...e,properties:i,required:a},t,r)}const n=R(e.oneOf)??R(e.anyOf);if(n)return g(n,t,r+1);switch(fe(e)){case"object":return G(e,t,r);case"array":return pe(e,t,r);case"string":return ue(e);case"integer":case"number":return 0;case"boolean":return!1;case"null":return null;default:return null}}function me(e){if(!e)return;const t=g(e);return`Example: ${JSON.stringify(t)}`}const j=[I.id,T.id,v.id,L.id,F.id,U.id],_e={"date-time":{encode:"$.toISOString()",decode:"new Date($)",mode:"native"},int64:{encode:"String($)",decode:"BigInt($)",mode:"native"},decimal:{encode:"$.toString()",decode:"new Decimal($)",imports:[],dep:{name:"decimal.js",version:"^10"},mode:"branded"},byte:{encode:"Buffer.from($).toString('base64')",decode:"new Uint8Array(Buffer.from($, 'base64'))",mode:"native"},uuid:{encode:"$.toLowerCase()",decode:"$",mode:"native"},"number-special":{encode:"numToWire($)",decode:"wireToNum($)",mode:"native"}},le={"date-time":{encode:"$.isoformat()",decode:"datetime.fromisoformat($)",imports:["from datetime import datetime"],mode:"native"},int64:{encode:"str($)",decode:"int($)",mode:"native"},decimal:{encode:"str($)",decode:"Decimal($)",imports:["from decimal import Decimal"],mode:"native"},byte:{encode:"b64encode($).decode()",decode:"b64decode($)",imports:["from base64 import b64encode, b64decode"],mode:"native"},uuid:{encode:"str($)",decode:"UUID($)",imports:["from uuid import UUID"],mode:"native"},"number-special":{encode:"num_to_wire($)",decode:"wire_to_num($)",mode:"native"}},ye={"date-time":{encode:"__SCAFFOLD_RUST_DATETIME_ENCODE__",decode:"__SCAFFOLD_RUST_DATETIME_DECODE__",imports:["use chrono::{DateTime, Utc};"],dep:{name:"chrono",version:"^0.4"},mode:"lib"},int64:{encode:"__SCAFFOLD_RUST_INT64_ENCODE__",decode:"__SCAFFOLD_RUST_INT64_DECODE__",imports:["use serde_with::DisplayFromStr;"],dep:{name:"serde_with",version:"^3"},mode:"lib"},decimal:{encode:"__SCAFFOLD_RUST_DECIMAL_ENCODE__",decode:"__SCAFFOLD_RUST_DECIMAL_DECODE__",imports:["use rust_decimal::Decimal;"],dep:{name:"rust_decimal",version:"^1"},mode:"lib"},byte:{encode:"__SCAFFOLD_RUST_BYTE_ENCODE__",decode:"__SCAFFOLD_RUST_BYTE_DECODE__",imports:["use serde_with::base64::Base64;"],dep:{name:"serde_with",version:"^3"},mode:"lib"},uuid:{encode:"__SCAFFOLD_RUST_UUID_ENCODE__",decode:"__SCAFFOLD_RUST_UUID_DECODE__",imports:["use uuid::Uuid;"],dep:{name:"uuid",version:"^1"},mode:"lib"},"number-special":{encode:"__SCAFFOLD_RUST_NUMSPECIAL_ENCODE__",decode:"__SCAFFOLD_RUST_NUMSPECIAL_DECODE__",mode:"native"}},ge={"date-time":{encode:"__SCAFFOLD_GO_DATETIME_ENCODE__",decode:"__SCAFFOLD_GO_DATETIME_DECODE__",imports:['"time"'],mode:"native"},int64:{encode:"__SCAFFOLD_GO_INT64_ENCODE__",decode:"__SCAFFOLD_GO_INT64_DECODE__",imports:['"strconv"'],mode:"native"},decimal:{encode:"__SCAFFOLD_GO_DECIMAL_ENCODE__",decode:"__SCAFFOLD_GO_DECIMAL_DECODE__",imports:['"github.com/shopspring/decimal"'],dep:{name:"github.com/shopspring/decimal",version:"v1"},mode:"lib"},byte:{encode:"__SCAFFOLD_GO_BYTE_ENCODE__",decode:"__SCAFFOLD_GO_BYTE_DECODE__",imports:['"encoding/base64"'],mode:"native"},uuid:{encode:"__SCAFFOLD_GO_UUID_ENCODE__",decode:"__SCAFFOLD_GO_UUID_DECODE__",imports:['"github.com/google/uuid"'],dep:{name:"github.com/google/uuid",version:"v1"},mode:"lib"},"number-special":{encode:"__SCAFFOLD_GO_NUMSPECIAL_ENCODE__",decode:"__SCAFFOLD_GO_NUMSPECIAL_DECODE__",mode:"native"}},Ee={"date-time":{encode:"$.toString()",decode:"Instant.parse($)",imports:["import java.time.Instant;"],dep:{name:"jackson-datatype-jsr310",version:"2.x"},mode:"lib"},int64:{encode:"@JsonFormat(shape = JsonFormat.Shape.STRING)",decode:"@JsonFormat(shape = JsonFormat.Shape.STRING)",imports:["import com.fasterxml.jackson.annotation.JsonFormat;"],mode:"native"},decimal:{encode:"@JsonSerialize(using = ToStringSerializer.class)",decode:"new BigDecimal($)",imports:["import java.math.BigDecimal;","import com.fasterxml.jackson.databind.annotation.JsonSerialize;","import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;"],mode:"native"},byte:{encode:"$",decode:"$",mode:"native"},uuid:{encode:"$.toString()",decode:"UUID.fromString($)",imports:["import java.util.UUID;"],mode:"native"},"number-special":{encode:"@JsonSerialize(using = NumberSpecialSerializer.class)",decode:"@JsonDeserialize(using = NumberSpecialDeserializer.class)",imports:["import com.fasterxml.jackson.databind.annotation.JsonSerialize;","import com.fasterxml.jackson.databind.annotation.JsonDeserialize;"],mode:"native"}},O=Object.freeze({typescript:_e,python:le,rust:ye,go:ge,java:Ee});function be(e){return O[e]}function Oe(e,t){const r=O[t],n=[];for(const o of e){const i=r[o];i!=null&&i.dep&&n.push({id:o,dep:i.dep})}return n}function Ae(){const e=O.typescript,t={};for(const r of j){const n=e[r].dep;n&&(t[r]=n)}return Object.freeze(t)}function De(e,t){const r=t??O[e],n=[];for(const o of j)r[o]||n.push(o);if(n.length>0)throw new Error(`[hints] assertNoEmptyCells: language "${e}" is missing cells for: ${n.join(", ")}`)}exports.CANONICAL_LOGICAL_TYPE_IDS=j;exports.CodecRegistryError=C;exports.ENVELOPE_KEY=A;exports.LOGICAL_TYPE_VERSION=W;exports.TEMPLATE_CELLS=O;exports.X_APIGEN_CODEC=w;exports.X_APIGEN_CTOR=Y;exports.X_APIGEN_LOGICAL=M;exports.X_APIGEN_TOJSON=K;exports.assertNoEmptyCells=De;exports.buildTranscoder=ee;exports.byteCodec=L;exports.cellsFor=be;exports.codecIdOf=Z;exports.createRegistry=V;exports.dateTimeCodec=I;exports.decimalCodec=v;exports.depsForLogicalTypes=Oe;exports.int64Codec=T;exports.logicalKindOf=q;exports.makeDecimal=ne;exports.numberSpecialCodec=U;exports.registerWellKnown=ce;exports.renderExampleNote=me;exports.synthesizeExample=g;exports.tryRegister=te;exports.tsDepMap=Ae;exports.uuidCodec=F;exports.validateSchemaRefs=x;
|