@adhd/apigen-base-logical 0.1.1 → 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 CHANGED
@@ -1,3 +1,30 @@
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
+
1
28
  ## 0.1.1 (2026-08-07)
2
29
 
3
30
 
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g="$apigen";class C extends Error{constructor(r){super(r),this.code="E_DUP_CODEC",this.name="CodecRegistryError"}}function G(e){e==null||e.wellKnown;const r=new Map;return{register(n,o){if(r.has(n.id)&&!(o!=null&&o.override))throw new C(`E_DUP_CODEC: a codec is already registered for id "${n.id}"`);r.set(n.id,n)},resolve(n){for(const o of r.values())if(o.matches(n))return o},get(n){return r.get(n)},ids(){return[...r.keys()]},freeze(){const n=new Map(r),o={register(){throw new C("E_DUP_CODEC: registry is frozen and cannot accept new codecs")},resolve(i){for(const c of n.values())if(c.matches(i))return c},get(i){return n.get(i)},ids(){return[...n.keys()]},freeze(){return o}};return o}}}const j="x-apigen-logical",R="x-apigen-codec",M="x-apigen-ctor",k="x-apigen-tojson",w="0.1.0",z=["scalar","nominal","union","map","set"];function B(e){const r=e[j];return typeof r=="string"&&z.includes(r)?r:void 0}function J(e){const r=e[R];return typeof r=="string"?r:void 0}function L(e,r){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",...r}}function m(e,r,t){const n=t.registry.resolve(r);if(n)return n.encode(e,r,t);const o=r.$ref;if(typeof o=="string"){const s=t.resolve(o);return m(e,s,t)}const i=r.oneOf;if(Array.isArray(i)){const s=P(e,i,r);return m(e,s,t)}const c=r.type;if(c==="array"){if(!Array.isArray(e))return O(e,r,t);const s=r.items;if(!s)return e.map(a=>_(a));const u=t.path;return Array.isArray(s)?e.map((a,f)=>{const d=s[f];return d===void 0?_(a):m(a,d,{...t,path:`${u}/${f}`})}):e.map((a,f)=>m(a,s,{...t,path:`${u}/${f}`}))}if(c==="object"){if(e===null||typeof e!="object"||Array.isArray(e))return O(e,r,t);const s=r.properties;if(!s)return _(e);const u=t.path,a={};for(const[f,d]of Object.entries(s)){const p=e[f];p!==void 0&&(a[f]=m(p,d,{...t,path:`${u}/${f}`}))}return a}return c==null?O(e,r,t):_(e)}function l(e,r,t){const n=t.registry.resolve(r);if(n)return n.decode(e,r,t);const o=r.$ref;if(typeof o=="string"){const s=t.resolve(o);return l(e,s,t)}const i=r.oneOf;if(Array.isArray(i)){const s=P(e,i,r);return l(e,s,t)}const c=r.type;if(c==="array"){if(!Array.isArray(e))return e;const s=r.items;if(!s)return e;const u=t.path;return Array.isArray(s)?e.map((a,f)=>{const d=s[f];return d===void 0?a:l(a,d,{...t,path:`${u}/${f}`})}):e.map((a,f)=>l(a,s,{...t,path:`${u}/${f}`}))}if(c==="object"){if(e===null||typeof e!="object"||Array.isArray(e))return e;const s=r.properties;if(!s)return e;const u=t.path,a={};for(const[f,d]of Object.entries(s)){const p=e[f];p!==void 0&&(a[f]=l(p,d,{...t,path:`${u}/${f}`}))}return a}return c==null?Y(e,t):e}function P(e,r,t,n){const o=t.discriminator;if(o!=null&&o.propertyName){const i=e!==null&&typeof e=="object"?e[o.propertyName]:void 0;if(typeof i=="string"&&o.mapping){const c=o.mapping[i];if(c){const s=r.find(u=>u.$ref===c);if(s)return s}}}return r[0]??{}}function O(e,r,t){for(const n of t.registry.ids()){const o=t.registry.get(n);if(o)try{const i=o.encode(e,o.schema,t);return{[g]:n,v:i}}catch{}}return _(e)}function Y(e,r){if(e!==null&&typeof e=="object"&&!Array.isArray(e)&&g in e){const t=e,n=t[g],o=r.registry.get(n);if(o)return o.decode(t.v,o.schema,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 r={};for(const[t,n]of Object.entries(e))n!==void 0&&(r[t]=_(n));return r}return null}function K(e,r){const t=new Set,n=[e];for(;n.length>0;){const o=n.pop();if(!o||t.has(o))continue;t.add(o);const i=o.$ref;if(typeof i=="string"){if(r){const d=r[i];if(!d){const p=Object.keys(r).join(", ")||"(none)";throw new Error(`[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${p}`)}n.push(d)}continue}const c=o.oneOf;if(Array.isArray(c))for(const d of c)typeof d=="object"&&d!==null&&n.push(d);const s=o.properties;if(s)for(const d of Object.values(s))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 a=o.additionalProperties;typeof a=="object"&&a!==null&&n.push(a);const f=o.propertyNames;typeof f=="object"&&f!==null&&n.push(f)}}function W(e){return{encode(r,t,n){const o=L(e,n);return m(r,t,o)},decode(r,t,n){const o=L(e,n);return l(r,t,o)}}}function X(e,r,t){try{const n=t();e.register(n)}catch(n){if(q(n))return;throw n}}function q(e){return e===null||typeof e!="object"?!1:e.code==="MODULE_NOT_FOUND"}const A={id:"date-time",kind:"scalar",schema:{type:"string",format:"date-time"},matches(e){return e.type==="string"&&e.format==="date-time"},encode(e,r,t){return e.toISOString()},decode(e,r,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)}},h={id:"int64",kind:"scalar",schema:{type:"string",format:"int64"},matches(e){return e.type==="string"&&e.format==="int64"},encode(e,r,t){return String(e)},decode(e,r,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 V(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,r,t){return e},decode(e,r,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}},$={id:"byte",kind:"scalar",schema:{type:"string",format:"byte"},matches(e){return e.type==="string"&&e.format==="byte"},encode(e,r,t){return Buffer.from(e).toString("base64")},decode(e,r,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}$/,I={id:"uuid",kind:"scalar",schema:{type:"string",format:"uuid"},matches(e){return e.type==="string"&&e.format==="uuid"},encode(e,r,t){return e.toLowerCase()},decode(e,r,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}},D="NaN",b="Infinity",S="-Infinity",H=new Set([D,b,S]),T={id:"number-special",kind:"scalar",schema:{type:"number"},matches(e){return e.type==="number"&&e.format===void 0},encode(e,r,t){return Number.isNaN(e)?D:e===1/0?b:e===-1/0?S:e},decode(e,r,t){if(typeof e=="number")return e;if(typeof e=="string"){if(e===D)return NaN;if(e===b)return 1/0;if(e===S)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 ${[...H].join(", ")}.`);return NaN}},Q=[A,h,N,$,I,T];function x(e,r={}){for(const t of Q)e.register(t,{override:r.override??!1})}const ee=12;function te(e,r){const t=/^#\/(definitions|\$defs)\/(.+)$/.exec(e);if(!t)return;const[,n,o]=t,i=n==="$defs"?r.$defs:r.definitions;return i==null?void 0:i[decodeURIComponent(o)]}function F(e){return Array.isArray(e)&&e.length>0?e[0]:void 0}function re(e){if(Array.isArray(e.type))return e.type.find(r=>r!=="null")??e.type[0];if(typeof e.type=="string")return e.type;if(e.properties)return"object"}function ne(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 U(e,r,t){const n=e.properties??{},o=e.required??[],i={};for(const c of o){const s=n[c];i[c]=s!==void 0?y(s,r,t+1):`<${c}>`}return i}function oe(e,r,t){const n=Array.isArray(e.items)?e.items[0]:e.items;return n?[y(n,r,t+1)]:[]}function y(e,r=e??{},t=0){if(!e||t>ee)return null;if(typeof e.$ref=="string"){const i=te(e.$ref,r);return i!==void 0?y(i,r,t+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={},c=[];for(const s of e.allOf){Object.assign(i,s.properties??{});for(const u of s.required??[])c.includes(u)||c.push(u)}return U({...e,properties:i,required:c},r,t)}const n=F(e.oneOf)??F(e.anyOf);if(n)return y(n,r,t+1);switch(re(e)){case"object":return U(e,r,t);case"array":return oe(e,r,t);case"string":return ne(e);case"integer":case"number":return 0;case"boolean":return!1;case"null":return null;default:return null}}function ie(e){if(!e)return;const r=y(e);return`Example: ${JSON.stringify(r)}`}const v=[A.id,h.id,N.id,$.id,I.id,T.id],se={"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"}},ce={"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"}},de={"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"}},ae={"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"}},fe={"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"}},E=Object.freeze({typescript:se,python:ce,rust:de,go:ae,java:fe});function ue(e){return E[e]}function pe(e,r){const t=E[r],n=[];for(const o of e){const i=t[o];i!=null&&i.dep&&n.push({id:o,dep:i.dep})}return n}function _e(){const e=E.typescript,r={};for(const t of v){const n=e[t].dep;n&&(r[t]=n)}return Object.freeze(r)}function me(e,r){const t=r??E[e],n=[];for(const o of v)t[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=v;exports.CodecRegistryError=C;exports.ENVELOPE_KEY=g;exports.LOGICAL_TYPE_VERSION=w;exports.TEMPLATE_CELLS=E;exports.X_APIGEN_CODEC=R;exports.X_APIGEN_CTOR=M;exports.X_APIGEN_LOGICAL=j;exports.X_APIGEN_TOJSON=k;exports.assertNoEmptyCells=me;exports.buildTranscoder=W;exports.byteCodec=$;exports.cellsFor=ue;exports.codecIdOf=J;exports.createRegistry=G;exports.dateTimeCodec=A;exports.decimalCodec=N;exports.depsForLogicalTypes=pe;exports.int64Codec=h;exports.logicalKindOf=B;exports.makeDecimal=V;exports.numberSpecialCodec=T;exports.registerWellKnown=x;exports.renderExampleNote=ie;exports.synthesizeExample=y;exports.tryRegister=X;exports.tsDepMap=_e;exports.uuidCodec=I;exports.validateSchemaRefs=K;
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;
package/index.mjs CHANGED
@@ -1,42 +1,42 @@
1
1
  const D = "$apigen";
2
- class C extends Error {
3
- constructor(r) {
4
- super(r), this.code = "E_DUP_CODEC", this.name = "CodecRegistryError";
2
+ class N extends Error {
3
+ constructor(t) {
4
+ super(t), this.code = "E_DUP_CODEC", this.name = "CodecRegistryError";
5
5
  }
6
6
  }
7
- function x(e) {
7
+ function de(e) {
8
8
  e == null || e.wellKnown;
9
- const r = /* @__PURE__ */ new Map();
9
+ const t = /* @__PURE__ */ new Map();
10
10
  return {
11
11
  register(n, o) {
12
- if (r.has(n.id) && !(o != null && o.override))
13
- throw new C(
12
+ if (t.has(n.id) && !(o != null && o.override))
13
+ throw new N(
14
14
  `E_DUP_CODEC: a codec is already registered for id "${n.id}"`
15
15
  );
16
- r.set(n.id, n);
16
+ t.set(n.id, n);
17
17
  },
18
18
  resolve(n) {
19
- for (const o of r.values())
19
+ for (const o of t.values())
20
20
  if (o.matches(n))
21
21
  return o;
22
22
  },
23
23
  get(n) {
24
- return r.get(n);
24
+ return t.get(n);
25
25
  },
26
26
  ids() {
27
- return [...r.keys()];
27
+ return [...t.keys()];
28
28
  },
29
29
  freeze() {
30
- const n = new Map(r), o = {
30
+ const n = new Map(t), o = {
31
31
  register() {
32
- throw new C(
32
+ throw new N(
33
33
  "E_DUP_CODEC: registry is frozen and cannot accept new codecs"
34
34
  );
35
35
  },
36
36
  resolve(i) {
37
- for (const c of n.values())
38
- if (c.matches(i))
39
- return c;
37
+ for (const a of n.values())
38
+ if (a.matches(i))
39
+ return a;
40
40
  },
41
41
  get(i) {
42
42
  return n.get(i);
@@ -52,190 +52,316 @@ function x(e) {
52
52
  }
53
53
  };
54
54
  }
55
- const R = "x-apigen-logical", P = "x-apigen-codec", ee = "x-apigen-ctor", te = "x-apigen-tojson", re = "0.1.0", M = [
55
+ const z = "x-apigen-logical", J = "x-apigen-codec", ae = "x-apigen-ctor", fe = "x-apigen-tojson", ue = "0.1.0", V = [
56
56
  "scalar",
57
57
  "nominal",
58
58
  "union",
59
59
  "map",
60
60
  "set"
61
61
  ];
62
- function ne(e) {
63
- const r = e[R];
64
- return typeof r == "string" && M.includes(r) ? r : void 0;
62
+ function pe(e) {
63
+ const t = e[z];
64
+ return typeof t == "string" && V.includes(t) ? t : void 0;
65
65
  }
66
- function oe(e) {
67
- const r = e[P];
68
- return typeof r == "string" ? r : void 0;
66
+ function me(e) {
67
+ const t = e[J];
68
+ return typeof t == "string" ? t : void 0;
69
69
  }
70
- function h(e, r) {
70
+ function v(e, t) {
71
71
  return {
72
72
  registry: e,
73
- resolve: (t) => {
73
+ resolve: (r) => {
74
74
  throw new Error(
75
- `[apigen-logical] $ref "${t}" cannot be resolved in run-mode without a descriptor root. Supply a resolve() in the ctx override to handle $ref.`
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
76
  );
77
77
  },
78
78
  seen: /* @__PURE__ */ new WeakSet(),
79
79
  path: "",
80
80
  mode: "strict",
81
- ...r
81
+ ...t
82
82
  };
83
83
  }
84
- function _(e, r, t) {
85
- const n = t.registry.resolve(r);
84
+ function l(e, t, r) {
85
+ const n = r.registry.resolve(t);
86
86
  if (n)
87
- return n.encode(e, r, t);
88
- const o = r.$ref;
87
+ return n.encode(e, t, r);
88
+ const o = t.$ref;
89
89
  if (typeof o == "string") {
90
- const s = t.resolve(o);
91
- return _(e, s, t);
90
+ const c = r.resolve(o);
91
+ return l(e, c, r);
92
92
  }
93
- const i = r.oneOf;
93
+ const i = t.oneOf;
94
94
  if (Array.isArray(i)) {
95
- const s = N(e, i, r);
96
- return _(e, s, t);
95
+ const c = L(e, i, t, r);
96
+ return l(e, c, r);
97
97
  }
98
- const c = r.type;
99
- if (c === "array") {
98
+ const a = t.type;
99
+ if (a === "array") {
100
100
  if (!Array.isArray(e))
101
- return E(e, r, t);
102
- const s = r.items;
103
- if (!s)
104
- return e.map((a) => m(a));
105
- const u = t.path;
106
- return Array.isArray(s) ? e.map((a, f) => {
107
- const d = s[f];
108
- return d === void 0 ? m(a) : _(a, d, { ...t, path: `${u}/${f}` });
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
109
  }) : e.map(
110
- (a, f) => _(a, s, { ...t, path: `${u}/${f}` })
110
+ (f, s) => l(f, c, { ...r, path: `${u}/${s}` })
111
111
  );
112
112
  }
113
- if (c === "object") {
113
+ if (a === "object") {
114
114
  if (e === null || typeof e != "object" || Array.isArray(e))
115
- return E(e, r, t);
116
- const s = r.properties;
117
- if (!s)
118
- return m(e);
119
- const u = t.path, a = {};
120
- for (const [f, d] of Object.entries(s)) {
121
- const p = e[f];
122
- p !== void 0 && (a[f] = _(p, d, {
123
- ...t,
124
- path: `${u}/${f}`
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
125
  }));
126
126
  }
127
- return a;
127
+ return f;
128
128
  }
129
- return c == null ? E(e, r, t) : m(e);
129
+ return a == null ? b(e, t, r) : _(e);
130
130
  }
131
- function l(e, r, t) {
132
- const n = t.registry.resolve(r);
131
+ function y(e, t, r) {
132
+ const n = r.registry.resolve(t);
133
133
  if (n)
134
- return n.decode(e, r, t);
135
- const o = r.$ref;
134
+ return n.decode(e, t, r);
135
+ const o = t.$ref;
136
136
  if (typeof o == "string") {
137
- const s = t.resolve(o);
138
- return l(e, s, t);
137
+ const c = r.resolve(o);
138
+ return y(e, c, r);
139
139
  }
140
- const i = r.oneOf;
140
+ const i = t.oneOf;
141
141
  if (Array.isArray(i)) {
142
- const s = N(e, i, r);
143
- return l(e, s, t);
142
+ const c = L(e, i, t, r);
143
+ return y(e, c, r);
144
144
  }
145
- const c = r.type;
146
- if (c === "array") {
145
+ const a = t.type;
146
+ if (a === "array") {
147
147
  if (!Array.isArray(e))
148
148
  return e;
149
- const s = r.items;
150
- if (!s)
149
+ const c = t.items;
150
+ if (!c)
151
151
  return e;
152
- const u = t.path;
153
- return Array.isArray(s) ? e.map((a, f) => {
154
- const d = s[f];
155
- return d === void 0 ? a : l(a, d, { ...t, path: `${u}/${f}` });
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
156
  }) : e.map(
157
- (a, f) => l(a, s, { ...t, path: `${u}/${f}` })
157
+ (f, s) => y(f, c, { ...r, path: `${u}/${s}` })
158
158
  );
159
159
  }
160
- if (c === "object") {
160
+ if (a === "object") {
161
161
  if (e === null || typeof e != "object" || Array.isArray(e))
162
162
  return e;
163
- const s = r.properties;
164
- if (!s)
163
+ const c = t.properties;
164
+ if (!c)
165
165
  return e;
166
- const u = t.path, a = {};
167
- for (const [f, d] of Object.entries(s)) {
168
- const p = e[f];
169
- p !== void 0 && (a[f] = l(p, d, {
170
- ...t,
171
- path: `${u}/${f}`
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
172
  }));
173
173
  }
174
- return a;
174
+ for (const [s, d] of Object.entries(e))
175
+ !(s in c) && d !== void 0 && (f[s] = d);
176
+ return f;
175
177
  }
176
- return c == null ? k(e, t) : e;
178
+ return a == null ? S(e, r) : e;
177
179
  }
178
- function N(e, r, t, n) {
179
- const o = t.discriminator;
180
+ function L(e, t, r, n) {
181
+ var u;
182
+ const o = r.discriminator;
180
183
  if (o != null && o.propertyName) {
181
- const i = e !== null && typeof e == "object" ? e[o.propertyName] : void 0;
182
- if (typeof i == "string" && o.mapping) {
183
- const c = o.mapping[i];
184
- if (c) {
185
- const s = r.find((u) => u.$ref === c);
186
- if (s)
187
- return s;
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;
188
201
  }
189
202
  }
190
203
  }
191
- return r[0] ?? {};
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] ?? {};
192
213
  }
193
- function E(e, r, t) {
194
- for (const n of t.registry.ids()) {
195
- const o = t.registry.get(n);
196
- if (o)
197
- try {
198
- const i = o.encode(e, o.schema, t);
199
- return { [D]: n, v: i };
200
- } catch {
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;
201
286
  }
202
287
  }
203
- return m(e);
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;
204
322
  }
205
- function k(e, r) {
323
+ function S(e, t) {
206
324
  if (e !== null && typeof e == "object" && !Array.isArray(e) && D in e) {
207
- const t = e, n = t[D], o = r.registry.get(n);
325
+ const r = e, n = r[D], o = t.registry.get(n);
208
326
  if (o)
209
- return o.decode(t.v, o.schema, r);
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;
210
336
  }
211
337
  return e;
212
338
  }
213
- function m(e) {
339
+ function _(e) {
214
340
  if (e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean")
215
341
  return e;
216
342
  if (Array.isArray(e))
217
- return e.map(m);
343
+ return e.map(_);
218
344
  if (typeof e == "object") {
219
- const r = {};
220
- for (const [t, n] of Object.entries(e))
221
- n !== void 0 && (r[t] = m(n));
222
- return r;
345
+ const t = {};
346
+ for (const [r, n] of Object.entries(e))
347
+ n !== void 0 && (t[r] = _(n));
348
+ return t;
223
349
  }
224
350
  return null;
225
351
  }
226
- function ie(e, r) {
227
- const t = /* @__PURE__ */ new Set(), n = [e];
352
+ function _e(e, t) {
353
+ const r = /* @__PURE__ */ new Set(), n = [e];
228
354
  for (; n.length > 0; ) {
229
355
  const o = n.pop();
230
- if (!o || t.has(o))
356
+ if (!o || r.has(o))
231
357
  continue;
232
- t.add(o);
358
+ r.add(o);
233
359
  const i = o.$ref;
234
360
  if (typeof i == "string") {
235
- if (r) {
236
- const d = r[i];
361
+ if (t) {
362
+ const d = t[i];
237
363
  if (!d) {
238
- const p = Object.keys(r).join(", ") || "(none)";
364
+ const p = Object.keys(t).join(", ") || "(none)";
239
365
  throw new Error(
240
366
  `[apigen-logical] $ref "${i}" cannot be resolved. Available $defs: ${p}`
241
367
  );
@@ -244,13 +370,13 @@ function ie(e, r) {
244
370
  }
245
371
  continue;
246
372
  }
247
- const c = o.oneOf;
248
- if (Array.isArray(c))
249
- for (const d of c)
373
+ const a = o.oneOf;
374
+ if (Array.isArray(a))
375
+ for (const d of a)
250
376
  typeof d == "object" && d !== null && n.push(d);
251
- const s = o.properties;
252
- if (s)
253
- for (const d of Object.values(s))
377
+ const c = o.properties;
378
+ if (c)
379
+ for (const d of Object.values(c))
254
380
  n.push(d);
255
381
  const u = o.items;
256
382
  if (Array.isArray(u))
@@ -258,76 +384,82 @@ function ie(e, r) {
258
384
  typeof d == "object" && d !== null && n.push(d);
259
385
  else
260
386
  typeof u == "object" && u !== null && n.push(u);
261
- const a = o.additionalProperties;
262
- typeof a == "object" && a !== null && n.push(a);
263
- const f = o.propertyNames;
387
+ const f = o.additionalProperties;
264
388
  typeof f == "object" && f !== null && n.push(f);
389
+ const s = o.propertyNames;
390
+ typeof s == "object" && s !== null && n.push(s);
265
391
  }
266
392
  }
267
- function se(e) {
393
+ function le(e) {
268
394
  return {
269
- encode(r, t, n) {
270
- const o = h(e, n);
271
- return _(r, t, o);
395
+ encode(t, r, n) {
396
+ const o = v(e, n);
397
+ return l(t, r, o);
272
398
  },
273
- decode(r, t, n) {
274
- const o = h(e, n);
275
- return l(r, t, o);
399
+ decode(t, r, n) {
400
+ const o = v(e, n);
401
+ return y(t, r, o);
276
402
  }
277
403
  };
278
404
  }
279
- function ce(e, r, t) {
405
+ function ye(e, t, r) {
280
406
  try {
281
- const n = t();
407
+ const n = r();
282
408
  e.register(n);
283
409
  } catch (n) {
284
- if (G(n))
410
+ if (q(n))
285
411
  return;
286
412
  throw n;
287
413
  }
288
414
  }
289
- function G(e) {
415
+ function q(e) {
290
416
  return e === null || typeof e != "object" ? !1 : e.code === "MODULE_NOT_FOUND";
291
417
  }
292
- const v = {
418
+ const k = {
293
419
  id: "date-time",
294
420
  kind: "scalar",
295
421
  schema: { type: "string", format: "date-time" },
296
422
  matches(e) {
297
423
  return e.type === "string" && e.format === "date-time";
298
424
  },
299
- encode(e, r, t) {
425
+ ownsValue(e) {
426
+ return e instanceof Date;
427
+ },
428
+ encode(e, t, r) {
300
429
  return e.toISOString();
301
430
  },
302
- decode(e, r, t) {
431
+ decode(e, t, r) {
303
432
  if (typeof e != "string") {
304
- if (t.mode === "strict")
433
+ if (r.mode === "strict")
305
434
  throw new TypeError(
306
- `[date-time] expected a string on the wire at "${t.path}", got ${typeof e}`
435
+ `[date-time] expected a string on the wire at "${r.path}", got ${typeof e}`
307
436
  );
308
437
  return new Date(String(e));
309
438
  }
310
- if (t.mode === "strict" && Number.isNaN(new Date(e).getTime()))
439
+ if (r.mode === "strict" && Number.isNaN(new Date(e).getTime()))
311
440
  throw new TypeError(
312
- `[date-time] invalid date-time string at "${t.path}": ${JSON.stringify(e)}`
441
+ `[date-time] invalid date-time string at "${r.path}": ${JSON.stringify(e)}`
313
442
  );
314
443
  return new Date(e);
315
444
  }
316
- }, I = {
445
+ }, R = {
317
446
  id: "int64",
318
447
  kind: "scalar",
319
448
  schema: { type: "string", format: "int64" },
320
449
  matches(e) {
321
450
  return e.type === "string" && e.format === "int64";
322
451
  },
323
- encode(e, r, t) {
452
+ ownsValue(e) {
453
+ return typeof e == "bigint";
454
+ },
455
+ encode(e, t, r) {
324
456
  return String(e);
325
457
  },
326
- decode(e, r, t) {
458
+ decode(e, t, r) {
327
459
  if (typeof e != "string") {
328
- if (t.mode === "strict")
460
+ if (r.mode === "strict")
329
461
  throw new TypeError(
330
- `[int64] expected a string on the wire at "${t.path}", got ${typeof e}`
462
+ `[int64] expected a string on the wire at "${r.path}", got ${typeof e}`
331
463
  );
332
464
  try {
333
465
  return BigInt(Math.trunc(Number(e)));
@@ -335,7 +467,7 @@ const v = {
335
467
  return BigInt(0);
336
468
  }
337
469
  }
338
- if (t.mode === "strict")
470
+ if (r.mode === "strict")
339
471
  return BigInt(e);
340
472
  if (/^-?\d+$/.test(e))
341
473
  try {
@@ -346,147 +478,153 @@ const v = {
346
478
  return BigInt(0);
347
479
  }
348
480
  };
349
- function de(e) {
481
+ function ge(e) {
350
482
  return e;
351
483
  }
352
- const T = {
484
+ const P = {
353
485
  id: "decimal",
354
486
  kind: "scalar",
355
487
  schema: { type: "string", format: "decimal" },
356
488
  matches(e) {
357
489
  return e.type === "string" && e.format === "decimal";
358
490
  },
359
- encode(e, r, t) {
491
+ encode(e, t, r) {
360
492
  return e;
361
493
  },
362
- decode(e, r, t) {
494
+ decode(e, t, r) {
363
495
  if (typeof e != "string") {
364
- if (t.mode === "strict")
496
+ if (r.mode === "strict")
365
497
  throw new TypeError(
366
- `[decimal] expected a string on the wire at "${t.path}", got ${typeof e}`
498
+ `[decimal] expected a string on the wire at "${r.path}", got ${typeof e}`
367
499
  );
368
500
  return String(e);
369
501
  }
370
- if (t.mode === "strict" && !/^-?\d+(\.\d+)?$/.test(e))
502
+ if (r.mode === "strict" && !/^-?\d+(\.\d+)?$/.test(e))
371
503
  throw new TypeError(
372
- `[decimal] wire value "${e}" at "${t.path}" is not a valid decimal string`
504
+ `[decimal] wire value "${e}" at "${r.path}" is not a valid decimal string`
373
505
  );
374
506
  return e;
375
507
  }
376
- }, F = {
508
+ }, B = {
377
509
  id: "byte",
378
510
  kind: "scalar",
379
511
  schema: { type: "string", format: "byte" },
380
512
  matches(e) {
381
513
  return e.type === "string" && e.format === "byte";
382
514
  },
383
- encode(e, r, t) {
515
+ ownsValue(e) {
516
+ return e instanceof Uint8Array;
517
+ },
518
+ encode(e, t, r) {
384
519
  return Buffer.from(e).toString("base64");
385
520
  },
386
- decode(e, r, t) {
521
+ decode(e, t, r) {
387
522
  if (typeof e != "string") {
388
- if (t.mode === "strict")
523
+ if (r.mode === "strict")
389
524
  throw new TypeError(
390
- `[byte] expected a base64 string on the wire at "${t.path}", got ${typeof e}`
525
+ `[byte] expected a base64 string on the wire at "${r.path}", got ${typeof e}`
391
526
  );
392
527
  return new Uint8Array(0);
393
528
  }
394
- if (t.mode === "strict" && !/^[A-Za-z0-9+/]*={0,2}$/.test(e))
529
+ if (r.mode === "strict" && !/^[A-Za-z0-9+/]*={0,2}$/.test(e))
395
530
  throw new TypeError(
396
- `[byte] wire value at "${t.path}" is not standard base64 (format:byte): "${e}"`
531
+ `[byte] wire value at "${r.path}" is not standard base64 (format:byte): "${e}"`
397
532
  );
398
533
  return new Uint8Array(Buffer.from(e, "base64"));
399
534
  }
400
- }, w = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, L = {
535
+ }, K = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, w = {
401
536
  id: "uuid",
402
537
  kind: "scalar",
403
538
  schema: { type: "string", format: "uuid" },
404
539
  matches(e) {
405
540
  return e.type === "string" && e.format === "uuid";
406
541
  },
407
- encode(e, r, t) {
542
+ encode(e, t, r) {
408
543
  return e.toLowerCase();
409
544
  },
410
- decode(e, r, t) {
545
+ decode(e, t, r) {
411
546
  if (typeof e != "string") {
412
- if (t.mode === "strict")
547
+ if (r.mode === "strict")
413
548
  throw new TypeError(
414
- `[uuid] expected a string on the wire at "${t.path}", got ${typeof e}`
549
+ `[uuid] expected a string on the wire at "${r.path}", got ${typeof e}`
415
550
  );
416
551
  return String(e).toLowerCase();
417
552
  }
418
- if (t.mode === "strict" && !w.test(e))
553
+ if (r.mode === "strict" && !K.test(e))
419
554
  throw new TypeError(
420
- `[uuid] wire value "${e}" at "${t.path}" is not a lowercase-hyphenated RFC 4122 UUID`
555
+ `[uuid] wire value "${e}" at "${r.path}" is not a lowercase-hyphenated RFC 4122 UUID`
421
556
  );
422
557
  return e;
423
558
  }
424
- }, O = "NaN", b = "Infinity", S = "-Infinity", z = /* @__PURE__ */ new Set([
425
- O,
426
- b,
427
- S
428
- ]), U = {
559
+ }, h = "NaN", C = "Infinity", $ = "-Infinity", X = /* @__PURE__ */ new Set([
560
+ h,
561
+ C,
562
+ $
563
+ ]), M = {
429
564
  id: "number-special",
430
565
  kind: "scalar",
431
566
  schema: { type: "number" },
432
567
  matches(e) {
433
568
  return e.type === "number" && e.format === void 0;
434
569
  },
435
- encode(e, r, t) {
436
- return Number.isNaN(e) ? O : e === 1 / 0 ? b : e === -1 / 0 ? S : e;
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;
437
575
  },
438
- decode(e, r, t) {
576
+ decode(e, t, r) {
439
577
  if (typeof e == "number")
440
578
  return e;
441
579
  if (typeof e == "string") {
442
- if (e === O)
580
+ if (e === h)
443
581
  return NaN;
444
- if (e === b)
582
+ if (e === C)
445
583
  return 1 / 0;
446
- if (e === S)
584
+ if (e === $)
447
585
  return -1 / 0;
448
586
  const n = Number(e);
449
587
  if (!Number.isNaN(n))
450
588
  return n;
451
589
  }
452
- if (t.mode === "strict")
590
+ if (r.mode === "strict")
453
591
  throw new TypeError(
454
- `[number-special] unrecognized wire value at "${t.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...z].join(", ")}.`
592
+ `[number-special] unrecognized wire value at "${r.path}": ${JSON.stringify(e)}. Expected a number or one of ${[...X].join(", ")}.`
455
593
  );
456
594
  return NaN;
457
595
  }
458
- }, B = [
459
- v,
460
- I,
461
- T,
462
- F,
463
- L,
464
- U
596
+ }, Z = [
597
+ k,
598
+ R,
599
+ P,
600
+ B,
601
+ w,
602
+ M
465
603
  ];
466
- function ae(e, r = {}) {
467
- for (const t of B)
468
- e.register(t, { override: r.override ?? !1 });
469
- }
470
- const J = 12;
471
- function Y(e, r) {
472
- const t = /^#\/(definitions|\$defs)\/(.+)$/.exec(e);
473
- if (!t)
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)
474
612
  return;
475
- const [, n, o] = t, i = n === "$defs" ? r.$defs : r.definitions;
613
+ const [, n, o] = r, i = n === "$defs" ? t.$defs : t.definitions;
476
614
  return i == null ? void 0 : i[decodeURIComponent(o)];
477
615
  }
478
- function A(e) {
616
+ function T(e) {
479
617
  return Array.isArray(e) && e.length > 0 ? e[0] : void 0;
480
618
  }
481
- function W(e) {
619
+ function x(e) {
482
620
  if (Array.isArray(e.type))
483
- return e.type.find((r) => r !== "null") ?? e.type[0];
621
+ return e.type.find((t) => t !== "null") ?? e.type[0];
484
622
  if (typeof e.type == "string")
485
623
  return e.type;
486
624
  if (e.properties)
487
625
  return "object";
488
626
  }
489
- function K(e) {
627
+ function ee(e) {
490
628
  switch (e.format) {
491
629
  case "date-time":
492
630
  return "1970-01-01T00:00:00.000Z";
@@ -518,52 +656,52 @@ function K(e) {
518
656
  return "<string>";
519
657
  }
520
658
  }
521
- function $(e, r, t) {
659
+ function F(e, t, r) {
522
660
  const n = e.properties ?? {}, o = e.required ?? [], i = {};
523
- for (const c of o) {
524
- const s = n[c];
525
- i[c] = s !== void 0 ? y(s, r, t + 1) : `<${c}>`;
661
+ for (const a of o) {
662
+ const c = n[a];
663
+ i[a] = c !== void 0 ? E(c, t, r + 1) : `<${a}>`;
526
664
  }
527
665
  return i;
528
666
  }
529
- function q(e, r, t) {
667
+ function te(e, t, r) {
530
668
  const n = Array.isArray(e.items) ? e.items[0] : e.items;
531
- return n ? [y(n, r, t + 1)] : [];
669
+ return n ? [E(n, t, r + 1)] : [];
532
670
  }
533
- function y(e, r = e ?? {}, t = 0) {
534
- if (!e || t > J)
671
+ function E(e, t = e ?? {}, r = 0) {
672
+ if (!e || r > H)
535
673
  return null;
536
674
  if (typeof e.$ref == "string") {
537
- const i = Y(e.$ref, r);
538
- return i !== void 0 ? y(i, r, t + 1) : null;
675
+ const i = Q(e.$ref, t);
676
+ return i !== void 0 ? E(i, t, r + 1) : null;
539
677
  }
540
678
  if (e.const !== void 0)
541
679
  return e.const;
542
680
  if (Array.isArray(e.enum) && e.enum.length > 0)
543
681
  return e.enum[0];
544
682
  if (Array.isArray(e.allOf) && e.allOf.length > 0) {
545
- const i = {}, c = [];
546
- for (const s of e.allOf) {
547
- Object.assign(i, s.properties ?? {});
548
- for (const u of s.required ?? [])
549
- c.includes(u) || c.push(u);
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);
550
688
  }
551
- return $(
552
- { ...e, properties: i, required: c },
553
- r,
554
- t
689
+ return F(
690
+ { ...e, properties: i, required: a },
691
+ t,
692
+ r
555
693
  );
556
694
  }
557
- const n = A(e.oneOf) ?? A(e.anyOf);
695
+ const n = T(e.oneOf) ?? T(e.anyOf);
558
696
  if (n)
559
- return y(n, r, t + 1);
560
- switch (W(e)) {
697
+ return E(n, t, r + 1);
698
+ switch (x(e)) {
561
699
  case "object":
562
- return $(e, r, t);
700
+ return F(e, t, r);
563
701
  case "array":
564
- return q(e, r, t);
702
+ return te(e, t, r);
565
703
  case "string":
566
- return K(e);
704
+ return ee(e);
567
705
  case "integer":
568
706
  case "number":
569
707
  return 0;
@@ -575,26 +713,26 @@ function y(e, r = e ?? {}, t = 0) {
575
713
  return null;
576
714
  }
577
715
  }
578
- function fe(e) {
716
+ function Ee(e) {
579
717
  if (!e)
580
718
  return;
581
- const r = y(e);
582
- return `Example: ${JSON.stringify(r)}`;
719
+ const t = E(e);
720
+ return `Example: ${JSON.stringify(t)}`;
583
721
  }
584
- const j = [
585
- v.id,
722
+ const G = [
723
+ k.id,
586
724
  // 'date-time'
587
- I.id,
725
+ R.id,
588
726
  // 'int64'
589
- T.id,
727
+ P.id,
590
728
  // 'decimal'
591
- F.id,
729
+ B.id,
592
730
  // 'byte'
593
- L.id,
731
+ w.id,
594
732
  // 'uuid'
595
- U.id
733
+ M.id
596
734
  // 'number-special'
597
- ], X = {
735
+ ], re = {
598
736
  "date-time": {
599
737
  encode: "$.toISOString()",
600
738
  decode: "new Date($)",
@@ -633,7 +771,7 @@ const j = [
633
771
  decode: "wireToNum($)",
634
772
  mode: "native"
635
773
  }
636
- }, V = {
774
+ }, ne = {
637
775
  "date-time": {
638
776
  encode: "$.isoformat()",
639
777
  decode: "datetime.fromisoformat($)",
@@ -670,7 +808,7 @@ const j = [
670
808
  decode: "wire_to_num($)",
671
809
  mode: "native"
672
810
  }
673
- }, Z = {
811
+ }, oe = {
674
812
  "date-time": {
675
813
  // chrono::DateTime<Utc> serialises as RFC3339 via serde.
676
814
  encode: "__SCAFFOLD_RUST_DATETIME_ENCODE__",
@@ -714,7 +852,7 @@ const j = [
714
852
  decode: "__SCAFFOLD_RUST_NUMSPECIAL_DECODE__",
715
853
  mode: "native"
716
854
  }
717
- }, H = {
855
+ }, ie = {
718
856
  "date-time": {
719
857
  // time.Time serialises as RFC3339Nano via MarshalJSON.
720
858
  encode: "__SCAFFOLD_GO_DATETIME_ENCODE__",
@@ -755,7 +893,7 @@ const j = [
755
893
  decode: "__SCAFFOLD_GO_NUMSPECIAL_DECODE__",
756
894
  mode: "native"
757
895
  }
758
- }, Q = {
896
+ }, se = {
759
897
  "date-time": {
760
898
  // Instant.toString() (RFC-3339/ISO-8601, UTC) / Instant.parse($) — the
761
899
  // jackson-datatype-jsr310 module is registered on the shared ObjectMapper
@@ -823,36 +961,36 @@ const j = [
823
961
  ],
824
962
  mode: "native"
825
963
  }
826
- }, g = Object.freeze({
827
- typescript: X,
828
- python: V,
829
- rust: Z,
830
- go: H,
831
- java: Q
964
+ }, A = Object.freeze({
965
+ typescript: re,
966
+ python: ne,
967
+ rust: oe,
968
+ go: ie,
969
+ java: se
832
970
  });
833
- function ue(e) {
834
- return g[e];
971
+ function Oe(e) {
972
+ return A[e];
835
973
  }
836
- function pe(e, r) {
837
- const t = g[r], n = [];
974
+ function Ae(e, t) {
975
+ const r = A[t], n = [];
838
976
  for (const o of e) {
839
- const i = t[o];
977
+ const i = r[o];
840
978
  i != null && i.dep && n.push({ id: o, dep: i.dep });
841
979
  }
842
980
  return n;
843
981
  }
844
- function me() {
845
- const e = g.typescript, r = {};
846
- for (const t of j) {
847
- const n = e[t].dep;
848
- n && (r[t] = n);
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);
849
987
  }
850
- return Object.freeze(r);
988
+ return Object.freeze(t);
851
989
  }
852
- function _e(e, r) {
853
- const t = r ?? g[e], n = [];
854
- for (const o of j)
855
- t[o] || n.push(o);
990
+ function Se(e, t) {
991
+ const r = t ?? A[e], n = [];
992
+ for (const o of G)
993
+ r[o] || n.push(o);
856
994
  if (n.length > 0)
857
995
  throw new Error(
858
996
  `[hints] assertNoEmptyCells: language "${e}" is missing cells for: ${n.join(
@@ -861,33 +999,33 @@ function _e(e, r) {
861
999
  );
862
1000
  }
863
1001
  export {
864
- j as CANONICAL_LOGICAL_TYPE_IDS,
865
- C as CodecRegistryError,
1002
+ G as CANONICAL_LOGICAL_TYPE_IDS,
1003
+ N as CodecRegistryError,
866
1004
  D as ENVELOPE_KEY,
867
- re as LOGICAL_TYPE_VERSION,
868
- g as TEMPLATE_CELLS,
869
- P as X_APIGEN_CODEC,
870
- ee as X_APIGEN_CTOR,
871
- R as X_APIGEN_LOGICAL,
872
- te as X_APIGEN_TOJSON,
873
- _e as assertNoEmptyCells,
874
- se as buildTranscoder,
875
- F as byteCodec,
876
- ue as cellsFor,
877
- oe as codecIdOf,
878
- x as createRegistry,
879
- v as dateTimeCodec,
880
- T as decimalCodec,
881
- pe as depsForLogicalTypes,
882
- I as int64Codec,
883
- ne as logicalKindOf,
884
- de as makeDecimal,
885
- U as numberSpecialCodec,
886
- ae as registerWellKnown,
887
- fe as renderExampleNote,
888
- y as synthesizeExample,
889
- ce as tryRegister,
890
- me as tsDepMap,
891
- L as uuidCodec,
892
- ie as validateSchemaRefs
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
893
1031
  };
@@ -28,6 +28,25 @@ export interface LogicalTypeCodec<Host = unknown> {
28
28
  readonly schema: SchemaNode;
29
29
  /** Structural, cheap test: does this codec own `node`? (format match, or x-apigen-codec===id). */
30
30
  matches(node: SchemaNode): boolean;
31
+ /**
32
+ * Value-side claim test, used ONLY on the schemaless path (a `{}` / `any`
33
+ * schema node, where `matches(node)` cannot decide anything).
34
+ *
35
+ * Implement this on — and only on — codecs whose HOST type is not
36
+ * JSON-native (`Date`, `bigint`, `Uint8Array`, non-finite `number`). Those
37
+ * are the values that genuinely need the `{$apigen,v}` envelope to survive
38
+ * a JSON round-trip. A codec whose host type IS JSON-native (`uuid` and
39
+ * `decimal` are both branded strings) must NOT implement it: a plain string
40
+ * already round-trips, and claiming it would rewrite a consumer's payload
41
+ * for no gain.
42
+ *
43
+ * A codec that omits `ownsValue` is never eligible for schemaless claiming.
44
+ * That is the safe default — passthrough loses nothing, whereas a wrong
45
+ * claim silently destroys data.
46
+ *
47
+ * MUST be pure and side-effect free.
48
+ */
49
+ ownsValue?(value: unknown): boolean;
31
50
  /** Host -> wire. MUST NOT mutate `value`; MUST be deterministic. */
32
51
  encode(value: Host, node: SchemaNode, ctx: TranscodeCtx): Wire;
33
52
  /** Wire -> host. MUST validate-then-construct; MUST be the inverse of `encode` across the vectors. */
package/package.json CHANGED
@@ -1,7 +1,25 @@
1
1
  {
2
2
  "name": "@adhd/apigen-base-logical",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "main": "./index.js",
5
5
  "module": "./index.mjs",
6
- "typings": "./index.d.ts"
6
+ "typings": "./index.d.ts",
7
+ "description": "Logical-type transcoding contracts for apigen",
8
+ "keywords": [
9
+ "api",
10
+ "codegen",
11
+ "logical-types",
12
+ "schema",
13
+ "typescript"
14
+ ],
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/PseudoSky/adhd.git",
19
+ "directory": "packages/apigen/apigen-base-logical"
20
+ },
21
+ "homepage": "https://github.com/PseudoSky/adhd/tree/main/packages/apigen/apigen-base-logical#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/PseudoSky/adhd/issues"
24
+ }
7
25
  }