@orpc/contract 0.0.0-next.fb0d07c → 0.0.0-next.fc1ae52

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
@@ -30,7 +30,8 @@
30
30
  - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
31
  - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
32
32
  - **📝 Contract-First Development**: Optionally define your API contract before implementation.
33
- - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
33
+ - **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
34
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
34
35
  - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
36
  - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
37
  - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
@@ -38,7 +39,6 @@
38
39
  - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
39
40
  - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
40
41
  - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
41
- - **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
42
42
 
43
43
  ## Documentation
44
44
 
@@ -49,14 +49,14 @@ You can find the full documentation [here](https://orpc.unnoq.com).
49
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
50
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
51
51
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
- - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
52
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
53
+ - [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
54
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
53
55
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
54
- - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
55
- - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
56
- - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
57
- - [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
56
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
57
+ - [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
58
58
  - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
59
- - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
59
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
60
60
  - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
61
61
  - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
62
62
  - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
package/dist/index.d.mts CHANGED
@@ -1,236 +1,11 @@
1
- import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath, ClientContext, Client } from '@orpc/client';
1
+ import { HTTPPath, HTTPMethod, ClientContext, Client } from '@orpc/client';
2
2
  export { HTTPMethod, HTTPPath, ORPCError } from '@orpc/client';
3
- import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
4
- export { Registry, ThrowableError } from '@orpc/shared';
5
- import { StandardSchemaV1 } from '@standard-schema/spec';
6
-
7
- type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
8
- type AnySchema = Schema<any, any>;
9
- type SchemaIssue = StandardSchemaV1.Issue;
10
- type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
11
- type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
12
- type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
13
- /**
14
- * The schema for things can be trust without validation.
15
- * If the TInput and TOutput are different, you need pass a map function.
16
- *
17
- * @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
18
- */
19
- declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
20
-
21
- interface ValidationErrorOptions extends ErrorOptions {
22
- message: string;
23
- issues: readonly SchemaIssue[];
24
- }
25
- /**
26
- * This errors usually used for ORPCError.cause when the error is a validation error.
27
- *
28
- * @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
29
- */
30
- declare class ValidationError extends Error {
31
- readonly issues: readonly SchemaIssue[];
32
- constructor(options: ValidationErrorOptions);
33
- }
34
- interface ErrorMapItem<TDataSchema extends AnySchema> {
35
- status?: number;
36
- message?: string;
37
- data?: TDataSchema;
38
- }
39
- type ErrorMap = {
40
- [key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
41
- };
42
- type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
43
- declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
44
- type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
45
- [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
46
- }[keyof TErrorMap];
47
- type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
48
-
49
- type Meta = Record<string, any>;
50
- declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
51
-
52
- type InputStructure = 'compact' | 'detailed';
53
- type OutputStructure = 'compact' | 'detailed';
54
- interface Route {
55
- /**
56
- * The HTTP method of the procedure.
57
- * This option is typically relevant when integrating with OpenAPI.
58
- *
59
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
60
- */
61
- method?: HTTPMethod;
62
- /**
63
- * The HTTP path of the procedure.
64
- * This option is typically relevant when integrating with OpenAPI.
65
- *
66
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
67
- */
68
- path?: HTTPPath;
69
- /**
70
- * The summary of the procedure.
71
- * This option is typically relevant when integrating with OpenAPI.
72
- *
73
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
74
- */
75
- summary?: string;
76
- /**
77
- * The description of the procedure.
78
- * This option is typically relevant when integrating with OpenAPI.
79
- *
80
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
81
- */
82
- description?: string;
83
- /**
84
- * Marks the procedure as deprecated.
85
- * This option is typically relevant when integrating with OpenAPI.
86
- *
87
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
88
- */
89
- deprecated?: boolean;
90
- /**
91
- * The tags of the procedure.
92
- * This option is typically relevant when integrating with OpenAPI.
93
- *
94
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
95
- */
96
- tags?: readonly string[];
97
- /**
98
- * The status code of the response when the procedure is successful.
99
- * The status code must be in the 200-399 range.
100
- * This option is typically relevant when integrating with OpenAPI.
101
- *
102
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
103
- * @default 200
104
- */
105
- successStatus?: number;
106
- /**
107
- * The description of the response when the procedure is successful.
108
- * This option is typically relevant when integrating with OpenAPI.
109
- *
110
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
111
- * @default 'OK'
112
- */
113
- successDescription?: string;
114
- /**
115
- * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
116
- *
117
- * @option 'compact'
118
- * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
119
- *
120
- * @option 'detailed'
121
- * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
122
- *
123
- * Example:
124
- * ```ts
125
- * const input = {
126
- * params: { id: 1 },
127
- * query: { search: 'hello' },
128
- * headers: { 'Content-Type': 'application/json' },
129
- * body: { name: 'John' },
130
- * }
131
- * ```
132
- *
133
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
134
- * @default 'compact'
135
- */
136
- inputStructure?: InputStructure;
137
- /**
138
- * Determines how the response should be structured based on the output.
139
- *
140
- * @option 'compact'
141
- * The output data is directly returned as the response body.
142
- *
143
- * @option 'detailed'
144
- * Return an object with optional properties:
145
- * - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
146
- * - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
147
- * - `body`: The response body.
148
- *
149
- * Example:
150
- * ```ts
151
- * const output = {
152
- * status: 201,
153
- * headers: { 'x-custom-header': 'value' },
154
- * body: { message: 'Hello, world!' },
155
- * };
156
- * ```
157
- *
158
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
159
- * @default 'compact'
160
- */
161
- outputStructure?: OutputStructure;
162
- }
163
- declare function mergeRoute(a: Route, b: Route): Route;
164
- declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
165
- declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
166
- declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
167
- declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
168
- interface EnhanceRouteOptions {
169
- prefix?: HTTPPath;
170
- tags?: readonly string[];
171
- }
172
- declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
173
-
174
- interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
175
- meta: TMeta;
176
- route: Route;
177
- inputSchema?: TInputSchema;
178
- outputSchema?: TOutputSchema;
179
- errorMap: TErrorMap;
180
- }
181
- /**
182
- * This class represents a contract procedure.
183
- *
184
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
185
- */
186
- declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
187
- /**
188
- * This property holds the defined options for the contract procedure.
189
- */
190
- '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
191
- constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
192
- }
193
- type AnyContractProcedure = ContractProcedure<any, any, any, any>;
194
- declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
195
-
196
- /**
197
- * Represents a contract router, which defines a hierarchical structure of contract procedures.
198
- *
199
- * @info A contract procedure is a contract router too.
200
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
201
- */
202
- type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
203
- [k: string]: ContractRouter<TMeta>;
204
- };
205
- type AnyContractRouter = ContractRouter<any>;
206
- /**
207
- * Infer all inputs of the contract router.
208
- *
209
- * @info A contract procedure is a contract router too.
210
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
211
- */
212
- type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
213
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
214
- };
215
- /**
216
- * Infer all outputs of the contract router.
217
- *
218
- * @info A contract procedure is a contract router too.
219
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
220
- */
221
- type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
222
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
223
- };
224
- /**
225
- * Infer all errors of the contract router.
226
- *
227
- * @info A contract procedure is a contract router too.
228
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
229
- */
230
- type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
231
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
232
- }[keyof T];
233
- type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
3
+ import { E as ErrorMap, a as EnhanceRouteOptions, A as AnyContractRouter, C as ContractProcedure, M as MergedErrorMap, b as AnySchema, c as Meta, R as Route, d as ContractRouter, e as ContractProcedureDef, S as Schema, I as InputStructure, O as OutputStructure, f as InferSchemaInput, g as InferSchemaOutput, h as ErrorFromErrorMap, i as SchemaIssue } from './shared/contract.CvRxURhn.mjs';
4
+ export { o as AnyContractProcedure, k as ErrorMapItem, z as InferContractRouterErrorMap, x as InferContractRouterInputs, B as InferContractRouterMeta, y as InferContractRouterOutputs, l as ORPCErrorFromErrorMap, T as TypeRest, j as ValidationError, V as ValidationErrorOptions, w as enhanceRoute, p as isContractProcedure, m as mergeErrorMap, n as mergeMeta, s as mergePrefix, q as mergeRoute, t as mergeTags, r as prefixRoute, D as type, u as unshiftTagRoute, v as validateORPCError } from './shared/contract.CvRxURhn.mjs';
5
+ import { AsyncIteratorClass } from '@orpc/shared';
6
+ export { AsyncIteratorClass, Registry, ThrowableError } from '@orpc/shared';
7
+ export { OpenAPIV3_1 as OpenAPI } from 'openapi-types';
8
+ import '@standard-schema/spec';
234
9
 
235
10
  declare function getContractRouter(router: AnyContractRouter, path: readonly string[]): AnyContractRouter | undefined;
236
11
  type EnhancedContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : {
@@ -240,6 +15,15 @@ interface EnhanceContractRouterOptions<TErrorMap extends ErrorMap> extends Enhan
240
15
  errorMap: TErrorMap;
241
16
  }
242
17
  declare function enhanceContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap>(router: T, options: EnhanceContractRouterOptions<TErrorMap>): EnhancedContractRouter<T, TErrorMap>;
18
+ /**
19
+ * Minify a contract router into a smaller object.
20
+ *
21
+ * You should export the result to a JSON file. On the client side, you can import this JSON file and use it as a contract router.
22
+ * This reduces the size of the contract and helps prevent leaking internal details of the router to the client.
23
+ *
24
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/router-to-contract#minify-export-the-contract-router-for-the-client Router to Contract Docs}
25
+ */
26
+ declare function minifyContractRouter(router: AnyContractRouter): AnyContractRouter;
243
27
 
244
28
  interface ContractProcedureBuilder<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
245
29
  /**
@@ -501,9 +285,16 @@ interface EventIteratorSchemaDetails {
501
285
  *
502
286
  * @see {@link https://orpc.unnoq.com/docs/event-iterator#validate-event-iterator Validate Event Iterator Docs}
503
287
  */
504
- declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
288
+ declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorClass<TYieldOut, TReturnOut, void>>;
505
289
  declare function getEventIteratorSchemaDetails(schema: AnySchema | undefined): undefined | EventIteratorSchemaDetails;
506
290
 
291
+ /**
292
+ * Help RPCLink automatically send requests using the specified HTTP method in the contract.
293
+ *
294
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link#custom-request-method RPCLink Custom Request Method}
295
+ */
296
+ declare function inferRPCMethodFromContractRouter(contract: AnyContractRouter): (options: unknown, path: readonly string[]) => Exclude<HTTPMethod, 'HEAD'>;
297
+
507
298
  type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
508
299
 
509
300
  type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext extends ClientContext = Record<never, never>> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
@@ -512,5 +303,5 @@ type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext exte
512
303
 
513
304
  declare function isSchemaIssue(issue: unknown): issue is SchemaIssue;
514
305
 
515
- export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
516
- export type { AnyContractProcedure, AnyContractRouter, AnySchema, ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractProcedureDef, ContractRouter, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhanceRouteOptions, EnhancedContractRouter, ErrorFromErrorMap, ErrorMap, ErrorMapItem, EventIteratorSchemaDetails, InferContractRouterErrorMap, InferContractRouterInputs, InferContractRouterMeta, InferContractRouterOutputs, InferSchemaInput, InferSchemaOutput, InputStructure, MergedErrorMap, Meta, ORPCErrorFromErrorMap, OutputStructure, Route, Schema, SchemaIssue, TypeRest, ValidationErrorOptions };
306
+ export { AnyContractRouter, AnySchema, ContractBuilder, ContractProcedure, ContractProcedureDef, ContractRouter, EnhanceRouteOptions, ErrorFromErrorMap, ErrorMap, InferSchemaInput, InferSchemaOutput, InputStructure, MergedErrorMap, Meta, OutputStructure, Route, Schema, SchemaIssue, enhanceContractRouter, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isSchemaIssue, minifyContractRouter, oc };
307
+ export type { ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhancedContractRouter, EventIteratorSchemaDetails };
package/dist/index.d.ts CHANGED
@@ -1,236 +1,11 @@
1
- import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath, ClientContext, Client } from '@orpc/client';
1
+ import { HTTPPath, HTTPMethod, ClientContext, Client } from '@orpc/client';
2
2
  export { HTTPMethod, HTTPPath, ORPCError } from '@orpc/client';
3
- import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
4
- export { Registry, ThrowableError } from '@orpc/shared';
5
- import { StandardSchemaV1 } from '@standard-schema/spec';
6
-
7
- type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
8
- type AnySchema = Schema<any, any>;
9
- type SchemaIssue = StandardSchemaV1.Issue;
10
- type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
11
- type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
12
- type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
13
- /**
14
- * The schema for things can be trust without validation.
15
- * If the TInput and TOutput are different, you need pass a map function.
16
- *
17
- * @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
18
- */
19
- declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
20
-
21
- interface ValidationErrorOptions extends ErrorOptions {
22
- message: string;
23
- issues: readonly SchemaIssue[];
24
- }
25
- /**
26
- * This errors usually used for ORPCError.cause when the error is a validation error.
27
- *
28
- * @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
29
- */
30
- declare class ValidationError extends Error {
31
- readonly issues: readonly SchemaIssue[];
32
- constructor(options: ValidationErrorOptions);
33
- }
34
- interface ErrorMapItem<TDataSchema extends AnySchema> {
35
- status?: number;
36
- message?: string;
37
- data?: TDataSchema;
38
- }
39
- type ErrorMap = {
40
- [key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
41
- };
42
- type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
43
- declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
44
- type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
45
- [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
46
- }[keyof TErrorMap];
47
- type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
48
-
49
- type Meta = Record<string, any>;
50
- declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
51
-
52
- type InputStructure = 'compact' | 'detailed';
53
- type OutputStructure = 'compact' | 'detailed';
54
- interface Route {
55
- /**
56
- * The HTTP method of the procedure.
57
- * This option is typically relevant when integrating with OpenAPI.
58
- *
59
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
60
- */
61
- method?: HTTPMethod;
62
- /**
63
- * The HTTP path of the procedure.
64
- * This option is typically relevant when integrating with OpenAPI.
65
- *
66
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
67
- */
68
- path?: HTTPPath;
69
- /**
70
- * The summary of the procedure.
71
- * This option is typically relevant when integrating with OpenAPI.
72
- *
73
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
74
- */
75
- summary?: string;
76
- /**
77
- * The description of the procedure.
78
- * This option is typically relevant when integrating with OpenAPI.
79
- *
80
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
81
- */
82
- description?: string;
83
- /**
84
- * Marks the procedure as deprecated.
85
- * This option is typically relevant when integrating with OpenAPI.
86
- *
87
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
88
- */
89
- deprecated?: boolean;
90
- /**
91
- * The tags of the procedure.
92
- * This option is typically relevant when integrating with OpenAPI.
93
- *
94
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
95
- */
96
- tags?: readonly string[];
97
- /**
98
- * The status code of the response when the procedure is successful.
99
- * The status code must be in the 200-399 range.
100
- * This option is typically relevant when integrating with OpenAPI.
101
- *
102
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
103
- * @default 200
104
- */
105
- successStatus?: number;
106
- /**
107
- * The description of the response when the procedure is successful.
108
- * This option is typically relevant when integrating with OpenAPI.
109
- *
110
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
111
- * @default 'OK'
112
- */
113
- successDescription?: string;
114
- /**
115
- * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
116
- *
117
- * @option 'compact'
118
- * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
119
- *
120
- * @option 'detailed'
121
- * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
122
- *
123
- * Example:
124
- * ```ts
125
- * const input = {
126
- * params: { id: 1 },
127
- * query: { search: 'hello' },
128
- * headers: { 'Content-Type': 'application/json' },
129
- * body: { name: 'John' },
130
- * }
131
- * ```
132
- *
133
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
134
- * @default 'compact'
135
- */
136
- inputStructure?: InputStructure;
137
- /**
138
- * Determines how the response should be structured based on the output.
139
- *
140
- * @option 'compact'
141
- * The output data is directly returned as the response body.
142
- *
143
- * @option 'detailed'
144
- * Return an object with optional properties:
145
- * - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
146
- * - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
147
- * - `body`: The response body.
148
- *
149
- * Example:
150
- * ```ts
151
- * const output = {
152
- * status: 201,
153
- * headers: { 'x-custom-header': 'value' },
154
- * body: { message: 'Hello, world!' },
155
- * };
156
- * ```
157
- *
158
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
159
- * @default 'compact'
160
- */
161
- outputStructure?: OutputStructure;
162
- }
163
- declare function mergeRoute(a: Route, b: Route): Route;
164
- declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
165
- declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
166
- declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
167
- declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
168
- interface EnhanceRouteOptions {
169
- prefix?: HTTPPath;
170
- tags?: readonly string[];
171
- }
172
- declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
173
-
174
- interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
175
- meta: TMeta;
176
- route: Route;
177
- inputSchema?: TInputSchema;
178
- outputSchema?: TOutputSchema;
179
- errorMap: TErrorMap;
180
- }
181
- /**
182
- * This class represents a contract procedure.
183
- *
184
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
185
- */
186
- declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
187
- /**
188
- * This property holds the defined options for the contract procedure.
189
- */
190
- '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
191
- constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
192
- }
193
- type AnyContractProcedure = ContractProcedure<any, any, any, any>;
194
- declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
195
-
196
- /**
197
- * Represents a contract router, which defines a hierarchical structure of contract procedures.
198
- *
199
- * @info A contract procedure is a contract router too.
200
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
201
- */
202
- type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
203
- [k: string]: ContractRouter<TMeta>;
204
- };
205
- type AnyContractRouter = ContractRouter<any>;
206
- /**
207
- * Infer all inputs of the contract router.
208
- *
209
- * @info A contract procedure is a contract router too.
210
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
211
- */
212
- type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
213
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
214
- };
215
- /**
216
- * Infer all outputs of the contract router.
217
- *
218
- * @info A contract procedure is a contract router too.
219
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
220
- */
221
- type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
222
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
223
- };
224
- /**
225
- * Infer all errors of the contract router.
226
- *
227
- * @info A contract procedure is a contract router too.
228
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
229
- */
230
- type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
231
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
232
- }[keyof T];
233
- type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
3
+ import { E as ErrorMap, a as EnhanceRouteOptions, A as AnyContractRouter, C as ContractProcedure, M as MergedErrorMap, b as AnySchema, c as Meta, R as Route, d as ContractRouter, e as ContractProcedureDef, S as Schema, I as InputStructure, O as OutputStructure, f as InferSchemaInput, g as InferSchemaOutput, h as ErrorFromErrorMap, i as SchemaIssue } from './shared/contract.CvRxURhn.js';
4
+ export { o as AnyContractProcedure, k as ErrorMapItem, z as InferContractRouterErrorMap, x as InferContractRouterInputs, B as InferContractRouterMeta, y as InferContractRouterOutputs, l as ORPCErrorFromErrorMap, T as TypeRest, j as ValidationError, V as ValidationErrorOptions, w as enhanceRoute, p as isContractProcedure, m as mergeErrorMap, n as mergeMeta, s as mergePrefix, q as mergeRoute, t as mergeTags, r as prefixRoute, D as type, u as unshiftTagRoute, v as validateORPCError } from './shared/contract.CvRxURhn.js';
5
+ import { AsyncIteratorClass } from '@orpc/shared';
6
+ export { AsyncIteratorClass, Registry, ThrowableError } from '@orpc/shared';
7
+ export { OpenAPIV3_1 as OpenAPI } from 'openapi-types';
8
+ import '@standard-schema/spec';
234
9
 
235
10
  declare function getContractRouter(router: AnyContractRouter, path: readonly string[]): AnyContractRouter | undefined;
236
11
  type EnhancedContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : {
@@ -240,6 +15,15 @@ interface EnhanceContractRouterOptions<TErrorMap extends ErrorMap> extends Enhan
240
15
  errorMap: TErrorMap;
241
16
  }
242
17
  declare function enhanceContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap>(router: T, options: EnhanceContractRouterOptions<TErrorMap>): EnhancedContractRouter<T, TErrorMap>;
18
+ /**
19
+ * Minify a contract router into a smaller object.
20
+ *
21
+ * You should export the result to a JSON file. On the client side, you can import this JSON file and use it as a contract router.
22
+ * This reduces the size of the contract and helps prevent leaking internal details of the router to the client.
23
+ *
24
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/router-to-contract#minify-export-the-contract-router-for-the-client Router to Contract Docs}
25
+ */
26
+ declare function minifyContractRouter(router: AnyContractRouter): AnyContractRouter;
243
27
 
244
28
  interface ContractProcedureBuilder<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
245
29
  /**
@@ -501,9 +285,16 @@ interface EventIteratorSchemaDetails {
501
285
  *
502
286
  * @see {@link https://orpc.unnoq.com/docs/event-iterator#validate-event-iterator Validate Event Iterator Docs}
503
287
  */
504
- declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
288
+ declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorClass<TYieldOut, TReturnOut, void>>;
505
289
  declare function getEventIteratorSchemaDetails(schema: AnySchema | undefined): undefined | EventIteratorSchemaDetails;
506
290
 
291
+ /**
292
+ * Help RPCLink automatically send requests using the specified HTTP method in the contract.
293
+ *
294
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link#custom-request-method RPCLink Custom Request Method}
295
+ */
296
+ declare function inferRPCMethodFromContractRouter(contract: AnyContractRouter): (options: unknown, path: readonly string[]) => Exclude<HTTPMethod, 'HEAD'>;
297
+
507
298
  type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
508
299
 
509
300
  type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext extends ClientContext = Record<never, never>> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
@@ -512,5 +303,5 @@ type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext exte
512
303
 
513
304
  declare function isSchemaIssue(issue: unknown): issue is SchemaIssue;
514
305
 
515
- export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
516
- export type { AnyContractProcedure, AnyContractRouter, AnySchema, ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractProcedureDef, ContractRouter, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhanceRouteOptions, EnhancedContractRouter, ErrorFromErrorMap, ErrorMap, ErrorMapItem, EventIteratorSchemaDetails, InferContractRouterErrorMap, InferContractRouterInputs, InferContractRouterMeta, InferContractRouterOutputs, InferSchemaInput, InferSchemaOutput, InputStructure, MergedErrorMap, Meta, ORPCErrorFromErrorMap, OutputStructure, Route, Schema, SchemaIssue, TypeRest, ValidationErrorOptions };
306
+ export { AnyContractRouter, AnySchema, ContractBuilder, ContractProcedure, ContractProcedureDef, ContractRouter, EnhanceRouteOptions, ErrorFromErrorMap, ErrorMap, InferSchemaInput, InferSchemaOutput, InputStructure, MergedErrorMap, Meta, OutputStructure, Route, Schema, SchemaIssue, enhanceContractRouter, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isSchemaIssue, minifyContractRouter, oc };
307
+ export type { ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhancedContractRouter, EventIteratorSchemaDetails };