@orpc/contract 2.0.0-beta.47 → 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 +2 -3
  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
@@ -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 };
@@ -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 };
@@ -1,177 +0,0 @@
1
- import { getOwn, toArray, getConstructor, isTypescriptObject } from '@orpc/shared';
2
- import { cloneORPCError } from '@orpc/client';
3
-
4
- function mergeErrorMap(errorMap1, errorMap2) {
5
- return { ...errorMap1, ...errorMap2 };
6
- }
7
- async function reconcileORPCError(map, error) {
8
- const config = getOwn(map, error.code);
9
- if (!config) {
10
- if (!error.defined) {
11
- return error;
12
- }
13
- const cloned2 = cloneORPCError(error);
14
- cloned2.defined = false;
15
- return cloned2;
16
- }
17
- if (!config.data) {
18
- if (error.defined) {
19
- return error;
20
- }
21
- const cloned2 = cloneORPCError(error);
22
- cloned2.defined = true;
23
- return cloned2;
24
- }
25
- const validated = await config.data["~standard"].validate(error.data);
26
- if (validated.issues) {
27
- if (!error.defined) {
28
- return error;
29
- }
30
- const cloned2 = cloneORPCError(error);
31
- cloned2.defined = false;
32
- return cloned2;
33
- }
34
- if (error.data === validated.value && error.defined) {
35
- return error;
36
- }
37
- const cloned = cloneORPCError(error);
38
- cloned.data = validated.value;
39
- cloned.defined = true;
40
- return cloned;
41
- }
42
-
43
- function resolveMetaPlugins(baseMeta, existingPlugins, incomingPlugins) {
44
- existingPlugins = toArray(existingPlugins);
45
- incomingPlugins = toArray(incomingPlugins);
46
- let meta = baseMeta;
47
- for (const plugin of incomingPlugins) {
48
- if (plugin.init) {
49
- meta = plugin.init(meta);
50
- }
51
- }
52
- const plugins = [...existingPlugins, ...incomingPlugins];
53
- for (const plugin of plugins) {
54
- if (plugin.apply) {
55
- meta = plugin.apply(meta);
56
- }
57
- }
58
- return [meta, plugins];
59
- }
60
- function defineMeta(name, merge) {
61
- const metaPlugin = (value) => ({
62
- name,
63
- init: (meta) => {
64
- const current = meta[name];
65
- return {
66
- ...meta,
67
- [name]: merge(value, current)
68
- };
69
- }
70
- });
71
- const getMeta = (procedureOrLazy) => procedureOrLazy["~orpc"].meta[name];
72
- return [metaPlugin, getMeta];
73
- }
74
-
75
- class ProcedureContract {
76
- "~orpc";
77
- constructor(def) {
78
- this["~orpc"] = def;
79
- }
80
- /**
81
- * Checks if the given instance satisfies the {@link ProcedureContract} class/interface.
82
- */
83
- static [Symbol.hasInstance](instance) {
84
- if (this !== ProcedureContract) {
85
- return Function.prototype[Symbol.hasInstance].call(this, instance);
86
- }
87
- const constructor = getConstructor(instance);
88
- if (constructor === ProcedureContract) {
89
- return true;
90
- }
91
- return isTypescriptObject(instance) && isTypescriptObject(instance["~orpc"]) && isTypescriptObject(instance["~orpc"].errorMap) && isTypescriptObject(instance["~orpc"].meta) && (instance["~orpc"].inputSchemas === void 0 || Array.isArray(instance["~orpc"].inputSchemas)) && (instance["~orpc"].outputSchemas === void 0 || Array.isArray(instance["~orpc"].outputSchemas));
92
- }
93
- }
94
-
95
- function augmentContractRouter(router, options) {
96
- if (router instanceof ProcedureContract) {
97
- const [meta, metaPlugins] = resolveMetaPlugins(
98
- options.meta,
99
- options.metaPlugins,
100
- router["~orpc"].metaPlugins
101
- );
102
- const enhanced2 = new ProcedureContract({
103
- ...router["~orpc"],
104
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
105
- meta,
106
- metaPlugins
107
- });
108
- return enhanced2;
109
- }
110
- if (!isTypescriptObject(router)) {
111
- return router;
112
- }
113
- const enhanced = {};
114
- for (const key of Object.keys(router)) {
115
- enhanced[key] = augmentContractRouter(router[key], options);
116
- }
117
- return enhanced;
118
- }
119
- function getRouterContract(router, path) {
120
- let current = router;
121
- for (let i = 0; i < path.length; i++) {
122
- const segment = path[i];
123
- if (!isTypescriptObject(current)) {
124
- return void 0;
125
- }
126
- if (current instanceof ProcedureContract) {
127
- return void 0;
128
- }
129
- current = getOwn(current, segment);
130
- }
131
- if (!isTypescriptObject(current)) {
132
- return void 0;
133
- }
134
- return current;
135
- }
136
- function getProcedureContractOrThrow(router, path) {
137
- const procedure = getRouterContract(router, path);
138
- if (!(procedure instanceof ProcedureContract)) {
139
- throw new TypeError(`No valid procedure found at path "${path.join(".")}", this may happen when the router contract is not properly configured.`);
140
- }
141
- return procedure;
142
- }
143
- function minifyRouterContract(router) {
144
- if (router instanceof ProcedureContract) {
145
- const procedure = {
146
- "~orpc": {
147
- errorMap: {},
148
- meta: router["~orpc"].meta
149
- }
150
- };
151
- return procedure;
152
- }
153
- if (!isTypescriptObject(router)) {
154
- return router;
155
- }
156
- const json = {};
157
- for (const [key, child] of Object.entries(router)) {
158
- json[key] = minifyRouterContract(child);
159
- }
160
- return json;
161
- }
162
-
163
- class ValidationError extends Error {
164
- name = "ValidationError";
165
- /**
166
- * This array is readonly because the upstream Standard Schema returns readonly issues.
167
- */
168
- issues;
169
- invalidData;
170
- constructor(options) {
171
- super(options.message, options);
172
- this.issues = options.issues;
173
- this.invalidData = options.invalidData;
174
- }
175
- }
176
-
177
- export { ProcedureContract as P, ValidationError as V, augmentContractRouter as a, getProcedureContractOrThrow as b, minifyRouterContract as c, defineMeta as d, reconcileORPCError as e, getRouterContract as g, mergeErrorMap as m, resolveMetaPlugins as r };