@orpc/arktype 2.0.0-beta.49 → 2.0.0-beta.50

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,16 @@
1
- import type { ToJsonSchema } from '@ark/schema';
2
- import type { AnySchema, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from '@orpc/json-schema';
3
- export interface ArkTypeToJsonSchemaConverterOptions extends Omit<ToJsonSchema.Options, 'dialect' | 'target'> {
4
- /**
5
- * Caches conversion results in a WeakMap keyed by the ArkType schema instance,
6
- * so converting the same schema again is free.
7
- *
8
- * When enabled, repeated conversions return the same JSON schema object,
9
- * so treat returned schemas as immutable.
10
- *
11
- * @default false
12
- */
13
- cache?: boolean | undefined;
1
+ import { AnySchema, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from "@orpc/json-schema";
2
+ import { ToJsonSchema } from "@ark/schema";
3
+ export interface ArkTypeToJsonSchemaConverterOptions extends Omit<ToJsonSchema.Options, "dialect" | "target"> {
4
+ /**
5
+ * Caches conversion results in a WeakMap keyed by the ArkType schema instance,
6
+ * so converting the same schema again is free.
7
+ *
8
+ * When enabled, repeated conversions return the same JSON schema object,
9
+ * so treat returned schemas as immutable.
10
+ *
11
+ * @default false
12
+ */
13
+ cache?: boolean | undefined;
14
14
  }
15
15
  /**
16
16
  * Converts ArkType schemas into JSON Schema using ArkType's built-in `toJsonSchema`,
@@ -19,11 +19,11 @@ export interface ArkTypeToJsonSchemaConverterOptions extends Omit<ToJsonSchema.O
19
19
  * @see {@link https://orpc.dev/docs/integrations/arktype | ArkType Integration}
20
20
  */
21
21
  export declare class ArkTypeToJsonSchemaConverter implements JsonSchemaConverter {
22
- private readonly toJsonSchemaOptions;
23
- private readonly cache;
24
- constructor({ cache, ...options }?: ArkTypeToJsonSchemaConverterOptions);
25
- condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
26
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
27
- private convertUncached;
28
- private convertArkType;
29
- }
22
+ private readonly toJsonSchemaOptions;
23
+ private readonly cache;
24
+ constructor({ cache, ...options }?: ArkTypeToJsonSchemaConverterOptions);
25
+ condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
26
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
27
+ private convertUncached;
28
+ private convertArkType;
29
+ }
package/dist/converter.js CHANGED
@@ -1,84 +1,65 @@
1
- import { isStandardSchemaOptional, JsonSchemaFormat, JsonSchemaXNativeType, } from '@orpc/json-schema';
1
+ import { JsonSchemaFormat, JsonSchemaXNativeType, isStandardSchemaOptional } from "@orpc/json-schema";
2
+ // src/converter.ts
2
3
  /**
3
- * Converts ArkType schemas into JSON Schema using ArkType's built-in `toJsonSchema`,
4
- * with additional support for types such as `bigint`, `Date`, `File`, and `Blob`.
5
- *
6
- * @see {@link https://orpc.dev/docs/integrations/arktype | ArkType Integration}
7
- */
8
- export class ArkTypeToJsonSchemaConverter {
9
- toJsonSchemaOptions;
10
- cache;
11
- constructor({ cache, ...options } = {}) {
12
- if (cache) {
13
- this.cache = { input: new WeakMap(), output: new WeakMap() };
14
- }
15
- this.toJsonSchemaOptions = {
16
- ...options,
17
- target: 'draft-2020-12',
18
- fallback: {
19
- ...(options.fallback && typeof options.fallback !== 'function'
20
- ? options.fallback
21
- : undefined),
22
- default: (ctx) => {
23
- if (ctx.code === 'domain') {
24
- if (ctx.domain === 'bigint') {
25
- ;
26
- ctx.base.type = 'string';
27
- ctx.base.pattern = '^-?[0-9]+$';
28
- ctx.base['x-native-type'] = JsonSchemaXNativeType.BigInt;
29
- }
30
- }
31
- else if (ctx.code === 'date') {
32
- ;
33
- ctx.base.type = 'string';
34
- ctx.base.format = JsonSchemaFormat.DateTime;
35
- ctx.base['x-native-type'] = JsonSchemaXNativeType.Date;
36
- }
37
- else if (ctx.code === 'proto' &&
38
- (ctx.proto === Blob || ctx.proto.prototype instanceof Blob)) {
39
- ;
40
- ctx.base.type = 'string';
41
- ctx.base.contentMediaType = '*/*';
42
- }
43
- if (typeof options.fallback === 'function') {
44
- return options.fallback(ctx);
45
- }
46
- if (options.fallback?.default) {
47
- return options.fallback.default(ctx);
48
- }
49
- return ctx.base;
50
- },
51
- },
52
- };
53
- }
54
- condition(schema, _direction) {
55
- return schema?.['~standard'].vendor === 'arktype';
56
- }
57
- convert(schema, direction) {
58
- const arkTypeSchema = schema;
59
- if (this.cache) {
60
- const cached = this.cache[direction].get(arkTypeSchema);
61
- if (cached) {
62
- return cached;
63
- }
64
- }
65
- const result = this.convertUncached(arkTypeSchema, direction);
66
- if (this.cache) {
67
- this.cache[direction].set(arkTypeSchema, result);
68
- }
69
- return result;
70
- }
71
- convertUncached(arkTypeSchema, direction) {
72
- const jsonSchema = this.convertArkType(arkTypeSchema, direction);
73
- return [jsonSchema, isStandardSchemaOptional(arkTypeSchema, direction)];
74
- }
75
- convertArkType(schema, direction) {
76
- // `toJsonSchema` describes the input side (and keeps `default` annotations),
77
- // so output conversions go through `.out` to reflect morph outputs and applied defaults.
78
- const jsonSchema = (direction === 'output' ? schema.out : schema).toJsonSchema(this.toJsonSchemaOptions);
79
- // Since the default oRPC format is always draft/2020-12,
80
- // `$schema` can be safely omitted here.
81
- const { $schema: _schema, ...rest } = jsonSchema;
82
- return rest;
83
- }
84
- }
4
+ * Converts ArkType schemas into JSON Schema using ArkType's built-in `toJsonSchema`,
5
+ * with additional support for types such as `bigint`, `Date`, `File`, and `Blob`.
6
+ *
7
+ * @see {@link https://orpc.dev/docs/integrations/arktype | ArkType Integration}
8
+ */
9
+ var ArkTypeToJsonSchemaConverter = class {
10
+ toJsonSchemaOptions;
11
+ cache;
12
+ constructor({ cache, ...options } = {}) {
13
+ if (cache) this.cache = {
14
+ input: /* @__PURE__ */ new WeakMap(),
15
+ output: /* @__PURE__ */ new WeakMap()
16
+ };
17
+ this.toJsonSchemaOptions = {
18
+ ...options,
19
+ target: "draft-2020-12",
20
+ fallback: {
21
+ ...options.fallback && typeof options.fallback !== "function" ? options.fallback : void 0,
22
+ default: (ctx) => {
23
+ if (ctx.code === "domain") {
24
+ if (ctx.domain === "bigint") {
25
+ ctx.base.type = "string";
26
+ ctx.base.pattern = "^-?[0-9]+$";
27
+ ctx.base["x-native-type"] = JsonSchemaXNativeType.BigInt;
28
+ }
29
+ } else if (ctx.code === "date") {
30
+ ctx.base.type = "string";
31
+ ctx.base.format = JsonSchemaFormat.DateTime;
32
+ ctx.base["x-native-type"] = JsonSchemaXNativeType.Date;
33
+ } else if (ctx.code === "proto" && (ctx.proto === Blob || ctx.proto.prototype instanceof Blob)) {
34
+ ctx.base.type = "string";
35
+ ctx.base.contentMediaType = "*/*";
36
+ }
37
+ if (typeof options.fallback === "function") return options.fallback(ctx);
38
+ if (options.fallback?.default) return options.fallback.default(ctx);
39
+ return ctx.base;
40
+ }
41
+ }
42
+ };
43
+ }
44
+ condition(schema, _direction) {
45
+ return schema?.["~standard"].vendor === "arktype";
46
+ }
47
+ convert(schema, direction) {
48
+ const arkTypeSchema = schema;
49
+ if (this.cache) {
50
+ const cached = this.cache[direction].get(arkTypeSchema);
51
+ if (cached) return cached;
52
+ }
53
+ const result = this.convertUncached(arkTypeSchema, direction);
54
+ if (this.cache) this.cache[direction].set(arkTypeSchema, result);
55
+ return result;
56
+ }
57
+ convertUncached(arkTypeSchema, direction) {
58
+ return [this.convertArkType(arkTypeSchema, direction), isStandardSchemaOptional(arkTypeSchema, direction)];
59
+ }
60
+ convertArkType(schema, direction) {
61
+ const { $schema: _schema, ...rest } = (direction === "output" ? schema.out : schema).toJsonSchema(this.toJsonSchemaOptions);
62
+ return rest;
63
+ }
64
+ };
65
+ export { ArkTypeToJsonSchemaConverter };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export * from './converter.ts';
2
- export type { JsonSchema } from '@orpc/json-schema';
1
+ import { ArkTypeToJsonSchemaConverter, ArkTypeToJsonSchemaConverterOptions } from "./converter.js";
2
+ import { JsonSchema } from "@orpc/json-schema";
3
+ export { ArkTypeToJsonSchemaConverter, ArkTypeToJsonSchemaConverterOptions, type JsonSchema };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export * from './converter.js';
1
+ export { ArkTypeToJsonSchemaConverter } from "./converter.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orpc/arktype",
3
- "version": "2.0.0-beta.49",
3
+ "version": "2.0.0-beta.50",
4
4
  "description": "ArkType integration for oRPC: convert ArkType schemas into JSON Schema for OpenAPI generation",
5
5
  "keywords": [
6
6
  "arktype",
@@ -36,7 +36,7 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "@orpc/json-schema": "2.0.0-beta.49"
39
+ "@orpc/json-schema": "2.0.0-beta.50"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ark/schema": "^0.56.8",