@orpc/contract 2.0.0-beta.49 → 2.0.0-beta.50

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 (43) hide show
  1. package/dist/builder-variants.d.ts +79 -79
  2. package/dist/builder.d.ts +52 -52
  3. package/dist/builder.js +106 -110
  4. package/dist/client-factory.d.ts +12 -12
  5. package/dist/client-factory.js +36 -36
  6. package/dist/error-factory.d.ts +20 -23
  7. package/dist/error-factory.js +105 -117
  8. package/dist/error-utils.d.ts +3 -3
  9. package/dist/error-utils.js +35 -38
  10. package/dist/error.d.ts +23 -27
  11. package/dist/error.js +21 -19
  12. package/dist/index.d.ts +20 -62
  13. package/dist/index.js +12 -31
  14. package/dist/meta-built-in.d.ts +10 -10
  15. package/dist/meta-built-in.js +41 -50
  16. package/dist/meta-utils.d.ts +10 -13
  17. package/dist/meta-utils.js +67 -73
  18. package/dist/meta.d.ts +27 -27
  19. package/dist/meta.js +9 -11
  20. package/dist/plugins/index.d.ts +3 -12
  21. package/dist/plugins/index.js +2 -12
  22. package/dist/plugins/request-validation.d.ts +19 -19
  23. package/dist/plugins/request-validation.js +49 -54
  24. package/dist/plugins/response-validation.d.ts +8 -8
  25. package/dist/plugins/response-validation.js +66 -71
  26. package/dist/procedure-client.d.ts +5 -5
  27. package/dist/procedure.d.ts +24 -24
  28. package/dist/procedure.js +17 -28
  29. package/dist/router-client.d.ts +5 -7
  30. package/dist/router-utils.d.ts +10 -17
  31. package/dist/router-utils.js +53 -73
  32. package/dist/router.d.ts +10 -20
  33. package/dist/schema-built-in.d.ts +5 -10
  34. package/dist/schema-built-in.js +64 -71
  35. package/dist/schema-utils.d.ts +3 -3
  36. package/dist/schema-utils.js +32 -40
  37. package/dist/schema.d.ts +2 -2
  38. package/package.json +3 -3
  39. package/dist/builder-variants.js +0 -1
  40. package/dist/procedure-client.js +0 -1
  41. package/dist/router-client.js +0 -1
  42. package/dist/router.js +0 -1
  43. package/dist/schema.js +0 -1
package/dist/index.d.ts CHANGED
@@ -1,62 +1,20 @@
1
- export * from './builder.ts';
2
- export * from './builder-variants.ts';
3
- export * from './client-factory.ts';
4
- export * from './error.ts';
5
- export * from './error-factory.ts';
6
- export * from './error-utils.ts';
7
- export * from './meta.ts';
8
- export * from './meta-built-in.ts';
9
- export * from './meta-utils.ts';
10
- export * from './procedure.ts';
11
- export type {
12
- /**
13
- * @deprecated Use `AnyProcedureContract` instead.
14
- */
15
- AnyProcedureContract as AnyContractProcedure, } from './procedure.ts';
16
- export * from './procedure-client.ts';
17
- export * from './router.ts';
18
- export type {
19
- /**
20
- * @deprecated Use `RouterContract` instead.
21
- */
22
- RouterContract as AnyContractRouter,
23
- /**
24
- * @deprecated Use `InferRouterContractErrorMap` instead.
25
- */
26
- InferRouterContractErrorMap as InferContractRouterErrorMap,
27
- /**
28
- * @deprecated Use `InferRouterContractInputs` instead.
29
- */
30
- InferRouterContractInputs as InferContractRouterInputs,
31
- /**
32
- * @deprecated Use `InferRouterContractOutputs` instead.
33
- */
34
- InferRouterContractOutputs as InferContractRouterOutputs, } from './router.ts';
35
- export * from './router-client.ts';
36
- export type {
37
- /**
38
- * @deprecated Use `RouterContractClient` instead.
39
- */
40
- RouterContractClient as ContractRouterClient, } from './router-client.ts';
41
- export { getProcedureContractOrThrow, getRouterContract, minifyRouterContract, } from './router-utils.ts';
42
- export type { AugmentContractRouterOptions, AugmentedContractRouter } from './router-utils.ts';
43
- export {
44
- /**
45
- * @deprecated Use `getRouterContract` instead.
46
- */
47
- getRouterContract as getContractRouter,
48
- /**
49
- * @deprecated Use `minifyRouterContract` instead.
50
- */
51
- minifyRouterContract as minifyContractRouter, } from './router-utils.ts';
52
- export * from './schema.ts';
53
- export { asyncIteratorObject, getAsyncIteratorObjectSchemasDetails } from './schema-built-in.ts';
54
- export type { AsyncIteratorObjectSchemasDetails } from './schema-built-in.ts';
55
- export {
56
- /**
57
- * @deprecated Use `asyncIteratorObject` instead.
58
- */
59
- asyncIteratorObject as eventIterator, } from './schema-built-in.ts';
60
- export * from './schema-utils.ts';
61
- export type { Client, ClientContext, ClientOptions, ClientRest, FriendlyClientOptions, } from '@orpc/client';
62
- export type { AsyncIteratorClass, PromiseWithError, Registry, ThrowableError } from '@orpc/shared';
1
+ import { ContractBuilder, InitialInputSchema, InitialOutputSchema, oc } from "./builder.js";
2
+ import { ProcedureContractBuilderWithInput, ProcedureContractBuilderWithInputOutput, ProcedureContractBuilderWithOutput } from "./builder-variants.js";
3
+ import { ContractClientFactory, ContractClientFactoryOptions, createContractClientFactory } from "./client-factory.js";
4
+ import { ErrorMap, ErrorMapItem, ORPCErrorFromErrorMap, ValidationError, ValidationErrorOptions } from "./error.js";
5
+ import { ORPCErrorConstructorMap, ORPCErrorConstructorMapItem, ORPCErrorConstructorMapItemOptions, ORPCErrorFactory, ORPCErrorFactoryOptions, createORPCErrorConstructorMap, error } from "./error-factory.js";
6
+ import { MergedErrorMap, mergeErrorMap, reconcileORPCError } from "./error-utils.js";
7
+ import { AnyMetaPlugin, Meta, MetaPlugin, MetaPluginDefinition, getHiddenMetaPlugins, setHiddenMetaPlugins } from "./meta.js";
8
+ import { PathMetaPlugin, getPathMeta, meta, resolveBasePathMeta } from "./meta-built-in.js";
9
+ import { defineMeta, resolveMetaPlugins } from "./meta-utils.js";
10
+ import { AnyProcedureContract, ProcedureContract, ProcedureContractDefinition } from "./procedure.js";
11
+ import { ProcedureContractClient } from "./procedure-client.js";
12
+ import { InferRouterContractError, InferRouterContractErrorMap, InferRouterContractErrors, InferRouterContractInputs, InferRouterContractOutputs, RouterContract } from "./router.js";
13
+ import { RouterContractClient } from "./router-client.js";
14
+ import { AugmentContractRouterOptions, AugmentedContractRouter, getProcedureContractOrThrow, getRouterContract, minifyRouterContract } from "./router-utils.js";
15
+ import { AnySchema, InferSchemaInput, InferSchemaOutput, MergedSchema, Schema, SchemaIssue } from "./schema.js";
16
+ import { AsyncIteratorObjectSchemasDetails, asyncIteratorObject, getAsyncIteratorObjectSchemasDetails } from "./schema-built-in.js";
17
+ import { TypeRest, isSchemaIssue, type } from "./schema-utils.js";
18
+ import { AsyncIteratorClass, PromiseWithError, Registry, ThrowableError } from "@orpc/shared";
19
+ import { Client, ClientContext, ClientOptions, ClientRest, FriendlyClientOptions } from "@orpc/client";
20
+ export { type AnyProcedureContract as AnyContractProcedure, type RouterContract as AnyContractRouter, AnyMetaPlugin, AnyProcedureContract, AnySchema, type AsyncIteratorClass, type AsyncIteratorObjectSchemasDetails, type AugmentContractRouterOptions, type AugmentedContractRouter, type Client, type ClientContext, type ClientOptions, type ClientRest, ContractBuilder, ContractClientFactory, ContractClientFactoryOptions, type RouterContractClient as ContractRouterClient, ErrorMap, ErrorMapItem, type FriendlyClientOptions, type InferRouterContractErrorMap as InferContractRouterErrorMap, type InferRouterContractInputs as InferContractRouterInputs, type InferRouterContractOutputs as InferContractRouterOutputs, InferRouterContractError, InferRouterContractErrorMap, InferRouterContractErrors, InferRouterContractInputs, InferRouterContractOutputs, InferSchemaInput, InferSchemaOutput, InitialInputSchema, InitialOutputSchema, MergedErrorMap, MergedSchema, Meta, MetaPlugin, MetaPluginDefinition, ORPCErrorConstructorMap, ORPCErrorConstructorMapItem, ORPCErrorConstructorMapItemOptions, ORPCErrorFactory, ORPCErrorFactoryOptions, ORPCErrorFromErrorMap, PathMetaPlugin, ProcedureContract, ProcedureContractBuilderWithInput, ProcedureContractBuilderWithInputOutput, ProcedureContractBuilderWithOutput, ProcedureContractClient, ProcedureContractDefinition, type PromiseWithError, type Registry, RouterContract, RouterContractClient, Schema, SchemaIssue, type ThrowableError, TypeRest, ValidationError, ValidationErrorOptions, asyncIteratorObject, createContractClientFactory, createORPCErrorConstructorMap, defineMeta, error, asyncIteratorObject as eventIterator, getAsyncIteratorObjectSchemasDetails, getRouterContract as getContractRouter, getHiddenMetaPlugins, getPathMeta, getProcedureContractOrThrow, getRouterContract, isSchemaIssue, mergeErrorMap, meta, minifyRouterContract as minifyContractRouter, minifyRouterContract, oc, reconcileORPCError, resolveBasePathMeta, resolveMetaPlugins, setHiddenMetaPlugins, type };
package/dist/index.js CHANGED
@@ -1,31 +1,12 @@
1
- export * from './builder.js';
2
- export * from './builder-variants.js';
3
- export * from './client-factory.js';
4
- export * from './error.js';
5
- export * from './error-factory.js';
6
- export * from './error-utils.js';
7
- export * from './meta.js';
8
- export * from './meta-built-in.js';
9
- export * from './meta-utils.js';
10
- export * from './procedure.js';
11
- export * from './procedure-client.js';
12
- export * from './router.js';
13
- export * from './router-client.js';
14
- export { getProcedureContractOrThrow, getRouterContract, minifyRouterContract, } from './router-utils.js';
15
- export {
16
- /**
17
- * @deprecated Use `getRouterContract` instead.
18
- */
19
- getRouterContract as getContractRouter,
20
- /**
21
- * @deprecated Use `minifyRouterContract` instead.
22
- */
23
- minifyRouterContract as minifyContractRouter, } from './router-utils.js';
24
- export * from './schema.js';
25
- export { asyncIteratorObject, getAsyncIteratorObjectSchemasDetails } from './schema-built-in.js';
26
- export {
27
- /**
28
- * @deprecated Use `asyncIteratorObject` instead.
29
- */
30
- asyncIteratorObject as eventIterator, } from './schema-built-in.js';
31
- export * from './schema-utils.js';
1
+ export { ContractBuilder, oc } from "./builder.js";
2
+ export { createContractClientFactory } from "./client-factory.js";
3
+ export { ValidationError } from "./error.js";
4
+ export { createORPCErrorConstructorMap, error } from "./error-factory.js";
5
+ export { mergeErrorMap, reconcileORPCError } from "./error-utils.js";
6
+ export { getHiddenMetaPlugins, setHiddenMetaPlugins } from "./meta.js";
7
+ export { getPathMeta, meta, resolveBasePathMeta } from "./meta-built-in.js";
8
+ export { defineMeta, resolveMetaPlugins } from "./meta-utils.js";
9
+ export { ProcedureContract } from "./procedure.js";
10
+ export { getProcedureContractOrThrow, getRouterContract as getContractRouter, getRouterContract, minifyRouterContract as minifyContractRouter, minifyRouterContract } from "./router-utils.js";
11
+ export { asyncIteratorObject, asyncIteratorObject as eventIterator, getAsyncIteratorObjectSchemasDetails } from "./schema-built-in.js";
12
+ export { isSchemaIssue, type } from "./schema-utils.js";
@@ -1,9 +1,9 @@
1
- import type { ErrorMap } from './error.ts';
2
- import type { Meta, MetaPlugin } from './meta.ts';
3
- import type { RouterContract } from './router.ts';
4
- import type { AnySchema } from './schema.ts';
1
+ import { ErrorMap } from "./error.js";
2
+ import { Meta, MetaPlugin } from "./meta.js";
3
+ import { RouterContract } from "./router.js";
4
+ import { AnySchema } from "./schema.js";
5
5
  export interface PathMetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
6
- name: '~path';
6
+ name: "~path";
7
7
  }
8
8
  /**
9
9
  * Built-in metadata plugins.
@@ -13,12 +13,12 @@ export interface PathMetaPlugin<TInputSchema extends AnySchema, TOutputSchema ex
13
13
  * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
14
14
  */
15
15
  export declare const meta: {
16
- path<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(path: string[]): PathMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
16
+ path<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(path: string[]): PathMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
17
17
  };
18
18
  export declare function getPathMeta(procedureOrLazy: {
19
- '~orpc': {
20
- meta: Meta;
21
- };
19
+ "~orpc": {
20
+ meta: Meta;
21
+ };
22
22
  }): string[] | undefined;
23
23
  /**
24
24
  * Resolve the base path of a contract from the first procedure-contract
@@ -27,4 +27,4 @@ export declare function getPathMeta(procedureOrLazy: {
27
27
  *
28
28
  * Returns `undefined` when no procedure-contract defines `meta.path`.
29
29
  */
30
- export declare function resolveBasePathMeta(contract: RouterContract, currentPath?: string[]): string[] | undefined;
30
+ export declare function resolveBasePathMeta(contract: RouterContract, currentPath?: string[]): string[] | undefined;
@@ -1,54 +1,45 @@
1
- import { isNonPrimitive } from '@orpc/shared';
2
- import { ProcedureContract } from './procedure.js';
1
+ import { ProcedureContract } from "./procedure.js";
2
+ import { isNonPrimitive } from "@orpc/shared";
3
+ // src/meta-built-in.ts
3
4
  /**
4
- * Built-in metadata plugins.
5
- * `meta.path` records a procedure contract's path inside the root contract,
6
- * which is required for the contract client factory pattern.
7
- *
8
- * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
9
- */
10
- export const meta = {
11
- path(path) {
12
- return {
13
- name: '~path',
14
- init(meta) {
15
- return {
16
- ...meta,
17
- '~path': path,
18
- };
19
- },
20
- };
21
- },
22
- };
23
- export function getPathMeta(procedureOrLazy) {
24
- return procedureOrLazy['~orpc'].meta['~path'];
5
+ * Built-in metadata plugins.
6
+ * `meta.path` records a procedure contract's path inside the root contract,
7
+ * which is required for the contract client factory pattern.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
10
+ */
11
+ const meta = { path(path) {
12
+ return {
13
+ name: "~path",
14
+ init(meta) {
15
+ return {
16
+ ...meta,
17
+ "~path": path
18
+ };
19
+ }
20
+ };
21
+ } };
22
+ function getPathMeta(procedureOrLazy) {
23
+ return procedureOrLazy["~orpc"].meta["~path"];
25
24
  }
26
25
  /**
27
- * Resolve the base path of a contract from the first procedure-contract
28
- * that defines `meta.path`: its `meta.path` minus its path inside the given contract.
29
- * For a procedure-contract, this is its `meta.path` itself.
30
- *
31
- * Returns `undefined` when no procedure-contract defines `meta.path`.
32
- */
33
- export function resolveBasePathMeta(contract, currentPath = []) {
34
- if (contract instanceof ProcedureContract) {
35
- const path = getPathMeta(contract);
36
- if (!path) {
37
- return undefined;
38
- }
39
- const base = path.slice(0, Math.max(0, path.length - currentPath.length));
40
- if (currentPath.some((key, i) => path[base.length + i] !== key)) {
41
- throw new TypeError(`Procedure contract at "${currentPath.join('.')}" defines meta.path "${path.join('.')}" that does not match its path inside the given router contract.`);
42
- }
43
- return base;
44
- }
45
- if (isNonPrimitive(contract)) {
46
- for (const [key, child] of Object.entries(contract)) {
47
- const base = resolveBasePathMeta(child, [...currentPath, key]);
48
- if (base !== undefined) {
49
- return base;
50
- }
51
- }
52
- }
53
- return undefined;
26
+ * Resolve the base path of a contract from the first procedure-contract
27
+ * that defines `meta.path`: its `meta.path` minus its path inside the given contract.
28
+ * For a procedure-contract, this is its `meta.path` itself.
29
+ *
30
+ * Returns `undefined` when no procedure-contract defines `meta.path`.
31
+ */
32
+ function resolveBasePathMeta(contract, currentPath = []) {
33
+ if (contract instanceof ProcedureContract) {
34
+ const path = getPathMeta(contract);
35
+ if (!path) return;
36
+ const base = path.slice(0, Math.max(0, path.length - currentPath.length));
37
+ if (currentPath.some((key, i) => path[base.length + i] !== key)) throw new TypeError(`Procedure contract at "${currentPath.join(".")}" defines meta.path "${path.join(".")}" that does not match its path inside the given router contract.`);
38
+ return base;
39
+ }
40
+ if (isNonPrimitive(contract)) for (const [key, child] of Object.entries(contract)) {
41
+ const base = resolveBasePathMeta(child, [...currentPath, key]);
42
+ if (base !== void 0) return base;
43
+ }
54
44
  }
45
+ export { getPathMeta, meta, resolveBasePathMeta };
@@ -1,6 +1,6 @@
1
- import type { ErrorMap } from './error.ts';
2
- import type { AnyMetaPlugin, Meta, MetaPlugin } from './meta.ts';
3
- import type { AnySchema } from './schema.ts';
1
+ import { ErrorMap } from "./error.js";
2
+ import { AnyMetaPlugin, Meta, MetaPlugin } from "./meta.js";
3
+ import { AnySchema } from "./schema.js";
4
4
  export declare function resolveMetaPlugins<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(baseMeta: Meta, existingPlugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[] | undefined, incomingPlugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[] | undefined): [meta: Meta, plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]];
5
5
  /**
6
6
  * Quickly defines a meta plugin factory and reader.
@@ -46,13 +46,10 @@ export declare function resolveMetaPlugins<TInputSchema extends AnySchema, TOutp
46
46
  *
47
47
  * @see {@link https://orpc.dev/docs/metadata | Metadata}
48
48
  */
49
- export declare function defineMeta<TName extends string, TData>(name: TName, merge: (incoming: TData, current: TData | undefined) => TData): [
50
- metaPlugin: (meta: TData) => AnyMetaPlugin & {
51
- name: TName;
52
- },
53
- getMeta: (procedureOrLazy: {
54
- '~orpc': {
55
- meta: Meta;
56
- };
57
- }) => TData | undefined
58
- ];
49
+ export declare function defineMeta<TName extends string, TData>(name: TName, merge: (incoming: TData, current: TData | undefined) => TData): [metaPlugin: (meta: TData) => AnyMetaPlugin & {
50
+ name: TName;
51
+ }, getMeta: (procedureOrLazy: {
52
+ "~orpc": {
53
+ meta: Meta;
54
+ };
55
+ }) => TData | undefined];
@@ -1,76 +1,70 @@
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];
1
+ import { toArray } from "@orpc/shared";
2
+ // src/meta-utils.ts
3
+ function resolveMetaPlugins(baseMeta, existingPlugins, incomingPlugins) {
4
+ existingPlugins = toArray(existingPlugins);
5
+ incomingPlugins = toArray(incomingPlugins);
6
+ let meta = baseMeta;
7
+ for (const plugin of incomingPlugins) if (plugin.init) meta = plugin.init(meta);
8
+ const plugins = [...existingPlugins, ...incomingPlugins];
9
+ for (const plugin of plugins) if (plugin.apply) meta = plugin.apply(meta);
10
+ return [meta, plugins];
18
11
  }
19
12
  /**
20
- * Quickly defines a meta plugin factory and reader.
21
- *
22
- * @example Mark a procedure as requiring authentication and read it in middleware.
23
- * ```ts
24
- * interface AuthMeta {
25
- * required?: boolean
26
- * scope?: 'user' | 'admin'
27
- * }
28
- *
29
- * const [authMeta, getAuthMeta] = defineMeta(
30
- * 'auth',
31
- * (incoming: AuthMeta, current) => ({ ...current, ...incoming }),
32
- * )
33
- *
34
- * const deletePostContract = oc
35
- * .meta(authMeta({ required: true, scope: 'admin' }))
36
- * .input(z.object({ postId: z.string() }))
37
- * .output(z.object({ success: z.boolean() }))
38
- *
39
- * const authMiddleware = os.middleware(async ({ context, procedure, next }) => {
40
- * const auth = getAuthMeta(procedure)
41
- *
42
- * if (auth?.required && !context.user) {
43
- * throw new ORPCError('UNAUTHORIZED')
44
- * }
45
- *
46
- * if (auth?.scope === 'admin' && !context.user?.isAdmin) {
47
- * throw new ORPCError('FORBIDDEN')
48
- * }
49
- *
50
- * return next()
51
- * })
52
- * ```
53
- *
54
- * @param name - Unique key for storing this meta entry.
55
- * @param merge - Merges the existing value (or `undefined`) with the incoming value when applied multiple times.
56
- *
57
- * @returns A `[metaPlugin, getMeta]` tuple:
58
- * - `metaPlugin(metadata)` - Attaches metadata to a procedure under `name`.
59
- * - `getMeta(procedureOrLazy)` - Retrieves the metadata, or `undefined` if not set.
60
- *
61
- * @see {@link https://orpc.dev/docs/metadata | Metadata}
62
- */
63
- export function defineMeta(name, merge) {
64
- const metaPlugin = (value) => ({
65
- name,
66
- init: (meta) => {
67
- const current = meta[name];
68
- return {
69
- ...meta,
70
- [name]: merge(value, current),
71
- };
72
- },
73
- });
74
- const getMeta = (procedureOrLazy) => procedureOrLazy['~orpc'].meta[name];
75
- return [metaPlugin, getMeta];
13
+ * Quickly defines a meta plugin factory and reader.
14
+ *
15
+ * @example Mark a procedure as requiring authentication and read it in middleware.
16
+ * ```ts
17
+ * interface AuthMeta {
18
+ * required?: boolean
19
+ * scope?: 'user' | 'admin'
20
+ * }
21
+ *
22
+ * const [authMeta, getAuthMeta] = defineMeta(
23
+ * 'auth',
24
+ * (incoming: AuthMeta, current) => ({ ...current, ...incoming }),
25
+ * )
26
+ *
27
+ * const deletePostContract = oc
28
+ * .meta(authMeta({ required: true, scope: 'admin' }))
29
+ * .input(z.object({ postId: z.string() }))
30
+ * .output(z.object({ success: z.boolean() }))
31
+ *
32
+ * const authMiddleware = os.middleware(async ({ context, procedure, next }) => {
33
+ * const auth = getAuthMeta(procedure)
34
+ *
35
+ * if (auth?.required && !context.user) {
36
+ * throw new ORPCError('UNAUTHORIZED')
37
+ * }
38
+ *
39
+ * if (auth?.scope === 'admin' && !context.user?.isAdmin) {
40
+ * throw new ORPCError('FORBIDDEN')
41
+ * }
42
+ *
43
+ * return next()
44
+ * })
45
+ * ```
46
+ *
47
+ * @param name - Unique key for storing this meta entry.
48
+ * @param merge - Merges the existing value (or `undefined`) with the incoming value when applied multiple times.
49
+ *
50
+ * @returns A `[metaPlugin, getMeta]` tuple:
51
+ * - `metaPlugin(metadata)` - Attaches metadata to a procedure under `name`.
52
+ * - `getMeta(procedureOrLazy)` - Retrieves the metadata, or `undefined` if not set.
53
+ *
54
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
55
+ */
56
+ function defineMeta(name, merge) {
57
+ const metaPlugin = (value) => ({
58
+ name,
59
+ init: (meta) => {
60
+ const current = meta[name];
61
+ return {
62
+ ...meta,
63
+ [name]: merge(value, current)
64
+ };
65
+ }
66
+ });
67
+ const getMeta = (procedureOrLazy) => procedureOrLazy["~orpc"].meta[name];
68
+ return [metaPlugin, getMeta];
76
69
  }
70
+ export { defineMeta, resolveMetaPlugins };
package/dist/meta.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { ErrorMap } from './error.ts';
2
- import type { AnySchema } from './schema.ts';
1
+ import { ErrorMap } from "./error.js";
2
+ import { AnySchema } from "./schema.js";
3
3
  /**
4
4
  * Arbitrary metadata attached to a procedure.
5
5
  * Middleware, plugins, and tooling can read it later to control behavior.
@@ -7,18 +7,18 @@ import type { AnySchema } from './schema.ts';
7
7
  * @see {@link https://orpc.dev/docs/metadata | Metadata}
8
8
  */
9
9
  export interface Meta {
10
- [key: PropertyKey]: unknown;
10
+ [key: PropertyKey]: unknown;
11
11
  }
12
12
  export interface MetaPluginDefinition<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
13
- __TInputSchema?: {
14
- type: TInputSchema;
15
- };
16
- __TOutputSchema?: {
17
- type: TOutputSchema;
18
- };
19
- __TErrorMap?: {
20
- type: TErrorMap;
21
- };
13
+ __TInputSchema?: {
14
+ type: TInputSchema;
15
+ };
16
+ __TOutputSchema?: {
17
+ type: TOutputSchema;
18
+ };
19
+ __TErrorMap?: {
20
+ type: TErrorMap;
21
+ };
22
22
  }
23
23
  /**
24
24
  * A metadata plugin passed to `.meta(...)`.
@@ -27,20 +27,20 @@ export interface MetaPluginDefinition<TInputSchema extends AnySchema, TOutputSch
27
27
  * @see {@link https://orpc.dev/docs/metadata | Metadata}
28
28
  */
29
29
  export interface MetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
30
- /** This only for types, so it should be optional */
31
- '~orpc'?: MetaPluginDefinition<TInputSchema, TOutputSchema, TErrorMap> | undefined;
32
- /** Unique name of the plugin, used for identification. */
33
- 'name': string;
34
- /**
35
- * Runs once when this plugin is first added to the builder.
36
- * Use this to set up initial metadata values.
37
- */
38
- 'init'?: (meta: Meta) => Meta;
39
- /**
40
- * Runs every time metadata is updated.
41
- * This is called for all plugins in the chain whenever a new plugin is added.
42
- */
43
- 'apply'?: (meta: Meta) => Meta;
30
+ /** This only for types, so it should be optional */
31
+ "~orpc"?: MetaPluginDefinition<TInputSchema, TOutputSchema, TErrorMap> | undefined;
32
+ /** Unique name of the plugin, used for identification. */
33
+ "name": string;
34
+ /**
35
+ * Runs once when this plugin is first added to the builder.
36
+ * Use this to set up initial metadata values.
37
+ */
38
+ "init"?: (meta: Meta) => Meta;
39
+ /**
40
+ * Runs every time metadata is updated.
41
+ * This is called for all plugins in the chain whenever a new plugin is added.
42
+ */
43
+ "apply"?: (meta: Meta) => Meta;
44
44
  }
45
45
  /**
46
46
  * A `MetaPlugin` with all type parameters relaxed to `any`.
@@ -50,4 +50,4 @@ export interface MetaPlugin<TInputSchema extends AnySchema, TOutputSchema extend
50
50
  */
51
51
  export type AnyMetaPlugin = MetaPlugin<any, any, any>;
52
52
  export declare function getHiddenMetaPlugins(container: unknown): AnyMetaPlugin[] | undefined;
53
- export declare function setHiddenMetaPlugins<T extends object>(container: T, metaPlugins: AnyMetaPlugin[]): void;
53
+ export declare function setHiddenMetaPlugins<T extends object>(container: T, metaPlugins: AnyMetaPlugin[]): void;
package/dist/meta.js CHANGED
@@ -1,13 +1,11 @@
1
- import { isNonPrimitive } from '@orpc/shared';
2
- const HIDDEN_META_PLUGINS_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_HIDDEN_META_PLUGINS');
3
- export function getHiddenMetaPlugins(container) {
4
- if (!isNonPrimitive(container)) {
5
- return undefined;
6
- }
7
- return container[HIDDEN_META_PLUGINS_SYMBOL];
1
+ import { isNonPrimitive } from "@orpc/shared";
2
+ // src/meta.ts
3
+ const HIDDEN_META_PLUGINS_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_HIDDEN_META_PLUGINS");
4
+ function getHiddenMetaPlugins(container) {
5
+ if (!isNonPrimitive(container)) return;
6
+ return container[HIDDEN_META_PLUGINS_SYMBOL];
8
7
  }
9
- // oxlint-disable-next-line typescript/no-unnecessary-type-parameters
10
- export function setHiddenMetaPlugins(container, metaPlugins) {
11
- ;
12
- container[HIDDEN_META_PLUGINS_SYMBOL] = metaPlugins;
8
+ function setHiddenMetaPlugins(container, metaPlugins) {
9
+ container[HIDDEN_META_PLUGINS_SYMBOL] = metaPlugins;
13
10
  }
11
+ export { getHiddenMetaPlugins, setHiddenMetaPlugins };
@@ -1,12 +1,3 @@
1
- export * from './request-validation.ts';
2
- export {
3
- /**
4
- * @deprecated Use `RequestValidationLinkPlugin` instead.
5
- */
6
- RequestValidationLinkPlugin as RequestValidationPlugin, } from './request-validation.ts';
7
- export * from './response-validation.ts';
8
- export {
9
- /**
10
- * @deprecated Use `ResponseValidationLinkPlugin` instead.
11
- */
12
- ResponseValidationLinkPlugin as ResponseValidationPlugin, } from './response-validation.ts';
1
+ import { RequestValidationLinkPlugin, RequestValidationLinkPluginOptions } from "./request-validation.js";
2
+ import { ResponseValidationLinkPlugin } from "./response-validation.js";
3
+ export { RequestValidationLinkPlugin, RequestValidationLinkPluginOptions, RequestValidationLinkPlugin as RequestValidationPlugin, ResponseValidationLinkPlugin, ResponseValidationLinkPlugin as ResponseValidationPlugin };
@@ -1,12 +1,2 @@
1
- export * from './request-validation.js';
2
- export {
3
- /**
4
- * @deprecated Use `RequestValidationLinkPlugin` instead.
5
- */
6
- RequestValidationLinkPlugin as RequestValidationPlugin, } from './request-validation.js';
7
- export * from './response-validation.js';
8
- export {
9
- /**
10
- * @deprecated Use `ResponseValidationLinkPlugin` instead.
11
- */
12
- ResponseValidationLinkPlugin as ResponseValidationPlugin, } from './response-validation.js';
1
+ export { RequestValidationLinkPlugin, RequestValidationLinkPlugin as RequestValidationPlugin } from "./request-validation.js";
2
+ export { ResponseValidationLinkPlugin, ResponseValidationLinkPlugin as ResponseValidationPlugin } from "./response-validation.js";
@@ -1,17 +1,17 @@
1
- import type { ClientContext } from '@orpc/client';
2
- import type { StandardLinkOptions, StandardLinkPlugin } from '@orpc/client/standard';
3
- import type { RouterContract } from '../router.ts';
1
+ import { RouterContract } from "../router.js";
2
+ import { ClientContext } from "@orpc/client";
3
+ import { StandardLinkOptions, StandardLinkPlugin } from "@orpc/client/standard";
4
4
  export interface RequestValidationLinkPluginOptions<_T extends ClientContext> {
5
- /**
6
- * Forwards the locally validated/transformed input downstream.
7
- *
8
- * Disabled by default because some schema transforms produce a locally valid
9
- * value that cannot be validated successfully again by the server.
10
- * Keeping the original input as the flow input is the safer default.
11
- *
12
- * @default false
13
- */
14
- forwardValidatedInput?: boolean | undefined;
5
+ /**
6
+ * Forwards the locally validated/transformed input downstream.
7
+ *
8
+ * Disabled by default because some schema transforms produce a locally valid
9
+ * value that cannot be validated successfully again by the server.
10
+ * Keeping the original input as the flow input is the safer default.
11
+ *
12
+ * @default false
13
+ */
14
+ forwardValidatedInput?: boolean | undefined;
15
15
  }
16
16
  /**
17
17
  * Validates client request input against contract schemas before the request is encoded.
@@ -20,9 +20,9 @@ export interface RequestValidationLinkPluginOptions<_T extends ClientContext> {
20
20
  * @see {@link https://orpc.dev/docs/plugins/request-validation | Request Validation Plugin}
21
21
  */
22
22
  export declare class RequestValidationLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
23
- private readonly contract;
24
- name: string;
25
- private readonly forwardValidatedInput;
26
- constructor(contract: RouterContract, options?: RequestValidationLinkPluginOptions<T>);
27
- init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
28
- }
23
+ private readonly contract;
24
+ name: string;
25
+ private readonly forwardValidatedInput;
26
+ constructor(contract: RouterContract, options?: RequestValidationLinkPluginOptions<T>);
27
+ init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
28
+ }