llmz 0.2.0 → 0.2.1
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/chunk-665O7CAN.js +788 -0
- package/dist/chunk-DD4AZSP5.cjs +788 -0
- package/dist/{chunk-WODK7Y4J.cjs → chunk-ETYFOCWC.cjs} +2 -2
- package/dist/{chunk-IRCB4FRM.js → chunk-GC5IQDGV.js} +1 -1
- package/dist/{chunk-IKLFMVSW.cjs → chunk-MY26EAOP.cjs} +2 -2
- package/dist/{chunk-33AB6SVX.js → chunk-ZWUWCRHG.js} +1 -1
- package/dist/{execute-BXPFSEVX.cjs → execute-2AGZ5QUN.cjs} +16 -16
- package/dist/{execute-HIRXXR6X.js → execute-LNUBLVMZ.js} +3 -3
- package/dist/formatting.d.ts +1 -2
- package/dist/index.cjs +14 -14
- package/dist/index.js +8 -8
- package/dist/{tool-DNHVW3KC.js → tool-HTAYKIA2.js} +2 -2
- package/dist/{tool-GJ6ZXWS2.cjs → tool-V55GD2FS.cjs} +3 -3
- package/dist/{typings-ZTFPPA5W.cjs → typings-S6I6IPTG.cjs} +2 -2
- package/dist/{typings-3YV3RDMP.js → typings-SCKZCAPN.js} +1 -1
- package/dist/typings-formatter.d.ts +15 -0
- package/dist/typings-formatter.test.d.ts +1 -0
- package/package.json +2 -3
- package/dist/chunk-7LMIOS6H.cjs +0 -287
- package/dist/chunk-TOGYDN6U.js +0 -287
package/dist/chunk-7LMIOS6H.cjs
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
-
|
|
3
|
-
var _chunkPK72FAKDcjs = require('./chunk-PK72FAKD.cjs');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var _chunkFPLSJF4Scjs = require('./chunk-FPLSJF4S.cjs');
|
|
9
|
-
|
|
10
|
-
// src/typings.ts
|
|
11
|
-
var _zui = require('@bpinternal/zui');
|
|
12
|
-
|
|
13
|
-
// src/formatting.ts
|
|
14
|
-
var _lrucache = require('lru-cache');
|
|
15
|
-
var _babel = require('prettier/plugins/babel'); var _babel2 = _interopRequireDefault(_babel);
|
|
16
|
-
var _estree = require('prettier/plugins/estree'); var _estree2 = _interopRequireDefault(_estree);
|
|
17
|
-
var _typescript = require('prettier/plugins/typescript'); var _typescript2 = _interopRequireDefault(_typescript);
|
|
18
|
-
var _standalone = require('prettier/standalone');
|
|
19
|
-
var cache = new (0, _lrucache.LRUCache)({ max: 1e3 });
|
|
20
|
-
async function formatTypings(typings, options) {
|
|
21
|
-
if (cache.has(typings)) {
|
|
22
|
-
return cache.get(typings);
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
options ??= {};
|
|
26
|
-
options.throwOnError ??= true;
|
|
27
|
-
const result = (await _standalone.format.call(void 0, typings, {
|
|
28
|
-
singleAttributePerLine: true,
|
|
29
|
-
bracketSameLine: true,
|
|
30
|
-
semi: false,
|
|
31
|
-
...options,
|
|
32
|
-
embeddedLanguageFormatting: "off",
|
|
33
|
-
plugins: [_estree2.default, _babel2.default, _typescript2.default],
|
|
34
|
-
parser: "typescript",
|
|
35
|
-
filepath: "tools.d.ts"
|
|
36
|
-
})).trim();
|
|
37
|
-
cache.set(typings, result);
|
|
38
|
-
return result;
|
|
39
|
-
} catch (err) {
|
|
40
|
-
if (options == null ? void 0 : options.throwOnError) {
|
|
41
|
-
throw new (0, _chunkPK72FAKDcjs.CodeFormattingError)(err instanceof Error ? err.message : _nullishCoalesce((err == null ? void 0 : err.toString()), () => ( "Unknown Error")), typings);
|
|
42
|
-
}
|
|
43
|
-
return typings;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// src/typings.ts
|
|
48
|
-
var Primitives = [
|
|
49
|
-
"string",
|
|
50
|
-
"number",
|
|
51
|
-
"boolean",
|
|
52
|
-
"unknown",
|
|
53
|
-
"void",
|
|
54
|
-
"any",
|
|
55
|
-
"null",
|
|
56
|
-
"undefined",
|
|
57
|
-
"never",
|
|
58
|
-
"bigint",
|
|
59
|
-
"symbol",
|
|
60
|
-
"object"
|
|
61
|
-
];
|
|
62
|
-
var LARGE_DECLARATION_LINES = 5;
|
|
63
|
-
var isPrimitive = (type) => Primitives.includes(type);
|
|
64
|
-
var isArrayOfPrimitives = (type) => Primitives.map((p) => `${p}[]`).includes(type);
|
|
65
|
-
var stripSpaces = (typings) => typings.replace(/ +/g, " ").trim();
|
|
66
|
-
var KeyValue = class {
|
|
67
|
-
constructor(key, value) {
|
|
68
|
-
this.key = key;
|
|
69
|
-
this.value = value;
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
var FnParameters = class {
|
|
73
|
-
constructor(schema) {
|
|
74
|
-
this.schema = schema;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
var FnReturn = class {
|
|
78
|
-
constructor(schema) {
|
|
79
|
-
this.schema = schema;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var Declaration = class {
|
|
83
|
-
constructor(schema, identifier) {
|
|
84
|
-
this.schema = schema;
|
|
85
|
-
this.identifier = identifier;
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
async function getTypings(schema, options) {
|
|
89
|
-
options ??= {};
|
|
90
|
-
options.declaration ??= false;
|
|
91
|
-
let wrappedSchema = schema;
|
|
92
|
-
if ((options == null ? void 0 : options.declaration) && _zui.z.is.zuiType(schema)) {
|
|
93
|
-
const title = "title" in schema.ui ? schema.ui.title : null;
|
|
94
|
-
if (!title) {
|
|
95
|
-
throw new Error('Only schemas with "title" Zui property can be declared.');
|
|
96
|
-
}
|
|
97
|
-
wrappedSchema = new Declaration(schema, title);
|
|
98
|
-
}
|
|
99
|
-
let dts = await sUnwrapZodRecursive(wrappedSchema, { ...options });
|
|
100
|
-
dts = await formatTypings(dts, { throwOnError: false });
|
|
101
|
-
return dts;
|
|
102
|
-
}
|
|
103
|
-
async function sUnwrapZodRecursive(schema, options) {
|
|
104
|
-
return sUnwrapZod(schema, options);
|
|
105
|
-
}
|
|
106
|
-
async function sUnwrapZod(schema, options) {
|
|
107
|
-
var _a, _b;
|
|
108
|
-
const newOptions = {
|
|
109
|
-
...options,
|
|
110
|
-
declaration: false,
|
|
111
|
-
parent: schema
|
|
112
|
-
};
|
|
113
|
-
if (schema instanceof Declaration) {
|
|
114
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema.schema.description);
|
|
115
|
-
const withoutDesc = schema.schema.describe("");
|
|
116
|
-
const typings = await sUnwrapZodRecursive(withoutDesc, { ...newOptions, declaration: true });
|
|
117
|
-
const isLargeDeclaration = typings.split("\n").length >= LARGE_DECLARATION_LINES;
|
|
118
|
-
const closingTag = isLargeDeclaration ? `// end of ${schema.identifier}` : "";
|
|
119
|
-
if (_zui.z.is.zuiFunction(schema.schema)) {
|
|
120
|
-
return stripSpaces(`${description}
|
|
121
|
-
declare function ${schema.identifier}${typings};${closingTag}`);
|
|
122
|
-
}
|
|
123
|
-
return stripSpaces(`${description}
|
|
124
|
-
declare const ${schema.identifier}: ${typings};${closingTag}`);
|
|
125
|
-
}
|
|
126
|
-
if (schema instanceof KeyValue) {
|
|
127
|
-
if (_zui.z.is.zuiOptional(schema.value) || _zui.z.is.zuiDefault(schema.value)) {
|
|
128
|
-
let innerType = schema.value._def.innerType;
|
|
129
|
-
if (_zui.z.is.zuiType(innerType) && !innerType.description && schema.value.description) {
|
|
130
|
-
innerType = innerType == null ? void 0 : innerType.describe(schema.value.description);
|
|
131
|
-
}
|
|
132
|
-
const optionalToken = schema.key.endsWith("?") ? "" : "?";
|
|
133
|
-
return sUnwrapZodRecursive(new KeyValue(schema.key + optionalToken, innerType), newOptions);
|
|
134
|
-
}
|
|
135
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema.value._def.description || schema.value.description);
|
|
136
|
-
const delimiter = (description == null ? void 0 : description.trim().length) > 0 ? "\n" : "";
|
|
137
|
-
const withoutDesc = schema.value.describe("");
|
|
138
|
-
return `${delimiter}${description}${delimiter}${schema.key}: ${await sUnwrapZodRecursive(withoutDesc, newOptions)}${delimiter}`;
|
|
139
|
-
}
|
|
140
|
-
if (schema instanceof FnParameters) {
|
|
141
|
-
if (_zui.z.is.zuiTuple(schema.schema)) {
|
|
142
|
-
let args = "";
|
|
143
|
-
for (let i = 0; i < schema.schema.items.length; i++) {
|
|
144
|
-
const argName = _nullishCoalesce(((_b = (_a = schema.schema.items[i]) == null ? void 0 : _a.ui) == null ? void 0 : _b.title), () => ( `arg${i}`));
|
|
145
|
-
const item = schema.schema.items[i];
|
|
146
|
-
args += `${await sUnwrapZodRecursive(new KeyValue(_chunkFPLSJF4Scjs.toPropertyKey.call(void 0, argName), item), newOptions)}, `;
|
|
147
|
-
}
|
|
148
|
-
return args;
|
|
149
|
-
}
|
|
150
|
-
const isLiteral = _zui.z.is.zuiLiteral(schema.schema.naked());
|
|
151
|
-
const typings = (await sUnwrapZodRecursive(schema.schema, newOptions)).trim();
|
|
152
|
-
const startsWithPairs = typings.startsWith("{") && typings.endsWith("}") || typings.startsWith("[") && typings.endsWith("]") || typings.startsWith("(") && typings.endsWith(")") || typings.startsWith("Array<") && typings.endsWith(">") || typings.startsWith("Record<") && typings.endsWith(">") || isArrayOfPrimitives(typings);
|
|
153
|
-
if (startsWithPairs || isLiteral) {
|
|
154
|
-
return `args: ${typings}`;
|
|
155
|
-
} else {
|
|
156
|
-
return typings;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (schema instanceof FnReturn) {
|
|
160
|
-
if (_zui.z.is.zuiOptional(schema.schema)) {
|
|
161
|
-
return `${await sUnwrapZodRecursive(schema.schema.unwrap(), newOptions)} | undefined`;
|
|
162
|
-
}
|
|
163
|
-
return sUnwrapZodRecursive(schema.schema, newOptions);
|
|
164
|
-
}
|
|
165
|
-
if (schema === null) {
|
|
166
|
-
return "unknown";
|
|
167
|
-
}
|
|
168
|
-
schema;
|
|
169
|
-
if (_zui.z.is.zuiDefault(schema)) {
|
|
170
|
-
return sUnwrapZodRecursive(schema._def.innerType, options);
|
|
171
|
-
}
|
|
172
|
-
if (_zui.z.is.zuiVoid(schema)) {
|
|
173
|
-
return "void";
|
|
174
|
-
}
|
|
175
|
-
if (_zui.z.is.zuiUnknown(schema)) {
|
|
176
|
-
return "unknown";
|
|
177
|
-
}
|
|
178
|
-
if (_zui.z.is.zuiAny(schema)) {
|
|
179
|
-
return "any";
|
|
180
|
-
}
|
|
181
|
-
if (_zui.z.is.zuiPromise(schema)) {
|
|
182
|
-
return `Promise<${await sUnwrapZodRecursive(schema.unwrap(), newOptions)}>`;
|
|
183
|
-
}
|
|
184
|
-
if (_zui.z.is.zuiFunction(schema)) {
|
|
185
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
186
|
-
const input = await sUnwrapZodRecursive(new FnParameters(schema._def.args), newOptions);
|
|
187
|
-
const output = await sUnwrapZodRecursive(new FnReturn(schema._def.returns), newOptions);
|
|
188
|
-
if (options == null ? void 0 : options.declaration) {
|
|
189
|
-
return `${description}
|
|
190
|
-
(${input}): ${output}`;
|
|
191
|
-
}
|
|
192
|
-
return `${description}
|
|
193
|
-
(${input}) => ${output}`;
|
|
194
|
-
}
|
|
195
|
-
if (_zui.z.is.zuiArray(schema)) {
|
|
196
|
-
const item = await sUnwrapZodRecursive(schema._def.type, newOptions);
|
|
197
|
-
if (isPrimitive(item)) {
|
|
198
|
-
return `${item}[]`;
|
|
199
|
-
}
|
|
200
|
-
return `Array<${item}>`;
|
|
201
|
-
}
|
|
202
|
-
if (_zui.z.is.zuiEnum(schema)) {
|
|
203
|
-
const values = schema._def.values.map(_chunkFPLSJF4Scjs.escapeString);
|
|
204
|
-
return values.join(" | ");
|
|
205
|
-
}
|
|
206
|
-
if (_zui.z.is.zuiTuple(schema)) {
|
|
207
|
-
if (schema.items.length === 0) {
|
|
208
|
-
return "[]";
|
|
209
|
-
}
|
|
210
|
-
const items = await Promise.all(schema.items.map((i) => sUnwrapZodRecursive(i, newOptions)));
|
|
211
|
-
return `[${items.join(", ")}]`;
|
|
212
|
-
}
|
|
213
|
-
if (_zui.z.is.zuiNullable(schema)) {
|
|
214
|
-
return `${await sUnwrapZodRecursive(schema.unwrap(), options)} | null`;
|
|
215
|
-
}
|
|
216
|
-
if (_zui.z.is.zuiOptional(schema)) {
|
|
217
|
-
if ((options == null ? void 0 : options.declaration) || _zui.z.is.zuiType(options == null ? void 0 : options.parent) && options.parent.typeName === "ZodRecord") {
|
|
218
|
-
return `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)} | undefined`;
|
|
219
|
-
}
|
|
220
|
-
const optionalToken = options.parent instanceof KeyValue ? "| undefined" : "";
|
|
221
|
-
const val = `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)}${optionalToken}`;
|
|
222
|
-
return val;
|
|
223
|
-
}
|
|
224
|
-
if (_zui.z.is.zuiObject(schema)) {
|
|
225
|
-
const props = await Promise.all(
|
|
226
|
-
Object.entries(schema.shape).map(async ([key, value]) => {
|
|
227
|
-
if (_zui.z.is.zuiType(value)) {
|
|
228
|
-
return sUnwrapZodRecursive(new KeyValue(_chunkFPLSJF4Scjs.toPropertyKey.call(void 0, key), value), newOptions);
|
|
229
|
-
}
|
|
230
|
-
return `${key}: unknown`;
|
|
231
|
-
})
|
|
232
|
-
);
|
|
233
|
-
return `{ ${props.join("; ")} }`;
|
|
234
|
-
}
|
|
235
|
-
if (_zui.z.is.zuiString(schema)) {
|
|
236
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
237
|
-
return `${description} string`.trim();
|
|
238
|
-
}
|
|
239
|
-
if (_zui.z.is.zuiUnion(schema)) {
|
|
240
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
241
|
-
const options2 = await Promise.all(
|
|
242
|
-
schema.options.map(async (option) => {
|
|
243
|
-
return sUnwrapZodRecursive(option, newOptions);
|
|
244
|
-
})
|
|
245
|
-
);
|
|
246
|
-
return `${description}
|
|
247
|
-
${options2.join(" | ")}`;
|
|
248
|
-
}
|
|
249
|
-
if (_zui.z.is.zuiLiteral(schema)) {
|
|
250
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
251
|
-
return `${description}
|
|
252
|
-
${typeof schema.value === "string" ? _chunkFPLSJF4Scjs.escapeString.call(void 0, schema.value) : String(schema.value)}`.trim();
|
|
253
|
-
}
|
|
254
|
-
if (_zui.z.is.zuiNumber(schema)) {
|
|
255
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
256
|
-
return `${description} number`.trim();
|
|
257
|
-
}
|
|
258
|
-
if (_zui.z.is.zuiBoolean(schema)) {
|
|
259
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
260
|
-
return `${description} boolean`.trim();
|
|
261
|
-
}
|
|
262
|
-
if (_zui.z.is.zuiCatch(schema)) {
|
|
263
|
-
return sUnwrapZodRecursive(schema.removeCatch(), newOptions);
|
|
264
|
-
}
|
|
265
|
-
if (_zui.z.is.zuiLazy(schema)) {
|
|
266
|
-
return sUnwrapZodRecursive(schema._def.getter(), newOptions);
|
|
267
|
-
}
|
|
268
|
-
if (_zui.z.is.zuiRecord(schema)) {
|
|
269
|
-
const description = _chunkFPLSJF4Scjs.getMultilineComment.call(void 0, schema._def.description);
|
|
270
|
-
const keyType = await sUnwrapZodRecursive(schema._def.keyType, newOptions);
|
|
271
|
-
const valueType = await sUnwrapZodRecursive(schema._def.valueType, newOptions);
|
|
272
|
-
return `${description} { [key: (${keyType})]: (${valueType}) }`;
|
|
273
|
-
}
|
|
274
|
-
try {
|
|
275
|
-
let typings = schema == null ? void 0 : schema.toTypescriptType({ treatDefaultAsOptional: true });
|
|
276
|
-
typings ??= "unknown";
|
|
277
|
-
return stripSpaces(typings);
|
|
278
|
-
} catch (error) {
|
|
279
|
-
console.error("Error in sUnwrapZod", { error, schema, parent: options == null ? void 0 : options.parent });
|
|
280
|
-
return "unknown";
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
exports.formatTypings = formatTypings; exports.getTypings = getTypings;
|
package/dist/chunk-TOGYDN6U.js
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CodeFormattingError
|
|
3
|
-
} from "./chunk-WSVDMGMR.js";
|
|
4
|
-
import {
|
|
5
|
-
escapeString,
|
|
6
|
-
getMultilineComment,
|
|
7
|
-
toPropertyKey
|
|
8
|
-
} from "./chunk-YX27XM67.js";
|
|
9
|
-
|
|
10
|
-
// src/typings.ts
|
|
11
|
-
import { z } from "@bpinternal/zui";
|
|
12
|
-
|
|
13
|
-
// src/formatting.ts
|
|
14
|
-
import { LRUCache } from "lru-cache";
|
|
15
|
-
import babel from "prettier/plugins/babel";
|
|
16
|
-
import estree from "prettier/plugins/estree";
|
|
17
|
-
import typescript from "prettier/plugins/typescript";
|
|
18
|
-
import { format } from "prettier/standalone";
|
|
19
|
-
var cache = new LRUCache({ max: 1e3 });
|
|
20
|
-
async function formatTypings(typings, options) {
|
|
21
|
-
if (cache.has(typings)) {
|
|
22
|
-
return cache.get(typings);
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
options ??= {};
|
|
26
|
-
options.throwOnError ??= true;
|
|
27
|
-
const result = (await format(typings, {
|
|
28
|
-
singleAttributePerLine: true,
|
|
29
|
-
bracketSameLine: true,
|
|
30
|
-
semi: false,
|
|
31
|
-
...options,
|
|
32
|
-
embeddedLanguageFormatting: "off",
|
|
33
|
-
plugins: [estree, babel, typescript],
|
|
34
|
-
parser: "typescript",
|
|
35
|
-
filepath: "tools.d.ts"
|
|
36
|
-
})).trim();
|
|
37
|
-
cache.set(typings, result);
|
|
38
|
-
return result;
|
|
39
|
-
} catch (err) {
|
|
40
|
-
if (options == null ? void 0 : options.throwOnError) {
|
|
41
|
-
throw new CodeFormattingError(err instanceof Error ? err.message : (err == null ? void 0 : err.toString()) ?? "Unknown Error", typings);
|
|
42
|
-
}
|
|
43
|
-
return typings;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// src/typings.ts
|
|
48
|
-
var Primitives = [
|
|
49
|
-
"string",
|
|
50
|
-
"number",
|
|
51
|
-
"boolean",
|
|
52
|
-
"unknown",
|
|
53
|
-
"void",
|
|
54
|
-
"any",
|
|
55
|
-
"null",
|
|
56
|
-
"undefined",
|
|
57
|
-
"never",
|
|
58
|
-
"bigint",
|
|
59
|
-
"symbol",
|
|
60
|
-
"object"
|
|
61
|
-
];
|
|
62
|
-
var LARGE_DECLARATION_LINES = 5;
|
|
63
|
-
var isPrimitive = (type) => Primitives.includes(type);
|
|
64
|
-
var isArrayOfPrimitives = (type) => Primitives.map((p) => `${p}[]`).includes(type);
|
|
65
|
-
var stripSpaces = (typings) => typings.replace(/ +/g, " ").trim();
|
|
66
|
-
var KeyValue = class {
|
|
67
|
-
constructor(key, value) {
|
|
68
|
-
this.key = key;
|
|
69
|
-
this.value = value;
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
var FnParameters = class {
|
|
73
|
-
constructor(schema) {
|
|
74
|
-
this.schema = schema;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
var FnReturn = class {
|
|
78
|
-
constructor(schema) {
|
|
79
|
-
this.schema = schema;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var Declaration = class {
|
|
83
|
-
constructor(schema, identifier) {
|
|
84
|
-
this.schema = schema;
|
|
85
|
-
this.identifier = identifier;
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
async function getTypings(schema, options) {
|
|
89
|
-
options ??= {};
|
|
90
|
-
options.declaration ??= false;
|
|
91
|
-
let wrappedSchema = schema;
|
|
92
|
-
if ((options == null ? void 0 : options.declaration) && z.is.zuiType(schema)) {
|
|
93
|
-
const title = "title" in schema.ui ? schema.ui.title : null;
|
|
94
|
-
if (!title) {
|
|
95
|
-
throw new Error('Only schemas with "title" Zui property can be declared.');
|
|
96
|
-
}
|
|
97
|
-
wrappedSchema = new Declaration(schema, title);
|
|
98
|
-
}
|
|
99
|
-
let dts = await sUnwrapZodRecursive(wrappedSchema, { ...options });
|
|
100
|
-
dts = await formatTypings(dts, { throwOnError: false });
|
|
101
|
-
return dts;
|
|
102
|
-
}
|
|
103
|
-
async function sUnwrapZodRecursive(schema, options) {
|
|
104
|
-
return sUnwrapZod(schema, options);
|
|
105
|
-
}
|
|
106
|
-
async function sUnwrapZod(schema, options) {
|
|
107
|
-
var _a, _b;
|
|
108
|
-
const newOptions = {
|
|
109
|
-
...options,
|
|
110
|
-
declaration: false,
|
|
111
|
-
parent: schema
|
|
112
|
-
};
|
|
113
|
-
if (schema instanceof Declaration) {
|
|
114
|
-
const description = getMultilineComment(schema.schema.description);
|
|
115
|
-
const withoutDesc = schema.schema.describe("");
|
|
116
|
-
const typings = await sUnwrapZodRecursive(withoutDesc, { ...newOptions, declaration: true });
|
|
117
|
-
const isLargeDeclaration = typings.split("\n").length >= LARGE_DECLARATION_LINES;
|
|
118
|
-
const closingTag = isLargeDeclaration ? `// end of ${schema.identifier}` : "";
|
|
119
|
-
if (z.is.zuiFunction(schema.schema)) {
|
|
120
|
-
return stripSpaces(`${description}
|
|
121
|
-
declare function ${schema.identifier}${typings};${closingTag}`);
|
|
122
|
-
}
|
|
123
|
-
return stripSpaces(`${description}
|
|
124
|
-
declare const ${schema.identifier}: ${typings};${closingTag}`);
|
|
125
|
-
}
|
|
126
|
-
if (schema instanceof KeyValue) {
|
|
127
|
-
if (z.is.zuiOptional(schema.value) || z.is.zuiDefault(schema.value)) {
|
|
128
|
-
let innerType = schema.value._def.innerType;
|
|
129
|
-
if (z.is.zuiType(innerType) && !innerType.description && schema.value.description) {
|
|
130
|
-
innerType = innerType == null ? void 0 : innerType.describe(schema.value.description);
|
|
131
|
-
}
|
|
132
|
-
const optionalToken = schema.key.endsWith("?") ? "" : "?";
|
|
133
|
-
return sUnwrapZodRecursive(new KeyValue(schema.key + optionalToken, innerType), newOptions);
|
|
134
|
-
}
|
|
135
|
-
const description = getMultilineComment(schema.value._def.description || schema.value.description);
|
|
136
|
-
const delimiter = (description == null ? void 0 : description.trim().length) > 0 ? "\n" : "";
|
|
137
|
-
const withoutDesc = schema.value.describe("");
|
|
138
|
-
return `${delimiter}${description}${delimiter}${schema.key}: ${await sUnwrapZodRecursive(withoutDesc, newOptions)}${delimiter}`;
|
|
139
|
-
}
|
|
140
|
-
if (schema instanceof FnParameters) {
|
|
141
|
-
if (z.is.zuiTuple(schema.schema)) {
|
|
142
|
-
let args = "";
|
|
143
|
-
for (let i = 0; i < schema.schema.items.length; i++) {
|
|
144
|
-
const argName = ((_b = (_a = schema.schema.items[i]) == null ? void 0 : _a.ui) == null ? void 0 : _b.title) ?? `arg${i}`;
|
|
145
|
-
const item = schema.schema.items[i];
|
|
146
|
-
args += `${await sUnwrapZodRecursive(new KeyValue(toPropertyKey(argName), item), newOptions)}, `;
|
|
147
|
-
}
|
|
148
|
-
return args;
|
|
149
|
-
}
|
|
150
|
-
const isLiteral = z.is.zuiLiteral(schema.schema.naked());
|
|
151
|
-
const typings = (await sUnwrapZodRecursive(schema.schema, newOptions)).trim();
|
|
152
|
-
const startsWithPairs = typings.startsWith("{") && typings.endsWith("}") || typings.startsWith("[") && typings.endsWith("]") || typings.startsWith("(") && typings.endsWith(")") || typings.startsWith("Array<") && typings.endsWith(">") || typings.startsWith("Record<") && typings.endsWith(">") || isArrayOfPrimitives(typings);
|
|
153
|
-
if (startsWithPairs || isLiteral) {
|
|
154
|
-
return `args: ${typings}`;
|
|
155
|
-
} else {
|
|
156
|
-
return typings;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (schema instanceof FnReturn) {
|
|
160
|
-
if (z.is.zuiOptional(schema.schema)) {
|
|
161
|
-
return `${await sUnwrapZodRecursive(schema.schema.unwrap(), newOptions)} | undefined`;
|
|
162
|
-
}
|
|
163
|
-
return sUnwrapZodRecursive(schema.schema, newOptions);
|
|
164
|
-
}
|
|
165
|
-
if (schema === null) {
|
|
166
|
-
return "unknown";
|
|
167
|
-
}
|
|
168
|
-
schema;
|
|
169
|
-
if (z.is.zuiDefault(schema)) {
|
|
170
|
-
return sUnwrapZodRecursive(schema._def.innerType, options);
|
|
171
|
-
}
|
|
172
|
-
if (z.is.zuiVoid(schema)) {
|
|
173
|
-
return "void";
|
|
174
|
-
}
|
|
175
|
-
if (z.is.zuiUnknown(schema)) {
|
|
176
|
-
return "unknown";
|
|
177
|
-
}
|
|
178
|
-
if (z.is.zuiAny(schema)) {
|
|
179
|
-
return "any";
|
|
180
|
-
}
|
|
181
|
-
if (z.is.zuiPromise(schema)) {
|
|
182
|
-
return `Promise<${await sUnwrapZodRecursive(schema.unwrap(), newOptions)}>`;
|
|
183
|
-
}
|
|
184
|
-
if (z.is.zuiFunction(schema)) {
|
|
185
|
-
const description = getMultilineComment(schema._def.description);
|
|
186
|
-
const input = await sUnwrapZodRecursive(new FnParameters(schema._def.args), newOptions);
|
|
187
|
-
const output = await sUnwrapZodRecursive(new FnReturn(schema._def.returns), newOptions);
|
|
188
|
-
if (options == null ? void 0 : options.declaration) {
|
|
189
|
-
return `${description}
|
|
190
|
-
(${input}): ${output}`;
|
|
191
|
-
}
|
|
192
|
-
return `${description}
|
|
193
|
-
(${input}) => ${output}`;
|
|
194
|
-
}
|
|
195
|
-
if (z.is.zuiArray(schema)) {
|
|
196
|
-
const item = await sUnwrapZodRecursive(schema._def.type, newOptions);
|
|
197
|
-
if (isPrimitive(item)) {
|
|
198
|
-
return `${item}[]`;
|
|
199
|
-
}
|
|
200
|
-
return `Array<${item}>`;
|
|
201
|
-
}
|
|
202
|
-
if (z.is.zuiEnum(schema)) {
|
|
203
|
-
const values = schema._def.values.map(escapeString);
|
|
204
|
-
return values.join(" | ");
|
|
205
|
-
}
|
|
206
|
-
if (z.is.zuiTuple(schema)) {
|
|
207
|
-
if (schema.items.length === 0) {
|
|
208
|
-
return "[]";
|
|
209
|
-
}
|
|
210
|
-
const items = await Promise.all(schema.items.map((i) => sUnwrapZodRecursive(i, newOptions)));
|
|
211
|
-
return `[${items.join(", ")}]`;
|
|
212
|
-
}
|
|
213
|
-
if (z.is.zuiNullable(schema)) {
|
|
214
|
-
return `${await sUnwrapZodRecursive(schema.unwrap(), options)} | null`;
|
|
215
|
-
}
|
|
216
|
-
if (z.is.zuiOptional(schema)) {
|
|
217
|
-
if ((options == null ? void 0 : options.declaration) || z.is.zuiType(options == null ? void 0 : options.parent) && options.parent.typeName === "ZodRecord") {
|
|
218
|
-
return `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)} | undefined`;
|
|
219
|
-
}
|
|
220
|
-
const optionalToken = options.parent instanceof KeyValue ? "| undefined" : "";
|
|
221
|
-
const val = `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)}${optionalToken}`;
|
|
222
|
-
return val;
|
|
223
|
-
}
|
|
224
|
-
if (z.is.zuiObject(schema)) {
|
|
225
|
-
const props = await Promise.all(
|
|
226
|
-
Object.entries(schema.shape).map(async ([key, value]) => {
|
|
227
|
-
if (z.is.zuiType(value)) {
|
|
228
|
-
return sUnwrapZodRecursive(new KeyValue(toPropertyKey(key), value), newOptions);
|
|
229
|
-
}
|
|
230
|
-
return `${key}: unknown`;
|
|
231
|
-
})
|
|
232
|
-
);
|
|
233
|
-
return `{ ${props.join("; ")} }`;
|
|
234
|
-
}
|
|
235
|
-
if (z.is.zuiString(schema)) {
|
|
236
|
-
const description = getMultilineComment(schema._def.description);
|
|
237
|
-
return `${description} string`.trim();
|
|
238
|
-
}
|
|
239
|
-
if (z.is.zuiUnion(schema)) {
|
|
240
|
-
const description = getMultilineComment(schema._def.description);
|
|
241
|
-
const options2 = await Promise.all(
|
|
242
|
-
schema.options.map(async (option) => {
|
|
243
|
-
return sUnwrapZodRecursive(option, newOptions);
|
|
244
|
-
})
|
|
245
|
-
);
|
|
246
|
-
return `${description}
|
|
247
|
-
${options2.join(" | ")}`;
|
|
248
|
-
}
|
|
249
|
-
if (z.is.zuiLiteral(schema)) {
|
|
250
|
-
const description = getMultilineComment(schema._def.description);
|
|
251
|
-
return `${description}
|
|
252
|
-
${typeof schema.value === "string" ? escapeString(schema.value) : String(schema.value)}`.trim();
|
|
253
|
-
}
|
|
254
|
-
if (z.is.zuiNumber(schema)) {
|
|
255
|
-
const description = getMultilineComment(schema._def.description);
|
|
256
|
-
return `${description} number`.trim();
|
|
257
|
-
}
|
|
258
|
-
if (z.is.zuiBoolean(schema)) {
|
|
259
|
-
const description = getMultilineComment(schema._def.description);
|
|
260
|
-
return `${description} boolean`.trim();
|
|
261
|
-
}
|
|
262
|
-
if (z.is.zuiCatch(schema)) {
|
|
263
|
-
return sUnwrapZodRecursive(schema.removeCatch(), newOptions);
|
|
264
|
-
}
|
|
265
|
-
if (z.is.zuiLazy(schema)) {
|
|
266
|
-
return sUnwrapZodRecursive(schema._def.getter(), newOptions);
|
|
267
|
-
}
|
|
268
|
-
if (z.is.zuiRecord(schema)) {
|
|
269
|
-
const description = getMultilineComment(schema._def.description);
|
|
270
|
-
const keyType = await sUnwrapZodRecursive(schema._def.keyType, newOptions);
|
|
271
|
-
const valueType = await sUnwrapZodRecursive(schema._def.valueType, newOptions);
|
|
272
|
-
return `${description} { [key: (${keyType})]: (${valueType}) }`;
|
|
273
|
-
}
|
|
274
|
-
try {
|
|
275
|
-
let typings = schema == null ? void 0 : schema.toTypescriptType({ treatDefaultAsOptional: true });
|
|
276
|
-
typings ??= "unknown";
|
|
277
|
-
return stripSpaces(typings);
|
|
278
|
-
} catch (error) {
|
|
279
|
-
console.error("Error in sUnwrapZod", { error, schema, parent: options == null ? void 0 : options.parent });
|
|
280
|
-
return "unknown";
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export {
|
|
285
|
-
formatTypings,
|
|
286
|
-
getTypings
|
|
287
|
-
};
|