exact-mirror 0.2.7 → 1.0.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.
@@ -1,16 +1,50 @@
1
- import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
- import type { TAnySchema, TModule } from '@sinclair/typebox';
3
- export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
1
+ import type { TSchema, TModule, Static } from 'typebox';
2
+ import type { Compile, Validator } from 'typebox/compile';
3
+ interface BaseSchema {
4
+ '~kind': string;
5
+ id?: string;
6
+ $id?: string;
7
+ type?: string;
8
+ $schema?: string;
9
+ const?: unknown[];
10
+ pattern?: string;
11
+ additionalItems?: boolean | AnySchema;
12
+ items?: AnySchema | AnySchema[];
13
+ required?: string[];
14
+ additionalProperties?: boolean | AnySchema;
15
+ definitions?: {
16
+ [name: string]: AnySchema;
17
+ };
18
+ properties?: {
19
+ [name: string]: AnySchema;
20
+ };
21
+ patternProperties?: {
22
+ [name: string]: AnySchema;
23
+ };
24
+ dependencies?: {
25
+ [name: string]: AnySchema | string[];
26
+ };
27
+ enum?: any[];
28
+ allOf?: AnySchema[];
29
+ anyOf?: AnySchema[];
30
+ oneOf?: AnySchema[];
31
+ not?: AnySchema;
32
+ $ref?: string;
33
+ $defs?: Record<string, AnySchema>;
34
+ }
35
+ type AnySchema = TSchema & BaseSchema;
36
+ export declare const mergeObjectIntersection: (schema: AnySchema) => AnySchema;
4
37
  type MaybeArray<T> = T | T[];
5
- export interface Instruction {
38
+ export interface Instruction<Emit extends boolean = false> {
6
39
  optionals: string[];
7
40
  optionalsInArray: string[][];
8
41
  parentIsOptional: boolean;
9
42
  array: number;
10
- unions: TypeCheck<any>[][];
43
+ unions: Validator<any>[][];
11
44
  unionKeys: Record<string, 1>;
12
45
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
46
  fromUnion?: boolean;
47
+ emit?: Emit;
14
48
  /**
15
49
  * TypeCompiler is required when using Union
16
50
  *
@@ -19,10 +53,10 @@ export interface Instruction {
19
53
  *
20
54
  * @default undefined
21
55
  */
22
- TypeCompiler?: typeof TypeCompiler;
56
+ Compile?: typeof Compile;
23
57
  typeCompilerWanred?: boolean;
24
- modules?: TModule<any, any>;
25
- definitions: Record<string, TAnySchema>;
58
+ modules?: TModule<{}>;
59
+ definitions: Record<string, AnySchema>;
26
60
  recursion: number;
27
61
  /**
28
62
  * @default 8
@@ -39,5 +73,9 @@ export interface Instruction {
39
73
  removeUnknownUnionType: boolean;
40
74
  }
41
75
  export declare function deepClone<T>(source: T, weak?: WeakMap<object, any>): T;
42
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: T["static"]) => T["static"]);
76
+ export interface Manifest {
77
+ unions: Validator<any, TSchema, unknown, unknown>[][];
78
+ hof?: Record<string, Function>;
79
+ }
80
+ export declare const createMirror: <T extends TSchema, Emit extends boolean = false>(schema: T, { Compile, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType, emit }?: Partial<Pick<Instruction<Emit>, "Compile" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType" | "emit">>) => Emit extends true ? Manifest : (v: Static<T>) => Static<T>;
43
81
  export default createMirror;
package/dist/cjs/index.js CHANGED
@@ -26,9 +26,8 @@ __export(index_exports, {
26
26
  mergeObjectIntersection: () => mergeObjectIntersection
27
27
  });
28
28
  module.exports = __toCommonJS(index_exports);
29
- var import_compiler = require("@sinclair/typebox/compiler");
30
- var Kind = Symbol.for("TypeBox.Kind");
31
- var Hint = Symbol.for("TypeBox.Hint");
29
+ var Kind = "~kind";
30
+ var Hint = "~hint";
32
31
  var isSpecialProperty = (name) => /(\ |-|\t|\n|\.|\[|\]|\{|\})/.test(name) || !isNaN(+name[0]);
33
32
  var joinProperty = (v1, v2, isOptional = false) => {
34
33
  if (typeof v2 === "number") return `${v1}[${v2}]`;
@@ -124,7 +123,7 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
124
123
  return source;
125
124
  }
126
125
  var handleUnion = (schemas, property, instruction) => {
127
- if (instruction.TypeCompiler === void 0) {
126
+ if (instruction.Compile === void 0) {
128
127
  if (!instruction.typeCompilerWanred) {
129
128
  console.warn(
130
129
  new Error(
@@ -142,19 +141,16 @@ var handleUnion = (schemas, property, instruction) => {
142
141
  `;
143
142
  const unwrapRef = (type) => {
144
143
  if (!(Kind in type) || !type.$ref) return type;
145
- if (type[Kind] === "This") {
144
+ if (type[Kind] === "This")
146
145
  return deepClone(instruction.definitions[type.$ref]);
147
- } else if (type[Kind] === "Ref") {
146
+ else if (type[Kind] === "Cyclic") {
148
147
  if (!instruction.modules)
149
148
  console.warn(
150
149
  new Error(
151
150
  "[exact-mirror] modules is required when using nested cyclic reference"
152
151
  )
153
152
  );
154
- else
155
- return instruction.modules.Import(
156
- type.$ref
157
- );
153
+ else return instruction.modules.$defs[type.$ref];
158
154
  }
159
155
  return type;
160
156
  };
@@ -170,7 +166,7 @@ var handleUnion = (schemas, property, instruction) => {
170
166
  type.items[i2] = unwrapRef(type.items[i2]);
171
167
  else type.items = unwrapRef(type.items);
172
168
  }
173
- typeChecks.push(import_compiler.TypeCompiler.Compile(type));
169
+ typeChecks.push(instruction.Compile(type));
174
170
  v += `if(d.unions[${ui}][${i}].Check(${property})){return ${mirror(
175
171
  type,
176
172
  property,
@@ -198,7 +194,7 @@ if(d.unions[${ui}][${i}].Check(tmp))return tmp
198
194
  var mirror = (schema, property, instruction) => {
199
195
  if (!schema) return "";
200
196
  const isRoot = property === "v" && !instruction.unions.length;
201
- if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
197
+ if (Kind in schema && schema[Kind] === "~Cyclic" && schema.$ref in schema.$defs)
202
198
  return mirror(schema.$defs[schema.$ref], property, {
203
199
  ...instruction,
204
200
  definitions: Object.assign(instruction.definitions, schema.$defs)
@@ -274,7 +270,11 @@ var mirror = (schema, property, instruction) => {
274
270
  v += "}";
275
271
  break;
276
272
  case "array":
277
- if (schema.items.type !== "object" && schema.items.type !== "array") {
273
+ if (
274
+ // @ts-expect-error
275
+ schema.items.type !== "object" && // @ts-expect-error
276
+ schema.items.type !== "array"
277
+ ) {
278
278
  if (Array.isArray(schema.items)) {
279
279
  v = handleTuple(schema.items, property, instruction);
280
280
  break;
@@ -348,12 +348,13 @@ var mirror = (schema, property, instruction) => {
348
348
  return `${v}return x`;
349
349
  };
350
350
  var createMirror = (schema, {
351
- TypeCompiler: TypeCompiler2,
351
+ Compile,
352
352
  modules,
353
353
  definitions,
354
354
  sanitize: sanitize2,
355
355
  recursionLimit = 8,
356
- removeUnknownUnionType = false
356
+ removeUnknownUnionType = false,
357
+ emit
357
358
  } = {}) => {
358
359
  const unions = [];
359
360
  if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
@@ -364,7 +365,7 @@ var createMirror = (schema, {
364
365
  parentIsOptional: false,
365
366
  unions,
366
367
  unionKeys: {},
367
- TypeCompiler: TypeCompiler2,
368
+ Compile,
368
369
  modules,
369
370
  // @ts-ignore private property
370
371
  definitions: definitions ?? modules?.$defs ?? {},
@@ -373,19 +374,29 @@ var createMirror = (schema, {
373
374
  recursionLimit,
374
375
  removeUnknownUnionType
375
376
  });
376
- if (!unions.length && !sanitize2?.length) return Function("v", f);
377
+ if (!unions.length && !sanitize2?.length) {
378
+ if (emit) return { unions, hof: void 0 };
379
+ return Function("v", f);
380
+ }
377
381
  let hof;
378
382
  if (sanitize2?.length) {
379
383
  hof = {};
380
384
  for (let i = 0; i < sanitize2.length; i++) hof[`h${i}`] = sanitize2[i];
381
385
  }
386
+ if (emit)
387
+ return {
388
+ unions,
389
+ hof
390
+ };
382
391
  return Function(
383
392
  "d",
384
393
  `return function mirror(v){${f}}`
385
- )({
386
- unions,
387
- ...hof
388
- });
394
+ )(
395
+ hof ? unions ? {
396
+ unions,
397
+ ...hof
398
+ } : hof : unions ? { unions } : void 0
399
+ );
389
400
  };
390
401
  var index_default = createMirror;
391
402
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.ts CHANGED
@@ -1,16 +1,50 @@
1
- import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
2
- import type { TAnySchema, TModule } from '@sinclair/typebox';
3
- export declare const mergeObjectIntersection: (schema: TAnySchema) => TAnySchema;
1
+ import type { TSchema, TModule, Static } from 'typebox';
2
+ import type { Compile, Validator } from 'typebox/compile';
3
+ interface BaseSchema {
4
+ '~kind': string;
5
+ id?: string;
6
+ $id?: string;
7
+ type?: string;
8
+ $schema?: string;
9
+ const?: unknown[];
10
+ pattern?: string;
11
+ additionalItems?: boolean | AnySchema;
12
+ items?: AnySchema | AnySchema[];
13
+ required?: string[];
14
+ additionalProperties?: boolean | AnySchema;
15
+ definitions?: {
16
+ [name: string]: AnySchema;
17
+ };
18
+ properties?: {
19
+ [name: string]: AnySchema;
20
+ };
21
+ patternProperties?: {
22
+ [name: string]: AnySchema;
23
+ };
24
+ dependencies?: {
25
+ [name: string]: AnySchema | string[];
26
+ };
27
+ enum?: any[];
28
+ allOf?: AnySchema[];
29
+ anyOf?: AnySchema[];
30
+ oneOf?: AnySchema[];
31
+ not?: AnySchema;
32
+ $ref?: string;
33
+ $defs?: Record<string, AnySchema>;
34
+ }
35
+ type AnySchema = TSchema & BaseSchema;
36
+ export declare const mergeObjectIntersection: (schema: AnySchema) => AnySchema;
4
37
  type MaybeArray<T> = T | T[];
5
- export interface Instruction {
38
+ export interface Instruction<Emit extends boolean = false> {
6
39
  optionals: string[];
7
40
  optionalsInArray: string[][];
8
41
  parentIsOptional: boolean;
9
42
  array: number;
10
- unions: TypeCheck<any>[][];
43
+ unions: Validator<any>[][];
11
44
  unionKeys: Record<string, 1>;
12
45
  sanitize: MaybeArray<(v: string) => string> | undefined;
13
46
  fromUnion?: boolean;
47
+ emit?: Emit;
14
48
  /**
15
49
  * TypeCompiler is required when using Union
16
50
  *
@@ -19,10 +53,10 @@ export interface Instruction {
19
53
  *
20
54
  * @default undefined
21
55
  */
22
- TypeCompiler?: typeof TypeCompiler;
56
+ Compile?: typeof Compile;
23
57
  typeCompilerWanred?: boolean;
24
- modules?: TModule<any, any>;
25
- definitions: Record<string, TAnySchema>;
58
+ modules?: TModule<{}>;
59
+ definitions: Record<string, AnySchema>;
26
60
  recursion: number;
27
61
  /**
28
62
  * @default 8
@@ -39,5 +73,9 @@ export interface Instruction {
39
73
  removeUnknownUnionType: boolean;
40
74
  }
41
75
  export declare function deepClone<T>(source: T, weak?: WeakMap<object, any>): T;
42
- export declare const createMirror: <T extends TAnySchema>(schema: T, { TypeCompiler, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "TypeCompiler" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: T["static"]) => T["static"]);
76
+ export interface Manifest {
77
+ unions: Validator<any, TSchema, unknown, unknown>[][];
78
+ hof?: Record<string, Function>;
79
+ }
80
+ export declare const createMirror: <T extends TSchema, Emit extends boolean = false>(schema: T, { Compile, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType, emit }?: Partial<Pick<Instruction<Emit>, "Compile" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType" | "emit">>) => Emit extends true ? Manifest : (v: Static<T>) => Static<T>;
43
81
  export default createMirror;
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  // src/index.ts
2
- import { TypeCompiler } from "@sinclair/typebox/compiler";
3
- var Kind = Symbol.for("TypeBox.Kind");
4
- var Hint = Symbol.for("TypeBox.Hint");
2
+ var Kind = "~kind";
3
+ var Hint = "~hint";
5
4
  var isSpecialProperty = (name) => /(\ |-|\t|\n|\.|\[|\]|\{|\})/.test(name) || !isNaN(+name[0]);
6
5
  var joinProperty = (v1, v2, isOptional = false) => {
7
6
  if (typeof v2 === "number") return `${v1}[${v2}]`;
@@ -97,7 +96,7 @@ function deepClone(source, weak = /* @__PURE__ */ new WeakMap()) {
97
96
  return source;
98
97
  }
99
98
  var handleUnion = (schemas, property, instruction) => {
100
- if (instruction.TypeCompiler === void 0) {
99
+ if (instruction.Compile === void 0) {
101
100
  if (!instruction.typeCompilerWanred) {
102
101
  console.warn(
103
102
  new Error(
@@ -115,19 +114,16 @@ var handleUnion = (schemas, property, instruction) => {
115
114
  `;
116
115
  const unwrapRef = (type) => {
117
116
  if (!(Kind in type) || !type.$ref) return type;
118
- if (type[Kind] === "This") {
117
+ if (type[Kind] === "This")
119
118
  return deepClone(instruction.definitions[type.$ref]);
120
- } else if (type[Kind] === "Ref") {
119
+ else if (type[Kind] === "Cyclic") {
121
120
  if (!instruction.modules)
122
121
  console.warn(
123
122
  new Error(
124
123
  "[exact-mirror] modules is required when using nested cyclic reference"
125
124
  )
126
125
  );
127
- else
128
- return instruction.modules.Import(
129
- type.$ref
130
- );
126
+ else return instruction.modules.$defs[type.$ref];
131
127
  }
132
128
  return type;
133
129
  };
@@ -143,7 +139,7 @@ var handleUnion = (schemas, property, instruction) => {
143
139
  type.items[i2] = unwrapRef(type.items[i2]);
144
140
  else type.items = unwrapRef(type.items);
145
141
  }
146
- typeChecks.push(TypeCompiler.Compile(type));
142
+ typeChecks.push(instruction.Compile(type));
147
143
  v += `if(d.unions[${ui}][${i}].Check(${property})){return ${mirror(
148
144
  type,
149
145
  property,
@@ -171,7 +167,7 @@ if(d.unions[${ui}][${i}].Check(tmp))return tmp
171
167
  var mirror = (schema, property, instruction) => {
172
168
  if (!schema) return "";
173
169
  const isRoot = property === "v" && !instruction.unions.length;
174
- if (Kind in schema && schema[Kind] === "Import" && schema.$ref in schema.$defs)
170
+ if (Kind in schema && schema[Kind] === "~Cyclic" && schema.$ref in schema.$defs)
175
171
  return mirror(schema.$defs[schema.$ref], property, {
176
172
  ...instruction,
177
173
  definitions: Object.assign(instruction.definitions, schema.$defs)
@@ -247,7 +243,11 @@ var mirror = (schema, property, instruction) => {
247
243
  v += "}";
248
244
  break;
249
245
  case "array":
250
- if (schema.items.type !== "object" && schema.items.type !== "array") {
246
+ if (
247
+ // @ts-expect-error
248
+ schema.items.type !== "object" && // @ts-expect-error
249
+ schema.items.type !== "array"
250
+ ) {
251
251
  if (Array.isArray(schema.items)) {
252
252
  v = handleTuple(schema.items, property, instruction);
253
253
  break;
@@ -321,12 +321,13 @@ var mirror = (schema, property, instruction) => {
321
321
  return `${v}return x`;
322
322
  };
323
323
  var createMirror = (schema, {
324
- TypeCompiler: TypeCompiler2,
324
+ Compile,
325
325
  modules,
326
326
  definitions,
327
327
  sanitize: sanitize2,
328
328
  recursionLimit = 8,
329
- removeUnknownUnionType = false
329
+ removeUnknownUnionType = false,
330
+ emit
330
331
  } = {}) => {
331
332
  const unions = [];
332
333
  if (typeof sanitize2 === "function") sanitize2 = [sanitize2];
@@ -337,7 +338,7 @@ var createMirror = (schema, {
337
338
  parentIsOptional: false,
338
339
  unions,
339
340
  unionKeys: {},
340
- TypeCompiler: TypeCompiler2,
341
+ Compile,
341
342
  modules,
342
343
  // @ts-ignore private property
343
344
  definitions: definitions ?? modules?.$defs ?? {},
@@ -346,19 +347,29 @@ var createMirror = (schema, {
346
347
  recursionLimit,
347
348
  removeUnknownUnionType
348
349
  });
349
- if (!unions.length && !sanitize2?.length) return Function("v", f);
350
+ if (!unions.length && !sanitize2?.length) {
351
+ if (emit) return { unions, hof: void 0 };
352
+ return Function("v", f);
353
+ }
350
354
  let hof;
351
355
  if (sanitize2?.length) {
352
356
  hof = {};
353
357
  for (let i = 0; i < sanitize2.length; i++) hof[`h${i}`] = sanitize2[i];
354
358
  }
359
+ if (emit)
360
+ return {
361
+ unions,
362
+ hof
363
+ };
355
364
  return Function(
356
365
  "d",
357
366
  `return function mirror(v){${f}}`
358
- )({
359
- unions,
360
- ...hof
361
- });
367
+ )(
368
+ hof ? unions ? {
369
+ unions,
370
+ ...hof
371
+ } : hof : unions ? { unions } : void 0
372
+ );
362
373
  };
363
374
  var index_default = createMirror;
364
375
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.2.7",
3
+ "version": "1.0.1",
4
4
  "description": "Mirror exact value to TypeBox/OpenAPI model",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -11,21 +11,22 @@
11
11
  "release": "npm run build && npm run test && npm publish --access public"
12
12
  },
13
13
  "peerDependencies": {
14
- "@sinclair/typebox": "^0.34.15"
14
+ "typebox": ">= 1.1.0"
15
15
  },
16
16
  "peerDependenciesMeta": {
17
- "@sinclair/typebox": {
17
+ "typebox": {
18
18
  "optional": true
19
19
  }
20
20
  },
21
21
  "devDependencies": {
22
- "@types/bun": "1.2.2",
23
- "elysia": "^1.2.11",
22
+ "@types/bun": "1.3.8",
23
+ "elysia": "^1.4.28",
24
24
  "eslint": "9.6.0",
25
25
  "mitata": "^1.0.33",
26
26
  "tsup": "^8.1.0",
27
27
  "tsx": "^4.19.2",
28
- "typescript": "^5.5.3"
28
+ "typebox": "^1.1.15",
29
+ "typescript": "^6.0.0"
29
30
  },
30
31
  "main": "./dist/cjs/index.js",
31
32
  "module": "./dist/index.mjs",