exact-mirror 0.2.7 → 1.0.0

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,13 +1,46 @@
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
38
  export interface Instruction {
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;
@@ -19,10 +52,10 @@ export interface Instruction {
19
52
  *
20
53
  * @default undefined
21
54
  */
22
- TypeCompiler?: typeof TypeCompiler;
55
+ Compile?: typeof Compile;
23
56
  typeCompilerWanred?: boolean;
24
- modules?: TModule<any, any>;
25
- definitions: Record<string, TAnySchema>;
57
+ modules?: TModule<{}>;
58
+ definitions: Record<string, AnySchema>;
26
59
  recursion: number;
27
60
  /**
28
61
  * @default 8
@@ -39,5 +72,5 @@ export interface Instruction {
39
72
  removeUnknownUnionType: boolean;
40
73
  }
41
74
  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"]);
75
+ export declare const createMirror: <T extends TSchema>(schema: T, { Compile, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "Compile" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: Static<T>) => Static<T>);
43
76
  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,7 +348,7 @@ 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,
@@ -364,7 +364,7 @@ var createMirror = (schema, {
364
364
  parentIsOptional: false,
365
365
  unions,
366
366
  unionKeys: {},
367
- TypeCompiler: TypeCompiler2,
367
+ Compile,
368
368
  modules,
369
369
  // @ts-ignore private property
370
370
  definitions: definitions ?? modules?.$defs ?? {},
package/dist/index.d.ts CHANGED
@@ -1,13 +1,46 @@
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
38
  export interface Instruction {
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;
@@ -19,10 +52,10 @@ export interface Instruction {
19
52
  *
20
53
  * @default undefined
21
54
  */
22
- TypeCompiler?: typeof TypeCompiler;
55
+ Compile?: typeof Compile;
23
56
  typeCompilerWanred?: boolean;
24
- modules?: TModule<any, any>;
25
- definitions: Record<string, TAnySchema>;
57
+ modules?: TModule<{}>;
58
+ definitions: Record<string, AnySchema>;
26
59
  recursion: number;
27
60
  /**
28
61
  * @default 8
@@ -39,5 +72,5 @@ export interface Instruction {
39
72
  removeUnknownUnionType: boolean;
40
73
  }
41
74
  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"]);
75
+ export declare const createMirror: <T extends TSchema>(schema: T, { Compile, modules, definitions, sanitize, recursionLimit, removeUnknownUnionType }?: Partial<Pick<Instruction, "Compile" | "definitions" | "sanitize" | "modules" | "recursionLimit" | "removeUnknownUnionType">>) => ((v: Static<T>) => Static<T>);
43
76
  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,7 +321,7 @@ 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,
@@ -337,7 +337,7 @@ var createMirror = (schema, {
337
337
  parentIsOptional: false,
338
338
  unions,
339
339
  unionKeys: {},
340
- TypeCompiler: TypeCompiler2,
340
+ Compile,
341
341
  modules,
342
342
  // @ts-ignore private property
343
343
  definitions: definitions ?? modules?.$defs ?? {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exact-mirror",
3
- "version": "0.2.7",
3
+ "version": "1.0.0",
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",