@orpc/contract 0.0.0-next.371be67 → 0.0.0-next.3764ef9
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 +41 -28
- package/dist/index.d.mts +262 -167
- package/dist/index.d.ts +262 -167
- package/dist/index.mjs +172 -54
- package/dist/plugins/index.d.mts +43 -0
- package/dist/plugins/index.d.ts +43 -0
- package/dist/plugins/index.mjs +81 -0
- 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 +15 -11
package/dist/index.mjs
CHANGED
|
@@ -1,41 +1,15 @@
|
|
|
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 { toHttpPath } from '@orpc/client/standard';
|
|
4
|
+
import { toArray, isAsyncIteratorObject, get, isTypescriptObject, isPropertyKey } from '@orpc/shared';
|
|
5
|
+
export { AsyncIteratorClass } from '@orpc/shared';
|
|
1
6
|
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
7
|
export { ORPCError } from '@orpc/client';
|
|
3
|
-
import { isAsyncIteratorObject } from '@orpc/shared';
|
|
4
|
-
|
|
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
8
|
|
|
16
9
|
function mergeMeta(meta1, meta2) {
|
|
17
10
|
return { ...meta1, ...meta2 };
|
|
18
11
|
}
|
|
19
12
|
|
|
20
|
-
class ContractProcedure {
|
|
21
|
-
"~orpc";
|
|
22
|
-
constructor(def) {
|
|
23
|
-
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
24
|
-
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
25
|
-
}
|
|
26
|
-
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
|
|
27
|
-
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
|
|
28
|
-
}
|
|
29
|
-
this["~orpc"] = def;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function isContractProcedure(item) {
|
|
33
|
-
if (item instanceof ContractProcedure) {
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "inputSchema" in item["~orpc"] && "outputSchema" in item["~orpc"] && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
13
|
function mergeRoute(a, b) {
|
|
40
14
|
return { ...a, ...b };
|
|
41
15
|
}
|
|
@@ -60,31 +34,82 @@ function mergePrefix(a, b) {
|
|
|
60
34
|
function mergeTags(a, b) {
|
|
61
35
|
return a ? [...a, ...b] : b;
|
|
62
36
|
}
|
|
63
|
-
function
|
|
37
|
+
function enhanceRoute(route, options) {
|
|
64
38
|
let router = route;
|
|
65
39
|
if (options.prefix) {
|
|
66
40
|
router = prefixRoute(router, options.prefix);
|
|
67
41
|
}
|
|
68
|
-
if (options.tags) {
|
|
42
|
+
if (options.tags?.length) {
|
|
69
43
|
router = unshiftTagRoute(router, options.tags);
|
|
70
44
|
}
|
|
71
45
|
return router;
|
|
72
46
|
}
|
|
73
47
|
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
48
|
+
function getContractRouter(router, path) {
|
|
49
|
+
let current = router;
|
|
50
|
+
for (let i = 0; i < path.length; i++) {
|
|
51
|
+
const segment = path[i];
|
|
52
|
+
if (!current) {
|
|
53
|
+
return void 0;
|
|
54
|
+
}
|
|
55
|
+
if (isContractProcedure(current)) {
|
|
56
|
+
return void 0;
|
|
57
|
+
}
|
|
58
|
+
current = current[segment];
|
|
59
|
+
}
|
|
60
|
+
return current;
|
|
61
|
+
}
|
|
62
|
+
function enhanceContractRouter(router, options) {
|
|
63
|
+
if (isContractProcedure(router)) {
|
|
64
|
+
const enhanced2 = new ContractProcedure({
|
|
65
|
+
...router["~orpc"],
|
|
66
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
67
|
+
route: enhanceRoute(router["~orpc"].route, options)
|
|
80
68
|
});
|
|
81
|
-
return
|
|
69
|
+
return enhanced2;
|
|
70
|
+
}
|
|
71
|
+
const enhanced = {};
|
|
72
|
+
for (const key in router) {
|
|
73
|
+
enhanced[key] = enhanceContractRouter(router[key], options);
|
|
74
|
+
}
|
|
75
|
+
return enhanced;
|
|
76
|
+
}
|
|
77
|
+
function minifyContractRouter(router) {
|
|
78
|
+
if (isContractProcedure(router)) {
|
|
79
|
+
const procedure = {
|
|
80
|
+
"~orpc": {
|
|
81
|
+
errorMap: {},
|
|
82
|
+
meta: router["~orpc"].meta,
|
|
83
|
+
route: router["~orpc"].route
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
return procedure;
|
|
87
|
+
}
|
|
88
|
+
const json = {};
|
|
89
|
+
for (const key in router) {
|
|
90
|
+
json[key] = minifyContractRouter(router[key]);
|
|
91
|
+
}
|
|
92
|
+
return json;
|
|
93
|
+
}
|
|
94
|
+
function populateContractRouterPaths(router, options = {}) {
|
|
95
|
+
const path = toArray(options.path);
|
|
96
|
+
if (isContractProcedure(router)) {
|
|
97
|
+
if (router["~orpc"].route.path === void 0) {
|
|
98
|
+
return new ContractProcedure({
|
|
99
|
+
...router["~orpc"],
|
|
100
|
+
route: {
|
|
101
|
+
...router["~orpc"].route,
|
|
102
|
+
path: toHttpPath(path)
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return router;
|
|
82
107
|
}
|
|
83
|
-
const
|
|
84
|
-
for (const key in
|
|
85
|
-
|
|
108
|
+
const populated = {};
|
|
109
|
+
for (const key in router) {
|
|
110
|
+
populated[key] = populateContractRouterPaths(router[key], { ...options, path: [...path, key] });
|
|
86
111
|
}
|
|
87
|
-
return
|
|
112
|
+
return populated;
|
|
88
113
|
}
|
|
89
114
|
|
|
90
115
|
class ContractBuilder extends ContractProcedure {
|
|
@@ -94,7 +119,9 @@ class ContractBuilder extends ContractProcedure {
|
|
|
94
119
|
this["~orpc"].tags = def.tags;
|
|
95
120
|
}
|
|
96
121
|
/**
|
|
97
|
-
*
|
|
122
|
+
* Sets or overrides the initial meta.
|
|
123
|
+
*
|
|
124
|
+
* @see {@link https://orpc.dev/docs/metadata Metadata Docs}
|
|
98
125
|
*/
|
|
99
126
|
$meta(initialMeta) {
|
|
100
127
|
return new ContractBuilder({
|
|
@@ -103,7 +130,11 @@ class ContractBuilder extends ContractProcedure {
|
|
|
103
130
|
});
|
|
104
131
|
}
|
|
105
132
|
/**
|
|
106
|
-
*
|
|
133
|
+
* Sets or overrides the initial route.
|
|
134
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
135
|
+
*
|
|
136
|
+
* @see {@link https://orpc.dev/docs/openapi/routing OpenAPI Routing Docs}
|
|
137
|
+
* @see {@link https://orpc.dev/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
107
138
|
*/
|
|
108
139
|
$route(initialRoute) {
|
|
109
140
|
return new ContractBuilder({
|
|
@@ -111,56 +142,114 @@ class ContractBuilder extends ContractProcedure {
|
|
|
111
142
|
route: initialRoute
|
|
112
143
|
});
|
|
113
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Sets or overrides the initial input schema.
|
|
147
|
+
*
|
|
148
|
+
* @see {@link https://orpc.dev/docs/procedure#initial-configuration Initial Procedure Configuration Docs}
|
|
149
|
+
*/
|
|
150
|
+
$input(initialInputSchema) {
|
|
151
|
+
return new ContractBuilder({
|
|
152
|
+
...this["~orpc"],
|
|
153
|
+
inputSchema: initialInputSchema
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Adds type-safe custom errors to the contract.
|
|
158
|
+
* The provided errors are spared-merged with any existing errors in the contract.
|
|
159
|
+
*
|
|
160
|
+
* @see {@link https://orpc.dev/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
161
|
+
*/
|
|
114
162
|
errors(errors) {
|
|
115
163
|
return new ContractBuilder({
|
|
116
164
|
...this["~orpc"],
|
|
117
165
|
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
118
166
|
});
|
|
119
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Sets or updates the metadata for the contract.
|
|
170
|
+
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
171
|
+
*
|
|
172
|
+
* @see {@link https://orpc.dev/docs/metadata Metadata Docs}
|
|
173
|
+
*/
|
|
120
174
|
meta(meta) {
|
|
121
175
|
return new ContractBuilder({
|
|
122
176
|
...this["~orpc"],
|
|
123
177
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
124
178
|
});
|
|
125
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Sets or updates the route definition for the contract.
|
|
182
|
+
* The provided route is spared-merged with any existing route in the contract.
|
|
183
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
184
|
+
*
|
|
185
|
+
* @see {@link https://orpc.dev/docs/openapi/routing OpenAPI Routing Docs}
|
|
186
|
+
* @see {@link https://orpc.dev/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
187
|
+
*/
|
|
126
188
|
route(route) {
|
|
127
189
|
return new ContractBuilder({
|
|
128
190
|
...this["~orpc"],
|
|
129
191
|
route: mergeRoute(this["~orpc"].route, route)
|
|
130
192
|
});
|
|
131
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Defines the input validation schema for the contract.
|
|
196
|
+
*
|
|
197
|
+
* @see {@link https://orpc.dev/docs/procedure#input-output-validation Input Validation Docs}
|
|
198
|
+
*/
|
|
132
199
|
input(schema) {
|
|
133
200
|
return new ContractBuilder({
|
|
134
201
|
...this["~orpc"],
|
|
135
202
|
inputSchema: schema
|
|
136
203
|
});
|
|
137
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Defines the output validation schema for the contract.
|
|
207
|
+
*
|
|
208
|
+
* @see {@link https://orpc.dev/docs/procedure#input-output-validation Output Validation Docs}
|
|
209
|
+
*/
|
|
138
210
|
output(schema) {
|
|
139
211
|
return new ContractBuilder({
|
|
140
212
|
...this["~orpc"],
|
|
141
213
|
outputSchema: schema
|
|
142
214
|
});
|
|
143
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Prefixes all procedures in the contract router.
|
|
218
|
+
* The provided prefix is post-appended to any existing router prefix.
|
|
219
|
+
*
|
|
220
|
+
* @note This option does not affect procedures that do not define a path in their route definition.
|
|
221
|
+
*
|
|
222
|
+
* @see {@link https://orpc.dev/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
223
|
+
*/
|
|
144
224
|
prefix(prefix) {
|
|
145
225
|
return new ContractBuilder({
|
|
146
226
|
...this["~orpc"],
|
|
147
227
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
148
228
|
});
|
|
149
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Adds tags to all procedures in the contract router.
|
|
232
|
+
* This helpful when you want to group procedures together in the OpenAPI specification.
|
|
233
|
+
*
|
|
234
|
+
* @see {@link https://orpc.dev/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
235
|
+
*/
|
|
150
236
|
tag(...tags) {
|
|
151
237
|
return new ContractBuilder({
|
|
152
238
|
...this["~orpc"],
|
|
153
239
|
tags: mergeTags(this["~orpc"].tags, tags)
|
|
154
240
|
});
|
|
155
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Applies all of the previously defined options to the specified contract router.
|
|
244
|
+
*
|
|
245
|
+
* @see {@link https://orpc.dev/docs/router#extending-router Extending Router Docs}
|
|
246
|
+
*/
|
|
156
247
|
router(router) {
|
|
157
|
-
return
|
|
248
|
+
return enhanceContractRouter(router, this["~orpc"]);
|
|
158
249
|
}
|
|
159
250
|
}
|
|
160
251
|
const oc = new ContractBuilder({
|
|
161
252
|
errorMap: {},
|
|
162
|
-
inputSchema: void 0,
|
|
163
|
-
outputSchema: void 0,
|
|
164
253
|
route: {},
|
|
165
254
|
meta: {}
|
|
166
255
|
});
|
|
@@ -179,11 +268,11 @@ function fallbackContractConfig(key, value) {
|
|
|
179
268
|
return value;
|
|
180
269
|
}
|
|
181
270
|
|
|
182
|
-
const
|
|
271
|
+
const EVENT_ITERATOR_DETAILS_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_DETAILS");
|
|
183
272
|
function eventIterator(yields, returns) {
|
|
184
273
|
return {
|
|
185
274
|
"~standard": {
|
|
186
|
-
[
|
|
275
|
+
[EVENT_ITERATOR_DETAILS_SYMBOL]: { yields, returns },
|
|
187
276
|
vendor: "orpc",
|
|
188
277
|
version: 1,
|
|
189
278
|
validate(iterator) {
|
|
@@ -202,7 +291,8 @@ function eventIterator(yields, returns) {
|
|
|
202
291
|
message: "Event iterator validation failed",
|
|
203
292
|
cause: new ValidationError({
|
|
204
293
|
issues: result.issues,
|
|
205
|
-
message: "Event iterator validation failed"
|
|
294
|
+
message: "Event iterator validation failed",
|
|
295
|
+
data: value
|
|
206
296
|
})
|
|
207
297
|
});
|
|
208
298
|
}
|
|
@@ -219,7 +309,20 @@ function getEventIteratorSchemaDetails(schema) {
|
|
|
219
309
|
if (schema === void 0) {
|
|
220
310
|
return void 0;
|
|
221
311
|
}
|
|
222
|
-
return schema["~standard"][
|
|
312
|
+
return schema["~standard"][EVENT_ITERATOR_DETAILS_SYMBOL];
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function inferRPCMethodFromContractRouter(contract) {
|
|
316
|
+
return (_, path) => {
|
|
317
|
+
const procedure = get(contract, path);
|
|
318
|
+
if (!isContractProcedure(procedure)) {
|
|
319
|
+
throw new Error(
|
|
320
|
+
`[inferRPCMethodFromContractRouter] No valid procedure found at path "${path.join(".")}". This may happen when the contract router is not properly configured.`
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
324
|
+
return method === "HEAD" ? "GET" : method;
|
|
325
|
+
};
|
|
223
326
|
}
|
|
224
327
|
|
|
225
328
|
function type(...[map]) {
|
|
@@ -237,4 +340,19 @@ function type(...[map]) {
|
|
|
237
340
|
};
|
|
238
341
|
}
|
|
239
342
|
|
|
240
|
-
|
|
343
|
+
function isSchemaIssue(issue) {
|
|
344
|
+
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
if (issue.path !== void 0) {
|
|
348
|
+
if (!Array.isArray(issue.path)) {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
if (!issue.path.every((segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key))) {
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, oc, populateContractRouterPaths, prefixRoute, type, unshiftTagRoute };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ClientContext } from '@orpc/client';
|
|
2
|
+
import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
+
import { A as AnyContractRouter } from '../shared/contract.TuRtB1Ca.mjs';
|
|
4
|
+
import '@orpc/shared';
|
|
5
|
+
import '@standard-schema/spec';
|
|
6
|
+
import 'openapi-types';
|
|
7
|
+
|
|
8
|
+
declare class RequestValidationPluginError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A link plugin that validates client requests against your contract schema,
|
|
12
|
+
* ensuring that data sent to your server matches the expected types defined in your contract.
|
|
13
|
+
*
|
|
14
|
+
* @throws {ORPCError} with code `BAD_REQUEST` (same as server side) if input doesn't match the expected schema
|
|
15
|
+
* @see {@link https://orpc.dev/docs/plugins/request-validation Request Validation Plugin Docs}
|
|
16
|
+
*/
|
|
17
|
+
declare class RequestValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
18
|
+
private readonly contract;
|
|
19
|
+
constructor(contract: AnyContractRouter);
|
|
20
|
+
init(options: StandardLinkOptions<T>): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A link plugin that validates server responses against your contract schema,
|
|
25
|
+
* ensuring that data returned from your server matches the expected types defined in your contract.
|
|
26
|
+
*
|
|
27
|
+
* - Throws `ValidationError` if output doesn't match the expected schema
|
|
28
|
+
* - Converts mismatched defined errors to normal `ORPCError` instances
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.dev/docs/plugins/response-validation Response Validation Plugin Docs}
|
|
31
|
+
*/
|
|
32
|
+
declare class ResponseValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
33
|
+
private readonly contract;
|
|
34
|
+
constructor(contract: AnyContractRouter);
|
|
35
|
+
/**
|
|
36
|
+
* run before (validate after) retry plugin, because validation failed can't be retried
|
|
37
|
+
* run before (validate after) durable iterator plugin, because we expect durable iterator to validation (if user use it)
|
|
38
|
+
*/
|
|
39
|
+
order: number;
|
|
40
|
+
init(options: StandardLinkOptions<T>): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { RequestValidationPlugin, RequestValidationPluginError, ResponseValidationPlugin };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ClientContext } from '@orpc/client';
|
|
2
|
+
import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
+
import { A as AnyContractRouter } from '../shared/contract.TuRtB1Ca.js';
|
|
4
|
+
import '@orpc/shared';
|
|
5
|
+
import '@standard-schema/spec';
|
|
6
|
+
import 'openapi-types';
|
|
7
|
+
|
|
8
|
+
declare class RequestValidationPluginError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A link plugin that validates client requests against your contract schema,
|
|
12
|
+
* ensuring that data sent to your server matches the expected types defined in your contract.
|
|
13
|
+
*
|
|
14
|
+
* @throws {ORPCError} with code `BAD_REQUEST` (same as server side) if input doesn't match the expected schema
|
|
15
|
+
* @see {@link https://orpc.dev/docs/plugins/request-validation Request Validation Plugin Docs}
|
|
16
|
+
*/
|
|
17
|
+
declare class RequestValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
18
|
+
private readonly contract;
|
|
19
|
+
constructor(contract: AnyContractRouter);
|
|
20
|
+
init(options: StandardLinkOptions<T>): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A link plugin that validates server responses against your contract schema,
|
|
25
|
+
* ensuring that data returned from your server matches the expected types defined in your contract.
|
|
26
|
+
*
|
|
27
|
+
* - Throws `ValidationError` if output doesn't match the expected schema
|
|
28
|
+
* - Converts mismatched defined errors to normal `ORPCError` instances
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.dev/docs/plugins/response-validation Response Validation Plugin Docs}
|
|
31
|
+
*/
|
|
32
|
+
declare class ResponseValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
33
|
+
private readonly contract;
|
|
34
|
+
constructor(contract: AnyContractRouter);
|
|
35
|
+
/**
|
|
36
|
+
* run before (validate after) retry plugin, because validation failed can't be retried
|
|
37
|
+
* run before (validate after) durable iterator plugin, because we expect durable iterator to validation (if user use it)
|
|
38
|
+
*/
|
|
39
|
+
order: number;
|
|
40
|
+
init(options: StandardLinkOptions<T>): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { RequestValidationPlugin, RequestValidationPluginError, ResponseValidationPlugin };
|
|
@@ -0,0 +1,81 @@
|
|
|
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 RequestValidationPluginError extends Error {
|
|
6
|
+
}
|
|
7
|
+
class RequestValidationPlugin {
|
|
8
|
+
constructor(contract) {
|
|
9
|
+
this.contract = contract;
|
|
10
|
+
}
|
|
11
|
+
init(options) {
|
|
12
|
+
options.interceptors ??= [];
|
|
13
|
+
options.interceptors.push(async ({ next, path, input }) => {
|
|
14
|
+
const procedure = get(this.contract, path);
|
|
15
|
+
if (!isContractProcedure(procedure)) {
|
|
16
|
+
throw new RequestValidationPluginError(`No valid procedure found at path "${path.join(".")}", this may happen when the contract router is not properly configured.`);
|
|
17
|
+
}
|
|
18
|
+
const inputSchema = procedure["~orpc"].inputSchema;
|
|
19
|
+
if (inputSchema) {
|
|
20
|
+
const result = await inputSchema["~standard"].validate(input);
|
|
21
|
+
if (result.issues) {
|
|
22
|
+
throw new ORPCError("BAD_REQUEST", {
|
|
23
|
+
message: "Input validation failed",
|
|
24
|
+
data: {
|
|
25
|
+
issues: result.issues
|
|
26
|
+
},
|
|
27
|
+
cause: new ValidationError({
|
|
28
|
+
message: "Input validation failed",
|
|
29
|
+
issues: result.issues,
|
|
30
|
+
data: input
|
|
31
|
+
})
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return await next();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class ResponseValidationPlugin {
|
|
41
|
+
constructor(contract) {
|
|
42
|
+
this.contract = contract;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* run before (validate after) retry plugin, because validation failed can't be retried
|
|
46
|
+
* run before (validate after) durable iterator plugin, because we expect durable iterator to validation (if user use it)
|
|
47
|
+
*/
|
|
48
|
+
order = 12e5;
|
|
49
|
+
init(options) {
|
|
50
|
+
options.interceptors ??= [];
|
|
51
|
+
options.interceptors.push(async ({ next, path }) => {
|
|
52
|
+
const procedure = get(this.contract, path);
|
|
53
|
+
if (!isContractProcedure(procedure)) {
|
|
54
|
+
throw new Error(`[ResponseValidationPlugin] no valid procedure found at path "${path.join(".")}", this may happen when the contract router is not properly configured.`);
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const output = await next();
|
|
58
|
+
const outputSchema = procedure["~orpc"].outputSchema;
|
|
59
|
+
if (!outputSchema) {
|
|
60
|
+
return output;
|
|
61
|
+
}
|
|
62
|
+
const result = await outputSchema["~standard"].validate(output);
|
|
63
|
+
if (result.issues) {
|
|
64
|
+
throw new ValidationError({
|
|
65
|
+
message: "Server response output does not match expected schema",
|
|
66
|
+
issues: result.issues,
|
|
67
|
+
data: output
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return result.value;
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (e instanceof ORPCError) {
|
|
73
|
+
throw await validateORPCError(procedure["~orpc"].errorMap, e);
|
|
74
|
+
}
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { RequestValidationPlugin, RequestValidationPluginError, ResponseValidationPlugin };
|
|
@@ -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 };
|