@orpc/contract 2.0.0-beta.47 → 2.0.0-beta.49
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 +6 -7
- package/dist/builder-variants.d.ts +103 -0
- package/dist/builder-variants.js +1 -0
- package/dist/builder.d.ts +78 -0
- package/dist/builder.js +112 -0
- package/dist/client-factory.d.ts +24 -0
- package/dist/client-factory.js +38 -0
- package/dist/error-factory.d.ts +80 -0
- package/dist/error-factory.js +121 -0
- package/dist/error-utils.d.ts +5 -0
- package/dist/error-utils.js +40 -0
- package/dist/error.d.ts +45 -0
- package/dist/error.js +20 -0
- package/dist/index.d.ts +44 -415
- package/dist/index.js +31 -0
- package/dist/meta-built-in.d.ts +30 -0
- package/dist/meta-built-in.js +54 -0
- package/dist/meta-utils.d.ts +58 -0
- package/dist/meta-utils.js +76 -0
- package/dist/meta.d.ts +53 -0
- package/dist/meta.js +13 -0
- package/dist/plugins/index.d.ts +8 -43
- package/dist/plugins/index.js +12 -0
- package/dist/plugins/request-validation.d.ts +28 -0
- package/dist/plugins/request-validation.js +55 -0
- package/dist/plugins/response-validation.d.ts +15 -0
- package/dist/plugins/response-validation.js +72 -0
- package/dist/procedure-client.d.ts +5 -0
- package/dist/procedure-client.js +1 -0
- package/dist/procedure.d.ts +28 -0
- package/dist/procedure.js +28 -0
- package/dist/router-client.d.ts +13 -0
- package/dist/router-client.js +1 -0
- package/dist/router-utils.d.ts +30 -0
- package/dist/router-utils.js +79 -0
- package/dist/router.d.ts +52 -0
- package/dist/router.js +1 -0
- package/dist/schema-built-in.d.ts +23 -0
- package/dist/schema-built-in.js +76 -0
- package/dist/schema-utils.d.ts +20 -0
- package/dist/schema-utils.js +44 -0
- package/dist/schema.d.ts +25 -0
- package/dist/schema.js +1 -0
- package/package.json +29 -31
- package/dist/index.d.mts +0 -433
- package/dist/index.mjs +0 -342
- package/dist/plugins/index.d.mts +0 -47
- package/dist/plugins/index.mjs +0 -91
- package/dist/shared/contract.CM62dTQK.d.mts +0 -202
- package/dist/shared/contract.CM62dTQK.d.ts +0 -202
- package/dist/shared/contract.T7BNVKJ4.mjs +0 -177
package/dist/index.mjs
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
import { isTypescriptObject, toArray, get, set, isPropertyKey, ORPC_NAME, resolveMaybeOptionalOptions, getOwn, isAsyncIteratorObject } from '@orpc/shared';
|
|
2
|
-
import { P as ProcedureContract, r as resolveMetaPlugins, m as mergeErrorMap, a as augmentContractRouter, V as ValidationError } from './shared/contract.T7BNVKJ4.mjs';
|
|
3
|
-
export { d as defineMeta, g as getContractRouter, b as getProcedureContractOrThrow, g as getRouterContract, c as minifyContractRouter, c as minifyRouterContract, e as reconcileORPCError } from './shared/contract.T7BNVKJ4.mjs';
|
|
4
|
-
import { createORPCClient, RECURSIVE_CLIENT_UNWRAP_KEYS, ORPCError, wrapAsyncIteratorPreservingEventMeta } from '@orpc/client';
|
|
5
|
-
|
|
6
|
-
const HIDDEN_META_PLUGINS_SYMBOL = Symbol.for("ORPC_HIDDEN_META_PLUGINS");
|
|
7
|
-
function getHiddenMetaPlugins(container) {
|
|
8
|
-
if (!isTypescriptObject(container)) {
|
|
9
|
-
return void 0;
|
|
10
|
-
}
|
|
11
|
-
return container[HIDDEN_META_PLUGINS_SYMBOL];
|
|
12
|
-
}
|
|
13
|
-
function setHiddenMetaPlugins(container, metaPlugins) {
|
|
14
|
-
container[HIDDEN_META_PLUGINS_SYMBOL] = metaPlugins;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class ContractBuilder extends ProcedureContract {
|
|
18
|
-
/**
|
|
19
|
-
* Private constructor to prevent direct instantiation.
|
|
20
|
-
* Use the static `create` method to initialize a new instance with a safe initial definition.
|
|
21
|
-
*/
|
|
22
|
-
constructor(definition) {
|
|
23
|
-
super(definition);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Creates a fresh contract builder with an empty definition.
|
|
27
|
-
* Prefer the exported `oc` instance over calling this directly.
|
|
28
|
-
*
|
|
29
|
-
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
30
|
-
*/
|
|
31
|
-
static create() {
|
|
32
|
-
return new ContractBuilder({
|
|
33
|
-
errorMap: {},
|
|
34
|
-
meta: {}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Applies metadata plugins to contracts built from this builder.
|
|
39
|
-
*
|
|
40
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
41
|
-
*/
|
|
42
|
-
meta(...plugins) {
|
|
43
|
-
const [meta, metaPlugins] = resolveMetaPlugins(
|
|
44
|
-
this["~orpc"].meta,
|
|
45
|
-
this["~orpc"].metaPlugins,
|
|
46
|
-
plugins
|
|
47
|
-
);
|
|
48
|
-
return new ContractBuilder({
|
|
49
|
-
...this["~orpc"],
|
|
50
|
-
meta,
|
|
51
|
-
metaPlugins
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Defines typesafe errors that implementations of this contract can throw.
|
|
56
|
-
*
|
|
57
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
58
|
-
*/
|
|
59
|
-
errors(errors) {
|
|
60
|
-
let result = new ContractBuilder({
|
|
61
|
-
...this["~orpc"],
|
|
62
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
63
|
-
});
|
|
64
|
-
const plugins = getHiddenMetaPlugins(errors);
|
|
65
|
-
if (plugins) {
|
|
66
|
-
result = result.meta(...plugins);
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Defines the input schema used to validate and type the procedure input.
|
|
72
|
-
*
|
|
73
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
74
|
-
*/
|
|
75
|
-
input(schema) {
|
|
76
|
-
let result = new ContractBuilder({
|
|
77
|
-
...this["~orpc"],
|
|
78
|
-
inputSchemas: [...toArray(this["~orpc"].inputSchemas), schema]
|
|
79
|
-
});
|
|
80
|
-
const plugins = getHiddenMetaPlugins(schema);
|
|
81
|
-
if (plugins) {
|
|
82
|
-
result = result.meta(...plugins);
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Defines the output schema used to validate and type the procedure output.
|
|
88
|
-
*
|
|
89
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
90
|
-
*/
|
|
91
|
-
output(schema) {
|
|
92
|
-
let result = new ContractBuilder({
|
|
93
|
-
...this["~orpc"],
|
|
94
|
-
outputSchemas: [...toArray(this["~orpc"].outputSchemas), schema]
|
|
95
|
-
});
|
|
96
|
-
const plugins = getHiddenMetaPlugins(schema);
|
|
97
|
-
if (plugins) {
|
|
98
|
-
result = result.meta(...plugins);
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Applies the builder's errors and metadata to every procedure contract in
|
|
104
|
-
* the given router contract.
|
|
105
|
-
*
|
|
106
|
-
* @see {@link https://orpc.dev/docs/contract/router#extending-router | Router Contract - Extending Router}
|
|
107
|
-
*/
|
|
108
|
-
router(router) {
|
|
109
|
-
return augmentContractRouter(router, this["~orpc"]);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
const oc = ContractBuilder.create();
|
|
113
|
-
|
|
114
|
-
const meta = {
|
|
115
|
-
path(path) {
|
|
116
|
-
return {
|
|
117
|
-
name: "~path",
|
|
118
|
-
init(meta2) {
|
|
119
|
-
return {
|
|
120
|
-
...meta2,
|
|
121
|
-
"~path": path
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
function getPathMeta(procedureOrLazy) {
|
|
128
|
-
return procedureOrLazy["~orpc"].meta["~path"];
|
|
129
|
-
}
|
|
130
|
-
function resolveBasePathMeta(contract, currentPath = []) {
|
|
131
|
-
if (contract instanceof ProcedureContract) {
|
|
132
|
-
const path = getPathMeta(contract);
|
|
133
|
-
if (!path) {
|
|
134
|
-
return void 0;
|
|
135
|
-
}
|
|
136
|
-
const base = path.slice(0, Math.max(0, path.length - currentPath.length));
|
|
137
|
-
if (currentPath.some((key, i) => path[base.length + i] !== key)) {
|
|
138
|
-
throw new TypeError(
|
|
139
|
-
`Procedure contract at "${currentPath.join(".")}" defines meta.path "${path.join(".")}" that does not match its path inside the given router contract.`
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
return base;
|
|
143
|
-
}
|
|
144
|
-
if (isTypescriptObject(contract)) {
|
|
145
|
-
for (const [key, child] of Object.entries(contract)) {
|
|
146
|
-
const base = resolveBasePathMeta(child, [...currentPath, key]);
|
|
147
|
-
if (base !== void 0) {
|
|
148
|
-
return base;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return void 0;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function createContractClientFactory(link, options = {}) {
|
|
156
|
-
const factory = (contract) => {
|
|
157
|
-
const path = resolveBasePathMeta(contract);
|
|
158
|
-
if (path === void 0) {
|
|
159
|
-
throw new TypeError(
|
|
160
|
-
"ContractClientFactory: procedure contract must define `meta.path` that matches its path in the root router contract."
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
const contractRef = options.contractRef;
|
|
164
|
-
if (contractRef) {
|
|
165
|
-
const register = (contract2, path2) => {
|
|
166
|
-
if (contract2 instanceof ProcedureContract) {
|
|
167
|
-
set(contractRef, [...path2, "~orpc"], contract2["~orpc"]);
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
if (isTypescriptObject(contract2)) {
|
|
171
|
-
for (const [key, value] of Object.entries(contract2)) {
|
|
172
|
-
register(value, [...path2, key]);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
register(contract, path);
|
|
177
|
-
}
|
|
178
|
-
return createORPCClient(link, { ...options, scoped: get(options.scoped, path), path });
|
|
179
|
-
};
|
|
180
|
-
return factory;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function type(...[map]) {
|
|
184
|
-
return {
|
|
185
|
-
"~standard": {
|
|
186
|
-
vendor: ORPC_NAME,
|
|
187
|
-
version: 1,
|
|
188
|
-
async validate(value) {
|
|
189
|
-
if (map) {
|
|
190
|
-
return { value: await map(value) };
|
|
191
|
-
}
|
|
192
|
-
return { value };
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
function isSchemaIssue(issue) {
|
|
198
|
-
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
if (issue.path !== void 0) {
|
|
202
|
-
if (!Array.isArray(issue.path)) {
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
if (!issue.path.every((segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key))) {
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return true;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function error(code, { data: dataSchema, message } = {}) {
|
|
213
|
-
const validateData = (schema, value) => {
|
|
214
|
-
const result = schema["~standard"].validate(value);
|
|
215
|
-
if (result instanceof Promise) {
|
|
216
|
-
throw new TypeError(
|
|
217
|
-
`Error factory "${code}" does not support async data schemas.`
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
return result;
|
|
221
|
-
};
|
|
222
|
-
return class extends ORPCError {
|
|
223
|
-
static code = code;
|
|
224
|
-
static data = dataSchema ?? type();
|
|
225
|
-
static message = message;
|
|
226
|
-
constructor(...rest) {
|
|
227
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
228
|
-
let data = options.data;
|
|
229
|
-
if (dataSchema) {
|
|
230
|
-
const result = validateData(dataSchema, options.data);
|
|
231
|
-
if (result.issues) {
|
|
232
|
-
throw new ValidationError({
|
|
233
|
-
message: `Error factory "${code}" data validation failed`,
|
|
234
|
-
issues: result.issues,
|
|
235
|
-
invalidData: options.data
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
data = result.value;
|
|
239
|
-
}
|
|
240
|
-
super(code, { ...options, message: options.message ?? message, data });
|
|
241
|
-
}
|
|
242
|
-
static [Symbol.hasInstance](instance) {
|
|
243
|
-
if (!(instance instanceof ORPCError)) {
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
if (instance.code !== code) {
|
|
247
|
-
return false;
|
|
248
|
-
}
|
|
249
|
-
if (dataSchema && validateData(dataSchema, instance.data).issues) {
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function createORPCErrorConstructorMap(errorMap) {
|
|
257
|
-
const proxy = new Proxy(errorMap, {
|
|
258
|
-
get(target, code) {
|
|
259
|
-
if (typeof code !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(code)) {
|
|
260
|
-
return Reflect.get(target, code);
|
|
261
|
-
}
|
|
262
|
-
const item = (...rest) => {
|
|
263
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
264
|
-
const config = getOwn(errorMap, code);
|
|
265
|
-
const error2 = new ORPCError(code, {
|
|
266
|
-
message: options.message ?? config?.message,
|
|
267
|
-
data: options.data,
|
|
268
|
-
cause: options.cause
|
|
269
|
-
});
|
|
270
|
-
if (config) {
|
|
271
|
-
error2.defined = true;
|
|
272
|
-
}
|
|
273
|
-
return error2;
|
|
274
|
-
};
|
|
275
|
-
return item;
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
return proxy;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL = Symbol.for("ORPC_ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS");
|
|
282
|
-
function asyncIteratorObject(yieldSchema, returnSchema) {
|
|
283
|
-
return {
|
|
284
|
-
"~standard": {
|
|
285
|
-
[ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL]: { yieldSchema, returnSchema },
|
|
286
|
-
vendor: ORPC_NAME,
|
|
287
|
-
version: 1,
|
|
288
|
-
validate(iterator) {
|
|
289
|
-
if (!isAsyncIteratorObject(iterator)) {
|
|
290
|
-
return { issues: [{ message: "Expect AsyncIteratorObject", path: [] }] };
|
|
291
|
-
}
|
|
292
|
-
const mapped = wrapAsyncIteratorPreservingEventMeta(iterator, {
|
|
293
|
-
async mapResult(result) {
|
|
294
|
-
const schema = result.done ? returnSchema : yieldSchema;
|
|
295
|
-
if (!schema) {
|
|
296
|
-
return result;
|
|
297
|
-
}
|
|
298
|
-
const validated = await schema["~standard"].validate(result.value);
|
|
299
|
-
if (validated.issues) {
|
|
300
|
-
throw new ORPCError("ASYNC_ITERATOR_OBJECT_VALIDATION_FAILED", {
|
|
301
|
-
message: "AsyncIteratorObject validation failed",
|
|
302
|
-
cause: new ValidationError({
|
|
303
|
-
issues: validated.issues,
|
|
304
|
-
message: "AsyncIteratorObject validation failed",
|
|
305
|
-
invalidData: result.value
|
|
306
|
-
})
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
return { done: result.done, value: validated.value };
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
return { value: mapped };
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
function getAsyncIteratorObjectSchemaDetails(schema) {
|
|
318
|
-
if (schema === void 0) {
|
|
319
|
-
return void 0;
|
|
320
|
-
}
|
|
321
|
-
return schema["~standard"][ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL];
|
|
322
|
-
}
|
|
323
|
-
function getAsyncIteratorObjectSchemasDetails(schemas) {
|
|
324
|
-
if (!schemas || schemas.length === 0) {
|
|
325
|
-
return void 0;
|
|
326
|
-
}
|
|
327
|
-
const yieldSchemas = [];
|
|
328
|
-
const returnSchemas = [];
|
|
329
|
-
for (const schema of schemas) {
|
|
330
|
-
const details = getAsyncIteratorObjectSchemaDetails(schema);
|
|
331
|
-
if (!details) {
|
|
332
|
-
return void 0;
|
|
333
|
-
}
|
|
334
|
-
yieldSchemas.push(details.yieldSchema);
|
|
335
|
-
if (details.returnSchema) {
|
|
336
|
-
returnSchemas.push(details.returnSchema);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return { yieldSchemas, returnSchemas };
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export { ContractBuilder, ProcedureContract, ValidationError, asyncIteratorObject, createContractClientFactory, createORPCErrorConstructorMap, error, asyncIteratorObject as eventIterator, getAsyncIteratorObjectSchemasDetails, getHiddenMetaPlugins, getPathMeta, isSchemaIssue, mergeErrorMap, meta, oc, resolveBasePathMeta, resolveMetaPlugins, setHiddenMetaPlugins, type };
|
package/dist/plugins/index.d.mts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { ClientContext } from '@orpc/client';
|
|
2
|
-
import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
-
import { RouterContract } from '../index.mjs';
|
|
4
|
-
import '@orpc/shared';
|
|
5
|
-
import '@standard-schema/spec';
|
|
6
|
-
|
|
7
|
-
interface RequestValidationLinkPluginOptions<_T extends ClientContext> {
|
|
8
|
-
/**
|
|
9
|
-
* Forwards the locally validated/transformed input downstream.
|
|
10
|
-
*
|
|
11
|
-
* Disabled by default because some schema transforms produce a locally valid
|
|
12
|
-
* value that cannot be validated successfully again by the server.
|
|
13
|
-
* Keeping the original input as the flow input is the safer default.
|
|
14
|
-
*
|
|
15
|
-
* @default false
|
|
16
|
-
*/
|
|
17
|
-
forwardValidatedInput?: boolean | undefined;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Validates client request input against contract schemas before the request is encoded.
|
|
21
|
-
* This is useful when your application relies on server-side validation.
|
|
22
|
-
*
|
|
23
|
-
* @see {@link https://orpc.dev/docs/plugins/request-validation | Request Validation Plugin}
|
|
24
|
-
*/
|
|
25
|
-
declare class RequestValidationLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
26
|
-
private readonly contract;
|
|
27
|
-
name: string;
|
|
28
|
-
private readonly forwardValidatedInput;
|
|
29
|
-
constructor(contract: RouterContract, options?: RequestValidationLinkPluginOptions<T>);
|
|
30
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Validates server responses against contract schemas before your application uses them.
|
|
35
|
-
* This helps ensure the data returned by the server matches the types defined in your contract.
|
|
36
|
-
*
|
|
37
|
-
* @see {@link https://orpc.dev/docs/plugins/response-validation | Response Validation Plugin}
|
|
38
|
-
*/
|
|
39
|
-
declare class ResponseValidationLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
40
|
-
private readonly contract;
|
|
41
|
-
name: string;
|
|
42
|
-
constructor(contract: RouterContract);
|
|
43
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { RequestValidationLinkPlugin, RequestValidationLinkPlugin as RequestValidationPlugin, ResponseValidationLinkPlugin, ResponseValidationLinkPlugin as ResponseValidationPlugin };
|
|
47
|
-
export type { RequestValidationLinkPluginOptions };
|
package/dist/plugins/index.mjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { ORPCError } from '@orpc/client';
|
|
2
|
-
import { toArray, mergeTwoLevels } from '@orpc/shared';
|
|
3
|
-
import { b as getProcedureContractOrThrow, V as ValidationError, e as reconcileORPCError } from '../shared/contract.T7BNVKJ4.mjs';
|
|
4
|
-
|
|
5
|
-
class RequestValidationLinkPlugin {
|
|
6
|
-
constructor(contract, options = {}) {
|
|
7
|
-
this.contract = contract;
|
|
8
|
-
this.forwardValidatedInput = options.forwardValidatedInput ?? false;
|
|
9
|
-
}
|
|
10
|
-
name = "~request-validation";
|
|
11
|
-
forwardValidatedInput;
|
|
12
|
-
init(options) {
|
|
13
|
-
return {
|
|
14
|
-
...options,
|
|
15
|
-
interceptors: [...toArray(options.interceptors), async ({ next, ...interceptorOptions }) => {
|
|
16
|
-
const procedure = getProcedureContractOrThrow(this.contract, interceptorOptions.path);
|
|
17
|
-
const inputSchemas = toArray(procedure["~orpc"].inputSchemas);
|
|
18
|
-
const originalInput = interceptorOptions.input;
|
|
19
|
-
let currentInput = originalInput;
|
|
20
|
-
for (const [index, schema] of inputSchemas.entries()) {
|
|
21
|
-
const validating = index !== 0 ? mergeTwoLevels(originalInput, currentInput) : currentInput;
|
|
22
|
-
const result = await schema["~standard"].validate(validating);
|
|
23
|
-
if (result.issues) {
|
|
24
|
-
throw new ORPCError("BAD_REQUEST", {
|
|
25
|
-
message: "Input validation failed",
|
|
26
|
-
data: {
|
|
27
|
-
issues: result.issues
|
|
28
|
-
},
|
|
29
|
-
cause: new ValidationError({
|
|
30
|
-
message: "Input validation failed",
|
|
31
|
-
issues: result.issues,
|
|
32
|
-
invalidData: validating
|
|
33
|
-
})
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
currentInput = index !== 0 ? mergeTwoLevels(currentInput, result.value) : result.value;
|
|
37
|
-
}
|
|
38
|
-
return this.forwardValidatedInput ? next({ ...interceptorOptions, input: currentInput }) : next();
|
|
39
|
-
}]
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class ResponseValidationLinkPlugin {
|
|
45
|
-
constructor(contract) {
|
|
46
|
-
this.contract = contract;
|
|
47
|
-
}
|
|
48
|
-
name = "~response-validation";
|
|
49
|
-
init(options) {
|
|
50
|
-
return {
|
|
51
|
-
...options,
|
|
52
|
-
interceptors: [
|
|
53
|
-
async ({ next, path }) => {
|
|
54
|
-
const procedure = getProcedureContractOrThrow(this.contract, path);
|
|
55
|
-
try {
|
|
56
|
-
return await next();
|
|
57
|
-
} catch (error) {
|
|
58
|
-
if (error instanceof ORPCError) {
|
|
59
|
-
throw await reconcileORPCError(procedure["~orpc"].errorMap, error);
|
|
60
|
-
}
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
...toArray(options.interceptors),
|
|
65
|
-
async ({ next, path }) => {
|
|
66
|
-
const procedure = getProcedureContractOrThrow(this.contract, path);
|
|
67
|
-
const outputSchemas = toArray(procedure["~orpc"].outputSchemas);
|
|
68
|
-
let output = await next();
|
|
69
|
-
for (let i = outputSchemas.length - 1; i >= 0; i--) {
|
|
70
|
-
const schema = outputSchemas[i];
|
|
71
|
-
const result = await schema["~standard"].validate(output);
|
|
72
|
-
if (result.issues) {
|
|
73
|
-
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
74
|
-
message: "Output validation failed",
|
|
75
|
-
cause: new ValidationError({
|
|
76
|
-
message: "Output validation failed",
|
|
77
|
-
issues: result.issues,
|
|
78
|
-
invalidData: output
|
|
79
|
-
})
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
output = result.value;
|
|
83
|
-
}
|
|
84
|
-
return output;
|
|
85
|
-
}
|
|
86
|
-
]
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export { RequestValidationLinkPlugin, RequestValidationLinkPlugin as RequestValidationPlugin, ResponseValidationLinkPlugin, ResponseValidationLinkPlugin as ResponseValidationPlugin };
|
|
@@ -1,202 +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
|
-
/**
|
|
10
|
-
* Any Standard Schema compatible schema, regardless of its input and output types.
|
|
11
|
-
*
|
|
12
|
-
* @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
|
|
13
|
-
*/
|
|
14
|
-
type AnySchema = Schema<any>;
|
|
15
|
-
type SchemaIssue = StandardSchemaV1.Issue;
|
|
16
|
-
/**
|
|
17
|
-
* Infers the input type of a schema.
|
|
18
|
-
*
|
|
19
|
-
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
20
|
-
*/
|
|
21
|
-
type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
|
|
22
|
-
/**
|
|
23
|
-
* Infers the output type of a schema.
|
|
24
|
-
*
|
|
25
|
-
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
26
|
-
*/
|
|
27
|
-
type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
|
|
28
|
-
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;
|
|
29
|
-
|
|
30
|
-
interface ErrorMapItem {
|
|
31
|
-
/**
|
|
32
|
-
* Default message, can be overridden when constructing an error.
|
|
33
|
-
*/
|
|
34
|
-
message?: string | undefined;
|
|
35
|
-
/**
|
|
36
|
-
* Schema used to type and validate the error data.
|
|
37
|
-
*/
|
|
38
|
-
data?: AnySchema | undefined;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Map of error codes to their definitions, as passed to `.errors(...)`.
|
|
42
|
-
* Errors defined here remain properly typed on the client.
|
|
43
|
-
*
|
|
44
|
-
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
45
|
-
*/
|
|
46
|
-
type ErrorMap = {
|
|
47
|
-
[key in ORPCErrorCode]?: ErrorMapItem;
|
|
48
|
-
};
|
|
49
|
-
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
50
|
-
[K in keyof TErrorMap]: TErrorMap[K] extends ErrorMapItem ? ORPCError<K & ORPCErrorCode, TErrorMap[K]['data'] extends AnySchema ? InferSchemaOutput<TErrorMap[K]['data']> : unknown> : never;
|
|
51
|
-
}[keyof TErrorMap];
|
|
52
|
-
interface ValidationErrorOptions extends ErrorOptions {
|
|
53
|
-
message: string;
|
|
54
|
-
issues: readonly SchemaIssue[];
|
|
55
|
-
invalidData: unknown;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Error thrown when input, output, or error data fails schema validation,
|
|
59
|
-
* carrying the standard-schema `issues` and the invalid data.
|
|
60
|
-
* Usually found as the `cause` of an `ORPCError`.
|
|
61
|
-
*
|
|
62
|
-
* @see {@link https://orpc.dev/docs/recipes/validation-customization | Validation Customization}
|
|
63
|
-
*/
|
|
64
|
-
declare class ValidationError extends Error {
|
|
65
|
-
name: string;
|
|
66
|
-
/**
|
|
67
|
-
* This array is readonly because the upstream Standard Schema returns readonly issues.
|
|
68
|
-
*/
|
|
69
|
-
issues: readonly SchemaIssue[];
|
|
70
|
-
invalidData: unknown;
|
|
71
|
-
constructor(options: ValidationErrorOptions);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Arbitrary metadata attached to a procedure.
|
|
76
|
-
* Middleware, plugins, and tooling can read it later to control behavior.
|
|
77
|
-
*
|
|
78
|
-
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
79
|
-
*/
|
|
80
|
-
interface Meta {
|
|
81
|
-
[key: PropertyKey]: unknown;
|
|
82
|
-
}
|
|
83
|
-
interface MetaPluginDefinition<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
84
|
-
__TInputSchema?: {
|
|
85
|
-
type: TInputSchema;
|
|
86
|
-
};
|
|
87
|
-
__TOutputSchema?: {
|
|
88
|
-
type: TOutputSchema;
|
|
89
|
-
};
|
|
90
|
-
__TErrorMap?: {
|
|
91
|
-
type: TErrorMap;
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* A metadata plugin passed to `.meta(...)`.
|
|
96
|
-
* Defines how metadata is initialized and merged, and can infer or restrict procedure types.
|
|
97
|
-
*
|
|
98
|
-
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
99
|
-
*/
|
|
100
|
-
interface MetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
101
|
-
/** This only for types, so it should be optional */
|
|
102
|
-
'~orpc'?: MetaPluginDefinition<TInputSchema, TOutputSchema, TErrorMap> | undefined;
|
|
103
|
-
/** Unique name of the plugin, used for identification. */
|
|
104
|
-
'name': string;
|
|
105
|
-
/**
|
|
106
|
-
* Runs once when this plugin is first added to the builder.
|
|
107
|
-
* Use this to set up initial metadata values.
|
|
108
|
-
*/
|
|
109
|
-
'init'?: (meta: Meta) => Meta;
|
|
110
|
-
/**
|
|
111
|
-
* Runs every time metadata is updated.
|
|
112
|
-
* This is called for all plugins in the chain whenever a new plugin is added.
|
|
113
|
-
*/
|
|
114
|
-
'apply'?: (meta: Meta) => Meta;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* A `MetaPlugin` with all type parameters relaxed to `any`.
|
|
118
|
-
*
|
|
119
|
-
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
120
|
-
* @see {@link https://orpc.dev/docs/procedure | Procedure}
|
|
121
|
-
*/
|
|
122
|
-
type AnyMetaPlugin = MetaPlugin<any, any, any>;
|
|
123
|
-
declare function getHiddenMetaPlugins(container: unknown): AnyMetaPlugin[] | undefined;
|
|
124
|
-
declare function setHiddenMetaPlugins<T extends object>(container: T, metaPlugins: AnyMetaPlugin[]): void;
|
|
125
|
-
|
|
126
|
-
interface ProcedureContractDefinition<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
127
|
-
__TInputSchema?: {
|
|
128
|
-
type: TInputSchema;
|
|
129
|
-
};
|
|
130
|
-
__TOutputSchema?: {
|
|
131
|
-
type: TOutputSchema;
|
|
132
|
-
};
|
|
133
|
-
/**
|
|
134
|
-
* Non-serializable should be optional
|
|
135
|
-
*/
|
|
136
|
-
inputSchemas?: AnySchema[] | undefined;
|
|
137
|
-
outputSchemas?: AnySchema[] | undefined;
|
|
138
|
-
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
139
|
-
errorMap: TErrorMap;
|
|
140
|
-
meta: Meta;
|
|
141
|
-
}
|
|
142
|
-
declare class ProcedureContract<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
143
|
-
'~orpc': ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>;
|
|
144
|
-
constructor(def: ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>);
|
|
145
|
-
/**
|
|
146
|
-
* Checks if the given instance satisfies the {@link ProcedureContract} class/interface.
|
|
147
|
-
*/
|
|
148
|
-
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
149
|
-
}
|
|
150
|
-
type AnyProcedureContract = ProcedureContract<any, any, any>;
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* A router contract: a single procedure contract or a nested record of them.
|
|
154
|
-
*
|
|
155
|
-
* @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
|
|
156
|
-
*/
|
|
157
|
-
type RouterContract = AnyProcedureContract | {
|
|
158
|
-
[k: string]: RouterContract;
|
|
159
|
-
};
|
|
160
|
-
/**
|
|
161
|
-
* Infer the input types for each procedure-contract, preserving the router-contract shape.
|
|
162
|
-
*
|
|
163
|
-
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-inputs | Router Contract - Infer Router Contract Inputs}
|
|
164
|
-
*/
|
|
165
|
-
type InferRouterContractInputs<T extends RouterContract> = T extends ProcedureContract<infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
166
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractInputs<T[K]> : never;
|
|
167
|
-
};
|
|
168
|
-
/**
|
|
169
|
-
* Infer the output types for each procedure-contract, preserving the router-contract shape.
|
|
170
|
-
*
|
|
171
|
-
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-outputs | Router Contract - Infer Router Contract Outputs}
|
|
172
|
-
*/
|
|
173
|
-
type InferRouterContractOutputs<T extends RouterContract> = T extends ProcedureContract<any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
174
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractOutputs<T[K]> : never;
|
|
175
|
-
};
|
|
176
|
-
/**
|
|
177
|
-
* Infer the union of error maps defined across the entire router-contract.
|
|
178
|
-
*
|
|
179
|
-
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error-map | Router Contract - Infer Router Contract Error Map}
|
|
180
|
-
*/
|
|
181
|
-
type InferRouterContractErrorMap<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? UErrorMap : {
|
|
182
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrorMap<T[K]> : never;
|
|
183
|
-
}[keyof T];
|
|
184
|
-
/**
|
|
185
|
-
* Infer the union of throwable errors for entire router-contract.
|
|
186
|
-
*
|
|
187
|
-
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error | Router Contract - Infer Router Contract Error}
|
|
188
|
-
*/
|
|
189
|
-
type InferRouterContractError<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
|
|
190
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractError<T[K]> : never;
|
|
191
|
-
}[keyof T];
|
|
192
|
-
/**
|
|
193
|
-
* Infer throwable errors for each procedure-contract, preserving the router-contract shape.
|
|
194
|
-
*
|
|
195
|
-
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-errors | Router Contract - Infer Router Contract Errors}
|
|
196
|
-
*/
|
|
197
|
-
type InferRouterContractErrors<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
|
|
198
|
-
[K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrors<T[K]> : never;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
export { ProcedureContract as P, ValidationError as V, getHiddenMetaPlugins as p, setHiddenMetaPlugins as s };
|
|
202
|
-
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, ErrorMapItem as f, SchemaIssue as g, InferRouterContractErrorMap as h, InferRouterContractInputs as i, InferRouterContractOutputs as j, InferRouterContractError as k, InferRouterContractErrors as l, MetaPluginDefinition as m, ProcedureContractDefinition as n, ValidationErrorOptions as o };
|