@orpc/json-schema 2.0.0-beta.48 → 2.0.0-beta.49

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 (46) hide show
  1. package/README.md +4 -4
  2. package/dist/coercer.d.ts +29 -0
  3. package/dist/coercer.js +498 -0
  4. package/dist/composition-utils.d.ts +41 -0
  5. package/dist/composition-utils.js +380 -0
  6. package/dist/constants.d.ts +8 -0
  7. package/dist/constants.js +66 -0
  8. package/dist/convert.d.ts +29 -0
  9. package/dist/convert.js +14 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.js +9 -0
  12. package/dist/ref-utils.d.ts +59 -0
  13. package/dist/ref-utils.js +253 -0
  14. package/dist/smart-coercion-handler-plugin.d.ts +22 -0
  15. package/dist/smart-coercion-handler-plugin.js +50 -0
  16. package/dist/smart-coercion-link-plugin.d.ts +28 -0
  17. package/dist/smart-coercion-link-plugin.js +75 -0
  18. package/dist/standard-json-schema-converter.d.ts +16 -0
  19. package/dist/standard-json-schema-converter.js +45 -0
  20. package/dist/types.d.ts +53 -0
  21. package/dist/types.js +40 -0
  22. package/dist/utils.d.ts +45 -0
  23. package/dist/utils.js +106 -0
  24. package/package.json +25 -27
  25. package/dist/coercer.d.mts +0 -29
  26. package/dist/coercer.mjs +0 -465
  27. package/dist/composition-utils.d.mts +0 -41
  28. package/dist/composition-utils.mjs +0 -357
  29. package/dist/constants.d.mts +0 -4
  30. package/dist/constants.mjs +0 -61
  31. package/dist/convert.d.mts +0 -29
  32. package/dist/convert.mjs +0 -14
  33. package/dist/index.d.mts +0 -10
  34. package/dist/index.mjs +0 -9
  35. package/dist/ref-utils.d.mts +0 -44
  36. package/dist/ref-utils.mjs +0 -117
  37. package/dist/smart-coercion-handler-plugin.d.mts +0 -22
  38. package/dist/smart-coercion-handler-plugin.mjs +0 -42
  39. package/dist/smart-coercion-link-plugin.d.mts +0 -28
  40. package/dist/smart-coercion-link-plugin.mjs +0 -61
  41. package/dist/standard-json-schema-converter.d.mts +0 -16
  42. package/dist/standard-json-schema-converter.mjs +0 -30
  43. package/dist/types.d.mts +0 -51
  44. package/dist/types.mjs +0 -40
  45. package/dist/utils.d.mts +0 -40
  46. package/dist/utils.mjs +0 -50
@@ -1,22 +0,0 @@
1
- import type { Context } from "@orpc/server";
2
- import type { StandardHandlerOptions, StandardHandlerPlugin } from "@orpc/server/standard";
3
- import type { JsonSchemaCoercerOptions } from "./coercer.mjs";
4
- import type { JsonSchemaConverter } from "./convert.mjs";
5
- export interface SmartCoercionHandlerPluginOptions extends JsonSchemaCoercerOptions {
6
- converters?: JsonSchemaConverter[] | undefined;
7
- }
8
- /**
9
- * Coerces incoming request data to match the expected `.input` schema types before validation,
10
- * based on JSON schemas produced by the configured converters.
11
- *
12
- * @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
13
- */
14
- export declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
15
- name: string;
16
- private readonly converter;
17
- private readonly coercer;
18
- private readonly cache;
19
- constructor(options?: SmartCoercionHandlerPluginOptions);
20
- init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
21
- private coerceValue;
22
- }
@@ -1,42 +0,0 @@
1
- import { toArray } from "@orpc/shared";
2
- import { JsonSchemaCoercer } from "./coercer.mjs";
3
- import { DelegatingJsonSchemaConverter } from "./convert.mjs";
4
- import { StandardJsonSchemaConverter } from "./standard-json-schema-converter.mjs";
5
-
6
- export class SmartCoercionHandlerPlugin {
7
- name = "~smart-coercion";
8
- converter;
9
- coercer;
10
- cache = new WeakMap();
11
- constructor(options = {}) {
12
- this.converter = new DelegatingJsonSchemaConverter([...toArray(options.converters), new StandardJsonSchemaConverter()]);
13
- this.coercer = new JsonSchemaCoercer(options);
14
- }
15
- init(options) {
16
- return {
17
- ...options,
18
- clientInterceptors: [({ next, input, ...interceptorOptions }) => {
19
- const inputSchemas = interceptorOptions.procedure["~orpc"].inputSchemas;
20
- if (!inputSchemas) {
21
- return next();
22
- }
23
- const coercedInput = this.coerceValue(inputSchemas, input);
24
- return next({
25
- ...interceptorOptions,
26
- input: coercedInput
27
- });
28
- }, ...toArray(options.clientInterceptors)]
29
- };
30
- }
31
- coerceValue(schemas, value) {
32
- for (const schema of schemas) {
33
- let converted = this.cache.get(schema);
34
- if (!converted) {
35
- converted = this.converter.convert(schema, "input");
36
- this.cache.set(schema, converted);
37
- }
38
- value = this.coercer.coerce(converted, value);
39
- }
40
- return value;
41
- }
42
- }
@@ -1,28 +0,0 @@
1
- import type { ClientContext } from "@orpc/client";
2
- import type { StandardLinkOptions, StandardLinkPlugin } from "@orpc/client/standard";
3
- import type { RouterContract } from "@orpc/contract";
4
- import type { JsonSchemaCoercerOptions } from "./coercer.mjs";
5
- import type { JsonSchemaConverter } from "./convert.mjs";
6
- export interface SmartCoercionLinkPluginOptions extends JsonSchemaCoercerOptions {
7
- converters?: JsonSchemaConverter[] | undefined;
8
- }
9
- /**
10
- * Coerces server responses to match the expected `.output` or `.errors` schema types before validation,
11
- * based on JSON schemas produced by the configured converters.
12
- *
13
- * @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
14
- */
15
- export declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
16
- private readonly contract;
17
- name: string;
18
- /**
19
- * Output and error values should be coerced before validation.
20
- */
21
- after: string[];
22
- private readonly converter;
23
- private readonly coercer;
24
- private readonly cache;
25
- constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
26
- init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
27
- private coerceValue;
28
- }
@@ -1,61 +0,0 @@
1
- import { cloneORPCError, ORPCError } from "@orpc/client";
2
- import { getProcedureContractOrThrow } from "@orpc/contract";
3
- import { getOwn, toArray } from "@orpc/shared";
4
- import { JsonSchemaCoercer } from "./coercer.mjs";
5
- import { DelegatingJsonSchemaConverter } from "./convert.mjs";
6
- import { StandardJsonSchemaConverter } from "./standard-json-schema-converter.mjs";
7
-
8
- export class SmartCoercionLinkPlugin {
9
- contract;
10
- name = "~smart-coercion";
11
-
12
- after = ["~response-validation"];
13
- converter;
14
- coercer;
15
- cache = new WeakMap();
16
- constructor(contract, options = {}) {
17
- this.contract = contract;
18
- this.converter = new DelegatingJsonSchemaConverter([...toArray(options.converters), new StandardJsonSchemaConverter()]);
19
- this.coercer = new JsonSchemaCoercer(options);
20
- }
21
- init(options) {
22
- return {
23
- ...options,
24
- interceptors: [...toArray(options.interceptors), async ({ next, path }) => {
25
- const procedure = getProcedureContractOrThrow(this.contract, path);
26
- try {
27
- const output = await next();
28
- const outputSchemas = procedure["~orpc"].outputSchemas;
29
- if (!outputSchemas) {
30
- return output;
31
- }
32
- const coercedOutput = this.coerceValue(outputSchemas, output);
33
- return coercedOutput;
34
- } catch (error) {
35
- if (!(error instanceof ORPCError) || !error.defined) {
36
- throw error;
37
- }
38
- const errorMap = procedure["~orpc"].errorMap;
39
- const dataSchema = getOwn(errorMap, error.code)?.data;
40
- if (!dataSchema) {
41
- throw error;
42
- }
43
- const cloned = cloneORPCError(error);
44
- cloned.data = this.coerceValue([dataSchema], cloned.data);
45
- throw cloned;
46
- }
47
- }]
48
- };
49
- }
50
- coerceValue(schemas, value) {
51
- for (const schema of schemas) {
52
- let converted = this.cache.get(schema);
53
- if (!converted) {
54
- converted = this.converter.convert(schema, "output");
55
- this.cache.set(schema, converted);
56
- }
57
- value = this.coercer.coerce(converted, value);
58
- }
59
- return value;
60
- }
61
- }
@@ -1,16 +0,0 @@
1
- import type { AnySchema } from "@orpc/contract";
2
- import type { StandardJSONSchemaV1 } from "@standard-schema/spec";
3
- import type { JsonSchemaConverter, JsonSchemaConverterDirection } from "./convert.mjs";
4
- import type { JsonSchema } from "./types.mjs";
5
- /**
6
- * Returns true when the schema accepts `undefined` in the given direction,
7
- * found by validating `undefined` against it. For `output`, the validated value must also be `undefined`.
8
- *
9
- * Returns false when validation throws or is async. An async result is never left as an unhandled rejection.
10
- */
11
- export declare function isStandardSchemaOptional(schema: AnySchema, direction: JsonSchemaConverterDirection): boolean;
12
- export declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
13
- condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
14
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
15
- convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
16
- }
@@ -1,30 +0,0 @@
1
- import { isTypescriptObject } from "@orpc/shared";
2
-
3
- export function isStandardSchemaOptional(schema, direction) {
4
- try {
5
- const result = schema["~standard"].validate(undefined);
6
- if (result instanceof Promise) {
7
- result.catch(() => {});
8
- return false;
9
- }
10
- return !result.issues && (direction === "input" || result.value === undefined);
11
- } catch {
12
- return false;
13
- }
14
- }
15
- export class StandardJsonSchemaConverter {
16
- condition(schema, _direction) {
17
- return Boolean(schema && "jsonSchema" in schema["~standard"] && isTypescriptObject(schema["~standard"].jsonSchema) && "input" in schema["~standard"].jsonSchema && typeof schema["~standard"].jsonSchema.input === "function" && "output" in schema["~standard"].jsonSchema && typeof schema["~standard"].jsonSchema.output === "function");
18
- }
19
- convert(schema, direction) {
20
- return this.convertInternal(schema, direction);
21
- }
22
- convertInternal(schema, direction) {
23
- try {
24
- const jsonSchema = direction === "input" ? schema["~standard"].jsonSchema.input({ target: "draft-2020-12" }) : schema["~standard"].jsonSchema.output({ target: "draft-2020-12" });
25
- return [jsonSchema, isStandardSchemaOptional(schema, direction)];
26
- } catch {
27
- return [{}, true];
28
- }
29
- }
30
- }
package/dist/types.d.mts DELETED
@@ -1,51 +0,0 @@
1
- import type * as OpenAPIV3_2 from "@openapi-spec/types/v3.2";
2
- /**
3
- * A JSON Schema (draft 2020-12) representation used across oRPC's JSON schema tooling.
4
- *
5
- * @remarks
6
- * It is also the OpenAPI 3.2 Schema Object, so converted schemas embed into OpenAPI documents as-is.
7
- *
8
- * @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
9
- */
10
- export type JsonSchema<Value = any> = OpenAPIV3_2.SchemaObject<Value>;
11
- /**
12
- * The declared JSON Schema keywords, the index signature is filtered out.
13
- */
14
- export type JsonSchemaKeywords = keyof { [K in keyof OpenAPIV3_2.SchemaObjectFields as string extends K ? never : K]: unknown };
15
- export declare enum JsonSchemaType {
16
- Array = "array",
17
- Boolean = "boolean",
18
- Integer = "integer",
19
- Null = "null",
20
- Number = "number",
21
- Object = "object",
22
- String = "string"
23
- }
24
- export declare enum JsonSchemaFormat {
25
- Date = "date",
26
- DateTime = "date-time",
27
- Duration = "duration",
28
- Email = "email",
29
- Hostname = "hostname",
30
- IDNEmail = "idn-email",
31
- IDNHostname = "idn-hostname",
32
- IPv4 = "ipv4",
33
- IPv6 = "ipv6",
34
- IRI = "iri",
35
- IRIReference = "iri-reference",
36
- JSONPointer = "json-pointer",
37
- RegEx = "regex",
38
- RelativeJSONPointer = "relative-json-pointer",
39
- Time = "time",
40
- URI = "uri",
41
- URIReference = "uri-reference",
42
- URITemplate = "uri-template",
43
- UUID = "uuid"
44
- }
45
- export declare enum JsonSchemaXNativeType {
46
- BigInt = "bigint",
47
- Date = "date",
48
- Url = "url",
49
- Set = "set",
50
- Map = "map"
51
- }
package/dist/types.mjs DELETED
@@ -1,40 +0,0 @@
1
- export let JsonSchemaType = /* @__PURE__ */ function(JsonSchemaType) {
2
- JsonSchemaType["Array"] = "array";
3
- JsonSchemaType["Boolean"] = "boolean";
4
- JsonSchemaType["Integer"] = "integer";
5
- JsonSchemaType["Null"] = "null";
6
- JsonSchemaType["Number"] = "number";
7
- JsonSchemaType["Object"] = "object";
8
- JsonSchemaType["String"] = "string";
9
- return JsonSchemaType;
10
- }({});
11
- export let JsonSchemaFormat = /* @__PURE__ */ function(JsonSchemaFormat) {
12
- JsonSchemaFormat["Date"] = "date";
13
- JsonSchemaFormat["DateTime"] = "date-time";
14
- JsonSchemaFormat["Duration"] = "duration";
15
- JsonSchemaFormat["Email"] = "email";
16
- JsonSchemaFormat["Hostname"] = "hostname";
17
- JsonSchemaFormat["IDNEmail"] = "idn-email";
18
- JsonSchemaFormat["IDNHostname"] = "idn-hostname";
19
- JsonSchemaFormat["IPv4"] = "ipv4";
20
- JsonSchemaFormat["IPv6"] = "ipv6";
21
- JsonSchemaFormat["IRI"] = "iri";
22
- JsonSchemaFormat["IRIReference"] = "iri-reference";
23
- JsonSchemaFormat["JSONPointer"] = "json-pointer";
24
- JsonSchemaFormat["RegEx"] = "regex";
25
- JsonSchemaFormat["RelativeJSONPointer"] = "relative-json-pointer";
26
- JsonSchemaFormat["Time"] = "time";
27
- JsonSchemaFormat["URI"] = "uri";
28
- JsonSchemaFormat["URIReference"] = "uri-reference";
29
- JsonSchemaFormat["URITemplate"] = "uri-template";
30
- JsonSchemaFormat["UUID"] = "uuid";
31
- return JsonSchemaFormat;
32
- }({});
33
- export let JsonSchemaXNativeType = /* @__PURE__ */ function(JsonSchemaXNativeType) {
34
- JsonSchemaXNativeType["BigInt"] = "bigint";
35
- JsonSchemaXNativeType["Date"] = "date";
36
- JsonSchemaXNativeType["Url"] = "url";
37
- JsonSchemaXNativeType["Set"] = "set";
38
- JsonSchemaXNativeType["Map"] = "map";
39
- return JsonSchemaXNativeType;
40
- }({});
package/dist/utils.d.mts DELETED
@@ -1,40 +0,0 @@
1
- /**
2
- * These utilities assume the schema has only one root-level `$defs` object
3
- * and exclusively use absolute JSON pointers for `$ref` values.
4
- */
5
- import type { JsonSchema } from "./types.mjs";
6
- export declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
7
- export type JsonFileSchema = JsonSchema & object & {
8
- type: "string";
9
- contentMediaType?: string;
10
- };
11
- /**
12
- * Returns true when the schema is a file-like string schema.
13
- */
14
- export declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
15
- /**
16
- * Returns true when a file schema appears anywhere in the schema, such as in a property, a composition, or a def.
17
- */
18
- export declare function containsJsonFileSchema(schema: JsonSchema): boolean;
19
- export type JsonObjectSchema = JsonSchema & object & {
20
- type: "object";
21
- };
22
- /**
23
- * Returns true when the schema is an object schema.
24
- */
25
- export declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
26
- export type JsonArraySchema = JsonSchema & object & {
27
- type: "array";
28
- };
29
- /**
30
- * Returns true when the schema is an array schema.
31
- */
32
- export declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
33
- /**
34
- * Returns true when the schema does not apply any recognized constraints.
35
- */
36
- export declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
37
- /**
38
- * Ensures a JSON Schema is represented as an object schema document.
39
- */
40
- export declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
package/dist/utils.mjs DELETED
@@ -1,50 +0,0 @@
1
- import { findDeepMatches, isPlainObject } from "@orpc/shared";
2
- import { JSON_SCHEMA_LOGIC_KEYWORDS, JSON_SCHEMA_PRIMITIVE_TYPES } from "./constants.mjs";
3
- export function isJsonPrimitiveSchema(schema) {
4
- if (typeof schema === "boolean") {
5
- return false;
6
- }
7
- if (typeof schema.type === "string" && JSON_SCHEMA_PRIMITIVE_TYPES.has(schema.type)) {
8
- return true;
9
- }
10
- if (schema.const !== undefined) {
11
- return true;
12
- }
13
- if (schema.enum !== undefined) {
14
- return true;
15
- }
16
- return false;
17
- }
18
-
19
- export function isJsonFileSchema(schema) {
20
- return typeof schema !== "boolean" && schema.type === "string" && (typeof schema.contentMediaType === "string" || schema.format === "binary" || schema.contentEncoding === "binary");
21
- }
22
-
23
- export function containsJsonFileSchema(schema) {
24
- return findDeepMatches((v) => isPlainObject(v) && isJsonFileSchema(v), schema).values.length > 0;
25
- }
26
-
27
- export function isJsonObjectSchema(schema) {
28
- return typeof schema !== "boolean" && schema.type === "object";
29
- }
30
-
31
- export function isJsonArraySchema(schema) {
32
- return typeof schema !== "boolean" && schema.type === "array";
33
- }
34
-
35
- export function isUnconstrainedSchema(schema) {
36
- if (typeof schema === "boolean") {
37
- return schema;
38
- }
39
- if (Object.keys(schema).every((k) => !JSON_SCHEMA_LOGIC_KEYWORDS.has(k))) {
40
- return true;
41
- }
42
- return false;
43
- }
44
-
45
- export function ensureJsonSchemaObject(schema) {
46
- if (typeof schema === "boolean") {
47
- return schema ? {} : { not: {} };
48
- }
49
- return schema;
50
- }