@orpc/json-schema 1.14.10 → 1.14.11

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/README.md CHANGED
@@ -1,17 +1,16 @@
1
- <div align="center">
2
- <image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
3
- </div>
4
-
5
- <h1></h1>
1
+ <h1 align="center">oRPC - Typesafe APIs Made Simple 🪄</h1>
6
2
 
7
3
  <div align="center">
8
4
  <a href="https://codecov.io/gh/middleapi/orpc">
9
- <img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/1.x/graph/badge.svg">
5
+ <img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
10
6
  </a>
11
7
  <a href="https://www.npmjs.com/package/@orpc/json-schema">
12
8
  <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fjson-schema?logo=npm" />
13
9
  </a>
14
- <a href="https://github.com/middleapi/orpc/blob/1.x/LICENSE">
10
+ <a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
11
+ <img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
12
+ </a>
13
+ <a href="https://github.com/middleapi/orpc/blob/main/LICENSE">
15
14
  <img alt="MIT License" src="https://img.shields.io/github/license/middleapi/orpc?logo=open-source-initiative" />
16
15
  </a>
17
16
  <a href="https://discord.gg/TXEbwRBvQn">
@@ -22,55 +21,53 @@
22
21
  </a>
23
22
  </div>
24
23
 
25
- <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
24
+ ## Documentation
26
25
 
27
- **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
26
+ You can read the documentation [here](https://orpc.dev).
28
27
 
29
- ---
28
+ ## Packages
30
29
 
31
- ## Highlights
30
+ **Core**
32
31
 
33
- - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
34
- - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
35
- - **📝 Contract-First Development**: Optionally define your API contract before implementation.
36
- - **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
37
- - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
38
- - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
39
- - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
40
- - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
41
- - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
42
- - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
43
- - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
44
- - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
32
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Define API contract as the single source of truth.
33
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build APIs or implement contracts.
34
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume APIs with end-to-end type safety.
35
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Add OpenAPI compatibility to APIs.
45
36
 
46
- ## Documentation
37
+ **Schema validation**
47
38
 
48
- You can find the full documentation [here](https://orpc.dev).
39
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): Integrate with [Zod](https://zod.dev/).
40
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): Integrate with [Valibot](https://valibot.dev/).
41
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): Integrate with [ArkType](https://arktype.io/).
49
42
 
50
- ## Packages
43
+ **Built-in features**
44
+
45
+ - [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher): Pub/Sub with memory, Redis, and Upstash adapters.
46
+ - [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit): Rate limiting with memory, Redis, and Upstash adapters.
47
+ - [@orpc/json-schema](https://www.npmjs.com/package/@orpc/json-schema): Smart coercion for OpenAPI requests.
51
48
 
52
- - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
53
- - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
54
- - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
55
- - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
56
- - [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
57
- - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
58
- - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
59
- - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
60
- - [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
61
- - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
62
- - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
63
- - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
64
- - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
65
- - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
66
-
67
- ## `@orpc/json-schema`
68
-
69
- Json Schema related utilities for oRPC.
49
+ **Framework & ecosystem integrations**
50
+
51
+ - [@orpc/next](https://www.npmjs.com/package/@orpc/next): Integrate with [Next.js Server Functions](https://nextjs.org/docs/app/getting-started/mutating-data).
52
+ - [@orpc/ai-sdk](https://www.npmjs.com/package/@orpc/ai-sdk): Turn contracts and procedures into [AI SDK](https://ai-sdk.dev/) tools.
53
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): Integrate with [TanStack Query](https://tanstack.com/query/latest).
54
+ - [@orpc/pinia-colada](https://www.npmjs.com/package/@orpc/pinia-colada): Integrate with [Pinia Colada](https://pinia-colada.esm.dev/).
55
+ - [@orpc/swr](https://www.npmjs.com/package/@orpc/swr): Integrate with [SWR](https://swr.vercel.app/).
56
+ - [@orpc/experimental-effect](https://www.npmjs.com/package/@orpc/experimental-effect): Integrate with [Effect](https://effect.website/).
57
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Implement your contract with [NestJS](https://nestjs.com/).
58
+ - [@orpc/bun](https://www.npmjs.com/package/@orpc/bun): Adapters for [Bun's Redis](https://bun.sh/).
59
+ - [@orpc/cloudflare](https://www.npmjs.com/package/@orpc/cloudflare): Adapters for [Cloudflare's RateLimit and Durable Objects](https://developers.cloudflare.com/workers/).
60
+ - [@orpc/trpc](https://www.npmjs.com/package/@orpc/trpc): Reuse existing [tRPC](https://trpc.io/) routers within oRPC.
61
+
62
+ **Observability**
63
+
64
+ - [@orpc/opentelemetry](https://www.npmjs.com/package/@orpc/opentelemetry): Integrate with [OpenTelemetry](https://opentelemetry.io/) for distributed tracing.
65
+ - [@orpc/pino](https://www.npmjs.com/package/@orpc/pino): Integrate with [Pino](https://getpino.io/) for logging.
66
+ - [@orpc/evlog](https://www.npmjs.com/package/@orpc/evlog): Integrate with [Evlog](https://evlog.dev/) for logging.
70
67
 
71
68
  ## Sponsors
72
69
 
73
- If you find oRPC valuable and would like to support its development, you can do so here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh).
70
+ Like what we build over at [middleapi](https://github.com/middleapi)? You can help keep it going here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh). Every bit helps! 🚀
74
71
 
75
72
  ### 🏆 Platinum Sponsor
76
73
 
@@ -112,11 +109,10 @@ If you find oRPC valuable and would like to support its development, you can do
112
109
  <td align="center"><a href="https://github.com/valerii15298?ref=orpc" target="_blank" rel="noopener" title="Valerii Petryniak"><img src="https://avatars.githubusercontent.com/u/44531564?u=88ac74d9bacd20401518441907acad21063cd397&amp;v=4" width="139" alt="Valerii Petryniak"/><br />Valerii Petryniak</a></td>
113
110
  <td align="center"><a href="https://github.com/letstri?ref=orpc" target="_blank" rel="noopener" title="Valerii Strilets"><img src="https://avatars.githubusercontent.com/u/13253748?u=c7b10399ccc8f8081e24db94ec32cd9858e86ac3&amp;v=4" width="139" alt="Valerii Strilets"/><br />Valerii Strilets</a></td>
114
111
  <td align="center"><a href="https://github.com/K-Mistele?ref=orpc" target="_blank" rel="noopener" title="Kyle Mistele"><img src="https://avatars.githubusercontent.com/u/18430555?u=3afebeb81de666e35aaac3ed46f14159d7603ffb&amp;v=4" width="139" alt="Kyle Mistele"/><br />Kyle Mistele</a></td>
115
- <td align="center"><a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&amp;v=4" width="139" alt="Ryan Vogel"/><br />Ryan Vogel</a></td>
116
112
  <td align="center"><a href="https://github.com/christ12938?ref=orpc" target="_blank" rel="noopener" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td>
113
+ <td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&amp;v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
117
114
  </tr>
118
115
  <tr>
119
- <td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&amp;v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
120
116
  <td align="center"><a href="https://github.com/itigoore01?ref=orpc" target="_blank" rel="noopener" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&amp;v=4" width="139" alt="shota"/><br />shota</a></td>
121
117
  </tr>
122
118
  </table>
@@ -176,6 +172,7 @@ If you find oRPC valuable and would like to support its development, you can do
176
172
  <a href="https://github.com/nguyenlc1993?ref=orpc" target="_blank" rel="noopener" title="Lê Cao Nguyên"><img src="https://avatars.githubusercontent.com/u/13871971?u=83c8b69d9e35b589c4e1f066cc113b1d9461386f&amp;v=4" width="32" height="32" alt="Lê Cao Nguyên" /></a>
177
173
  <a href="https://github.com/wobsoriano?ref=orpc" target="_blank" rel="noopener" title="Robert Soriano"><img src="https://avatars.githubusercontent.com/u/13049130?u=6d72104182e7c9ed25934815313fb69107332111&amp;v=4" width="32" height="32" alt="Robert Soriano" /></a>
178
174
  <a href="https://github.com/andrewpeters9?ref=orpc" target="_blank" rel="noopener" title="Andrew Peters"><img src="https://avatars.githubusercontent.com/u/36251325?v=4" width="32" height="32" alt="Andrew Peters" /></a>
175
+ <a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&amp;v=4" width="32" height="32" alt="Ryan Vogel" /></a>
179
176
  <a href="https://github.com/SKostyukovich?ref=orpc" target="_blank" rel="noopener" title="SKostyukovich"><img src="https://avatars.githubusercontent.com/u/10700067?v=4" width="32" height="32" alt="SKostyukovich" /></a>
180
177
  <a href="https://github.com/peter-adam-dy?ref=orpc" target="_blank" rel="noopener" title="Peter Adam"><img src="https://avatars.githubusercontent.com/u/132129459?u=4f3dbbb3b443990b56acb7d6a5d11ed2c555f6db&amp;v=4" width="32" height="32" alt="Peter Adam" /></a>
181
178
  <a href="https://github.com/FabworksHQ?ref=orpc" target="_blank" rel="noopener" title="Fabworks"><img src="https://avatars.githubusercontent.com/u/160179500?v=4" width="32" height="32" alt="Fabworks" /></a>
@@ -190,6 +187,13 @@ If you find oRPC valuable and would like to support its development, you can do
190
187
  <a href="https://github.com/plancraft?ref=orpc" target="_blank" rel="noopener" title="plancraft"><img src="https://avatars.githubusercontent.com/u/46482287?v=4" width="32" height="32" alt="plancraft" /></a>
191
188
  </p>
192
189
 
190
+ ## References
191
+
192
+ oRPC is inspired by existing solutions that prioritize type safety and developer experience. Special acknowledgments to:
193
+
194
+ - [tRPC](https://trpc.io): For pioneering the concept of end-to-end type-safe RPC and influencing the development of type-safe APIs.
195
+ - [ts-rest](https://ts-rest.com): For its emphasis on contract-first development and OpenAPI integration, which have greatly inspired oRPC's feature set.
196
+
193
197
  ## License
194
198
 
195
- Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/1.x/LICENSE) for more information.
199
+ Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/main/LICENSE) for more information.
package/dist/index.d.mts CHANGED
@@ -1,11 +1,15 @@
1
- import * as Draft07 from 'json-schema-typed/draft-07';
2
- import * as Draft2019 from 'json-schema-typed/draft-2019-09';
3
1
  import * as Draft2020 from 'json-schema-typed/draft-2020-12';
4
- import { ConditionalSchemaConverter } from '@orpc/openapi';
2
+ export { Format as JsonSchemaFormat, TypeName as JsonSchemaType } from 'json-schema-typed/draft-2020-12';
3
+ import { AnySchema, RouterContract } from '@orpc/contract';
4
+ export { AnySchema, Schema } from '@orpc/contract';
5
5
  import { Context } from '@orpc/server';
6
6
  import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
7
+ import { ClientContext } from '@orpc/client';
8
+ import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
9
+ import { StandardJSONSchemaV1 } from '@standard-schema/spec';
7
10
 
8
- type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
11
+ type JsonSchema<Value = any> = Draft2020.JSONSchema<Value>;
12
+ type JsonSchemaKeywords = typeof Draft2020.keywords[number];
9
13
  declare enum JsonSchemaXNativeType {
10
14
  BigInt = "bigint",
11
15
  RegExp = "regexp",
@@ -15,25 +19,173 @@ declare enum JsonSchemaXNativeType {
15
19
  Map = "map"
16
20
  }
17
21
 
18
- interface JsonSchemaCoerceOptions {
19
- components?: Record<string, JsonSchema>;
20
- }
21
22
  declare class JsonSchemaCoercer {
22
- #private;
23
- coerce(schema: JsonSchema, value: unknown, options?: JsonSchemaCoerceOptions): unknown;
23
+ coerce([schema, optional]: [schema: JsonSchema, optional: boolean], value: unknown): unknown;
24
+ private coerceInternal;
25
+ }
26
+
27
+ /**
28
+ * These utilities assume the schema has only one root-level `$defs` object
29
+ * and exclusively use absolute JSON pointers for `$ref` values.
30
+ */
31
+
32
+ declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
33
+ type JsonFileSchema = JsonSchema & object & {
34
+ type: 'string';
35
+ contentMediaType?: string;
36
+ };
37
+ /**
38
+ * Returns true when the schema is a file-like string schema.
39
+ */
40
+ declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
41
+ type JsonObjectSchema = JsonSchema & object & {
42
+ type: 'object';
43
+ };
44
+ /**
45
+ * Returns true when the schema is an object schema.
46
+ */
47
+ declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
48
+ type JsonArraySchema = JsonSchema & object & {
49
+ type: 'array';
50
+ };
51
+ /**
52
+ * Returns true when the schema is an array schema.
53
+ */
54
+ declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
55
+ /**
56
+ * Returns true when the schema does not apply any recognized constraints.
57
+ */
58
+ declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
59
+ /**
60
+ * Ensures a JSON Schema is represented as an object schema document.
61
+ */
62
+ declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
63
+
64
+ /**
65
+ * These utilities assume the schema has only one root-level `$defs` object
66
+ * and exclusively use absolute JSON pointers for `$ref` values.
67
+ */
68
+
69
+ /**
70
+ * Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
71
+ */
72
+ declare function combineJsonSchemasWithComposition(keyword: 'allOf' | 'anyOf' | 'oneOf', schemas: JsonSchema[]): JsonSchema;
73
+ type JsonObjectSchemaEntry = [name: string, schema: JsonSchema, optional: boolean];
74
+ /**
75
+ * Combines object property entries back into a single object schema.
76
+ */
77
+ declare function combineJsonObjectSchemaEntries(entries: JsonObjectSchemaEntry[]): JsonObjectSchema;
78
+ /**
79
+ * Parses an object schema, or a composition of object schemas, into property entries.
80
+ */
81
+ declare function extractJsonObjectSchemaEntries(schema: JsonSchema): JsonObjectSchemaEntry[] | undefined;
82
+ /**
83
+ * Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
84
+ */
85
+ declare function flattenJsonUnionSchema(schema: JsonSchema): JsonSchema[];
86
+ /**
87
+ * Matches a union made of a single item schema and its array form.
88
+ */
89
+ declare function matchArrayableJsonSchema(schema: JsonSchema): undefined | [itemSchema: JsonSchema, arraySchema: JsonArraySchema];
90
+ declare function deduplicateJsonSchemas(schemas: JsonSchema[]): JsonSchema[];
91
+
92
+ type JsonSchemaConverterDirection = 'input' | 'output';
93
+ interface JsonSchemaConverter {
94
+ /**
95
+ * Determines whether this converter can handle the given schema.
96
+ */
97
+ condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): boolean;
98
+ /**
99
+ * Converts an ORPC schema to a JSON Schema representation.
100
+ */
101
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
102
+ }
103
+ declare class DelegatingJsonSchemaConverter implements Pick<JsonSchemaConverter, 'convert'> {
104
+ private readonly converters;
105
+ constructor(converters?: JsonSchemaConverter[]);
106
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
107
+ }
108
+
109
+ /**
110
+ * These utilities assume the schema has only one root-level `$defs` object
111
+ * and exclusively use absolute JSON pointers for `$ref` values.
112
+ */
113
+
114
+ /**
115
+ * Encodes a JSON Pointer segment according to RFC 6901.
116
+ *
117
+ * https://datatracker.ietf.org/doc/html/rfc6901
118
+ */
119
+ declare function encodeJsonPointerSegment(segment: string): string;
120
+ /**
121
+ * Decodes a JSON Pointer segment according to RFC 6901.
122
+ *
123
+ * https://datatracker.ietf.org/doc/html/rfc6901
124
+ */
125
+ declare function decodeJsonPointerSegment(segment: string): string;
126
+ /**
127
+ * Visits every `$ref` in a schema, traversing the same keywords as {@link mapJsonSchemaRefs}.
128
+ * Shared or cyclic object instances are visited once.
129
+ */
130
+ declare function visitJsonSchemaRefs(value: JsonSchema, visit: (ref: string) => void, schemaLevel?: boolean, seen?: Set<object>): void;
131
+ declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
132
+ /**
133
+ * Rewrites recursive root `#` refs by moving the schema body into `$defs`.
134
+ */
135
+ declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
136
+ /**
137
+ * Resolves a local `$ref` at the **root level** of the given schema, if present.
138
+ *
139
+ * Only handles refs of the form `#/$defs/<name>` pointing into the provided
140
+ * (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
141
+ * intentionally left untouched.
142
+ *
143
+ * If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
144
+ * schema is returned as-is.
145
+ *
146
+ * @param schema - The schema whose root-level `$ref` should be resolved.
147
+ * @param $defs - Definition map to resolve against. If omitted, falls back to
148
+ * `schema.$defs`. When provided, takes precedence over any `$defs` embedded
149
+ * in the schema.
150
+ */
151
+ declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclude<JsonSchema, boolean>['$defs']): JsonSchema;
152
+
153
+ interface SmartCoercionHandlerPluginOptions {
154
+ converters?: undefined | JsonSchemaConverter[];
155
+ }
156
+ declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
157
+ name: string;
158
+ private readonly converter;
159
+ private readonly coercer;
160
+ private readonly cache;
161
+ constructor(options?: SmartCoercionHandlerPluginOptions);
162
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
163
+ private coerceValue;
24
164
  }
25
165
 
26
- interface SmartCoercionPluginOptions {
27
- schemaConverters?: readonly ConditionalSchemaConverter[];
166
+ interface SmartCoercionLinkPluginOptions {
167
+ converters?: undefined | JsonSchemaConverter[];
28
168
  }
29
- declare class SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
30
- #private;
169
+ declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
170
+ private readonly contract;
171
+ name: string;
172
+ /**
173
+ * Output and error values should be coerced before validation.
174
+ */
175
+ after: string[];
31
176
  private readonly converter;
32
177
  private readonly coercer;
33
178
  private readonly cache;
34
- constructor(options?: SmartCoercionPluginOptions);
35
- init(options: StandardHandlerOptions<T>): void;
179
+ constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
180
+ init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
181
+ private coerceValue;
182
+ }
183
+
184
+ declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
185
+ condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
186
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
187
+ convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
36
188
  }
37
189
 
38
- export { JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionPlugin };
39
- export type { JsonSchema, JsonSchemaCoerceOptions, SmartCoercionPluginOptions };
190
+ export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef, visitJsonSchemaRefs };
191
+ export type { JsonArraySchema, JsonFileSchema, JsonObjectSchema, JsonObjectSchemaEntry, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection, JsonSchemaKeywords, SmartCoercionHandlerPluginOptions, SmartCoercionLinkPluginOptions };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,15 @@
1
- import * as Draft07 from 'json-schema-typed/draft-07';
2
- import * as Draft2019 from 'json-schema-typed/draft-2019-09';
3
1
  import * as Draft2020 from 'json-schema-typed/draft-2020-12';
4
- import { ConditionalSchemaConverter } from '@orpc/openapi';
2
+ export { Format as JsonSchemaFormat, TypeName as JsonSchemaType } from 'json-schema-typed/draft-2020-12';
3
+ import { AnySchema, RouterContract } from '@orpc/contract';
4
+ export { AnySchema, Schema } from '@orpc/contract';
5
5
  import { Context } from '@orpc/server';
6
6
  import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
7
+ import { ClientContext } from '@orpc/client';
8
+ import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
9
+ import { StandardJSONSchemaV1 } from '@standard-schema/spec';
7
10
 
8
- type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
11
+ type JsonSchema<Value = any> = Draft2020.JSONSchema<Value>;
12
+ type JsonSchemaKeywords = typeof Draft2020.keywords[number];
9
13
  declare enum JsonSchemaXNativeType {
10
14
  BigInt = "bigint",
11
15
  RegExp = "regexp",
@@ -15,25 +19,173 @@ declare enum JsonSchemaXNativeType {
15
19
  Map = "map"
16
20
  }
17
21
 
18
- interface JsonSchemaCoerceOptions {
19
- components?: Record<string, JsonSchema>;
20
- }
21
22
  declare class JsonSchemaCoercer {
22
- #private;
23
- coerce(schema: JsonSchema, value: unknown, options?: JsonSchemaCoerceOptions): unknown;
23
+ coerce([schema, optional]: [schema: JsonSchema, optional: boolean], value: unknown): unknown;
24
+ private coerceInternal;
25
+ }
26
+
27
+ /**
28
+ * These utilities assume the schema has only one root-level `$defs` object
29
+ * and exclusively use absolute JSON pointers for `$ref` values.
30
+ */
31
+
32
+ declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
33
+ type JsonFileSchema = JsonSchema & object & {
34
+ type: 'string';
35
+ contentMediaType?: string;
36
+ };
37
+ /**
38
+ * Returns true when the schema is a file-like string schema.
39
+ */
40
+ declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
41
+ type JsonObjectSchema = JsonSchema & object & {
42
+ type: 'object';
43
+ };
44
+ /**
45
+ * Returns true when the schema is an object schema.
46
+ */
47
+ declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
48
+ type JsonArraySchema = JsonSchema & object & {
49
+ type: 'array';
50
+ };
51
+ /**
52
+ * Returns true when the schema is an array schema.
53
+ */
54
+ declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
55
+ /**
56
+ * Returns true when the schema does not apply any recognized constraints.
57
+ */
58
+ declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
59
+ /**
60
+ * Ensures a JSON Schema is represented as an object schema document.
61
+ */
62
+ declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
63
+
64
+ /**
65
+ * These utilities assume the schema has only one root-level `$defs` object
66
+ * and exclusively use absolute JSON pointers for `$ref` values.
67
+ */
68
+
69
+ /**
70
+ * Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
71
+ */
72
+ declare function combineJsonSchemasWithComposition(keyword: 'allOf' | 'anyOf' | 'oneOf', schemas: JsonSchema[]): JsonSchema;
73
+ type JsonObjectSchemaEntry = [name: string, schema: JsonSchema, optional: boolean];
74
+ /**
75
+ * Combines object property entries back into a single object schema.
76
+ */
77
+ declare function combineJsonObjectSchemaEntries(entries: JsonObjectSchemaEntry[]): JsonObjectSchema;
78
+ /**
79
+ * Parses an object schema, or a composition of object schemas, into property entries.
80
+ */
81
+ declare function extractJsonObjectSchemaEntries(schema: JsonSchema): JsonObjectSchemaEntry[] | undefined;
82
+ /**
83
+ * Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
84
+ */
85
+ declare function flattenJsonUnionSchema(schema: JsonSchema): JsonSchema[];
86
+ /**
87
+ * Matches a union made of a single item schema and its array form.
88
+ */
89
+ declare function matchArrayableJsonSchema(schema: JsonSchema): undefined | [itemSchema: JsonSchema, arraySchema: JsonArraySchema];
90
+ declare function deduplicateJsonSchemas(schemas: JsonSchema[]): JsonSchema[];
91
+
92
+ type JsonSchemaConverterDirection = 'input' | 'output';
93
+ interface JsonSchemaConverter {
94
+ /**
95
+ * Determines whether this converter can handle the given schema.
96
+ */
97
+ condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): boolean;
98
+ /**
99
+ * Converts an ORPC schema to a JSON Schema representation.
100
+ */
101
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
102
+ }
103
+ declare class DelegatingJsonSchemaConverter implements Pick<JsonSchemaConverter, 'convert'> {
104
+ private readonly converters;
105
+ constructor(converters?: JsonSchemaConverter[]);
106
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
107
+ }
108
+
109
+ /**
110
+ * These utilities assume the schema has only one root-level `$defs` object
111
+ * and exclusively use absolute JSON pointers for `$ref` values.
112
+ */
113
+
114
+ /**
115
+ * Encodes a JSON Pointer segment according to RFC 6901.
116
+ *
117
+ * https://datatracker.ietf.org/doc/html/rfc6901
118
+ */
119
+ declare function encodeJsonPointerSegment(segment: string): string;
120
+ /**
121
+ * Decodes a JSON Pointer segment according to RFC 6901.
122
+ *
123
+ * https://datatracker.ietf.org/doc/html/rfc6901
124
+ */
125
+ declare function decodeJsonPointerSegment(segment: string): string;
126
+ /**
127
+ * Visits every `$ref` in a schema, traversing the same keywords as {@link mapJsonSchemaRefs}.
128
+ * Shared or cyclic object instances are visited once.
129
+ */
130
+ declare function visitJsonSchemaRefs(value: JsonSchema, visit: (ref: string) => void, schemaLevel?: boolean, seen?: Set<object>): void;
131
+ declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
132
+ /**
133
+ * Rewrites recursive root `#` refs by moving the schema body into `$defs`.
134
+ */
135
+ declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
136
+ /**
137
+ * Resolves a local `$ref` at the **root level** of the given schema, if present.
138
+ *
139
+ * Only handles refs of the form `#/$defs/<name>` pointing into the provided
140
+ * (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
141
+ * intentionally left untouched.
142
+ *
143
+ * If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
144
+ * schema is returned as-is.
145
+ *
146
+ * @param schema - The schema whose root-level `$ref` should be resolved.
147
+ * @param $defs - Definition map to resolve against. If omitted, falls back to
148
+ * `schema.$defs`. When provided, takes precedence over any `$defs` embedded
149
+ * in the schema.
150
+ */
151
+ declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclude<JsonSchema, boolean>['$defs']): JsonSchema;
152
+
153
+ interface SmartCoercionHandlerPluginOptions {
154
+ converters?: undefined | JsonSchemaConverter[];
155
+ }
156
+ declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
157
+ name: string;
158
+ private readonly converter;
159
+ private readonly coercer;
160
+ private readonly cache;
161
+ constructor(options?: SmartCoercionHandlerPluginOptions);
162
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
163
+ private coerceValue;
24
164
  }
25
165
 
26
- interface SmartCoercionPluginOptions {
27
- schemaConverters?: readonly ConditionalSchemaConverter[];
166
+ interface SmartCoercionLinkPluginOptions {
167
+ converters?: undefined | JsonSchemaConverter[];
28
168
  }
29
- declare class SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
30
- #private;
169
+ declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
170
+ private readonly contract;
171
+ name: string;
172
+ /**
173
+ * Output and error values should be coerced before validation.
174
+ */
175
+ after: string[];
31
176
  private readonly converter;
32
177
  private readonly coercer;
33
178
  private readonly cache;
34
- constructor(options?: SmartCoercionPluginOptions);
35
- init(options: StandardHandlerOptions<T>): void;
179
+ constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
180
+ init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
181
+ private coerceValue;
182
+ }
183
+
184
+ declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
185
+ condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
186
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
187
+ convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
36
188
  }
37
189
 
38
- export { JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionPlugin };
39
- export type { JsonSchema, JsonSchemaCoerceOptions, SmartCoercionPluginOptions };
190
+ export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef, visitJsonSchemaRefs };
191
+ export type { JsonArraySchema, JsonFileSchema, JsonObjectSchema, JsonObjectSchemaEntry, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection, JsonSchemaKeywords, SmartCoercionHandlerPluginOptions, SmartCoercionLinkPluginOptions };