@orpc/contract 2.0.0-beta.46 → 2.0.0-beta.48

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.
Files changed (49) hide show
  1. package/README.md +6 -4
  2. package/dist/builder-variants.d.mts +113 -0
  3. package/dist/builder-variants.mjs +1 -0
  4. package/dist/builder.d.mts +78 -0
  5. package/dist/builder.mjs +71 -0
  6. package/dist/client-factory.d.mts +24 -0
  7. package/dist/client-factory.mjs +34 -0
  8. package/dist/error-factory.d.mts +87 -0
  9. package/dist/error-factory.mjs +75 -0
  10. package/dist/error-utils.d.mts +11 -0
  11. package/dist/error-utils.mjs +43 -0
  12. package/dist/error.d.mts +41 -0
  13. package/dist/error.mjs +12 -0
  14. package/dist/index.d.mts +62 -433
  15. package/dist/index.mjs +25 -342
  16. package/dist/meta-built-in.d.mts +38 -0
  17. package/dist/meta-built-in.mjs +40 -0
  18. package/dist/meta-utils.d.mts +62 -0
  19. package/dist/meta-utils.mjs +33 -0
  20. package/dist/meta.d.mts +61 -0
  21. package/dist/meta.mjs +11 -0
  22. package/dist/plugins/index.d.mts +10 -45
  23. package/dist/plugins/index.mjs +6 -89
  24. package/dist/plugins/request-validation.d.mts +28 -0
  25. package/dist/plugins/request-validation.mjs +46 -0
  26. package/dist/plugins/response-validation.d.mts +15 -0
  27. package/dist/plugins/response-validation.mjs +53 -0
  28. package/dist/procedure-client.d.mts +10 -0
  29. package/dist/procedure-client.mjs +1 -0
  30. package/dist/procedure.d.mts +36 -0
  31. package/dist/procedure.mjs +19 -0
  32. package/dist/router-client.d.mts +14 -0
  33. package/dist/router-client.mjs +1 -0
  34. package/dist/router-utils.d.mts +34 -0
  35. package/dist/router-utils.mjs +67 -0
  36. package/dist/router.d.mts +42 -0
  37. package/dist/router.mjs +1 -0
  38. package/dist/schema-built-in.d.mts +28 -0
  39. package/dist/schema-built-in.mjs +73 -0
  40. package/dist/schema-utils.d.mts +26 -0
  41. package/dist/schema-utils.mjs +29 -0
  42. package/dist/schema.d.mts +31 -0
  43. package/dist/schema.mjs +1 -0
  44. package/package.json +4 -4
  45. package/dist/index.d.ts +0 -433
  46. package/dist/plugins/index.d.ts +0 -47
  47. package/dist/shared/contract.CM62dTQK.d.mts +0 -202
  48. package/dist/shared/contract.CM62dTQK.d.ts +0 -202
  49. package/dist/shared/contract.T7BNVKJ4.mjs +0 -177
package/dist/index.mjs CHANGED
@@ -1,342 +1,25 @@
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 };
1
+ export * from "./builder.mjs";
2
+ export * from "./builder-variants.mjs";
3
+ export * from "./client-factory.mjs";
4
+ export * from "./error.mjs";
5
+ export * from "./error-factory.mjs";
6
+ export * from "./error-utils.mjs";
7
+ export * from "./meta.mjs";
8
+ export * from "./meta-built-in.mjs";
9
+ export * from "./meta-utils.mjs";
10
+ export * from "./procedure.mjs";
11
+ export * from "./procedure-client.mjs";
12
+ export * from "./router.mjs";
13
+ export * from "./router-client.mjs";
14
+ export { getProcedureContractOrThrow, getRouterContract, minifyRouterContract } from "./router-utils.mjs";
15
+ export {
16
+
17
+ getRouterContract as getContractRouter,
18
+
19
+ minifyRouterContract as minifyContractRouter } from "./router-utils.mjs";
20
+ export * from "./schema.mjs";
21
+ export { asyncIteratorObject, getAsyncIteratorObjectSchemasDetails } from "./schema-built-in.mjs";
22
+ export {
23
+
24
+ asyncIteratorObject as eventIterator } from "./schema-built-in.mjs";
25
+ export * from "./schema-utils.mjs";
@@ -0,0 +1,38 @@
1
+ import type { ErrorMap } from "./error.mjs";
2
+ import type { Meta, MetaPlugin } from "./meta.mjs";
3
+ import type { RouterContract } from "./router.mjs";
4
+ import type { AnySchema } from "./schema.mjs";
5
+ export interface PathMetaPlugin<
6
+ TInputSchema extends AnySchema,
7
+ TOutputSchema extends AnySchema,
8
+ TErrorMap extends ErrorMap
9
+ > extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
10
+ name: "~path";
11
+ }
12
+ /**
13
+ * Built-in metadata plugins.
14
+ * `meta.path` records a procedure contract's path inside the root contract,
15
+ * which is required for the contract client factory pattern.
16
+ *
17
+ * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
18
+ */
19
+ export declare const meta: {
20
+ path<
21
+ TInputSchema extends AnySchema,
22
+ TOutputSchema extends AnySchema,
23
+ TErrorMap extends ErrorMap
24
+ >(path: string[]): PathMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
25
+ };
26
+ export declare function getPathMeta(procedureOrLazy: {
27
+ "~orpc": {
28
+ meta: Meta;
29
+ };
30
+ }): string[] | undefined;
31
+ /**
32
+ * Resolve the base path of a contract from the first procedure-contract
33
+ * that defines `meta.path`: its `meta.path` minus its path inside the given contract.
34
+ * For a procedure-contract, this is its `meta.path` itself.
35
+ *
36
+ * Returns `undefined` when no procedure-contract defines `meta.path`.
37
+ */
38
+ export declare function resolveBasePathMeta(contract: RouterContract, currentPath?: string[]): string[] | undefined;
@@ -0,0 +1,40 @@
1
+ import { isTypescriptObject } from "@orpc/shared";
2
+ import { ProcedureContract } from "./procedure.mjs";
3
+
4
+ export const meta = { path(path) {
5
+ return {
6
+ name: "~path",
7
+ init(meta) {
8
+ return {
9
+ ...meta,
10
+ "~path": path
11
+ };
12
+ }
13
+ };
14
+ } };
15
+ export function getPathMeta(procedureOrLazy) {
16
+ return procedureOrLazy["~orpc"].meta["~path"];
17
+ }
18
+
19
+ export function resolveBasePathMeta(contract, currentPath = []) {
20
+ if (contract instanceof ProcedureContract) {
21
+ const path = getPathMeta(contract);
22
+ if (!path) {
23
+ return undefined;
24
+ }
25
+ const base = path.slice(0, Math.max(0, path.length - currentPath.length));
26
+ if (currentPath.some((key, i) => path[base.length + i] !== key)) {
27
+ throw new TypeError(`Procedure contract at "${currentPath.join(".")}" defines meta.path "${path.join(".")}" that does not match its path inside the given router contract.`);
28
+ }
29
+ return base;
30
+ }
31
+ if (isTypescriptObject(contract)) {
32
+ for (const [key, child] of Object.entries(contract)) {
33
+ const base = resolveBasePathMeta(child, [...currentPath, key]);
34
+ if (base !== undefined) {
35
+ return base;
36
+ }
37
+ }
38
+ }
39
+ return undefined;
40
+ }
@@ -0,0 +1,62 @@
1
+ import type { ErrorMap } from "./error.mjs";
2
+ import type { AnyMetaPlugin, Meta, MetaPlugin } from "./meta.mjs";
3
+ import type { AnySchema } from "./schema.mjs";
4
+ export declare function resolveMetaPlugins<
5
+ TInputSchema extends AnySchema,
6
+ TOutputSchema extends AnySchema,
7
+ TErrorMap extends ErrorMap
8
+ >(baseMeta: Meta, existingPlugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[] | undefined, incomingPlugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[] | undefined): [meta: Meta, plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]];
9
+ /**
10
+ * Quickly defines a meta plugin factory and reader.
11
+ *
12
+ * @example Mark a procedure as requiring authentication and read it in middleware.
13
+ * ```ts
14
+ * interface AuthMeta {
15
+ * required?: boolean
16
+ * scope?: 'user' | 'admin'
17
+ * }
18
+ *
19
+ * const [authMeta, getAuthMeta] = defineMeta(
20
+ * 'auth',
21
+ * (incoming: AuthMeta, current) => ({ ...current, ...incoming }),
22
+ * )
23
+ *
24
+ * const deletePostContract = oc
25
+ * .meta(authMeta({ required: true, scope: 'admin' }))
26
+ * .input(z.object({ postId: z.string() }))
27
+ * .output(z.object({ success: z.boolean() }))
28
+ *
29
+ * const authMiddleware = os.middleware(async ({ context, procedure, next }) => {
30
+ * const auth = getAuthMeta(procedure)
31
+ *
32
+ * if (auth?.required && !context.user) {
33
+ * throw new ORPCError('UNAUTHORIZED')
34
+ * }
35
+ *
36
+ * if (auth?.scope === 'admin' && !context.user?.isAdmin) {
37
+ * throw new ORPCError('FORBIDDEN')
38
+ * }
39
+ *
40
+ * return next()
41
+ * })
42
+ * ```
43
+ *
44
+ * @param name - Unique key for storing this meta entry.
45
+ * @param merge - Merges the existing value (or `undefined`) with the incoming value when applied multiple times.
46
+ *
47
+ * @returns A `[metaPlugin, getMeta]` tuple:
48
+ * - `metaPlugin(metadata)` - Attaches metadata to a procedure under `name`.
49
+ * - `getMeta(procedureOrLazy)` - Retrieves the metadata, or `undefined` if not set.
50
+ *
51
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
52
+ */
53
+ export declare function defineMeta<
54
+ TName extends string,
55
+ TData
56
+ >(name: TName, merge: (incoming: TData, current: TData | undefined) => TData): [metaPlugin: (meta: TData) => AnyMetaPlugin & {
57
+ name: TName;
58
+ }, getMeta: (procedureOrLazy: {
59
+ "~orpc": {
60
+ meta: Meta;
61
+ };
62
+ }) => TData | undefined];
@@ -0,0 +1,33 @@
1
+ import { toArray } from "@orpc/shared";
2
+ export function resolveMetaPlugins(baseMeta, existingPlugins, incomingPlugins) {
3
+ existingPlugins = toArray(existingPlugins);
4
+ incomingPlugins = toArray(incomingPlugins);
5
+ let meta = baseMeta;
6
+ for (const plugin of incomingPlugins) {
7
+ if (plugin.init) {
8
+ meta = plugin.init(meta);
9
+ }
10
+ }
11
+ const plugins = [...existingPlugins, ...incomingPlugins];
12
+ for (const plugin of plugins) {
13
+ if (plugin.apply) {
14
+ meta = plugin.apply(meta);
15
+ }
16
+ }
17
+ return [meta, plugins];
18
+ }
19
+
20
+ export function defineMeta(name, merge) {
21
+ const metaPlugin = (value) => ({
22
+ name,
23
+ init: (meta) => {
24
+ const current = meta[name];
25
+ return {
26
+ ...meta,
27
+ [name]: merge(value, current)
28
+ };
29
+ }
30
+ });
31
+ const getMeta = (procedureOrLazy) => procedureOrLazy["~orpc"].meta[name];
32
+ return [metaPlugin, getMeta];
33
+ }
@@ -0,0 +1,61 @@
1
+ import type { ErrorMap } from "./error.mjs";
2
+ import type { AnySchema } from "./schema.mjs";
3
+ /**
4
+ * Arbitrary metadata attached to a procedure.
5
+ * Middleware, plugins, and tooling can read it later to control behavior.
6
+ *
7
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
8
+ */
9
+ export interface Meta {
10
+ [key: PropertyKey]: unknown;
11
+ }
12
+ export interface MetaPluginDefinition<
13
+ TInputSchema extends AnySchema,
14
+ TOutputSchema extends AnySchema,
15
+ TErrorMap extends ErrorMap
16
+ > {
17
+ __TInputSchema?: {
18
+ type: TInputSchema;
19
+ };
20
+ __TOutputSchema?: {
21
+ type: TOutputSchema;
22
+ };
23
+ __TErrorMap?: {
24
+ type: TErrorMap;
25
+ };
26
+ }
27
+ /**
28
+ * A metadata plugin passed to `.meta(...)`.
29
+ * Defines how metadata is initialized and merged, and can infer or restrict procedure types.
30
+ *
31
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
32
+ */
33
+ export interface MetaPlugin<
34
+ TInputSchema extends AnySchema,
35
+ TOutputSchema extends AnySchema,
36
+ TErrorMap extends ErrorMap
37
+ > {
38
+ /** This only for types, so it should be optional */
39
+ "~orpc"?: MetaPluginDefinition<TInputSchema, TOutputSchema, TErrorMap> | undefined;
40
+ /** Unique name of the plugin, used for identification. */
41
+ "name": string;
42
+ /**
43
+ * Runs once when this plugin is first added to the builder.
44
+ * Use this to set up initial metadata values.
45
+ */
46
+ "init"?: (meta: Meta) => Meta;
47
+ /**
48
+ * Runs every time metadata is updated.
49
+ * This is called for all plugins in the chain whenever a new plugin is added.
50
+ */
51
+ "apply"?: (meta: Meta) => Meta;
52
+ }
53
+ /**
54
+ * A `MetaPlugin` with all type parameters relaxed to `any`.
55
+ *
56
+ * @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
57
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
58
+ */
59
+ export type AnyMetaPlugin = MetaPlugin<any, any, any>;
60
+ export declare function getHiddenMetaPlugins(container: unknown): AnyMetaPlugin[] | undefined;
61
+ export declare function setHiddenMetaPlugins<T extends object>(container: T, metaPlugins: AnyMetaPlugin[]): void;
package/dist/meta.mjs ADDED
@@ -0,0 +1,11 @@
1
+ import { isTypescriptObject } from "@orpc/shared";
2
+ const HIDDEN_META_PLUGINS_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_HIDDEN_META_PLUGINS");
3
+ export function getHiddenMetaPlugins(container) {
4
+ if (!isTypescriptObject(container)) {
5
+ return undefined;
6
+ }
7
+ return container[HIDDEN_META_PLUGINS_SYMBOL];
8
+ }
9
+ export function setHiddenMetaPlugins(container, metaPlugins) {
10
+ container[HIDDEN_META_PLUGINS_SYMBOL] = metaPlugins;
11
+ }
@@ -1,47 +1,12 @@
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
- }
1
+ export * from "./request-validation.mjs";
2
+ export {
19
3
  /**
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
-
4
+ * @deprecated Use `RequestValidationLinkPlugin` instead.
5
+ */
6
+ RequestValidationLinkPlugin as RequestValidationPlugin } from "./request-validation.mjs";
7
+ export * from "./response-validation.mjs";
8
+ export {
33
9
  /**
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 };
10
+ * @deprecated Use `ResponseValidationLinkPlugin` instead.
11
+ */
12
+ ResponseValidationLinkPlugin as ResponseValidationPlugin } from "./response-validation.mjs";