@kubb/ast 5.0.0-beta.80 → 5.0.0-beta.82
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/dist/index.cjs +2280 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +420 -18
- package/dist/index.js +2133 -15
- package/dist/index.js.map +1 -1
- package/dist/{index-Cu2zmNxv.d.ts → types-XcYJovdT.d.ts} +717 -2
- package/dist/types.d.ts +1 -3
- package/package.json +1 -12
- package/dist/defineMacro-CqX4m8Dq.js +0 -98
- package/dist/defineMacro-CqX4m8Dq.js.map +0 -1
- package/dist/defineMacro-DmGR7vfu.cjs +0 -114
- package/dist/defineMacro-DmGR7vfu.cjs.map +0 -1
- package/dist/defineMacro-DzsACbFo.d.ts +0 -466
- package/dist/macros.cjs +0 -130
- package/dist/macros.cjs.map +0 -1
- package/dist/macros.d.ts +0 -61
- package/dist/macros.js +0 -128
- package/dist/macros.js.map +0 -1
- package/dist/refs-Bg3rmujP.cjs +0 -135
- package/dist/refs-Bg3rmujP.cjs.map +0 -1
- package/dist/refs-BjBHwunY.js +0 -101
- package/dist/refs-BjBHwunY.js.map +0 -1
- package/dist/types-CWF7DV0f.d.ts +0 -259
- package/dist/utils.cjs +0 -613
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.ts +0 -353
- package/dist/utils.js +0 -589
- package/dist/utils.js.map +0 -1
- package/dist/visitor-Dk0DNLfC.js +0 -1203
- package/dist/visitor-Dk0DNLfC.js.map +0 -1
- package/dist/visitor-h6dEM3vR.cjs +0 -1567
- package/dist/visitor-h6dEM3vR.cjs.map +0 -1
package/dist/macros.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import { Q as narrowSchema, o as createSchema, u as createProperty } from "./visitor-Dk0DNLfC.js";
|
|
3
|
-
import { r as defineMacro } from "./defineMacro-CqX4m8Dq.js";
|
|
4
|
-
import { n as enumPropName } from "./refs-BjBHwunY.js";
|
|
5
|
-
//#region src/macros/macroDiscriminatorEnum.ts
|
|
6
|
-
/**
|
|
7
|
-
* Builds a macro that replaces a discriminator property's schema with a string enum of the given
|
|
8
|
-
* values. Object schemas that lack the property are returned unchanged.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })
|
|
13
|
-
* const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
function macroDiscriminatorEnum({ propertyName, values, enumName }) {
|
|
17
|
-
return defineMacro({
|
|
18
|
-
name: "discriminator-enum",
|
|
19
|
-
schema(node) {
|
|
20
|
-
const objectNode = narrowSchema(node, "object");
|
|
21
|
-
if (!objectNode?.properties?.length) return void 0;
|
|
22
|
-
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return void 0;
|
|
23
|
-
return createSchema({
|
|
24
|
-
...objectNode,
|
|
25
|
-
properties: objectNode.properties.map((prop) => {
|
|
26
|
-
if (prop.name !== propertyName) return prop;
|
|
27
|
-
return createProperty({
|
|
28
|
-
...prop,
|
|
29
|
-
schema: createSchema({
|
|
30
|
-
type: "enum",
|
|
31
|
-
primitive: "string",
|
|
32
|
-
enumValues: values,
|
|
33
|
-
name: enumName,
|
|
34
|
-
readOnly: prop.schema.readOnly,
|
|
35
|
-
writeOnly: prop.schema.writeOnly
|
|
36
|
-
})
|
|
37
|
-
});
|
|
38
|
-
})
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/macros/macroEnumName.ts
|
|
45
|
-
/**
|
|
46
|
-
* Builds a macro that names an inline enum schema from its parent and property name. Boolean enums
|
|
47
|
-
* are left anonymous. Non-enum nodes are returned unchanged.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })
|
|
52
|
-
* const named = applyMacros(propSchema, [macro], { depth: 'shallow' })
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
function macroEnumName({ parentName, propName, enumSuffix }) {
|
|
56
|
-
return defineMacro({
|
|
57
|
-
name: "enum-name",
|
|
58
|
-
schema(node) {
|
|
59
|
-
const enumNode = narrowSchema(node, "enum");
|
|
60
|
-
if (enumNode?.primitive === "boolean") return {
|
|
61
|
-
...node,
|
|
62
|
-
name: null
|
|
63
|
-
};
|
|
64
|
-
if (enumNode) return {
|
|
65
|
-
...node,
|
|
66
|
-
name: enumPropName(parentName, propName, enumSuffix)
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
//#endregion
|
|
72
|
-
//#region src/macros/macroSimplifyUnion.ts
|
|
73
|
-
/**
|
|
74
|
-
* Scalar primitive schema types used for union simplification and type narrowing.
|
|
75
|
-
*/
|
|
76
|
-
const SCALAR_PRIMITIVE_TYPES = /* @__PURE__ */ new Set([
|
|
77
|
-
"string",
|
|
78
|
-
"number",
|
|
79
|
-
"integer",
|
|
80
|
-
"bigint",
|
|
81
|
-
"boolean"
|
|
82
|
-
]);
|
|
83
|
-
function isScalarPrimitive(type) {
|
|
84
|
-
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Filters union members, dropping enum members that a broader scalar primitive already covers.
|
|
88
|
-
*/
|
|
89
|
-
function simplifyUnionMembers(members) {
|
|
90
|
-
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
|
|
91
|
-
if (!scalarPrimitives.size) return members;
|
|
92
|
-
return members.filter((member) => {
|
|
93
|
-
const enumNode = narrowSchema(member, "enum");
|
|
94
|
-
if (!enumNode) return true;
|
|
95
|
-
const primitive = enumNode.primitive;
|
|
96
|
-
if (!primitive) return true;
|
|
97
|
-
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
98
|
-
if (scalarPrimitives.has(primitive)) return false;
|
|
99
|
-
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
100
|
-
return true;
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum
|
|
105
|
-
* sitting next to a plain `string`. Single-value enums are kept.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```ts
|
|
109
|
-
* const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
const macroSimplifyUnion = defineMacro({
|
|
113
|
-
name: "simplify-union",
|
|
114
|
-
schema(node) {
|
|
115
|
-
const unionNode = narrowSchema(node, "union");
|
|
116
|
-
if (!unionNode?.members?.length) return void 0;
|
|
117
|
-
const simplified = simplifyUnionMembers(unionNode.members);
|
|
118
|
-
if (simplified.length === unionNode.members.length) return void 0;
|
|
119
|
-
return {
|
|
120
|
-
...unionNode,
|
|
121
|
-
members: simplified
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
//#endregion
|
|
126
|
-
export { macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion };
|
|
127
|
-
|
|
128
|
-
//# sourceMappingURL=macros.js.map
|
package/dist/macros.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"macros.js","names":[],"sources":["../src/macros/macroDiscriminatorEnum.ts","../src/macros/macroEnumName.ts","../src/macros/macroSimplifyUnion.ts"],"sourcesContent":["import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { createProperty } from '../nodes/property.ts'\nimport { createSchema } from '../nodes/schema.ts'\n\ntype Props = {\n propertyName: string\n values: Array<string>\n enumName?: string\n}\n\n/**\n * Builds a macro that replaces a discriminator property's schema with a string enum of the given\n * values. Object schemas that lack the property are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })\n * const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroDiscriminatorEnum({ propertyName, values, enumName }: Props) {\n return defineMacro({\n name: 'discriminator-enum',\n schema(node) {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) return undefined\n if (!objectNode.properties.some((prop) => prop.name === propertyName)) return undefined\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) return prop\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { enumPropName } from '../utils/refs.ts'\n\ntype Props = {\n parentName: string | null | undefined\n propName: string\n enumSuffix: string\n}\n\n/**\n * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums\n * are left anonymous. Non-enum nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })\n * const named = applyMacros(propSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroEnumName({ parentName, propName, enumSuffix }: Props) {\n return defineMacro({\n name: 'enum-name',\n schema(node) {\n const enumNode = narrowSchema(node, 'enum')\n\n if (enumNode?.primitive === 'boolean') return { ...node, name: null }\n if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }\n\n return undefined\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\n\ntype ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\nfunction isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * Filters union members, dropping enum members that a broader scalar primitive already covers.\n */\nfunction simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n if (!scalarPrimitives.size) return members\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) return true\n\n const primitive = enumNode.primitive\n if (!primitive) return true\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) return true\n\n if (scalarPrimitives.has(primitive)) return false\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) return false\n\n return true\n })\n}\n\n/**\n * Removes union members a broader scalar primitive already covers, such as a multi-value string enum\n * sitting next to a plain `string`. Single-value enums are kept.\n *\n * @example\n * ```ts\n * const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })\n * ```\n */\nexport const macroSimplifyUnion = defineMacro({\n name: 'simplify-union',\n schema(node) {\n const unionNode = narrowSchema(node, 'union')\n if (!unionNode?.members?.length) return undefined\n\n const simplified = simplifyUnionMembers(unionNode.members)\n if (simplified.length === unionNode.members.length) return undefined\n\n return { ...unionNode, members: simplified }\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;AAqBA,SAAgB,uBAAuB,EAAE,cAAc,QAAQ,YAAmB;CAChF,OAAO,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,aAAa,aAAa,MAAM,QAAQ;GAC9C,IAAI,CAAC,YAAY,YAAY,QAAQ,OAAO,KAAA;GAC5C,IAAI,CAAC,WAAW,WAAW,MAAM,SAAS,KAAK,SAAS,YAAY,GAAG,OAAO,KAAA;GAE9E,OAAO,aAAa;IAClB,GAAG;IACH,YAAY,WAAW,WAAW,KAAK,SAAS;KAC9C,IAAI,KAAK,SAAS,cAAc,OAAO;KAEvC,OAAO,eAAe;MACpB,GAAG;MACH,QAAQ,aAAa;OACnB,MAAM;OACN,WAAW;OACX,YAAY;OACZ,MAAM;OACN,UAAU,KAAK,OAAO;OACtB,WAAW,KAAK,OAAO;MACzB,CAAC;KACH,CAAC;IACH,CAAC;GACH,CAAC;EACH;CACF,CAAC;AACH;;;;;;;;;;;;;AC7BA,SAAgB,cAAc,EAAE,YAAY,UAAU,cAAqB;CACzE,OAAO,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,WAAW,aAAa,MAAM,MAAM;GAE1C,IAAI,UAAU,cAAc,WAAW,OAAO;IAAE,GAAG;IAAM,MAAM;GAAK;GACpE,IAAI,UAAU,OAAO;IAAE,GAAG;IAAM,MAAM,aAAa,YAAY,UAAU,UAAU;GAAE;EAGvF;CACF,CAAC;AACH;;;;;;ACvBA,MAAM,yCAAyB,IAAI,IAAqB;CAAC;CAAU;CAAU;CAAW;CAAU;AAAS,CAAC;AAE5G,SAAS,kBAAkB,MAAuC;CAChE,OAAO,uBAAuB,IAAI,IAAuB;AAC3D;;;;AAKA,SAAS,qBAAqB,SAA+C;CAC3E,MAAM,mBAAmB,IAAI,IAAI,QAAQ,QAAQ,WAAW,kBAAkB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,IAAI,CAAC;CAC9G,IAAI,CAAC,iBAAiB,MAAM,OAAO;CAEnC,OAAO,QAAQ,QAAQ,WAAW;EAChC,MAAM,WAAW,aAAa,QAAQ,MAAM;EAC5C,IAAI,CAAC,UAAU,OAAO;EAEtB,MAAM,YAAY,SAAS;EAC3B,IAAI,CAAC,WAAW,OAAO;EAGvB,KADuB,SAAS,iBAAiB,UAAU,SAAS,YAAY,UAAU,MACpE,GAAG,OAAO;EAEhC,IAAI,iBAAiB,IAAI,SAAS,GAAG,OAAO;EAC5C,KAAK,cAAc,aAAa,cAAc,cAAc,iBAAiB,IAAI,SAAS,KAAK,iBAAiB,IAAI,QAAQ,IAAI,OAAO;EAEvI,OAAO;CACT,CAAC;AACH;;;;;;;;;;AAWA,MAAa,qBAAqB,YAAY;CAC5C,MAAM;CACN,OAAO,MAAM;EACX,MAAM,YAAY,aAAa,MAAM,OAAO;EAC5C,IAAI,CAAC,WAAW,SAAS,QAAQ,OAAO,KAAA;EAExC,MAAM,aAAa,qBAAqB,UAAU,OAAO;EACzD,IAAI,WAAW,WAAW,UAAU,QAAQ,QAAQ,OAAO,KAAA;EAE3D,OAAO;GAAE,GAAG;GAAW,SAAS;EAAW;CAC7C;AACF,CAAC"}
|
package/dist/refs-Bg3rmujP.cjs
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
const require_visitor = require("./visitor-h6dEM3vR.cjs");
|
|
2
|
-
//#region src/utils/refs.ts
|
|
3
|
-
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
4
|
-
"string",
|
|
5
|
-
"uuid",
|
|
6
|
-
"email",
|
|
7
|
-
"url",
|
|
8
|
-
"datetime"
|
|
9
|
-
]);
|
|
10
|
-
/**
|
|
11
|
-
* Returns the last path segment of a reference string.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* `extractRefName('#/components/schemas/Pet') // 'Pet'`
|
|
15
|
-
*/
|
|
16
|
-
function extractRefName(ref) {
|
|
17
|
-
return ref.split("/").at(-1) ?? ref;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls
|
|
21
|
-
* back to `name` then nested `schema.name`.
|
|
22
|
-
*
|
|
23
|
-
* Returns `null` for non-ref nodes or when no name resolves.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`
|
|
27
|
-
*/
|
|
28
|
-
function resolveRefName(node) {
|
|
29
|
-
if (!node || node.type !== "ref") return null;
|
|
30
|
-
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
|
|
31
|
-
return node.name ?? node.schema?.name ?? null;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Builds a PascalCase child schema name by joining a parent name and property name.
|
|
35
|
-
* Returns `null` when there is no parent to nest under.
|
|
36
|
-
*
|
|
37
|
-
* @example Nested under a parent
|
|
38
|
-
* `childName('Order', 'shipping_address') // 'OrderShippingAddress'`
|
|
39
|
-
*
|
|
40
|
-
* @example No parent
|
|
41
|
-
* `childName(undefined, 'params') // null`
|
|
42
|
-
*/
|
|
43
|
-
function childName(parentName, propName) {
|
|
44
|
-
return parentName ? require_visitor.pascalCase([parentName, propName].join(" ")) : null;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
|
|
48
|
-
* empty parts.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* `enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'`
|
|
52
|
-
*/
|
|
53
|
-
function enumPropName(parentName, propName, enumSuffix) {
|
|
54
|
-
return require_visitor.pascalCase([
|
|
55
|
-
parentName,
|
|
56
|
-
propName,
|
|
57
|
-
enumSuffix
|
|
58
|
-
].filter(Boolean).join(" "));
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
62
|
-
*
|
|
63
|
-
* Every field set on the ref node except `kind`, `type`, `name`, `ref`, and `schema` overrides the
|
|
64
|
-
* same field in the resolved `node.schema` (for example `description`, `nullable`, `readOnly`,
|
|
65
|
-
* `deprecated`). Fields left `undefined` on the ref do not shadow the resolved schema. Non-ref
|
|
66
|
-
* nodes and refs without a resolved `schema` are returned unchanged.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
71
|
-
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
function syncSchemaRef(node) {
|
|
75
|
-
const ref = require_visitor.narrowSchema(node, "ref");
|
|
76
|
-
if (!ref) return node;
|
|
77
|
-
if (!ref.schema) return node;
|
|
78
|
-
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
79
|
-
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
80
|
-
return require_visitor.createSchema({
|
|
81
|
-
...ref.schema,
|
|
82
|
-
...definedOverrides
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Type guard that returns `true` when a schema emits as a plain `string` type.
|
|
87
|
-
*
|
|
88
|
-
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
89
|
-
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
90
|
-
*/
|
|
91
|
-
function isStringType(node) {
|
|
92
|
-
if (plainStringTypes.has(node.type)) return true;
|
|
93
|
-
const temporal = require_visitor.narrowSchema(node, "date") ?? require_visitor.narrowSchema(node, "time");
|
|
94
|
-
if (temporal) return temporal.representation !== "date";
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
//#endregion
|
|
98
|
-
Object.defineProperty(exports, "childName", {
|
|
99
|
-
enumerable: true,
|
|
100
|
-
get: function() {
|
|
101
|
-
return childName;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
Object.defineProperty(exports, "enumPropName", {
|
|
105
|
-
enumerable: true,
|
|
106
|
-
get: function() {
|
|
107
|
-
return enumPropName;
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
Object.defineProperty(exports, "extractRefName", {
|
|
111
|
-
enumerable: true,
|
|
112
|
-
get: function() {
|
|
113
|
-
return extractRefName;
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
Object.defineProperty(exports, "isStringType", {
|
|
117
|
-
enumerable: true,
|
|
118
|
-
get: function() {
|
|
119
|
-
return isStringType;
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
Object.defineProperty(exports, "resolveRefName", {
|
|
123
|
-
enumerable: true,
|
|
124
|
-
get: function() {
|
|
125
|
-
return resolveRefName;
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
Object.defineProperty(exports, "syncSchemaRef", {
|
|
129
|
-
enumerable: true,
|
|
130
|
-
get: function() {
|
|
131
|
-
return syncSchemaRef;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
//# sourceMappingURL=refs-Bg3rmujP.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"refs-Bg3rmujP.cjs","names":["pascalCase","narrowSchema","createSchema"],"sources":["../src/utils/refs.ts"],"sourcesContent":["import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/index.ts'\nimport { createSchema, type SchemaType } from '../nodes/schema.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * `extractRefName('#/components/schemas/Pet') // 'Pet'`\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls\n * back to `name` then nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example Nested under a parent\n * `childName('Order', 'shipping_address') // 'OrderShippingAddress'`\n *\n * @example No parent\n * `childName(undefined, 'params') // null`\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * `enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'`\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Merges a ref node with its resolved schema, giving usage-site fields precedence.\n *\n * Every field set on the ref node except `kind`, `type`, `name`, `ref`, and `schema` overrides the\n * same field in the resolved `node.schema` (for example `description`, `nullable`, `readOnly`,\n * `deprecated`). Fields left `undefined` on the ref do not shadow the resolved schema. Non-ref\n * nodes and refs without a resolved `schema` are returned unchanged.\n *\n * @example\n * ```ts\n * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })\n * const merged = syncSchemaRef(ref) // merges with resolved Pet schema\n * ```\n */\nexport function syncSchemaRef(node: SchemaNode): SchemaNode {\n const ref = narrowSchema(node, 'ref')\n\n if (!ref) return node\n if (!ref.schema) return node\n\n const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref\n\n // Filter out undefined override values so they don't shadow the resolved schema's fields.\n const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))\n\n return createSchema({ ...ref.schema, ...definedOverrides })\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n"],"mappings":";;AAKA,MAAM,mCAAmB,IAAI,IAAgB;CAAC;CAAU;CAAQ;CAAS;CAAO;AAAU,CAAU;;;;;;;AAQpG,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;AAClC;;;;;;;;;;AAWA,SAAgB,eAAe,MAA6C;CAC1E,IAAI,CAAC,QAAQ,KAAK,SAAS,OAAO,OAAO;CACzC,IAAI,KAAK,KAAK,OAAO,eAAe,KAAK,GAAG,KAAK,KAAK,QAAQ,KAAK,QAAQ,QAAQ;CAEnF,OAAO,KAAK,QAAQ,KAAK,QAAQ,QAAQ;AAC3C;;;;;;;;;;;AAYA,SAAgB,UAAU,YAAuC,UAAiC;CAChG,OAAO,aAAaA,gBAAAA,WAAW,CAAC,YAAY,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI;AACrE;;;;;;;;AASA,SAAgB,aAAa,YAAuC,UAAkB,YAA4B;CAChH,OAAOA,gBAAAA,WAAW;EAAC;EAAY;EAAU;CAAU,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;AAChF;;;;;;;;;;;;;;;AAgBA,SAAgB,cAAc,MAA8B;CAC1D,MAAM,MAAMC,gBAAAA,aAAa,MAAM,KAAK;CAEpC,IAAI,CAAC,KAAK,OAAO;CACjB,IAAI,CAAC,IAAI,QAAQ,OAAO;CAExB,MAAM,EAAE,MAAM,OAAO,MAAM,OAAO,MAAM,OAAO,KAAK,MAAM,QAAQ,SAAS,GAAG,cAAc;CAG5F,MAAM,mBAAmB,OAAO,YAAY,OAAO,QAAQ,SAAS,CAAC,CAAC,QAAQ,GAAG,OAAO,MAAM,KAAA,CAAS,CAAC;CAExG,OAAOC,gBAAAA,aAAa;EAAE,GAAG,IAAI;EAAQ,GAAG;CAAiB,CAAC;AAC5D;;;;;;;AAQA,SAAgB,aAAa,MAA2B;CACtD,IAAI,iBAAiB,IAAI,KAAK,IAAI,GAChC,OAAO;CAGT,MAAM,WAAWD,gBAAAA,aAAa,MAAM,MAAM,KAAKA,gBAAAA,aAAa,MAAM,MAAM;CACxE,IAAI,UACF,OAAO,SAAS,mBAAmB;CAGrC,OAAO;AACT"}
|
package/dist/refs-BjBHwunY.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import { M as pascalCase, Q as narrowSchema, o as createSchema } from "./visitor-Dk0DNLfC.js";
|
|
3
|
-
//#region src/utils/refs.ts
|
|
4
|
-
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
5
|
-
"string",
|
|
6
|
-
"uuid",
|
|
7
|
-
"email",
|
|
8
|
-
"url",
|
|
9
|
-
"datetime"
|
|
10
|
-
]);
|
|
11
|
-
/**
|
|
12
|
-
* Returns the last path segment of a reference string.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* `extractRefName('#/components/schemas/Pet') // 'Pet'`
|
|
16
|
-
*/
|
|
17
|
-
function extractRefName(ref) {
|
|
18
|
-
return ref.split("/").at(-1) ?? ref;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls
|
|
22
|
-
* back to `name` then nested `schema.name`.
|
|
23
|
-
*
|
|
24
|
-
* Returns `null` for non-ref nodes or when no name resolves.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`
|
|
28
|
-
*/
|
|
29
|
-
function resolveRefName(node) {
|
|
30
|
-
if (!node || node.type !== "ref") return null;
|
|
31
|
-
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
|
|
32
|
-
return node.name ?? node.schema?.name ?? null;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Builds a PascalCase child schema name by joining a parent name and property name.
|
|
36
|
-
* Returns `null` when there is no parent to nest under.
|
|
37
|
-
*
|
|
38
|
-
* @example Nested under a parent
|
|
39
|
-
* `childName('Order', 'shipping_address') // 'OrderShippingAddress'`
|
|
40
|
-
*
|
|
41
|
-
* @example No parent
|
|
42
|
-
* `childName(undefined, 'params') // null`
|
|
43
|
-
*/
|
|
44
|
-
function childName(parentName, propName) {
|
|
45
|
-
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
|
|
49
|
-
* empty parts.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* `enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'`
|
|
53
|
-
*/
|
|
54
|
-
function enumPropName(parentName, propName, enumSuffix) {
|
|
55
|
-
return pascalCase([
|
|
56
|
-
parentName,
|
|
57
|
-
propName,
|
|
58
|
-
enumSuffix
|
|
59
|
-
].filter(Boolean).join(" "));
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
63
|
-
*
|
|
64
|
-
* Every field set on the ref node except `kind`, `type`, `name`, `ref`, and `schema` overrides the
|
|
65
|
-
* same field in the resolved `node.schema` (for example `description`, `nullable`, `readOnly`,
|
|
66
|
-
* `deprecated`). Fields left `undefined` on the ref do not shadow the resolved schema. Non-ref
|
|
67
|
-
* nodes and refs without a resolved `schema` are returned unchanged.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```ts
|
|
71
|
-
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
72
|
-
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
function syncSchemaRef(node) {
|
|
76
|
-
const ref = narrowSchema(node, "ref");
|
|
77
|
-
if (!ref) return node;
|
|
78
|
-
if (!ref.schema) return node;
|
|
79
|
-
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
80
|
-
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
81
|
-
return createSchema({
|
|
82
|
-
...ref.schema,
|
|
83
|
-
...definedOverrides
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Type guard that returns `true` when a schema emits as a plain `string` type.
|
|
88
|
-
*
|
|
89
|
-
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
90
|
-
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
91
|
-
*/
|
|
92
|
-
function isStringType(node) {
|
|
93
|
-
if (plainStringTypes.has(node.type)) return true;
|
|
94
|
-
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
|
|
95
|
-
if (temporal) return temporal.representation !== "date";
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
//#endregion
|
|
99
|
-
export { resolveRefName as a, isStringType as i, enumPropName as n, syncSchemaRef as o, extractRefName as r, childName as t };
|
|
100
|
-
|
|
101
|
-
//# sourceMappingURL=refs-BjBHwunY.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"refs-BjBHwunY.js","names":[],"sources":["../src/utils/refs.ts"],"sourcesContent":["import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/index.ts'\nimport { createSchema, type SchemaType } from '../nodes/schema.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * `extractRefName('#/components/schemas/Pet') // 'Pet'`\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls\n * back to `name` then nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example Nested under a parent\n * `childName('Order', 'shipping_address') // 'OrderShippingAddress'`\n *\n * @example No parent\n * `childName(undefined, 'params') // null`\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * `enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'`\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Merges a ref node with its resolved schema, giving usage-site fields precedence.\n *\n * Every field set on the ref node except `kind`, `type`, `name`, `ref`, and `schema` overrides the\n * same field in the resolved `node.schema` (for example `description`, `nullable`, `readOnly`,\n * `deprecated`). Fields left `undefined` on the ref do not shadow the resolved schema. Non-ref\n * nodes and refs without a resolved `schema` are returned unchanged.\n *\n * @example\n * ```ts\n * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })\n * const merged = syncSchemaRef(ref) // merges with resolved Pet schema\n * ```\n */\nexport function syncSchemaRef(node: SchemaNode): SchemaNode {\n const ref = narrowSchema(node, 'ref')\n\n if (!ref) return node\n if (!ref.schema) return node\n\n const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref\n\n // Filter out undefined override values so they don't shadow the resolved schema's fields.\n const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))\n\n return createSchema({ ...ref.schema, ...definedOverrides })\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n"],"mappings":";;;AAKA,MAAM,mCAAmB,IAAI,IAAgB;CAAC;CAAU;CAAQ;CAAS;CAAO;AAAU,CAAU;;;;;;;AAQpG,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;AAClC;;;;;;;;;;AAWA,SAAgB,eAAe,MAA6C;CAC1E,IAAI,CAAC,QAAQ,KAAK,SAAS,OAAO,OAAO;CACzC,IAAI,KAAK,KAAK,OAAO,eAAe,KAAK,GAAG,KAAK,KAAK,QAAQ,KAAK,QAAQ,QAAQ;CAEnF,OAAO,KAAK,QAAQ,KAAK,QAAQ,QAAQ;AAC3C;;;;;;;;;;;AAYA,SAAgB,UAAU,YAAuC,UAAiC;CAChG,OAAO,aAAa,WAAW,CAAC,YAAY,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI;AACrE;;;;;;;;AASA,SAAgB,aAAa,YAAuC,UAAkB,YAA4B;CAChH,OAAO,WAAW;EAAC;EAAY;EAAU;CAAU,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;AAChF;;;;;;;;;;;;;;;AAgBA,SAAgB,cAAc,MAA8B;CAC1D,MAAM,MAAM,aAAa,MAAM,KAAK;CAEpC,IAAI,CAAC,KAAK,OAAO;CACjB,IAAI,CAAC,IAAI,QAAQ,OAAO;CAExB,MAAM,EAAE,MAAM,OAAO,MAAM,OAAO,MAAM,OAAO,KAAK,MAAM,QAAQ,SAAS,GAAG,cAAc;CAG5F,MAAM,mBAAmB,OAAO,YAAY,OAAO,QAAQ,SAAS,CAAC,CAAC,QAAQ,GAAG,OAAO,MAAM,KAAA,CAAS,CAAC;CAExG,OAAO,aAAa;EAAE,GAAG,IAAI;EAAQ,GAAG;CAAiB,CAAC;AAC5D;;;;;;;AAQA,SAAgB,aAAa,MAA2B;CACtD,IAAI,iBAAiB,IAAI,KAAK,IAAI,GAChC,OAAO;CAGT,MAAM,WAAW,aAAa,MAAM,MAAM,KAAK,aAAa,MAAM,MAAM;CACxE,IAAI,UACF,OAAO,SAAS,mBAAmB;CAGrC,OAAO;AACT"}
|
package/dist/types-CWF7DV0f.d.ts
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import { et as SchemaNode, nt as SchemaType, tt as SchemaNodeByType } from "./index-Cu2zmNxv.js";
|
|
3
|
-
|
|
4
|
-
//#region src/defineDialect.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* The spec-specific questions a schema parser answers while turning a source document into Kubb
|
|
7
|
-
* AST nodes. The rest of the pipeline is generic, so this is the one seam where source formats
|
|
8
|
-
* differ.
|
|
9
|
-
*/
|
|
10
|
-
type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
|
|
11
|
-
/**
|
|
12
|
-
* Whether the schema is nullable.
|
|
13
|
-
*/
|
|
14
|
-
isNullable(schema?: TSchema): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Whether the value is a `$ref` pointer.
|
|
17
|
-
*/
|
|
18
|
-
isReference(value?: unknown): value is TRef;
|
|
19
|
-
/**
|
|
20
|
-
* Whether the schema carries a discriminator for polymorphism.
|
|
21
|
-
*/
|
|
22
|
-
isDiscriminator(value?: unknown): value is TDiscriminated;
|
|
23
|
-
/**
|
|
24
|
-
* Whether the schema is binary data, converted to a `blob` node.
|
|
25
|
-
*/
|
|
26
|
-
isBinary(schema: TSchema): boolean;
|
|
27
|
-
/**
|
|
28
|
-
* Resolves a local `$ref` against the document, or nullish when it cannot.
|
|
29
|
-
*/
|
|
30
|
-
resolveRef<TResolved>(document: TDocument, ref: string): TResolved | null | undefined;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* A spec adapter's dialect. `name` identifies it in logs and diagnostics, and `schema` holds the
|
|
34
|
-
* spec-specific schema questions the parser answers.
|
|
35
|
-
*/
|
|
36
|
-
type Dialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
|
|
37
|
-
/**
|
|
38
|
-
* Identifies the dialect in logs and diagnostics.
|
|
39
|
-
*/
|
|
40
|
-
name: string;
|
|
41
|
-
/**
|
|
42
|
-
* The spec-specific schema behavior. See {@link SchemaDialect}.
|
|
43
|
-
*/
|
|
44
|
-
schema: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
|
|
48
|
-
* dialect's type for inference.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```ts
|
|
52
|
-
* export const oasDialect = defineDialect({
|
|
53
|
-
* name: 'oas',
|
|
54
|
-
* schema: {
|
|
55
|
-
* isNullable,
|
|
56
|
-
* isReference,
|
|
57
|
-
* isDiscriminator,
|
|
58
|
-
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
59
|
-
* resolveRef,
|
|
60
|
-
* },
|
|
61
|
-
* })
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
declare function defineDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: Dialect<TSchema, TRef, TDiscriminated, TDocument>): Dialect<TSchema, TRef, TDiscriminated, TDocument>;
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/createPrinter.d.ts
|
|
67
|
-
/**
|
|
68
|
-
* Runtime context passed as `this` to printer handlers.
|
|
69
|
-
*
|
|
70
|
-
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* const context: PrinterHandlerContext<string, {}> = {
|
|
75
|
-
* options: {},
|
|
76
|
-
* transform: () => 'value',
|
|
77
|
-
* }
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
81
|
-
/**
|
|
82
|
-
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
83
|
-
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
84
|
-
*/
|
|
85
|
-
transform: (node: SchemaNode) => TOutput | null;
|
|
86
|
-
/**
|
|
87
|
-
* Run the printer's built-in handler for the node, ignoring any override for its type.
|
|
88
|
-
* Inside an override, `this.base(node)` returns what the printer would have emitted,
|
|
89
|
-
* so the override can wrap it instead of re-implementing the handler. Nested nodes
|
|
90
|
-
* still dispatch through the overrides.
|
|
91
|
-
*/
|
|
92
|
-
base: (node: SchemaNode) => TOutput | null;
|
|
93
|
-
/**
|
|
94
|
-
* Options for this printer instance.
|
|
95
|
-
*/
|
|
96
|
-
options: TOptions;
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Handler for one schema node type.
|
|
100
|
-
*
|
|
101
|
-
* Use a regular function (not an arrow function) if you need `this`.
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```ts
|
|
105
|
-
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
106
|
-
* return 'string'
|
|
107
|
-
* }
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
|
|
111
|
-
/**
|
|
112
|
-
* Partial map of per-node-type handler overrides for a printer.
|
|
113
|
-
*
|
|
114
|
-
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
115
|
-
* Supply only the handlers you want to replace. The printer's built-in
|
|
116
|
-
* defaults fill in the rest.
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```ts
|
|
120
|
-
* pluginZod({
|
|
121
|
-
* printer: {
|
|
122
|
-
* nodes: {
|
|
123
|
-
* date(): string {
|
|
124
|
-
* return 'z.string().date()'
|
|
125
|
-
* },
|
|
126
|
-
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
127
|
-
* },
|
|
128
|
-
* })
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
132
|
-
/**
|
|
133
|
-
* Generic shape used by `definePrinter`.
|
|
134
|
-
*
|
|
135
|
-
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
136
|
-
* - `TOptions` options passed to and stored on the printer instance
|
|
137
|
-
* - `TOutput` the type emitted by node handlers
|
|
138
|
-
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```ts
|
|
142
|
-
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
143
|
-
* ```
|
|
144
|
-
*/
|
|
145
|
-
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
146
|
-
name: TName;
|
|
147
|
-
options: TOptions;
|
|
148
|
-
output: TOutput;
|
|
149
|
-
printOutput: TPrintOutput;
|
|
150
|
-
};
|
|
151
|
-
/**
|
|
152
|
-
* Printer instance returned by a printer factory.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```ts
|
|
156
|
-
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
157
|
-
* ```
|
|
158
|
-
*/
|
|
159
|
-
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
160
|
-
/**
|
|
161
|
-
* Unique identifier supplied at creation time.
|
|
162
|
-
*/
|
|
163
|
-
name: T['name'];
|
|
164
|
-
/**
|
|
165
|
-
* Options for this printer instance.
|
|
166
|
-
*/
|
|
167
|
-
options: T['options'];
|
|
168
|
-
/**
|
|
169
|
-
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
170
|
-
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
171
|
-
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
172
|
-
*/
|
|
173
|
-
transform: (node: SchemaNode) => T['output'] | null;
|
|
174
|
-
/**
|
|
175
|
-
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
176
|
-
* higher-level function (which may produce full declarations).
|
|
177
|
-
* Otherwise, falls back to the node-level dispatcher.
|
|
178
|
-
*/
|
|
179
|
-
print: (node: SchemaNode) => T['printOutput'] | null;
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* Builder function passed to `definePrinter`.
|
|
183
|
-
*
|
|
184
|
-
* It receives resolved options and returns:
|
|
185
|
-
* - `name`
|
|
186
|
-
* - `options`
|
|
187
|
-
* - `nodes` handlers
|
|
188
|
-
* - optional top-level `print` override
|
|
189
|
-
*
|
|
190
|
-
* @example
|
|
191
|
-
* ```ts
|
|
192
|
-
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
196
|
-
name: T['name'];
|
|
197
|
-
/**
|
|
198
|
-
* Options to store on the printer.
|
|
199
|
-
*/
|
|
200
|
-
options: T['options'];
|
|
201
|
-
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
202
|
-
/**
|
|
203
|
-
* User-supplied handler overrides. An override wins over the matching `nodes` handler,
|
|
204
|
-
* and can call `this.base(node)` to reuse the handler it replaced. Pass overrides here
|
|
205
|
-
* instead of spreading them into `nodes`, otherwise `this.base` cannot find the original.
|
|
206
|
-
*/
|
|
207
|
-
overrides?: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
208
|
-
/**
|
|
209
|
-
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
210
|
-
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
211
|
-
* not the override itself, so recursion is safe.
|
|
212
|
-
*/
|
|
213
|
-
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
214
|
-
};
|
|
215
|
-
/**
|
|
216
|
-
* Creates a schema printer: a function that takes a `SchemaNode` and emits
|
|
217
|
-
* code in your target language. Each plugin that produces code from schemas
|
|
218
|
-
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
219
|
-
* with this helper.
|
|
220
|
-
*
|
|
221
|
-
* The builder receives resolved options and returns:
|
|
222
|
-
*
|
|
223
|
-
* - `name` unique identifier for the printer.
|
|
224
|
-
* - `options` stored on the returned printer instance.
|
|
225
|
-
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
226
|
-
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
227
|
-
* - `overrides` (optional), user-supplied handlers that win over `nodes`.
|
|
228
|
-
* An override can call `this.base(node)` to reuse the handler it replaced.
|
|
229
|
-
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
230
|
-
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
231
|
-
*
|
|
232
|
-
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
233
|
-
* (the node-level dispatcher).
|
|
234
|
-
*
|
|
235
|
-
* @example Tiny Zod printer
|
|
236
|
-
* ```ts
|
|
237
|
-
* import { createPrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
238
|
-
*
|
|
239
|
-
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
240
|
-
*
|
|
241
|
-
* export const zodPrinter = createPrinter<PrinterZod>((options) => ({
|
|
242
|
-
* name: 'zod',
|
|
243
|
-
* options: { strict: options.strict ?? true },
|
|
244
|
-
* nodes: {
|
|
245
|
-
* string: () => 'z.string()',
|
|
246
|
-
* object(node) {
|
|
247
|
-
* const props = node.properties
|
|
248
|
-
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
249
|
-
* .join(', ')
|
|
250
|
-
* return `z.object({ ${props} })`
|
|
251
|
-
* },
|
|
252
|
-
* },
|
|
253
|
-
* }))
|
|
254
|
-
* ```
|
|
255
|
-
*/
|
|
256
|
-
declare function createPrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
257
|
-
//#endregion
|
|
258
|
-
export { Dialect as a, createPrinter as i, PrinterFactoryOptions as n, SchemaDialect as o, PrinterPartial as r, defineDialect as s, Printer as t };
|
|
259
|
-
//# sourceMappingURL=types-CWF7DV0f.d.ts.map
|