exact-mirror 0.0.4 → 0.0.5
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/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +18 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +18 -2
- package/package.json +1 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ interface Instruction {
|
|
|
18
18
|
*/
|
|
19
19
|
TypeCompiler?: typeof TypeCompiler;
|
|
20
20
|
typeCompilerWanred?: boolean;
|
|
21
|
+
definitions: Record<string, TAnySchema>;
|
|
21
22
|
}
|
|
22
|
-
export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler }?: Pick<Instruction, "TypeCompiler"
|
|
23
|
+
export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions">>) => ((v: T["static"]) => T["static"]);
|
|
23
24
|
export default createMirror;
|
package/dist/cjs/index.js
CHANGED
|
@@ -27,6 +27,7 @@ __export(index_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
var import_compiler = require("@sinclair/typebox/compiler");
|
|
29
29
|
var Kind = Symbol.for("TypeBox.Kind");
|
|
30
|
+
var OptionalKind = Symbol.for("TypeBox.Optional");
|
|
30
31
|
var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
|
|
31
32
|
var joinProperty = (v1, v2, isOptional = false) => {
|
|
32
33
|
if (typeof v2 === "number") return `${v1}[${v2}]`;
|
|
@@ -112,6 +113,11 @@ var handleUnion = (schemas, property, instruction) => {
|
|
|
112
113
|
var mirror = (schema, property, instruction) => {
|
|
113
114
|
if (!schema) return "";
|
|
114
115
|
const isRoot = property === "v" && !instruction.unions.length;
|
|
116
|
+
if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
|
|
117
|
+
return mirror(schema.$defs[schema.$ref], property, {
|
|
118
|
+
...instruction,
|
|
119
|
+
definitions: Object.assign(instruction.definitions, schema.$defs)
|
|
120
|
+
});
|
|
115
121
|
if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf)
|
|
116
122
|
return `return v`;
|
|
117
123
|
let v = "";
|
|
@@ -182,6 +188,12 @@ var mirror = (schema, property, instruction) => {
|
|
|
182
188
|
if (!isRoot) v += `return ar${i}v})()`;
|
|
183
189
|
break;
|
|
184
190
|
default:
|
|
191
|
+
if (schema.$ref && schema.$ref in instruction.definitions)
|
|
192
|
+
return mirror(
|
|
193
|
+
instruction.definitions[schema.$ref],
|
|
194
|
+
property,
|
|
195
|
+
instruction
|
|
196
|
+
);
|
|
185
197
|
if (Array.isArray(schema.anyOf)) {
|
|
186
198
|
v = handleUnion(schema.anyOf, property, instruction);
|
|
187
199
|
break;
|
|
@@ -203,7 +215,10 @@ var mirror = (schema, property, instruction) => {
|
|
|
203
215
|
}
|
|
204
216
|
return `${v}return x`;
|
|
205
217
|
};
|
|
206
|
-
var createMirror = (schema, {
|
|
218
|
+
var createMirror = (schema, {
|
|
219
|
+
TypeCompiler: TypeCompiler2,
|
|
220
|
+
definitions = {}
|
|
221
|
+
} = {}) => {
|
|
207
222
|
const unions = [];
|
|
208
223
|
const f = mirror(schema, "v", {
|
|
209
224
|
optionals: [],
|
|
@@ -212,7 +227,8 @@ var createMirror = (schema, { TypeCompiler: TypeCompiler2 } = {}) => {
|
|
|
212
227
|
parentIsOptional: false,
|
|
213
228
|
unions,
|
|
214
229
|
unionKeys: {},
|
|
215
|
-
TypeCompiler: TypeCompiler2
|
|
230
|
+
TypeCompiler: TypeCompiler2,
|
|
231
|
+
definitions
|
|
216
232
|
});
|
|
217
233
|
if (!unions.length) return Function("v", f);
|
|
218
234
|
const fn = `return function mirror(v){${f}}`;
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ interface Instruction {
|
|
|
18
18
|
*/
|
|
19
19
|
TypeCompiler?: typeof TypeCompiler;
|
|
20
20
|
typeCompilerWanred?: boolean;
|
|
21
|
+
definitions: Record<string, TAnySchema>;
|
|
21
22
|
}
|
|
22
|
-
export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler }?: Pick<Instruction, "TypeCompiler"
|
|
23
|
+
export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, definitions }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions">>) => ((v: T["static"]) => T["static"]);
|
|
23
24
|
export default createMirror;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { TypeCompiler } from "@sinclair/typebox/compiler";
|
|
3
3
|
var Kind = Symbol.for("TypeBox.Kind");
|
|
4
|
+
var OptionalKind = Symbol.for("TypeBox.Optional");
|
|
4
5
|
var isSpecialProperty = (name) => /(\ |-|\t|\n)/.test(name);
|
|
5
6
|
var joinProperty = (v1, v2, isOptional = false) => {
|
|
6
7
|
if (typeof v2 === "number") return `${v1}[${v2}]`;
|
|
@@ -86,6 +87,11 @@ var handleUnion = (schemas, property, instruction) => {
|
|
|
86
87
|
var mirror = (schema, property, instruction) => {
|
|
87
88
|
if (!schema) return "";
|
|
88
89
|
const isRoot = property === "v" && !instruction.unions.length;
|
|
90
|
+
if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
|
|
91
|
+
return mirror(schema.$defs[schema.$ref], property, {
|
|
92
|
+
...instruction,
|
|
93
|
+
definitions: Object.assign(instruction.definitions, schema.$defs)
|
|
94
|
+
});
|
|
89
95
|
if (isRoot && schema.type !== "object" && schema.type !== "array" && !schema.anyOf)
|
|
90
96
|
return `return v`;
|
|
91
97
|
let v = "";
|
|
@@ -156,6 +162,12 @@ var mirror = (schema, property, instruction) => {
|
|
|
156
162
|
if (!isRoot) v += `return ar${i}v})()`;
|
|
157
163
|
break;
|
|
158
164
|
default:
|
|
165
|
+
if (schema.$ref && schema.$ref in instruction.definitions)
|
|
166
|
+
return mirror(
|
|
167
|
+
instruction.definitions[schema.$ref],
|
|
168
|
+
property,
|
|
169
|
+
instruction
|
|
170
|
+
);
|
|
159
171
|
if (Array.isArray(schema.anyOf)) {
|
|
160
172
|
v = handleUnion(schema.anyOf, property, instruction);
|
|
161
173
|
break;
|
|
@@ -177,7 +189,10 @@ var mirror = (schema, property, instruction) => {
|
|
|
177
189
|
}
|
|
178
190
|
return `${v}return x`;
|
|
179
191
|
};
|
|
180
|
-
var createMirror = (schema, {
|
|
192
|
+
var createMirror = (schema, {
|
|
193
|
+
TypeCompiler: TypeCompiler2,
|
|
194
|
+
definitions = {}
|
|
195
|
+
} = {}) => {
|
|
181
196
|
const unions = [];
|
|
182
197
|
const f = mirror(schema, "v", {
|
|
183
198
|
optionals: [],
|
|
@@ -186,7 +201,8 @@ var createMirror = (schema, { TypeCompiler: TypeCompiler2 } = {}) => {
|
|
|
186
201
|
parentIsOptional: false,
|
|
187
202
|
unions,
|
|
188
203
|
unionKeys: {},
|
|
189
|
-
TypeCompiler: TypeCompiler2
|
|
204
|
+
TypeCompiler: TypeCompiler2,
|
|
205
|
+
definitions
|
|
190
206
|
});
|
|
191
207
|
if (!unions.length) return Function("v", f);
|
|
192
208
|
const fn = `return function mirror(v){${f}}`;
|