@orpc/contract 1.14.11 → 1.14.13
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 +81 -52
- package/dist/index.d.mts +301 -161
- package/dist/index.d.ts +301 -161
- package/dist/index.mjs +283 -136
- package/dist/plugins/index.d.mts +29 -24
- package/dist/plugins/index.d.ts +29 -24
- package/dist/plugins/index.mjs +63 -72
- package/dist/shared/contract.D_dZrO__.mjs +53 -0
- package/dist/shared/contract.TuRtB1Ca.d.mts +254 -0
- package/dist/shared/contract.TuRtB1Ca.d.ts +254 -0
- package/package.json +8 -7
- package/dist/shared/contract.CW-2wl1i.mjs +0 -180
- package/dist/shared/contract.Do92aRJ4.d.mts +0 -126
- package/dist/shared/contract.Do92aRJ4.d.ts +0 -126
|
@@ -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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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 };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.14.
|
|
4
|
+
"version": "1.14.13",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
],
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"exports": {
|
|
17
|
-
"./package.json": "./package.json",
|
|
18
17
|
".": {
|
|
19
18
|
"types": "./dist/index.d.mts",
|
|
20
19
|
"import": "./dist/index.mjs",
|
|
@@ -31,16 +30,18 @@
|
|
|
31
30
|
],
|
|
32
31
|
"dependencies": {
|
|
33
32
|
"@standard-schema/spec": "^1.1.0",
|
|
34
|
-
"
|
|
35
|
-
"@orpc/client": "1.14.
|
|
33
|
+
"openapi-types": "^12.1.3",
|
|
34
|
+
"@orpc/client": "1.14.13",
|
|
35
|
+
"@orpc/shared": "1.14.13"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"arktype": "
|
|
39
|
-
"valibot": "^1.
|
|
40
|
-
"zod": "^4.
|
|
38
|
+
"arktype": "2.2.0",
|
|
39
|
+
"valibot": "^1.2.0",
|
|
40
|
+
"zod": "^4.3.6"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "unbuild",
|
|
44
|
+
"build:watch": "pnpm run build --watch",
|
|
44
45
|
"type:check": "tsc -b"
|
|
45
46
|
}
|
|
46
47
|
}
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { toArray, getConstructor, isTypescriptObject } from '@orpc/shared';
|
|
2
|
-
import { cloneORPCError } from '@orpc/client';
|
|
3
|
-
|
|
4
|
-
function mergeErrorMap(errorMap1, errorMap2) {
|
|
5
|
-
return { ...errorMap1, ...errorMap2 };
|
|
6
|
-
}
|
|
7
|
-
async function reconcileORPCError(map, error) {
|
|
8
|
-
const config = map[error.code];
|
|
9
|
-
if (!config) {
|
|
10
|
-
if (!error.defined) {
|
|
11
|
-
return error;
|
|
12
|
-
}
|
|
13
|
-
const cloned2 = cloneORPCError(error);
|
|
14
|
-
cloned2.defined = false;
|
|
15
|
-
cloned2.inferable = false;
|
|
16
|
-
return cloned2;
|
|
17
|
-
}
|
|
18
|
-
if (!config.data) {
|
|
19
|
-
if (error.defined && error.inferable) {
|
|
20
|
-
return error;
|
|
21
|
-
}
|
|
22
|
-
const cloned2 = cloneORPCError(error);
|
|
23
|
-
cloned2.defined = true;
|
|
24
|
-
cloned2.inferable = true;
|
|
25
|
-
return cloned2;
|
|
26
|
-
}
|
|
27
|
-
const validated = await config.data["~standard"].validate(error.data);
|
|
28
|
-
if (validated.issues) {
|
|
29
|
-
if (!error.defined) {
|
|
30
|
-
return error;
|
|
31
|
-
}
|
|
32
|
-
const cloned2 = cloneORPCError(error);
|
|
33
|
-
cloned2.defined = false;
|
|
34
|
-
cloned2.inferable = false;
|
|
35
|
-
return cloned2;
|
|
36
|
-
}
|
|
37
|
-
if (error.data === validated.value && error.defined && error.inferable) {
|
|
38
|
-
return error;
|
|
39
|
-
}
|
|
40
|
-
const cloned = cloneORPCError(error);
|
|
41
|
-
cloned.data = validated.value;
|
|
42
|
-
cloned.defined = true;
|
|
43
|
-
cloned.inferable = true;
|
|
44
|
-
return cloned;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function resolveMetaPlugins(baseMeta, existingPlugins, incomingPlugins) {
|
|
48
|
-
existingPlugins = toArray(existingPlugins);
|
|
49
|
-
incomingPlugins = toArray(incomingPlugins);
|
|
50
|
-
let meta = baseMeta;
|
|
51
|
-
for (const plugin of incomingPlugins) {
|
|
52
|
-
if (plugin.init) {
|
|
53
|
-
meta = plugin.init(meta);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const plugins = [...existingPlugins, ...incomingPlugins];
|
|
57
|
-
for (const plugin of plugins) {
|
|
58
|
-
if (plugin.apply) {
|
|
59
|
-
meta = plugin.apply(meta);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return [meta, plugins];
|
|
63
|
-
}
|
|
64
|
-
function defineMeta(name, merge) {
|
|
65
|
-
const metaPlugin = (value) => ({
|
|
66
|
-
name,
|
|
67
|
-
init: (meta) => {
|
|
68
|
-
const current = meta[name];
|
|
69
|
-
return {
|
|
70
|
-
...meta,
|
|
71
|
-
[name]: merge(value, current)
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
const getMeta = (procedureOrLazy) => procedureOrLazy["~orpc"].meta[name];
|
|
76
|
-
return [metaPlugin, getMeta];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
class ProcedureContract {
|
|
80
|
-
"~orpc";
|
|
81
|
-
constructor(def) {
|
|
82
|
-
this["~orpc"] = def;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Checks if the given instance satisfies the {@see ProcedureContract} class/interface.
|
|
86
|
-
*/
|
|
87
|
-
static [Symbol.hasInstance](instance) {
|
|
88
|
-
if (this !== ProcedureContract) {
|
|
89
|
-
return Function.prototype[Symbol.hasInstance].call(this, instance);
|
|
90
|
-
}
|
|
91
|
-
const constructor = getConstructor(instance);
|
|
92
|
-
if (constructor === ProcedureContract) {
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
return isTypescriptObject(instance) && isTypescriptObject(instance["~orpc"]) && isTypescriptObject(instance["~orpc"].errorMap) && isTypescriptObject(instance["~orpc"].meta) && (instance["~orpc"].inputSchemas === void 0 || Array.isArray(instance["~orpc"].inputSchemas)) && (instance["~orpc"].outputSchemas === void 0 || Array.isArray(instance["~orpc"].outputSchemas));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function augmentContractRouter(router, options) {
|
|
100
|
-
if (router instanceof ProcedureContract) {
|
|
101
|
-
const [meta, metaPlugins] = resolveMetaPlugins(
|
|
102
|
-
options.meta,
|
|
103
|
-
options.metaPlugins,
|
|
104
|
-
router["~orpc"].metaPlugins
|
|
105
|
-
);
|
|
106
|
-
const enhanced2 = new ProcedureContract({
|
|
107
|
-
...router["~orpc"],
|
|
108
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
109
|
-
meta,
|
|
110
|
-
metaPlugins
|
|
111
|
-
});
|
|
112
|
-
return enhanced2;
|
|
113
|
-
}
|
|
114
|
-
if (!isTypescriptObject(router)) {
|
|
115
|
-
return router;
|
|
116
|
-
}
|
|
117
|
-
const enhanced = {};
|
|
118
|
-
for (const key in router) {
|
|
119
|
-
enhanced[key] = augmentContractRouter(router[key], options);
|
|
120
|
-
}
|
|
121
|
-
return enhanced;
|
|
122
|
-
}
|
|
123
|
-
function getRouterContract(router, path) {
|
|
124
|
-
let current = router;
|
|
125
|
-
for (let i = 0; i < path.length; i++) {
|
|
126
|
-
const segment = path[i];
|
|
127
|
-
if (!isTypescriptObject(current)) {
|
|
128
|
-
return void 0;
|
|
129
|
-
}
|
|
130
|
-
if (current instanceof ProcedureContract) {
|
|
131
|
-
return void 0;
|
|
132
|
-
}
|
|
133
|
-
current = current[segment];
|
|
134
|
-
}
|
|
135
|
-
if (!isTypescriptObject(current)) {
|
|
136
|
-
return void 0;
|
|
137
|
-
}
|
|
138
|
-
return current;
|
|
139
|
-
}
|
|
140
|
-
function getProcedureContractOrThrow(router, path) {
|
|
141
|
-
const procedure = getRouterContract(router, path);
|
|
142
|
-
if (!(procedure instanceof ProcedureContract)) {
|
|
143
|
-
throw new TypeError(`No valid procedure found at path "${path.join(".")}", this may happen when the router contract is not properly configured.`);
|
|
144
|
-
}
|
|
145
|
-
return procedure;
|
|
146
|
-
}
|
|
147
|
-
function minifyRouterContract(router) {
|
|
148
|
-
if (router instanceof ProcedureContract) {
|
|
149
|
-
const procedure = {
|
|
150
|
-
"~orpc": {
|
|
151
|
-
errorMap: {},
|
|
152
|
-
meta: router["~orpc"].meta
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
return procedure;
|
|
156
|
-
}
|
|
157
|
-
if (!isTypescriptObject(router)) {
|
|
158
|
-
return router;
|
|
159
|
-
}
|
|
160
|
-
const json = {};
|
|
161
|
-
for (const key in router) {
|
|
162
|
-
json[key] = minifyRouterContract(router[key]);
|
|
163
|
-
}
|
|
164
|
-
return json;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
class ValidationError extends Error {
|
|
168
|
-
/**
|
|
169
|
-
* This array is readonly because the upstream Standard Schema returns readonly issues.
|
|
170
|
-
*/
|
|
171
|
-
issues;
|
|
172
|
-
invalidData;
|
|
173
|
-
constructor(options) {
|
|
174
|
-
super(options.message, options);
|
|
175
|
-
this.issues = options.issues;
|
|
176
|
-
this.invalidData = options.invalidData;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export { ProcedureContract as P, ValidationError as V, augmentContractRouter as a, getRouterContract as b, minifyRouterContract as c, defineMeta as d, reconcileORPCError as e, getProcedureContractOrThrow as g, mergeErrorMap as m, resolveMetaPlugins as r };
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { ThrowableError } from '@orpc/shared';
|
|
2
|
-
import { ORPCErrorCode, ORPCError } from '@orpc/client';
|
|
3
|
-
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* TOutput default = TInput for better readability (shorter) in-case both TInput, TOutput is equal
|
|
7
|
-
*/
|
|
8
|
-
type Schema<TInput, TOutput = TInput> = StandardSchemaV1<TInput, TOutput>;
|
|
9
|
-
type AnySchema = Schema<any>;
|
|
10
|
-
type SchemaIssue = StandardSchemaV1.Issue;
|
|
11
|
-
type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
|
|
12
|
-
type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
|
|
13
|
-
type MergedSchema<T extends AnySchema, U extends AnySchema> = T extends Schema<infer TInput, infer TOutput> ? U extends Schema<infer UInput, infer UOutput> ? Schema<TInput & UInput, TOutput & UOutput> : never : never;
|
|
14
|
-
|
|
15
|
-
interface ErrorMapItem<TDataSchema extends AnySchema> {
|
|
16
|
-
message?: string;
|
|
17
|
-
data?: TDataSchema;
|
|
18
|
-
}
|
|
19
|
-
type ErrorMap = {
|
|
20
|
-
[key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
|
|
21
|
-
};
|
|
22
|
-
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
23
|
-
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
|
|
24
|
-
}[keyof TErrorMap];
|
|
25
|
-
interface ValidationErrorOptions extends ErrorOptions {
|
|
26
|
-
message: string;
|
|
27
|
-
issues: readonly SchemaIssue[];
|
|
28
|
-
invalidData: unknown;
|
|
29
|
-
}
|
|
30
|
-
declare class ValidationError extends Error {
|
|
31
|
-
/**
|
|
32
|
-
* This array is readonly because the upstream Standard Schema returns readonly issues.
|
|
33
|
-
*/
|
|
34
|
-
issues: readonly SchemaIssue[];
|
|
35
|
-
invalidData: unknown;
|
|
36
|
-
constructor(options: ValidationErrorOptions);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface Meta {
|
|
40
|
-
[key: PropertyKey]: unknown;
|
|
41
|
-
}
|
|
42
|
-
interface MetaPluginDefinition<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
43
|
-
__TInputSchema?: {
|
|
44
|
-
type: TInputSchema;
|
|
45
|
-
};
|
|
46
|
-
__TOutputSchema?: {
|
|
47
|
-
type: TOutputSchema;
|
|
48
|
-
};
|
|
49
|
-
__TErrorMap?: {
|
|
50
|
-
type: TErrorMap;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
interface MetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
54
|
-
/** This only for types, so it should be optional */
|
|
55
|
-
'~orpc'?: MetaPluginDefinition<TInputSchema, TOutputSchema, TErrorMap> | undefined;
|
|
56
|
-
/** Unique name of the plugin, used for identification. */
|
|
57
|
-
'name': string;
|
|
58
|
-
/**
|
|
59
|
-
* Runs once when this plugin is first added to the builder.
|
|
60
|
-
* Use this to set up initial metadata values.
|
|
61
|
-
*/
|
|
62
|
-
'init'?: (meta: Meta) => Meta;
|
|
63
|
-
/**
|
|
64
|
-
* Runs every time metadata is updated.
|
|
65
|
-
* This is called for all plugins in the chain whenever a new plugin is added.
|
|
66
|
-
*/
|
|
67
|
-
'apply'?: (meta: Meta) => Meta;
|
|
68
|
-
}
|
|
69
|
-
type AnyMetaPlugin = MetaPlugin<any, any, any>;
|
|
70
|
-
declare const HIDDEN_META_PLUGINS_SYMBOL: unique symbol;
|
|
71
|
-
declare function getHiddenMetaPlugins(container: unknown): AnyMetaPlugin[] | undefined;
|
|
72
|
-
declare function setHiddenMetaPlugins<T extends object>(container: T, metaPlugins: AnyMetaPlugin[]): void;
|
|
73
|
-
|
|
74
|
-
interface ProcedureContractDefinition<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
75
|
-
__TInputSchema?: {
|
|
76
|
-
type: TInputSchema;
|
|
77
|
-
};
|
|
78
|
-
__TOutputSchema?: {
|
|
79
|
-
type: TOutputSchema;
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Non-serializable should be optional
|
|
83
|
-
*/
|
|
84
|
-
inputSchemas?: AnySchema[] | undefined;
|
|
85
|
-
outputSchemas?: AnySchema[] | undefined;
|
|
86
|
-
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
87
|
-
errorMap: TErrorMap;
|
|
88
|
-
meta: Meta;
|
|
89
|
-
}
|
|
90
|
-
declare class ProcedureContract<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
91
|
-
'~orpc': ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>;
|
|
92
|
-
constructor(def: ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>);
|
|
93
|
-
/**
|
|
94
|
-
* Checks if the given instance satisfies the {@see ProcedureContract} class/interface.
|
|
95
|
-
*/
|
|
96
|
-
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
97
|
-
}
|
|
98
|
-
type AnyProcedureContract = ProcedureContract<any, any, any>;
|
|
99
|
-
|
|
100
|
-
type RouterContract = AnyProcedureContract | {
|
|
101
|
-
[k: string]: RouterContract;
|
|
102
|
-
};
|
|
103
|
-
type InferRouterContractInputs<T extends RouterContract> = T extends ProcedureContract<infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
104
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractInputs<T[K]> : never;
|
|
105
|
-
};
|
|
106
|
-
type InferRouterContractOutputs<T extends RouterContract> = T extends ProcedureContract<any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
107
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractOutputs<T[K]> : never;
|
|
108
|
-
};
|
|
109
|
-
type InferRouterContractErrorMap<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? UErrorMap : {
|
|
110
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrorMap<T[K]> : never;
|
|
111
|
-
}[keyof T];
|
|
112
|
-
/**
|
|
113
|
-
* Infer the union of throwable errors for entire router-contract.
|
|
114
|
-
*/
|
|
115
|
-
type InferRouterContractError<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
|
|
116
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractError<T[K]> : never;
|
|
117
|
-
}[keyof T];
|
|
118
|
-
/**
|
|
119
|
-
* Infer throwable errors for each procedure-contract, preserving the router-contract shape.
|
|
120
|
-
*/
|
|
121
|
-
type InferRouterContractErrors<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
|
|
122
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrors<T[K]> : never;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
export { HIDDEN_META_PLUGINS_SYMBOL as H, ProcedureContract as P, ValidationError as V, getHiddenMetaPlugins as p, setHiddenMetaPlugins as s };
|
|
126
|
-
export type { AnySchema as A, ErrorMap as E, InferSchemaInput as I, MetaPlugin as M, ORPCErrorFromErrorMap as O, RouterContract as R, Schema as S, MergedSchema as a, Meta as b, AnyMetaPlugin as c, AnyProcedureContract as d, InferSchemaOutput as e, SchemaIssue as f, ErrorMapItem as g, InferRouterContractError as h, InferRouterContractErrorMap as i, InferRouterContractErrors as j, InferRouterContractInputs as k, InferRouterContractOutputs as l, MetaPluginDefinition as m, ProcedureContractDefinition as n, ValidationErrorOptions as o };
|