@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.
- package/dist/converter.d.ts +21 -21
- package/dist/converter.js +64 -83
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/converter.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
export interface ArkTypeToJsonSchemaConverterOptions extends Omit<ToJsonSchema.Options,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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 {
|
|
1
|
+
import { JsonSchemaFormat, JsonSchemaXNativeType, isStandardSchemaOptional } from "@orpc/json-schema";
|
|
2
|
+
// src/converter.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
2
|
-
|
|
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
|
|
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.
|
|
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.
|
|
39
|
+
"@orpc/json-schema": "2.0.0-beta.50"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@ark/schema": "^0.56.8",
|