@orpc/contract 0.0.0-next.dda04c5 → 0.0.0-next.de2bec7
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 +126 -14
- package/dist/index.d.mts +69 -281
- package/dist/index.d.ts +69 -281
- package/dist/index.mjs +64 -49
- 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 -10
package/dist/index.mjs
CHANGED
|
@@ -1,44 +1,15 @@
|
|
|
1
|
-
import {
|
|
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';
|
|
6
|
+
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
7
|
export { ORPCError } from '@orpc/client';
|
|
3
|
-
import { isAsyncIteratorObject, get, isTypescriptObject, isPropertyKey } 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
|
-
/**
|
|
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
13
|
function mergeRoute(a, b) {
|
|
43
14
|
return { ...a, ...b };
|
|
44
15
|
}
|
|
@@ -84,6 +55,9 @@ function getContractRouter(router, path) {
|
|
|
84
55
|
if (isContractProcedure(current)) {
|
|
85
56
|
return void 0;
|
|
86
57
|
}
|
|
58
|
+
if (typeof current !== "object") {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
87
61
|
current = current[segment];
|
|
88
62
|
}
|
|
89
63
|
return current;
|
|
@@ -97,6 +71,9 @@ function enhanceContractRouter(router, options) {
|
|
|
97
71
|
});
|
|
98
72
|
return enhanced2;
|
|
99
73
|
}
|
|
74
|
+
if (typeof router !== "object" || router === null) {
|
|
75
|
+
return router;
|
|
76
|
+
}
|
|
100
77
|
const enhanced = {};
|
|
101
78
|
for (const key in router) {
|
|
102
79
|
enhanced[key] = enhanceContractRouter(router[key], options);
|
|
@@ -114,12 +91,38 @@ function minifyContractRouter(router) {
|
|
|
114
91
|
};
|
|
115
92
|
return procedure;
|
|
116
93
|
}
|
|
94
|
+
if (typeof router !== "object" || router === null) {
|
|
95
|
+
return router;
|
|
96
|
+
}
|
|
117
97
|
const json = {};
|
|
118
98
|
for (const key in router) {
|
|
119
99
|
json[key] = minifyContractRouter(router[key]);
|
|
120
100
|
}
|
|
121
101
|
return json;
|
|
122
102
|
}
|
|
103
|
+
function populateContractRouterPaths(router, options = {}) {
|
|
104
|
+
const path = toArray(options.path);
|
|
105
|
+
if (isContractProcedure(router)) {
|
|
106
|
+
if (router["~orpc"].route.path === void 0) {
|
|
107
|
+
return new ContractProcedure({
|
|
108
|
+
...router["~orpc"],
|
|
109
|
+
route: {
|
|
110
|
+
...router["~orpc"].route,
|
|
111
|
+
path: toHttpPath(path)
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return router;
|
|
116
|
+
}
|
|
117
|
+
if (typeof router !== "object" || router === null) {
|
|
118
|
+
return router;
|
|
119
|
+
}
|
|
120
|
+
const populated = {};
|
|
121
|
+
for (const key in router) {
|
|
122
|
+
populated[key] = populateContractRouterPaths(router[key], { ...options, path: [...path, key] });
|
|
123
|
+
}
|
|
124
|
+
return populated;
|
|
125
|
+
}
|
|
123
126
|
|
|
124
127
|
class ContractBuilder extends ContractProcedure {
|
|
125
128
|
constructor(def) {
|
|
@@ -130,7 +133,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
130
133
|
/**
|
|
131
134
|
* Sets or overrides the initial meta.
|
|
132
135
|
*
|
|
133
|
-
* @see {@link https://orpc.
|
|
136
|
+
* @see {@link https://orpc.dev/docs/metadata Metadata Docs}
|
|
134
137
|
*/
|
|
135
138
|
$meta(initialMeta) {
|
|
136
139
|
return new ContractBuilder({
|
|
@@ -142,8 +145,8 @@ class ContractBuilder extends ContractProcedure {
|
|
|
142
145
|
* Sets or overrides the initial route.
|
|
143
146
|
* This option is typically relevant when integrating with OpenAPI.
|
|
144
147
|
*
|
|
145
|
-
* @see {@link https://orpc.
|
|
146
|
-
* @see {@link https://orpc.
|
|
148
|
+
* @see {@link https://orpc.dev/docs/openapi/routing OpenAPI Routing Docs}
|
|
149
|
+
* @see {@link https://orpc.dev/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
147
150
|
*/
|
|
148
151
|
$route(initialRoute) {
|
|
149
152
|
return new ContractBuilder({
|
|
@@ -151,11 +154,22 @@ class ContractBuilder extends ContractProcedure {
|
|
|
151
154
|
route: initialRoute
|
|
152
155
|
});
|
|
153
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Sets or overrides the initial input schema.
|
|
159
|
+
*
|
|
160
|
+
* @see {@link https://orpc.dev/docs/procedure#initial-configuration Initial Procedure Configuration Docs}
|
|
161
|
+
*/
|
|
162
|
+
$input(initialInputSchema) {
|
|
163
|
+
return new ContractBuilder({
|
|
164
|
+
...this["~orpc"],
|
|
165
|
+
inputSchema: initialInputSchema
|
|
166
|
+
});
|
|
167
|
+
}
|
|
154
168
|
/**
|
|
155
169
|
* Adds type-safe custom errors to the contract.
|
|
156
170
|
* The provided errors are spared-merged with any existing errors in the contract.
|
|
157
171
|
*
|
|
158
|
-
* @see {@link https://orpc.
|
|
172
|
+
* @see {@link https://orpc.dev/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
159
173
|
*/
|
|
160
174
|
errors(errors) {
|
|
161
175
|
return new ContractBuilder({
|
|
@@ -167,7 +181,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
167
181
|
* Sets or updates the metadata for the contract.
|
|
168
182
|
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
169
183
|
*
|
|
170
|
-
* @see {@link https://orpc.
|
|
184
|
+
* @see {@link https://orpc.dev/docs/metadata Metadata Docs}
|
|
171
185
|
*/
|
|
172
186
|
meta(meta) {
|
|
173
187
|
return new ContractBuilder({
|
|
@@ -180,8 +194,8 @@ class ContractBuilder extends ContractProcedure {
|
|
|
180
194
|
* The provided route is spared-merged with any existing route in the contract.
|
|
181
195
|
* This option is typically relevant when integrating with OpenAPI.
|
|
182
196
|
*
|
|
183
|
-
* @see {@link https://orpc.
|
|
184
|
-
* @see {@link https://orpc.
|
|
197
|
+
* @see {@link https://orpc.dev/docs/openapi/routing OpenAPI Routing Docs}
|
|
198
|
+
* @see {@link https://orpc.dev/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
185
199
|
*/
|
|
186
200
|
route(route) {
|
|
187
201
|
return new ContractBuilder({
|
|
@@ -192,7 +206,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
192
206
|
/**
|
|
193
207
|
* Defines the input validation schema for the contract.
|
|
194
208
|
*
|
|
195
|
-
* @see {@link https://orpc.
|
|
209
|
+
* @see {@link https://orpc.dev/docs/procedure#input-output-validation Input Validation Docs}
|
|
196
210
|
*/
|
|
197
211
|
input(schema) {
|
|
198
212
|
return new ContractBuilder({
|
|
@@ -203,7 +217,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
203
217
|
/**
|
|
204
218
|
* Defines the output validation schema for the contract.
|
|
205
219
|
*
|
|
206
|
-
* @see {@link https://orpc.
|
|
220
|
+
* @see {@link https://orpc.dev/docs/procedure#input-output-validation Output Validation Docs}
|
|
207
221
|
*/
|
|
208
222
|
output(schema) {
|
|
209
223
|
return new ContractBuilder({
|
|
@@ -217,7 +231,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
217
231
|
*
|
|
218
232
|
* @note This option does not affect procedures that do not define a path in their route definition.
|
|
219
233
|
*
|
|
220
|
-
* @see {@link https://orpc.
|
|
234
|
+
* @see {@link https://orpc.dev/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
221
235
|
*/
|
|
222
236
|
prefix(prefix) {
|
|
223
237
|
return new ContractBuilder({
|
|
@@ -229,7 +243,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
229
243
|
* Adds tags to all procedures in the contract router.
|
|
230
244
|
* This helpful when you want to group procedures together in the OpenAPI specification.
|
|
231
245
|
*
|
|
232
|
-
* @see {@link https://orpc.
|
|
246
|
+
* @see {@link https://orpc.dev/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
233
247
|
*/
|
|
234
248
|
tag(...tags) {
|
|
235
249
|
return new ContractBuilder({
|
|
@@ -240,7 +254,7 @@ class ContractBuilder extends ContractProcedure {
|
|
|
240
254
|
/**
|
|
241
255
|
* Applies all of the previously defined options to the specified contract router.
|
|
242
256
|
*
|
|
243
|
-
* @see {@link https://orpc.
|
|
257
|
+
* @see {@link https://orpc.dev/docs/router#extending-router Extending Router Docs}
|
|
244
258
|
*/
|
|
245
259
|
router(router) {
|
|
246
260
|
return enhanceContractRouter(router, this["~orpc"]);
|
|
@@ -289,7 +303,8 @@ function eventIterator(yields, returns) {
|
|
|
289
303
|
message: "Event iterator validation failed",
|
|
290
304
|
cause: new ValidationError({
|
|
291
305
|
issues: result.issues,
|
|
292
|
-
message: "Event iterator validation failed"
|
|
306
|
+
message: "Event iterator validation failed",
|
|
307
|
+
data: value
|
|
293
308
|
})
|
|
294
309
|
});
|
|
295
310
|
}
|
|
@@ -352,4 +367,4 @@ function isSchemaIssue(issue) {
|
|
|
352
367
|
return true;
|
|
353
368
|
}
|
|
354
369
|
|
|
355
|
-
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, oc, prefixRoute, type, unshiftTagRoute };
|
|
370
|
+
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 };
|