@orpc/contract 1.8.4 → 1.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,251 +1,10 @@
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
- import { OpenAPIV3_1 } from 'openapi-types';
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';
7
5
  export { OpenAPIV3_1 as OpenAPI } from 'openapi-types';
8
-
9
- type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
10
- type AnySchema = Schema<any, any>;
11
- type SchemaIssue = StandardSchemaV1.Issue;
12
- type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
13
- type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
14
- type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
15
- /**
16
- * The schema for things can be trust without validation.
17
- * If the TInput and TOutput are different, you need pass a map function.
18
- *
19
- * @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
20
- */
21
- declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
22
-
23
- interface ValidationErrorOptions extends ErrorOptions {
24
- message: string;
25
- issues: readonly SchemaIssue[];
26
- }
27
- /**
28
- * This errors usually used for ORPCError.cause when the error is a validation error.
29
- *
30
- * @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
31
- */
32
- declare class ValidationError extends Error {
33
- readonly issues: readonly SchemaIssue[];
34
- constructor(options: ValidationErrorOptions);
35
- }
36
- interface ErrorMapItem<TDataSchema extends AnySchema> {
37
- status?: number;
38
- message?: string;
39
- data?: TDataSchema;
40
- }
41
- type ErrorMap = {
42
- [key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
43
- };
44
- type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
45
- declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
46
- type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
47
- [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
48
- }[keyof TErrorMap];
49
- type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
50
-
51
- type Meta = Record<string, any>;
52
- declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
53
-
54
- type InputStructure = 'compact' | 'detailed';
55
- type OutputStructure = 'compact' | 'detailed';
56
- interface Route {
57
- /**
58
- * The HTTP method of the procedure.
59
- * This option is typically relevant when integrating with OpenAPI.
60
- *
61
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
62
- */
63
- method?: HTTPMethod;
64
- /**
65
- * The HTTP path of the procedure.
66
- * This option is typically relevant when integrating with OpenAPI.
67
- *
68
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
69
- */
70
- path?: HTTPPath;
71
- /**
72
- * The operation ID of the endpoint.
73
- * This option is typically relevant when integrating with OpenAPI.
74
- *
75
- * @default Concatenation of router segments
76
- */
77
- operationId?: string;
78
- /**
79
- * The summary of the procedure.
80
- * This option is typically relevant when integrating with OpenAPI.
81
- *
82
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
83
- */
84
- summary?: string;
85
- /**
86
- * The description of the procedure.
87
- * This option is typically relevant when integrating with OpenAPI.
88
- *
89
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
90
- */
91
- description?: string;
92
- /**
93
- * Marks the procedure as deprecated.
94
- * This option is typically relevant when integrating with OpenAPI.
95
- *
96
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
97
- */
98
- deprecated?: boolean;
99
- /**
100
- * The tags of the procedure.
101
- * This option is typically relevant when integrating with OpenAPI.
102
- *
103
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
104
- */
105
- tags?: readonly string[];
106
- /**
107
- * The status code of the response when the procedure is successful.
108
- * The status code must be in the 200-399 range.
109
- * This option is typically relevant when integrating with OpenAPI.
110
- *
111
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
112
- * @default 200
113
- */
114
- successStatus?: number;
115
- /**
116
- * The description of the response when the procedure is successful.
117
- * This option is typically relevant when integrating with OpenAPI.
118
- *
119
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
120
- * @default 'OK'
121
- */
122
- successDescription?: string;
123
- /**
124
- * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
125
- *
126
- * @option 'compact'
127
- * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
128
- *
129
- * @option 'detailed'
130
- * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
131
- *
132
- * Example:
133
- * ```ts
134
- * const input = {
135
- * params: { id: 1 },
136
- * query: { search: 'hello' },
137
- * headers: { 'Content-Type': 'application/json' },
138
- * body: { name: 'John' },
139
- * }
140
- * ```
141
- *
142
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
143
- * @default 'compact'
144
- */
145
- inputStructure?: InputStructure;
146
- /**
147
- * Determines how the response should be structured based on the output.
148
- *
149
- * @option 'compact'
150
- * The output data is directly returned as the response body.
151
- *
152
- * @option 'detailed'
153
- * Return an object with optional properties:
154
- * - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
155
- * - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
156
- * - `body`: The response body.
157
- *
158
- * Example:
159
- * ```ts
160
- * const output = {
161
- * status: 201,
162
- * headers: { 'x-custom-header': 'value' },
163
- * body: { message: 'Hello, world!' },
164
- * };
165
- * ```
166
- *
167
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
168
- * @default 'compact'
169
- */
170
- outputStructure?: OutputStructure;
171
- /**
172
- * Override entire auto-generated OpenAPI Operation Object Specification.
173
- *
174
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata Operation Metadata Docs}
175
- */
176
- spec?: OpenAPIV3_1.OperationObject | ((current: OpenAPIV3_1.OperationObject) => OpenAPIV3_1.OperationObject);
177
- }
178
- declare function mergeRoute(a: Route, b: Route): Route;
179
- declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
180
- declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
181
- declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
182
- declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
183
- interface EnhanceRouteOptions {
184
- prefix?: HTTPPath;
185
- tags?: readonly string[];
186
- }
187
- declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
188
-
189
- interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
190
- meta: TMeta;
191
- route: Route;
192
- inputSchema?: TInputSchema;
193
- outputSchema?: TOutputSchema;
194
- errorMap: TErrorMap;
195
- }
196
- /**
197
- * This class represents a contract procedure.
198
- *
199
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
200
- */
201
- declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
202
- /**
203
- * This property holds the defined options for the contract procedure.
204
- */
205
- '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
206
- constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
207
- }
208
- type AnyContractProcedure = ContractProcedure<any, any, any, any>;
209
- declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
210
-
211
- /**
212
- * Represents a contract router, which defines a hierarchical structure of contract procedures.
213
- *
214
- * @info A contract procedure is a contract router too.
215
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
216
- */
217
- type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
218
- [k: string]: ContractRouter<TMeta>;
219
- };
220
- type AnyContractRouter = ContractRouter<any>;
221
- /**
222
- * Infer all inputs of the contract router.
223
- *
224
- * @info A contract procedure is a contract router too.
225
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
226
- */
227
- type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
228
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
229
- };
230
- /**
231
- * Infer all outputs of the contract router.
232
- *
233
- * @info A contract procedure is a contract router too.
234
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
235
- */
236
- type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
237
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
238
- };
239
- /**
240
- * Infer all errors of the contract router.
241
- *
242
- * @info A contract procedure is a contract router too.
243
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
244
- */
245
- type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
246
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
247
- }[keyof T];
248
- type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
6
+ export { Registry, ThrowableError } from '@orpc/shared';
7
+ import '@standard-schema/spec';
249
8
 
250
9
  declare function getContractRouter(router: AnyContractRouter, path: readonly string[]): AnyContractRouter | undefined;
251
10
  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> : {
@@ -543,5 +302,5 @@ type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext exte
543
302
 
544
303
  declare function isSchemaIssue(issue: unknown): issue is SchemaIssue;
545
304
 
546
- export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, oc, prefixRoute, type, unshiftTagRoute };
547
- 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 };
305
+ 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 };
306
+ export type { ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhancedContractRouter, EventIteratorSchemaDetails };
package/dist/index.d.ts CHANGED
@@ -1,251 +1,10 @@
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
- import { OpenAPIV3_1 } from 'openapi-types';
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';
7
5
  export { OpenAPIV3_1 as OpenAPI } from 'openapi-types';
8
-
9
- type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
10
- type AnySchema = Schema<any, any>;
11
- type SchemaIssue = StandardSchemaV1.Issue;
12
- type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
13
- type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
14
- type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
15
- /**
16
- * The schema for things can be trust without validation.
17
- * If the TInput and TOutput are different, you need pass a map function.
18
- *
19
- * @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
20
- */
21
- declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
22
-
23
- interface ValidationErrorOptions extends ErrorOptions {
24
- message: string;
25
- issues: readonly SchemaIssue[];
26
- }
27
- /**
28
- * This errors usually used for ORPCError.cause when the error is a validation error.
29
- *
30
- * @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
31
- */
32
- declare class ValidationError extends Error {
33
- readonly issues: readonly SchemaIssue[];
34
- constructor(options: ValidationErrorOptions);
35
- }
36
- interface ErrorMapItem<TDataSchema extends AnySchema> {
37
- status?: number;
38
- message?: string;
39
- data?: TDataSchema;
40
- }
41
- type ErrorMap = {
42
- [key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
43
- };
44
- type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
45
- declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
46
- type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
47
- [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
48
- }[keyof TErrorMap];
49
- type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
50
-
51
- type Meta = Record<string, any>;
52
- declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
53
-
54
- type InputStructure = 'compact' | 'detailed';
55
- type OutputStructure = 'compact' | 'detailed';
56
- interface Route {
57
- /**
58
- * The HTTP method of the procedure.
59
- * This option is typically relevant when integrating with OpenAPI.
60
- *
61
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
62
- */
63
- method?: HTTPMethod;
64
- /**
65
- * The HTTP path of the procedure.
66
- * This option is typically relevant when integrating with OpenAPI.
67
- *
68
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
69
- */
70
- path?: HTTPPath;
71
- /**
72
- * The operation ID of the endpoint.
73
- * This option is typically relevant when integrating with OpenAPI.
74
- *
75
- * @default Concatenation of router segments
76
- */
77
- operationId?: string;
78
- /**
79
- * The summary of the procedure.
80
- * This option is typically relevant when integrating with OpenAPI.
81
- *
82
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
83
- */
84
- summary?: string;
85
- /**
86
- * The description of the procedure.
87
- * This option is typically relevant when integrating with OpenAPI.
88
- *
89
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
90
- */
91
- description?: string;
92
- /**
93
- * Marks the procedure as deprecated.
94
- * This option is typically relevant when integrating with OpenAPI.
95
- *
96
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
97
- */
98
- deprecated?: boolean;
99
- /**
100
- * The tags of the procedure.
101
- * This option is typically relevant when integrating with OpenAPI.
102
- *
103
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
104
- */
105
- tags?: readonly string[];
106
- /**
107
- * The status code of the response when the procedure is successful.
108
- * The status code must be in the 200-399 range.
109
- * This option is typically relevant when integrating with OpenAPI.
110
- *
111
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
112
- * @default 200
113
- */
114
- successStatus?: number;
115
- /**
116
- * The description of the response when the procedure is successful.
117
- * This option is typically relevant when integrating with OpenAPI.
118
- *
119
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
120
- * @default 'OK'
121
- */
122
- successDescription?: string;
123
- /**
124
- * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
125
- *
126
- * @option 'compact'
127
- * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
128
- *
129
- * @option 'detailed'
130
- * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
131
- *
132
- * Example:
133
- * ```ts
134
- * const input = {
135
- * params: { id: 1 },
136
- * query: { search: 'hello' },
137
- * headers: { 'Content-Type': 'application/json' },
138
- * body: { name: 'John' },
139
- * }
140
- * ```
141
- *
142
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
143
- * @default 'compact'
144
- */
145
- inputStructure?: InputStructure;
146
- /**
147
- * Determines how the response should be structured based on the output.
148
- *
149
- * @option 'compact'
150
- * The output data is directly returned as the response body.
151
- *
152
- * @option 'detailed'
153
- * Return an object with optional properties:
154
- * - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
155
- * - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
156
- * - `body`: The response body.
157
- *
158
- * Example:
159
- * ```ts
160
- * const output = {
161
- * status: 201,
162
- * headers: { 'x-custom-header': 'value' },
163
- * body: { message: 'Hello, world!' },
164
- * };
165
- * ```
166
- *
167
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
168
- * @default 'compact'
169
- */
170
- outputStructure?: OutputStructure;
171
- /**
172
- * Override entire auto-generated OpenAPI Operation Object Specification.
173
- *
174
- * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata Operation Metadata Docs}
175
- */
176
- spec?: OpenAPIV3_1.OperationObject | ((current: OpenAPIV3_1.OperationObject) => OpenAPIV3_1.OperationObject);
177
- }
178
- declare function mergeRoute(a: Route, b: Route): Route;
179
- declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
180
- declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
181
- declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
182
- declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
183
- interface EnhanceRouteOptions {
184
- prefix?: HTTPPath;
185
- tags?: readonly string[];
186
- }
187
- declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
188
-
189
- interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
190
- meta: TMeta;
191
- route: Route;
192
- inputSchema?: TInputSchema;
193
- outputSchema?: TOutputSchema;
194
- errorMap: TErrorMap;
195
- }
196
- /**
197
- * This class represents a contract procedure.
198
- *
199
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
200
- */
201
- declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
202
- /**
203
- * This property holds the defined options for the contract procedure.
204
- */
205
- '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
206
- constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
207
- }
208
- type AnyContractProcedure = ContractProcedure<any, any, any, any>;
209
- declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
210
-
211
- /**
212
- * Represents a contract router, which defines a hierarchical structure of contract procedures.
213
- *
214
- * @info A contract procedure is a contract router too.
215
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
216
- */
217
- type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
218
- [k: string]: ContractRouter<TMeta>;
219
- };
220
- type AnyContractRouter = ContractRouter<any>;
221
- /**
222
- * Infer all inputs of the contract router.
223
- *
224
- * @info A contract procedure is a contract router too.
225
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
226
- */
227
- type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
228
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
229
- };
230
- /**
231
- * Infer all outputs of the contract router.
232
- *
233
- * @info A contract procedure is a contract router too.
234
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
235
- */
236
- type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
237
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
238
- };
239
- /**
240
- * Infer all errors of the contract router.
241
- *
242
- * @info A contract procedure is a contract router too.
243
- * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
244
- */
245
- type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
246
- [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
247
- }[keyof T];
248
- type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
6
+ export { Registry, ThrowableError } from '@orpc/shared';
7
+ import '@standard-schema/spec';
249
8
 
250
9
  declare function getContractRouter(router: AnyContractRouter, path: readonly string[]): AnyContractRouter | undefined;
251
10
  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> : {
@@ -543,5 +302,5 @@ type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext exte
543
302
 
544
303
  declare function isSchemaIssue(issue: unknown): issue is SchemaIssue;
545
304
 
546
- export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, oc, prefixRoute, type, unshiftTagRoute };
547
- 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 };
305
+ 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 };
306
+ export type { ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhancedContractRouter, EventIteratorSchemaDetails };
package/dist/index.mjs CHANGED
@@ -1,44 +1,13 @@
1
- import { isORPCErrorStatus, mapEventIterator, ORPCError } from '@orpc/client';
1
+ import { i as isContractProcedure, C as ContractProcedure, m as mergeErrorMap, V as ValidationError } from './shared/contract.D_dZrO__.mjs';
2
+ export { v as validateORPCError } from './shared/contract.D_dZrO__.mjs';
3
+ import { mapEventIterator, ORPCError } from '@orpc/client';
2
4
  export { ORPCError } from '@orpc/client';
3
5
  import { isAsyncIteratorObject, get, isTypescriptObject, isPropertyKey } from '@orpc/shared';
4
6
 
5
- class ValidationError extends Error {
6
- issues;
7
- constructor(options) {
8
- super(options.message, options);
9
- this.issues = options.issues;
10
- }
11
- }
12
- function mergeErrorMap(errorMap1, errorMap2) {
13
- return { ...errorMap1, ...errorMap2 };
14
- }
15
-
16
7
  function mergeMeta(meta1, meta2) {
17
8
  return { ...meta1, ...meta2 };
18
9
  }
19
10
 
20
- class ContractProcedure {
21
- /**
22
- * This property holds the defined options for the contract procedure.
23
- */
24
- "~orpc";
25
- constructor(def) {
26
- if (def.route?.successStatus && isORPCErrorStatus(def.route.successStatus)) {
27
- throw new Error("[ContractProcedure] Invalid successStatus.");
28
- }
29
- if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
30
- throw new Error("[ContractProcedure] Invalid error status code.");
31
- }
32
- this["~orpc"] = def;
33
- }
34
- }
35
- function isContractProcedure(item) {
36
- if (item instanceof ContractProcedure) {
37
- return true;
38
- }
39
- return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
40
- }
41
-
42
11
  function mergeRoute(a, b) {
43
12
  return { ...a, ...b };
44
13
  }
@@ -289,7 +258,8 @@ function eventIterator(yields, returns) {
289
258
  message: "Event iterator validation failed",
290
259
  cause: new ValidationError({
291
260
  issues: result.issues,
292
- message: "Event iterator validation failed"
261
+ message: "Event iterator validation failed",
262
+ data: value
293
263
  })
294
264
  });
295
265
  }
@@ -0,0 +1,24 @@
1
+ import { ClientContext } from '@orpc/client';
2
+ import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
3
+ import { A as AnyContractRouter } from '../shared/contract.CvRxURhn.mjs';
4
+ import '@orpc/shared';
5
+ import '@standard-schema/spec';
6
+ import 'openapi-types';
7
+
8
+ /**
9
+ * A link plugin that validates server responses against your contract schema,
10
+ * ensuring that data returned from your server matches the expected types defined in your contract.
11
+ *
12
+ * - Throws `ValidationError` if output doesn't match the expected schema
13
+ * - Converts mismatched defined errors to normal `ORPCError` instances
14
+ *
15
+ * @see {@link https://orpc.unnoq.com/docs/plugins/response-validation Response Validation Plugin Docs}
16
+ */
17
+ declare class ResponseValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
18
+ private readonly contract;
19
+ constructor(contract: AnyContractRouter);
20
+ order: number;
21
+ init(options: StandardLinkOptions<T>): void;
22
+ }
23
+
24
+ export { ResponseValidationPlugin };
@@ -0,0 +1,24 @@
1
+ import { ClientContext } from '@orpc/client';
2
+ import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
3
+ import { A as AnyContractRouter } from '../shared/contract.CvRxURhn.js';
4
+ import '@orpc/shared';
5
+ import '@standard-schema/spec';
6
+ import 'openapi-types';
7
+
8
+ /**
9
+ * A link plugin that validates server responses against your contract schema,
10
+ * ensuring that data returned from your server matches the expected types defined in your contract.
11
+ *
12
+ * - Throws `ValidationError` if output doesn't match the expected schema
13
+ * - Converts mismatched defined errors to normal `ORPCError` instances
14
+ *
15
+ * @see {@link https://orpc.unnoq.com/docs/plugins/response-validation Response Validation Plugin Docs}
16
+ */
17
+ declare class ResponseValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
18
+ private readonly contract;
19
+ constructor(contract: AnyContractRouter);
20
+ order: number;
21
+ init(options: StandardLinkOptions<T>): void;
22
+ }
23
+
24
+ export { ResponseValidationPlugin };
@@ -0,0 +1,43 @@
1
+ import { ORPCError } from '@orpc/client';
2
+ import { get } from '@orpc/shared';
3
+ import { i as isContractProcedure, V as ValidationError, v as validateORPCError } from '../shared/contract.D_dZrO__.mjs';
4
+
5
+ class ResponseValidationPlugin {
6
+ constructor(contract) {
7
+ this.contract = contract;
8
+ }
9
+ order = 15e5;
10
+ // make sure run before DurableEventIteratorLinkPlugin
11
+ init(options) {
12
+ options.interceptors ??= [];
13
+ options.interceptors.push(async ({ next, path }) => {
14
+ const procedure = get(this.contract, path);
15
+ if (!isContractProcedure(procedure)) {
16
+ throw new Error(`[ResponseValidationPlugin] no valid procedure found at path "${path.join(".")}", this may happen when the contract router is not properly configured.`);
17
+ }
18
+ try {
19
+ const output = await next();
20
+ const outputSchema = procedure["~orpc"].outputSchema;
21
+ if (!outputSchema) {
22
+ return output;
23
+ }
24
+ const result = await outputSchema["~standard"].validate(output);
25
+ if (result.issues) {
26
+ throw new ValidationError({
27
+ message: "Server response output does not match expected schema",
28
+ issues: result.issues,
29
+ data: output
30
+ });
31
+ }
32
+ return result.value;
33
+ } catch (e) {
34
+ if (e instanceof ORPCError) {
35
+ throw await validateORPCError(procedure["~orpc"].errorMap, e);
36
+ }
37
+ throw e;
38
+ }
39
+ });
40
+ }
41
+ }
42
+
43
+ export { ResponseValidationPlugin };
@@ -0,0 +1,254 @@
1
+ import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath } from '@orpc/client';
2
+ import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
3
+ import { StandardSchemaV1 } from '@standard-schema/spec';
4
+ import { OpenAPIV3_1 } from 'openapi-types';
5
+
6
+ type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
7
+ type AnySchema = Schema<any, any>;
8
+ type SchemaIssue = StandardSchemaV1.Issue;
9
+ type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
10
+ type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
11
+ type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
12
+ /**
13
+ * The schema for things can be trust without validation.
14
+ * If the TInput and TOutput are different, you need pass a map function.
15
+ *
16
+ * @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
17
+ */
18
+ declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
19
+
20
+ interface ValidationErrorOptions extends ErrorOptions {
21
+ message: string;
22
+ issues: readonly SchemaIssue[];
23
+ /**
24
+ * @todo require this field in v2
25
+ */
26
+ data?: unknown;
27
+ }
28
+ /**
29
+ * This errors usually used for ORPCError.cause when the error is a validation error.
30
+ *
31
+ * @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
32
+ */
33
+ declare class ValidationError extends Error {
34
+ readonly issues: readonly SchemaIssue[];
35
+ readonly data: unknown;
36
+ constructor(options: ValidationErrorOptions);
37
+ }
38
+ interface ErrorMapItem<TDataSchema extends AnySchema> {
39
+ status?: number;
40
+ message?: string;
41
+ data?: TDataSchema;
42
+ }
43
+ type ErrorMap = {
44
+ [key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
45
+ };
46
+ type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
47
+ declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
48
+ type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
49
+ [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
50
+ }[keyof TErrorMap];
51
+ type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
52
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
53
+
54
+ type Meta = Record<string, any>;
55
+ declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
56
+
57
+ type InputStructure = 'compact' | 'detailed';
58
+ type OutputStructure = 'compact' | 'detailed';
59
+ interface Route {
60
+ /**
61
+ * The HTTP method of the procedure.
62
+ * This option is typically relevant when integrating with OpenAPI.
63
+ *
64
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
65
+ */
66
+ method?: HTTPMethod;
67
+ /**
68
+ * The HTTP path of the procedure.
69
+ * This option is typically relevant when integrating with OpenAPI.
70
+ *
71
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
72
+ */
73
+ path?: HTTPPath;
74
+ /**
75
+ * The operation ID of the endpoint.
76
+ * This option is typically relevant when integrating with OpenAPI.
77
+ *
78
+ * @default Concatenation of router segments
79
+ */
80
+ operationId?: string;
81
+ /**
82
+ * The summary of the procedure.
83
+ * This option is typically relevant when integrating with OpenAPI.
84
+ *
85
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
86
+ */
87
+ summary?: string;
88
+ /**
89
+ * The description of the procedure.
90
+ * This option is typically relevant when integrating with OpenAPI.
91
+ *
92
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
93
+ */
94
+ description?: string;
95
+ /**
96
+ * Marks the procedure as deprecated.
97
+ * This option is typically relevant when integrating with OpenAPI.
98
+ *
99
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
100
+ */
101
+ deprecated?: boolean;
102
+ /**
103
+ * The tags of the procedure.
104
+ * This option is typically relevant when integrating with OpenAPI.
105
+ *
106
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
107
+ */
108
+ tags?: readonly string[];
109
+ /**
110
+ * The status code of the response when the procedure is successful.
111
+ * The status code must be in the 200-399 range.
112
+ * This option is typically relevant when integrating with OpenAPI.
113
+ *
114
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
115
+ * @default 200
116
+ */
117
+ successStatus?: number;
118
+ /**
119
+ * The description of the response when the procedure is successful.
120
+ * This option is typically relevant when integrating with OpenAPI.
121
+ *
122
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
123
+ * @default 'OK'
124
+ */
125
+ successDescription?: string;
126
+ /**
127
+ * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
128
+ *
129
+ * @option 'compact'
130
+ * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
131
+ *
132
+ * @option 'detailed'
133
+ * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
134
+ *
135
+ * Example:
136
+ * ```ts
137
+ * const input = {
138
+ * params: { id: 1 },
139
+ * query: { search: 'hello' },
140
+ * headers: { 'Content-Type': 'application/json' },
141
+ * body: { name: 'John' },
142
+ * }
143
+ * ```
144
+ *
145
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
146
+ * @default 'compact'
147
+ */
148
+ inputStructure?: InputStructure;
149
+ /**
150
+ * Determines how the response should be structured based on the output.
151
+ *
152
+ * @option 'compact'
153
+ * The output data is directly returned as the response body.
154
+ *
155
+ * @option 'detailed'
156
+ * Return an object with optional properties:
157
+ * - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
158
+ * - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
159
+ * - `body`: The response body.
160
+ *
161
+ * Example:
162
+ * ```ts
163
+ * const output = {
164
+ * status: 201,
165
+ * headers: { 'x-custom-header': 'value' },
166
+ * body: { message: 'Hello, world!' },
167
+ * };
168
+ * ```
169
+ *
170
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
171
+ * @default 'compact'
172
+ */
173
+ outputStructure?: OutputStructure;
174
+ /**
175
+ * Override entire auto-generated OpenAPI Operation Object Specification.
176
+ *
177
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata Operation Metadata Docs}
178
+ */
179
+ spec?: OpenAPIV3_1.OperationObject | ((current: OpenAPIV3_1.OperationObject) => OpenAPIV3_1.OperationObject);
180
+ }
181
+ declare function mergeRoute(a: Route, b: Route): Route;
182
+ declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
183
+ declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
184
+ declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
185
+ declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
186
+ interface EnhanceRouteOptions {
187
+ prefix?: HTTPPath;
188
+ tags?: readonly string[];
189
+ }
190
+ declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
191
+
192
+ interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
193
+ meta: TMeta;
194
+ route: Route;
195
+ inputSchema?: TInputSchema;
196
+ outputSchema?: TOutputSchema;
197
+ errorMap: TErrorMap;
198
+ }
199
+ /**
200
+ * This class represents a contract procedure.
201
+ *
202
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
203
+ */
204
+ declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
205
+ /**
206
+ * This property holds the defined options for the contract procedure.
207
+ */
208
+ '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
209
+ constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
210
+ }
211
+ type AnyContractProcedure = ContractProcedure<any, any, any, any>;
212
+ declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
213
+
214
+ /**
215
+ * Represents a contract router, which defines a hierarchical structure of contract procedures.
216
+ *
217
+ * @info A contract procedure is a contract router too.
218
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
219
+ */
220
+ type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
221
+ [k: string]: ContractRouter<TMeta>;
222
+ };
223
+ type AnyContractRouter = ContractRouter<any>;
224
+ /**
225
+ * Infer all inputs 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 InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
231
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
232
+ };
233
+ /**
234
+ * Infer all outputs of the contract router.
235
+ *
236
+ * @info A contract procedure is a contract router too.
237
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
238
+ */
239
+ type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
240
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
241
+ };
242
+ /**
243
+ * Infer all errors of the contract router.
244
+ *
245
+ * @info A contract procedure is a contract router too.
246
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
247
+ */
248
+ type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
249
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
250
+ }[keyof T];
251
+ type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
252
+
253
+ export { ContractProcedure as C, type as D, ValidationError as j, mergeErrorMap as m, mergeMeta as n, isContractProcedure as p, mergeRoute as q, prefixRoute as r, mergePrefix as s, mergeTags as t, unshiftTagRoute as u, validateORPCError as v, enhanceRoute as w };
254
+ export type { AnyContractRouter as A, InferContractRouterMeta as B, ErrorMap as E, InputStructure as I, MergedErrorMap as M, OutputStructure as O, Route as R, Schema as S, TypeRest as T, ValidationErrorOptions as V, EnhanceRouteOptions as a, AnySchema as b, Meta as c, ContractRouter as d, ContractProcedureDef as e, InferSchemaInput as f, InferSchemaOutput as g, ErrorFromErrorMap as h, SchemaIssue as i, ErrorMapItem as k, ORPCErrorFromErrorMap as l, AnyContractProcedure as o, InferContractRouterInputs as x, InferContractRouterOutputs as y, InferContractRouterErrorMap as z };
@@ -0,0 +1,254 @@
1
+ import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath } from '@orpc/client';
2
+ import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
3
+ import { StandardSchemaV1 } from '@standard-schema/spec';
4
+ import { OpenAPIV3_1 } from 'openapi-types';
5
+
6
+ type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
7
+ type AnySchema = Schema<any, any>;
8
+ type SchemaIssue = StandardSchemaV1.Issue;
9
+ type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
10
+ type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
11
+ type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
12
+ /**
13
+ * The schema for things can be trust without validation.
14
+ * If the TInput and TOutput are different, you need pass a map function.
15
+ *
16
+ * @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
17
+ */
18
+ declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
19
+
20
+ interface ValidationErrorOptions extends ErrorOptions {
21
+ message: string;
22
+ issues: readonly SchemaIssue[];
23
+ /**
24
+ * @todo require this field in v2
25
+ */
26
+ data?: unknown;
27
+ }
28
+ /**
29
+ * This errors usually used for ORPCError.cause when the error is a validation error.
30
+ *
31
+ * @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
32
+ */
33
+ declare class ValidationError extends Error {
34
+ readonly issues: readonly SchemaIssue[];
35
+ readonly data: unknown;
36
+ constructor(options: ValidationErrorOptions);
37
+ }
38
+ interface ErrorMapItem<TDataSchema extends AnySchema> {
39
+ status?: number;
40
+ message?: string;
41
+ data?: TDataSchema;
42
+ }
43
+ type ErrorMap = {
44
+ [key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
45
+ };
46
+ type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
47
+ declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
48
+ type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
49
+ [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
50
+ }[keyof TErrorMap];
51
+ type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
52
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
53
+
54
+ type Meta = Record<string, any>;
55
+ declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
56
+
57
+ type InputStructure = 'compact' | 'detailed';
58
+ type OutputStructure = 'compact' | 'detailed';
59
+ interface Route {
60
+ /**
61
+ * The HTTP method of the procedure.
62
+ * This option is typically relevant when integrating with OpenAPI.
63
+ *
64
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
65
+ */
66
+ method?: HTTPMethod;
67
+ /**
68
+ * The HTTP path of the procedure.
69
+ * This option is typically relevant when integrating with OpenAPI.
70
+ *
71
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
72
+ */
73
+ path?: HTTPPath;
74
+ /**
75
+ * The operation ID of the endpoint.
76
+ * This option is typically relevant when integrating with OpenAPI.
77
+ *
78
+ * @default Concatenation of router segments
79
+ */
80
+ operationId?: string;
81
+ /**
82
+ * The summary of the procedure.
83
+ * This option is typically relevant when integrating with OpenAPI.
84
+ *
85
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
86
+ */
87
+ summary?: string;
88
+ /**
89
+ * The description of the procedure.
90
+ * This option is typically relevant when integrating with OpenAPI.
91
+ *
92
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
93
+ */
94
+ description?: string;
95
+ /**
96
+ * Marks the procedure as deprecated.
97
+ * This option is typically relevant when integrating with OpenAPI.
98
+ *
99
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
100
+ */
101
+ deprecated?: boolean;
102
+ /**
103
+ * The tags of the procedure.
104
+ * This option is typically relevant when integrating with OpenAPI.
105
+ *
106
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
107
+ */
108
+ tags?: readonly string[];
109
+ /**
110
+ * The status code of the response when the procedure is successful.
111
+ * The status code must be in the 200-399 range.
112
+ * This option is typically relevant when integrating with OpenAPI.
113
+ *
114
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
115
+ * @default 200
116
+ */
117
+ successStatus?: number;
118
+ /**
119
+ * The description of the response when the procedure is successful.
120
+ * This option is typically relevant when integrating with OpenAPI.
121
+ *
122
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
123
+ * @default 'OK'
124
+ */
125
+ successDescription?: string;
126
+ /**
127
+ * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
128
+ *
129
+ * @option 'compact'
130
+ * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
131
+ *
132
+ * @option 'detailed'
133
+ * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
134
+ *
135
+ * Example:
136
+ * ```ts
137
+ * const input = {
138
+ * params: { id: 1 },
139
+ * query: { search: 'hello' },
140
+ * headers: { 'Content-Type': 'application/json' },
141
+ * body: { name: 'John' },
142
+ * }
143
+ * ```
144
+ *
145
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
146
+ * @default 'compact'
147
+ */
148
+ inputStructure?: InputStructure;
149
+ /**
150
+ * Determines how the response should be structured based on the output.
151
+ *
152
+ * @option 'compact'
153
+ * The output data is directly returned as the response body.
154
+ *
155
+ * @option 'detailed'
156
+ * Return an object with optional properties:
157
+ * - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
158
+ * - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
159
+ * - `body`: The response body.
160
+ *
161
+ * Example:
162
+ * ```ts
163
+ * const output = {
164
+ * status: 201,
165
+ * headers: { 'x-custom-header': 'value' },
166
+ * body: { message: 'Hello, world!' },
167
+ * };
168
+ * ```
169
+ *
170
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
171
+ * @default 'compact'
172
+ */
173
+ outputStructure?: OutputStructure;
174
+ /**
175
+ * Override entire auto-generated OpenAPI Operation Object Specification.
176
+ *
177
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata Operation Metadata Docs}
178
+ */
179
+ spec?: OpenAPIV3_1.OperationObject | ((current: OpenAPIV3_1.OperationObject) => OpenAPIV3_1.OperationObject);
180
+ }
181
+ declare function mergeRoute(a: Route, b: Route): Route;
182
+ declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
183
+ declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
184
+ declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
185
+ declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
186
+ interface EnhanceRouteOptions {
187
+ prefix?: HTTPPath;
188
+ tags?: readonly string[];
189
+ }
190
+ declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
191
+
192
+ interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
193
+ meta: TMeta;
194
+ route: Route;
195
+ inputSchema?: TInputSchema;
196
+ outputSchema?: TOutputSchema;
197
+ errorMap: TErrorMap;
198
+ }
199
+ /**
200
+ * This class represents a contract procedure.
201
+ *
202
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
203
+ */
204
+ declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
205
+ /**
206
+ * This property holds the defined options for the contract procedure.
207
+ */
208
+ '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
209
+ constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
210
+ }
211
+ type AnyContractProcedure = ContractProcedure<any, any, any, any>;
212
+ declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
213
+
214
+ /**
215
+ * Represents a contract router, which defines a hierarchical structure of contract procedures.
216
+ *
217
+ * @info A contract procedure is a contract router too.
218
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
219
+ */
220
+ type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
221
+ [k: string]: ContractRouter<TMeta>;
222
+ };
223
+ type AnyContractRouter = ContractRouter<any>;
224
+ /**
225
+ * Infer all inputs 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 InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
231
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
232
+ };
233
+ /**
234
+ * Infer all outputs of the contract router.
235
+ *
236
+ * @info A contract procedure is a contract router too.
237
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
238
+ */
239
+ type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
240
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
241
+ };
242
+ /**
243
+ * Infer all errors of the contract router.
244
+ *
245
+ * @info A contract procedure is a contract router too.
246
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
247
+ */
248
+ type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
249
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
250
+ }[keyof T];
251
+ type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
252
+
253
+ export { ContractProcedure as C, type as D, ValidationError as j, mergeErrorMap as m, mergeMeta as n, isContractProcedure as p, mergeRoute as q, prefixRoute as r, mergePrefix as s, mergeTags as t, unshiftTagRoute as u, validateORPCError as v, enhanceRoute as w };
254
+ export type { AnyContractRouter as A, InferContractRouterMeta as B, ErrorMap as E, InputStructure as I, MergedErrorMap as M, OutputStructure as O, Route as R, Schema as S, TypeRest as T, ValidationErrorOptions as V, EnhanceRouteOptions as a, AnySchema as b, Meta as c, ContractRouter as d, ContractProcedureDef as e, InferSchemaInput as f, InferSchemaOutput as g, ErrorFromErrorMap as h, SchemaIssue as i, ErrorMapItem as k, ORPCErrorFromErrorMap as l, AnyContractProcedure as o, InferContractRouterInputs as x, InferContractRouterOutputs as y, InferContractRouterErrorMap as z };
@@ -0,0 +1,53 @@
1
+ import { fallbackORPCErrorStatus, ORPCError, isORPCErrorStatus } from '@orpc/client';
2
+
3
+ class ValidationError extends Error {
4
+ issues;
5
+ data;
6
+ constructor(options) {
7
+ super(options.message, options);
8
+ this.issues = options.issues;
9
+ this.data = options.data;
10
+ }
11
+ }
12
+ function mergeErrorMap(errorMap1, errorMap2) {
13
+ return { ...errorMap1, ...errorMap2 };
14
+ }
15
+ async function validateORPCError(map, error) {
16
+ const { code, status, message, data, cause, defined } = error;
17
+ const config = map?.[error.code];
18
+ if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
19
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
20
+ }
21
+ if (!config.data) {
22
+ return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
23
+ }
24
+ const validated = await config.data["~standard"].validate(error.data);
25
+ if (validated.issues) {
26
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
27
+ }
28
+ return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
29
+ }
30
+
31
+ class ContractProcedure {
32
+ /**
33
+ * This property holds the defined options for the contract procedure.
34
+ */
35
+ "~orpc";
36
+ constructor(def) {
37
+ if (def.route?.successStatus && isORPCErrorStatus(def.route.successStatus)) {
38
+ throw new Error("[ContractProcedure] Invalid successStatus.");
39
+ }
40
+ if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
41
+ throw new Error("[ContractProcedure] Invalid error status code.");
42
+ }
43
+ this["~orpc"] = def;
44
+ }
45
+ }
46
+ function isContractProcedure(item) {
47
+ if (item instanceof ContractProcedure) {
48
+ return true;
49
+ }
50
+ return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
51
+ }
52
+
53
+ export { ContractProcedure as C, ValidationError as V, isContractProcedure as i, mergeErrorMap as m, validateORPCError as v };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
3
  "type": "module",
4
- "version": "1.8.4",
4
+ "version": "1.8.6",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -18,6 +18,11 @@
18
18
  "types": "./dist/index.d.mts",
19
19
  "import": "./dist/index.mjs",
20
20
  "default": "./dist/index.mjs"
21
+ },
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
21
26
  }
22
27
  },
23
28
  "files": [
@@ -26,13 +31,13 @@
26
31
  "dependencies": {
27
32
  "@standard-schema/spec": "^1.0.0",
28
33
  "openapi-types": "^12.1.3",
29
- "@orpc/client": "1.8.4",
30
- "@orpc/shared": "1.8.4"
34
+ "@orpc/client": "1.8.6",
35
+ "@orpc/shared": "1.8.6"
31
36
  },
32
37
  "devDependencies": {
33
38
  "arktype": "2.1.20",
34
39
  "valibot": "^1.1.0",
35
- "zod": "^4.0.17"
40
+ "zod": "^4.1.3"
36
41
  },
37
42
  "scripts": {
38
43
  "build": "unbuild",