@powerlines/schema 0.11.28 → 0.11.38

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.
Files changed (82) hide show
  1. package/dist/bundle.cjs +34 -20
  2. package/dist/bundle.d.cts +3 -3
  3. package/dist/bundle.d.cts.map +1 -1
  4. package/dist/bundle.d.mts +3 -3
  5. package/dist/bundle.d.mts.map +1 -1
  6. package/dist/bundle.mjs +35 -21
  7. package/dist/bundle.mjs.map +1 -1
  8. package/dist/codegen.cjs +55 -8
  9. package/dist/codegen.d.cts +12 -6
  10. package/dist/codegen.d.cts.map +1 -1
  11. package/dist/codegen.d.mts +12 -6
  12. package/dist/codegen.d.mts.map +1 -1
  13. package/dist/codegen.mjs +52 -8
  14. package/dist/codegen.mjs.map +1 -1
  15. package/dist/constants.cjs +42 -0
  16. package/dist/constants.d.cts +15 -0
  17. package/dist/constants.d.cts.map +1 -0
  18. package/dist/constants.d.mts +15 -0
  19. package/dist/constants.d.mts.map +1 -0
  20. package/dist/constants.mjs +39 -0
  21. package/dist/constants.mjs.map +1 -0
  22. package/dist/extract.cjs +144 -95
  23. package/dist/extract.d.cts +34 -69
  24. package/dist/extract.d.cts.map +1 -1
  25. package/dist/extract.d.mts +34 -69
  26. package/dist/extract.d.mts.map +1 -1
  27. package/dist/extract.mjs +144 -95
  28. package/dist/extract.mjs.map +1 -1
  29. package/dist/helpers.cjs +87 -0
  30. package/dist/helpers.d.cts +48 -0
  31. package/dist/helpers.d.cts.map +1 -0
  32. package/dist/helpers.d.mts +48 -0
  33. package/dist/helpers.d.mts.map +1 -0
  34. package/dist/helpers.mjs +84 -0
  35. package/dist/helpers.mjs.map +1 -0
  36. package/dist/index.cjs +29 -5
  37. package/dist/index.d.cts +8 -5
  38. package/dist/index.d.mts +8 -5
  39. package/dist/index.mjs +7 -4
  40. package/dist/metadata.cjs +80 -0
  41. package/dist/metadata.d.cts +52 -0
  42. package/dist/metadata.d.cts.map +1 -0
  43. package/dist/metadata.d.mts +52 -0
  44. package/dist/metadata.d.mts.map +1 -0
  45. package/dist/metadata.mjs +76 -0
  46. package/dist/metadata.mjs.map +1 -0
  47. package/dist/persistence.cjs +75 -0
  48. package/dist/persistence.d.cts +47 -0
  49. package/dist/persistence.d.cts.map +1 -0
  50. package/dist/persistence.d.mts +47 -0
  51. package/dist/persistence.d.mts.map +1 -0
  52. package/dist/persistence.mjs +71 -0
  53. package/dist/persistence.mjs.map +1 -0
  54. package/dist/reflection.cjs +292 -299
  55. package/dist/reflection.d.cts +3 -16
  56. package/dist/reflection.d.cts.map +1 -1
  57. package/dist/reflection.d.mts +3 -16
  58. package/dist/reflection.d.mts.map +1 -1
  59. package/dist/reflection.mjs +291 -299
  60. package/dist/reflection.mjs.map +1 -1
  61. package/dist/resolve.cjs +7 -7
  62. package/dist/resolve.mjs +7 -7
  63. package/dist/resolve.mjs.map +1 -1
  64. package/dist/type-checks.cjs +126 -25
  65. package/dist/type-checks.d.cts +45 -23
  66. package/dist/type-checks.d.cts.map +1 -1
  67. package/dist/type-checks.d.mts +45 -23
  68. package/dist/type-checks.d.mts.map +1 -1
  69. package/dist/type-checks.mjs +125 -25
  70. package/dist/type-checks.mjs.map +1 -1
  71. package/dist/types.d.cts +237 -95
  72. package/dist/types.d.cts.map +1 -1
  73. package/dist/types.d.mts +237 -95
  74. package/dist/types.d.mts.map +1 -1
  75. package/package.json +24 -8
  76. package/dist/jtd.cjs +0 -385
  77. package/dist/jtd.d.cts +0 -15
  78. package/dist/jtd.d.cts.map +0 -1
  79. package/dist/jtd.d.mts +0 -15
  80. package/dist/jtd.d.mts.map +0 -1
  81. package/dist/jtd.mjs +0 -384
  82. package/dist/jtd.mjs.map +0 -1
@@ -1,90 +1,54 @@
1
- import { ExtractedSchema, JTDSchemaType, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant } from "./types.mjs";
2
- import { BuildOptions } from "esbuild";
1
+ import { ExtractedSchema, JsonSchema, Schema, SchemaInput, SchemaInputVariant, SchemaSource, SchemaSourceInput, SchemaSourceVariant } from "./types.mjs";
2
+ import { BuildOptions } from "rolldown";
3
3
  import { Context } from "@powerlines/core";
4
4
  import { Type } from "@powerlines/deepkit/vendor/type";
5
5
 
6
6
  //#region src/extract.d.ts
7
7
  /**
8
- * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.
8
+ * Creates a hash string for a given schema definition input.
9
9
  */
10
- declare function extractHash(variant: SchemaInputVariant, input: SchemaInput): string;
10
+ declare function extractHash<T = unknown>(variant: SchemaInputVariant, input: SchemaInput<T>): string;
11
11
  /**
12
- * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.
13
- *
14
- * @remarks
15
- * This function delegates to an internal recursive walker that handles the full set of Deepkit reflection kinds.
16
- *
17
- * @param reflection - The reflected Deepkit Type to convert.
18
- * @returns A JSON Schema (draft-07) fragment representing the type, or `undefined` when no schema could be produced.
12
+ * Converts a reflected Deepkit {@link Type} into a JSON Schema (draft-07) representation.
19
13
  */
20
- declare function extractReflection<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(reflection: Type): JTDSchemaType<TMetadata> | undefined;
14
+ declare function extractReflection<T = unknown>(reflection: Type): JsonSchema<T> | undefined;
21
15
  /**
22
- * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.
23
- *
24
- * @remarks
25
- * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.
26
- *
27
- * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
28
- * @returns The extracted JTD schema if successful, otherwise undefined.
29
- */
30
- declare function extractJsonSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(schema: unknown): JTDSchemaType<TMetadata> | undefined;
31
- /**
32
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
33
- *
34
- * @param input - The input object to extract the schema definition from.
35
- * @returns A `Schema` containing the extracted schema and its variant if successful.
36
- * @throws An error if the input does not contain a valid schema definition.
16
+ * Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.
37
17
  */
18
+ declare function extractJsonSchema<T = unknown>(schema: unknown): JsonSchema<T> | undefined;
38
19
  declare function extractResolvedVariant(input: SchemaSourceInput): SchemaSourceVariant;
20
+ declare function extractVariant<T = unknown>(input: SchemaInput<T>): SchemaInputVariant;
21
+ declare function extractSchemaSchema<T = unknown>(input: SchemaSourceInput, variant?: SchemaInputVariant): Promise<JsonSchema<T>>;
22
+ declare function extractSource(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource;
39
23
  /**
40
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
41
- *
42
- * @param input - The input object to extract the schema definition from.
43
- * @returns A `Schema` containing the extracted schema and its variant if successful.
44
- * @throws An error if the input does not contain a valid schema definition.
45
- */
46
- declare function extractVariant(input: SchemaInput): SchemaInputVariant;
47
- /**
48
- * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.
49
- *
50
- * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
51
- * @param variant - The variant of the schema definition to extract.
52
- * @returns The extracted JSON Schema if successful.
53
- * @throws An error if the input does not contain a valid schema definition.
54
- */
55
- declare function extractSchemaSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(input: SchemaSourceInput, variant?: SchemaInputVariant): Promise<JTDSchemaType<TMetadata>>;
56
- /**
57
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
58
- *
59
- * @param variant - The variant of the schema definition to extract.
60
- * @param input - The input object to extract the schema definition from.
61
- * @returns A `Schema` containing the extracted schema and its variant if successful.
62
- * @throws An error if the input does not contain a valid schema definition.
63
- */
64
- declare function extractSource<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource;
65
- /**
66
- * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
24
+ * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.
67
25
  *
68
26
  * @example
69
27
  * ```ts
70
28
  * // Resolve a schema definition from a file path
71
- * const schema1 = await extractSchema(context, "./schemas.ts#MySchema");
29
+ * const schema1 = await extract(context, "./schemas.ts#MySchema");
72
30
  * // Resolve a schema definition from a JSON Schema object
73
- * const schema2 = await extractSchema(context, schemaObject);
31
+ * const schema2 = await extract(context, schemaObject);
74
32
  * // Resolve a schema definition from a Zod schema
75
- * const schema3 = await extractSchema(context, zodSchema);
33
+ * const schema3 = await extract(context, zodSchema);
76
34
  * // Resolve a schema definition from a reflected Deepkit Type object
77
- * const schema4 = await extractSchema(context, reflectionType);
35
+ * const schema4 = await extract(context, reflectionType);
78
36
  * ```
79
37
  *
80
- * @param context - The plugin context used for resolving the schema definition input.
81
- * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
82
- * @param options - Optional overrides for the ESBuild configuration used during resolution.
83
- * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
38
+ * @see https://github.com/colinhacks/zod
39
+ * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
40
+ * @see https://json-schema.org/
41
+ * @see https://ajv.js.org/json-type-definition.html
42
+ * @see https://deepkit.io/en/documentation/runtime-types/reflection
43
+ *
44
+ * @param context - The context object providing access to the file system and cache path.
45
+ * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a string or a type definition reference, it will be resolved and bundled to obtain the actual schema definition before extraction.
46
+ * @param options - Optional overrides for the ESBuild configuration used during extraction. This can include custom plugins, loaders, or other build options to control how the schema definition is resolved and bundled when the input is a type definition reference.
47
+ * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object. The function will attempt to extract a valid JSON Schema from the provided input, and if successful, it will return the schema. If the extraction process fails or if the input is not a valid schema definition, it will throw an error indicating the failure.
84
48
  */
85
- declare function extractSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>, TContext extends Context = Context>(context: TContext, input: SchemaInput, options?: Partial<BuildOptions>): Promise<ExtractedSchema<TMetadata>>;
49
+ declare function extractSchema<T = unknown>(context: Context, input: SchemaInput, options?: Partial<BuildOptions>): Promise<ExtractedSchema<T>>;
86
50
  /**
87
- * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
51
+ * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.
88
52
  *
89
53
  * @example
90
54
  * ```ts
@@ -106,12 +70,13 @@ declare function extractSchema<TMetadata extends Partial<SchemaMetadata> = Parti
106
70
  * @see https://github.com/unjs/untyped
107
71
  * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
108
72
  *
109
- * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.
110
- * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).
111
- * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.
112
- * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.
73
+ * @param context - The context object providing access to the file system and cache path.
74
+ * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.
75
+ * @param options - Optional overrides for the ESBuild configuration used during extraction.
76
+ * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.
77
+ * @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.
113
78
  */
114
- declare function extract<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>, TContext extends Context = Context>(context: TContext, input: SchemaInput, options?: Partial<BuildOptions>): Promise<Schema<TMetadata>>;
79
+ declare function extract<T = unknown>(context: Context, input: SchemaInput, options?: Partial<BuildOptions>): Promise<Schema<T>>;
115
80
  //#endregion
116
81
  export { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant };
117
82
  //# sourceMappingURL=extract.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;AAmEA;iBAAgB,WAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAA;;;;;;;;;AA8BT;iBAAgB,iBAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,UAAA,EAAY,IAAA,GAAO,aAAA,CAAc,SAAA;;;;;;;;;;iBAiBnB,iBAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,MAAA,YAAkB,aAAA,CAAc,SAAA;;;;;;;;iBAsClB,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAA;;;;AA1CH;;;;iBAuEgB,cAAA,CAAe,KAAA,EAAO,WAAA,GAAc,kBAAA;;;;;;;;;iBAgB9B,mBAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CAEpD,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,aAAA,CAAc,SAAA;;;;;;;;AApDzB;iBA6FgB,aAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,OAAA,EAAS,mBAAA,EAAqB,KAAA,EAAO,iBAAA,GAAoB,YAAA;;;;;;;;AAhE3D;;;;;;;;;AAgBA;;;;iBAgHsB,aAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,oBACnC,OAAA,GAAU,OAAA,CAAA,CAE3B,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,eAAA,CAAgB,SAAA;;;;;;;;;;;;;;;;;;;;;;;;AAzE3B;;;;;iBAkKsB,OAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,oBACnC,OAAA,GAAU,OAAA,CAAA,CAE3B,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,MAAA,CAAO,SAAA"}
1
+ {"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;AAiNA;iBAAgB,WAAA,aAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAA,CAAY,CAAA;;;;iBAkCL,iBAAA,aAAA,CACd,UAAA,EAAY,IAAA,GACX,UAAA,CAAW,CAAA;;;;iBAWE,iBAAA,aAAA,CACd,MAAA,YACC,UAAU,CAAC,CAAA;AAAA,iBA6BE,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAmB;AAAA,iBAoBN,cAAA,aAAA,CACd,KAAA,EAAO,WAAA,CAAY,CAAA,IAClB,kBAAA;AAAA,iBAQmB,mBAAA,aAAA,CACpB,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,UAAA,CAAW,CAAA;AAAA,iBA4BN,aAAA,CACd,OAAA,EAAS,mBAAA,EACT,KAAA,EAAO,iBAAA,GACN,YAAA;;AAhJoB;AAkCvB;;;;;;;;;;;;;;AAEe;AAWf;;;;;;;;;iBAiKsB,aAAA,aAAA,CACpB,OAAA,EAAS,OAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,eAAA,CAAgB,CAAA;AAtI3B;;;;;;;;AAEsB;AAoBtB;;;;;;;;;;;;;;AAEqB;AAQrB;;;;;AAhCA,iBAiOsB,OAAA,aAAA,CACpB,OAAA,EAAS,OAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,MAAA,CAAO,CAAA"}
package/dist/extract.mjs CHANGED
@@ -1,20 +1,91 @@
1
- import { jsonSchemaToJtd } from "./jtd.mjs";
1
+ import { isExtractedSchema, isJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
2
+ import { getCacheDirectory, writeSchema } from "./persistence.mjs";
2
3
  import { reflectionToJsonSchema } from "./reflection.mjs";
3
4
  import { resolve } from "./resolve.mjs";
4
- import { isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
5
5
  import defu from "defu";
6
+ import { isSetString } from "@stryke/type-checks";
7
+ import { isSetObject as isSetObject$1 } from "@stryke/type-checks/is-set-object";
8
+ import { isStandardJsonSchema } from "@stryke/json";
6
9
  import { isTypeDefinition } from "@powerlines/core";
7
- import { esbuildPlugin } from "@powerlines/deepkit/esbuild-plugin";
10
+ import { rolldownPlugin } from "@powerlines/deepkit/rolldown-plugin";
8
11
  import { isType, stringifyType } from "@powerlines/deepkit/vendor/type";
9
12
  import { murmurhash } from "@stryke/hash";
10
- import { isJsonSchemaObjectType, isStandardJsonSchema } from "@stryke/json";
11
- import { isSetString } from "@stryke/type-checks";
12
- import { isSetObject as isSetObject$1 } from "@stryke/type-checks/is-set-object";
13
+ import { joinPaths } from "@stryke/path/join";
13
14
  import { extractJsonSchema as extractJsonSchema$1, isZod3Type } from "@stryke/zod";
14
15
 
15
16
  //#region src/extract.ts
17
+ function convertNestedUntypedSchema(value) {
18
+ if (isUntypedSchema(value)) return convertUntypedSchemaToJsonSchema(value);
19
+ if (isSetObject$1(value)) {
20
+ if (isUntypedInput(value)) return convertUntypedInputToJsonSchema(value);
21
+ const nested = value;
22
+ if ("$schema" in nested && isUntypedSchema(nested.$schema)) return convertUntypedSchemaToJsonSchema(nested.$schema);
23
+ }
24
+ return value;
25
+ }
26
+ function convertNestedUntypedSchemaArray(value) {
27
+ if (!Array.isArray(value)) return value;
28
+ return value.map((item) => convertNestedUntypedSchema(item));
29
+ }
30
+ function convertUntypedSchemaToJsonSchema(schema) {
31
+ const source = schema;
32
+ const jsonSchema = {};
33
+ for (const [key, value] of Object.entries(source)) {
34
+ if (key === "tsType" || key === "markdownType" || key === "tags" || key === "args" || key === "resolve") continue;
35
+ if (key === "id" && isSetString(value)) {
36
+ jsonSchema.$id = value;
37
+ continue;
38
+ }
39
+ if (key === "properties" || key === "patternProperties" || key === "dependentSchemas" || key === "$defs" || key === "definitions") {
40
+ if (!isSetObject$1(value)) {
41
+ jsonSchema[key] = value;
42
+ continue;
43
+ }
44
+ jsonSchema[key] = Object.fromEntries(Object.entries(value).map(([propertyKey, propertyValue]) => [propertyKey, convertNestedUntypedSchema(propertyValue)]));
45
+ continue;
46
+ }
47
+ if (key === "items" || key === "contains" || key === "if" || key === "then" || key === "else" || key === "not" || key === "propertyNames" || key === "additionalProperties" || key === "unevaluatedProperties") {
48
+ jsonSchema[key] = convertNestedUntypedSchema(value);
49
+ continue;
50
+ }
51
+ if (key === "oneOf" || key === "anyOf" || key === "allOf") {
52
+ jsonSchema[key] = convertNestedUntypedSchemaArray(value);
53
+ continue;
54
+ }
55
+ jsonSchema[key] = value;
56
+ }
57
+ return jsonSchema;
58
+ }
59
+ function convertUntypedInputToJsonSchema(input) {
60
+ const inputObject = input;
61
+ const base = isUntypedSchema(inputObject.$schema) ? convertUntypedSchemaToJsonSchema(inputObject.$schema) : {};
62
+ const properties = {};
63
+ for (const [key, value] of Object.entries(inputObject)) {
64
+ if (key.startsWith("$")) continue;
65
+ if (!isSetObject$1(value)) continue;
66
+ if (isUntypedInput(value)) {
67
+ properties[key] = convertUntypedInputToJsonSchema(value);
68
+ continue;
69
+ }
70
+ const nested = value;
71
+ if ("$schema" in nested && isUntypedSchema(nested.$schema)) {
72
+ properties[key] = convertUntypedSchemaToJsonSchema(nested.$schema);
73
+ continue;
74
+ }
75
+ if (isUntypedSchema(value)) properties[key] = convertUntypedSchemaToJsonSchema(value);
76
+ }
77
+ const mergedProperties = {
78
+ ...isSetObject$1(base.properties) ? base.properties : {},
79
+ ...properties
80
+ };
81
+ return {
82
+ ...base,
83
+ type: base.type ?? "object",
84
+ ...Object.keys(mergedProperties).length > 0 ? { properties: mergedProperties } : {}
85
+ };
86
+ }
16
87
  /**
17
- * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.
88
+ * Creates a hash string for a given schema definition input.
18
89
  */
19
90
  function extractHash(variant, input) {
20
91
  if (isSetString(input)) return murmurhash({
@@ -30,10 +101,18 @@ function extractHash(variant, input) {
30
101
  variant,
31
102
  input: input["~standard"]
32
103
  });
33
- else if (isJsonSchemaObjectType(input)) return murmurhash({
104
+ else if (isJsonSchema(input)) return murmurhash({
34
105
  variant,
35
106
  input
36
107
  });
108
+ else if (isUntypedInput(input)) return murmurhash({
109
+ variant,
110
+ input: convertUntypedInputToJsonSchema(input)
111
+ });
112
+ else if (isUntypedSchema(input)) return murmurhash({
113
+ variant,
114
+ input: convertUntypedSchemaToJsonSchema(input)
115
+ });
37
116
  else if (isType(input)) return murmurhash({
38
117
  variant,
39
118
  input: stringifyType(input)
@@ -42,95 +121,47 @@ function extractHash(variant, input) {
42
121
  throw new Error(`Failed to create an input hash for the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object.`);
43
122
  }
44
123
  /**
45
- * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.
46
- *
47
- * @remarks
48
- * This function delegates to an internal recursive walker that handles the full set of Deepkit reflection kinds.
49
- *
50
- * @param reflection - The reflected Deepkit Type to convert.
51
- * @returns A JSON Schema (draft-07) fragment representing the type, or `undefined` when no schema could be produced.
124
+ * Converts a reflected Deepkit {@link Type} into a JSON Schema (draft-07) representation.
52
125
  */
53
126
  function extractReflection(reflection) {
54
127
  if (!isType(reflection)) return;
55
128
  return reflectionToJsonSchema(reflection);
56
129
  }
57
130
  /**
58
- * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.
59
- *
60
- * @remarks
61
- * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.
62
- *
63
- * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
64
- * @returns The extracted JTD schema if successful, otherwise undefined.
131
+ * Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.
65
132
  */
66
133
  function extractJsonSchema(schema) {
67
- if (isSetObject$1(schema) && (isZod3Type(schema) || isStandardJsonSchema(schema) || isJsonSchemaObjectType(schema) || isUntypedInput(schema) || isUntypedSchema(schema))) {
68
- let jsonSchema;
69
- if (isZod3Type(schema)) jsonSchema = extractJsonSchema$1(schema);
70
- else if (isStandardJsonSchema(schema)) jsonSchema = schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
71
- else if (isUntypedInput(schema)) jsonSchema = schema.$schema;
72
- else jsonSchema = schema;
73
- const jtd = jsonSchemaToJtd(jsonSchema);
74
- if (jtd) return jtd;
134
+ if (isSetObject$1(schema) && (isZod3Type(schema) || isStandardJsonSchema(schema) || isJsonSchema(schema) || isUntypedInput(schema) || isUntypedSchema(schema))) {
135
+ if (isZod3Type(schema)) return extractJsonSchema$1(schema);
136
+ if (isStandardJsonSchema(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
137
+ if (isUntypedInput(schema)) return convertUntypedInputToJsonSchema(schema);
138
+ if (isUntypedSchema(schema)) return convertUntypedSchemaToJsonSchema(schema);
139
+ return schema;
75
140
  }
76
141
  }
77
- /**
78
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
79
- *
80
- * @param input - The input object to extract the schema definition from.
81
- * @returns A `Schema` containing the extracted schema and its variant if successful.
82
- * @throws An error if the input does not contain a valid schema definition.
83
- */
84
142
  function extractResolvedVariant(input) {
85
143
  if (isSetObject$1(input)) {
86
144
  if (isZod3Type(input)) return "zod3";
87
145
  else if (isStandardJsonSchema(input)) return "standard-schema";
88
- else if (isJTDSchema(input)) return "jtd-schema";
89
- else if (isJsonSchemaObjectType(input)) return "json-schema";
146
+ else if (isJsonSchema(input)) return "json-schema";
90
147
  else if (isType(input)) return "reflection";
91
148
  else if (isUntypedInput(input) || isUntypedSchema(input)) return "untyped";
92
149
  }
93
150
  throw new Error(`Failed to determine the variant of the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema.`);
94
151
  }
95
- /**
96
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
97
- *
98
- * @param input - The input object to extract the schema definition from.
99
- * @returns A `Schema` containing the extracted schema and its variant if successful.
100
- * @throws An error if the input does not contain a valid schema definition.
101
- */
102
152
  function extractVariant(input) {
103
153
  if (isSetString(input) || isTypeDefinition(input)) return "type-definition";
104
154
  return extractResolvedVariant(input);
105
155
  }
106
- /**
107
- * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.
108
- *
109
- * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
110
- * @param variant - The variant of the schema definition to extract.
111
- * @returns The extracted JSON Schema if successful.
112
- * @throws An error if the input does not contain a valid schema definition.
113
- */
114
156
  async function extractSchemaSchema(input, variant) {
115
157
  if (isExtractedSchema(input)) return input.schema;
116
158
  const resolvedVariant = variant ?? extractResolvedVariant(input);
117
159
  let schema;
118
- if (resolvedVariant === "zod3" || resolvedVariant === "json-schema" || resolvedVariant === "standard-schema" || resolvedVariant === "untyped") {
119
- const jsonSchema = extractJsonSchema(input);
120
- if (jsonSchema) schema = jsonSchemaToJtd(jsonSchema);
121
- } else if (resolvedVariant === "reflection") schema = extractReflection(input);
122
- else if (resolvedVariant === "jtd-schema") schema = input;
160
+ if (resolvedVariant === "zod3" || resolvedVariant === "json-schema" || resolvedVariant === "standard-schema" || resolvedVariant === "untyped") schema = extractJsonSchema(input);
161
+ else if (resolvedVariant === "reflection") schema = extractReflection(input);
123
162
  if (schema) return schema;
124
- throw new Error(`Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`);
163
+ throw new Error(`Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`);
125
164
  }
126
- /**
127
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
128
- *
129
- * @param variant - The variant of the schema definition to extract.
130
- * @param input - The input object to extract the schema definition from.
131
- * @returns A `Schema` containing the extracted schema and its variant if successful.
132
- * @throws An error if the input does not contain a valid schema definition.
133
- */
134
165
  function extractSource(variant, input) {
135
166
  if (variant === "zod3") return {
136
167
  hash: extractHash(variant, input),
@@ -157,69 +188,70 @@ function extractSource(variant, input) {
157
188
  variant: "reflection",
158
189
  schema: input
159
190
  };
160
- else if (variant === "jtd-schema") return {
161
- hash: extractHash(variant, input),
162
- variant: "jtd-schema",
163
- schema: input
164
- };
165
- throw new Error(`Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`);
191
+ throw new Error(`Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`);
166
192
  }
167
193
  /**
168
- * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
194
+ * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.
169
195
  *
170
196
  * @example
171
197
  * ```ts
172
198
  * // Resolve a schema definition from a file path
173
- * const schema1 = await extractSchema(context, "./schemas.ts#MySchema");
199
+ * const schema1 = await extract(context, "./schemas.ts#MySchema");
174
200
  * // Resolve a schema definition from a JSON Schema object
175
- * const schema2 = await extractSchema(context, schemaObject);
201
+ * const schema2 = await extract(context, schemaObject);
176
202
  * // Resolve a schema definition from a Zod schema
177
- * const schema3 = await extractSchema(context, zodSchema);
203
+ * const schema3 = await extract(context, zodSchema);
178
204
  * // Resolve a schema definition from a reflected Deepkit Type object
179
- * const schema4 = await extractSchema(context, reflectionType);
205
+ * const schema4 = await extract(context, reflectionType);
180
206
  * ```
181
207
  *
182
- * @param context - The plugin context used for resolving the schema definition input.
183
- * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
184
- * @param options - Optional overrides for the ESBuild configuration used during resolution.
185
- * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
208
+ * @see https://github.com/colinhacks/zod
209
+ * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
210
+ * @see https://json-schema.org/
211
+ * @see https://ajv.js.org/json-type-definition.html
212
+ * @see https://deepkit.io/en/documentation/runtime-types/reflection
213
+ *
214
+ * @param context - The context object providing access to the file system and cache path.
215
+ * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a string or a type definition reference, it will be resolved and bundled to obtain the actual schema definition before extraction.
216
+ * @param options - Optional overrides for the ESBuild configuration used during extraction. This can include custom plugins, loaders, or other build options to control how the schema definition is resolved and bundled when the input is a type definition reference.
217
+ * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object. The function will attempt to extract a valid JSON Schema from the provided input, and if successful, it will return the schema. If the extraction process fails or if the input is not a valid schema definition, it will throw an error indicating the failure.
186
218
  */
187
219
  async function extractSchema(context, input, options = {}) {
188
220
  if (isExtractedSchema(input)) return input;
189
221
  if (isSchema(input)) return {
190
222
  ...input,
191
223
  source: {
192
- hash: extractHash("jtd-schema", input.schema),
193
- variant: "jtd-schema",
224
+ hash: extractHash("json-schema", input.schema),
225
+ variant: "json-schema",
194
226
  schema: input.schema
195
227
  }
196
228
  };
197
229
  let source;
198
230
  const variant = extractVariant(input);
231
+ const hash = extractHash(variant, input);
199
232
  if (variant === "type-definition") {
200
- const resolved = await resolve(context, input, defu(options, { plugins: [esbuildPlugin(context, {
233
+ const resolved = await resolve(context, input, defu(options, { plugins: [rolldownPlugin(context, {
201
234
  reflection: "default",
202
235
  level: "all"
203
236
  })] }));
204
237
  source = extractSource(extractResolvedVariant(resolved), resolved);
205
238
  } else if ([
206
239
  "json-schema",
207
- "jtd-schema",
208
240
  "standard-schema",
209
241
  "zod3",
210
242
  "untyped",
211
243
  "reflection"
212
244
  ].includes(variant)) source = extractSource(variant, input);
213
- else throw new Error(`Invalid schema definition input "${variant}". The variant must be one of "type-definition", "json-schema", "jtd-schema", "standard-schema", "zod3", "untyped", or "reflection".`);
245
+ else throw new Error(`Invalid schema definition input "${variant}". The variant must be one of "type-definition", "json-schema", "standard-schema", "zod3", "untyped", or "reflection".`);
214
246
  return {
215
247
  variant,
216
248
  source,
217
249
  schema: await extractSchemaSchema(source.schema, source.variant),
218
- hash: extractHash(variant, input)
250
+ hash
219
251
  };
220
252
  }
221
253
  /**
222
- * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
254
+ * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.
223
255
  *
224
256
  * @example
225
257
  * ```ts
@@ -241,13 +273,30 @@ async function extractSchema(context, input, options = {}) {
241
273
  * @see https://github.com/unjs/untyped
242
274
  * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
243
275
  *
244
- * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.
245
- * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).
246
- * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.
247
- * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.
276
+ * @param context - The context object providing access to the file system and cache path.
277
+ * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.
278
+ * @param options - Optional overrides for the ESBuild configuration used during extraction.
279
+ * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.
280
+ * @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.
248
281
  */
249
282
  async function extract(context, input, options = {}) {
250
- return await extractSchema(context, input, options);
283
+ if (isExtractedSchema(input) || isSchema(input)) return input;
284
+ let result;
285
+ const variant = extractVariant(input);
286
+ const hash = extractHash(variant, input);
287
+ const cacheFilePath = joinPaths(getCacheDirectory(context), `${hash}.json`);
288
+ if (context.config.skipCache !== true && context.fs.existsSync(cacheFilePath)) {
289
+ const schema = await context.fs.read(cacheFilePath);
290
+ if (schema) result = {
291
+ variant,
292
+ hash,
293
+ schema: JSON.parse(schema)
294
+ };
295
+ }
296
+ result ??= await extractSchema(context, input, options);
297
+ if (!result?.schema) throw new Error(`Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`);
298
+ if (context.config.skipCache !== true) await writeSchema(context, result);
299
+ return result;
251
300
  }
252
301
 
253
302
  //#endregion