@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.
- package/README.md +2 -3
- package/dist/builder-variants.d.mts +113 -0
- package/dist/builder-variants.mjs +1 -0
- package/dist/builder.d.mts +78 -0
- package/dist/builder.mjs +71 -0
- package/dist/client-factory.d.mts +24 -0
- package/dist/client-factory.mjs +34 -0
- package/dist/error-factory.d.mts +87 -0
- package/dist/error-factory.mjs +75 -0
- package/dist/error-utils.d.mts +11 -0
- package/dist/error-utils.mjs +43 -0
- package/dist/error.d.mts +41 -0
- package/dist/error.mjs +12 -0
- package/dist/index.d.mts +62 -433
- package/dist/index.mjs +25 -342
- package/dist/meta-built-in.d.mts +38 -0
- package/dist/meta-built-in.mjs +40 -0
- package/dist/meta-utils.d.mts +62 -0
- package/dist/meta-utils.mjs +33 -0
- package/dist/meta.d.mts +61 -0
- package/dist/meta.mjs +11 -0
- package/dist/plugins/index.d.mts +10 -45
- package/dist/plugins/index.mjs +6 -89
- package/dist/plugins/request-validation.d.mts +28 -0
- package/dist/plugins/request-validation.mjs +46 -0
- package/dist/plugins/response-validation.d.mts +15 -0
- package/dist/plugins/response-validation.mjs +53 -0
- package/dist/procedure-client.d.mts +10 -0
- package/dist/procedure-client.mjs +1 -0
- package/dist/procedure.d.mts +36 -0
- package/dist/procedure.mjs +19 -0
- package/dist/router-client.d.mts +14 -0
- package/dist/router-client.mjs +1 -0
- package/dist/router-utils.d.mts +34 -0
- package/dist/router-utils.mjs +67 -0
- package/dist/router.d.mts +42 -0
- package/dist/router.mjs +1 -0
- package/dist/schema-built-in.d.mts +28 -0
- package/dist/schema-built-in.mjs +73 -0
- package/dist/schema-utils.d.mts +26 -0
- package/dist/schema-utils.mjs +29 -0
- package/dist/schema.d.mts +31 -0
- package/dist/schema.mjs +1 -0
- package/package.json +4 -4
- package/dist/index.d.ts +0 -433
- package/dist/plugins/index.d.ts +0 -47
- 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/README.md
CHANGED
|
@@ -191,15 +191,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
191
191
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
192
192
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
193
193
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
194
|
-
<td align="center"><a href="https://
|
|
194
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
195
195
|
</tr>
|
|
196
196
|
<tr>
|
|
197
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
198
197
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
199
198
|
</tr>
|
|
200
199
|
</table>
|
|
201
200
|
|
|
202
|
-
With thanks to [
|
|
201
|
+
With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
202
|
|
|
204
203
|
## Open Source Programs
|
|
205
204
|
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { InitialInputSchema, InitialOutputSchema } from "./builder.mjs";
|
|
2
|
+
import type { ErrorMap } from "./error.mjs";
|
|
3
|
+
import type { MergedErrorMap } from "./error-utils.mjs";
|
|
4
|
+
import type { MetaPlugin } from "./meta.mjs";
|
|
5
|
+
import type { ProcedureContract } from "./procedure.mjs";
|
|
6
|
+
import type { AnySchema, MergedSchema } from "./schema.mjs";
|
|
7
|
+
/**
|
|
8
|
+
* The contract builder variant returned after `.input` is called.
|
|
9
|
+
* It is already a complete procedure contract that can still be refined.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
12
|
+
*/
|
|
13
|
+
export interface ProcedureContractBuilderWithInput<
|
|
14
|
+
TInputSchema extends AnySchema,
|
|
15
|
+
TErrorMap extends ErrorMap
|
|
16
|
+
> extends ProcedureContract<TInputSchema, InitialOutputSchema, TErrorMap> {
|
|
17
|
+
/**
|
|
18
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
21
|
+
*/
|
|
22
|
+
meta(...plugins: MetaPlugin<TInputSchema, InitialOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithInput<TInputSchema, TErrorMap>;
|
|
23
|
+
/**
|
|
24
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
27
|
+
*/
|
|
28
|
+
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithInput<TInputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
29
|
+
/**
|
|
30
|
+
* Adds an additional input schema, merged with the previously defined one.
|
|
31
|
+
*
|
|
32
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
33
|
+
*/
|
|
34
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInput<MergedSchema<T, TInputSchema>, TErrorMap>;
|
|
35
|
+
/**
|
|
36
|
+
* Defines the output schema used to validate and type the procedure output.
|
|
37
|
+
*
|
|
38
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
39
|
+
*/
|
|
40
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<TInputSchema, T, TErrorMap>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The contract builder variant returned after `.output` is called.
|
|
44
|
+
* It is already a complete procedure contract that can still be refined.
|
|
45
|
+
*
|
|
46
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
47
|
+
*/
|
|
48
|
+
export interface ProcedureContractBuilderWithOutput<
|
|
49
|
+
TOutputSchema extends AnySchema,
|
|
50
|
+
TErrorMap extends ErrorMap
|
|
51
|
+
> extends ProcedureContract<InitialInputSchema, TOutputSchema, TErrorMap> {
|
|
52
|
+
/**
|
|
53
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
56
|
+
*/
|
|
57
|
+
meta(...plugins: MetaPlugin<InitialInputSchema, TOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
58
|
+
/**
|
|
59
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
60
|
+
*
|
|
61
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
62
|
+
*/
|
|
63
|
+
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithOutput<TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
64
|
+
/**
|
|
65
|
+
* Defines the input schema used to validate and type the procedure input.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
68
|
+
*/
|
|
69
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<T, TOutputSchema, TErrorMap>;
|
|
70
|
+
/**
|
|
71
|
+
* Adds an additional output schema, merged with the previously defined one.
|
|
72
|
+
*
|
|
73
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
74
|
+
*/
|
|
75
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithOutput<MergedSchema<T, TOutputSchema>, TErrorMap>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The contract builder variant returned after both `.input` and `.output`
|
|
79
|
+
* are called. It is already a complete procedure contract that can still be
|
|
80
|
+
* refined.
|
|
81
|
+
*
|
|
82
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
83
|
+
*/
|
|
84
|
+
export interface ProcedureContractBuilderWithInputOutput<
|
|
85
|
+
TInputSchema extends AnySchema,
|
|
86
|
+
TOutputSchema extends AnySchema,
|
|
87
|
+
TErrorMap extends ErrorMap
|
|
88
|
+
> extends ProcedureContract<TInputSchema, TOutputSchema, TErrorMap> {
|
|
89
|
+
/**
|
|
90
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
93
|
+
*/
|
|
94
|
+
meta(...plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap>;
|
|
95
|
+
/**
|
|
96
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
97
|
+
*
|
|
98
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
99
|
+
*/
|
|
100
|
+
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
101
|
+
/**
|
|
102
|
+
* Adds an additional input schema, merged with the previously defined one.
|
|
103
|
+
*
|
|
104
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
105
|
+
*/
|
|
106
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<MergedSchema<T, TInputSchema>, TOutputSchema, TErrorMap>;
|
|
107
|
+
/**
|
|
108
|
+
* Adds an additional output schema, merged with the previously defined one.
|
|
109
|
+
*
|
|
110
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
111
|
+
*/
|
|
112
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<TInputSchema, MergedSchema<T, TOutputSchema>, TErrorMap>;
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ProcedureContractBuilderWithInput, ProcedureContractBuilderWithOutput } from "./builder-variants.mjs";
|
|
2
|
+
import type { ErrorMap } from "./error.mjs";
|
|
3
|
+
import type { MergedErrorMap } from "./error-utils.mjs";
|
|
4
|
+
import type { MetaPlugin } from "./meta.mjs";
|
|
5
|
+
import type { RouterContract } from "./router.mjs";
|
|
6
|
+
import type { AugmentedContractRouter } from "./router-utils.mjs";
|
|
7
|
+
import type { AnySchema, Schema } from "./schema.mjs";
|
|
8
|
+
import { ProcedureContract } from "./procedure.mjs";
|
|
9
|
+
/**
|
|
10
|
+
* The input schema type used before `.input` is called.
|
|
11
|
+
* Input is `void` for better compatibility with third-party libraries,
|
|
12
|
+
* such as TanStack Query, which allow calling mutations without input.
|
|
13
|
+
*/
|
|
14
|
+
export type InitialInputSchema = Schema<void, unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* The output schema type used before `.output` is called.
|
|
17
|
+
*/
|
|
18
|
+
export type InitialOutputSchema = Schema<unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* The contract builder behind `oc`. Chain its methods to define procedure
|
|
21
|
+
* contracts — metadata, errors, and input/output schemas — without any
|
|
22
|
+
* business logic.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
25
|
+
*/
|
|
26
|
+
export declare class ContractBuilder<TErrorMap extends ErrorMap> extends ProcedureContract<InitialInputSchema, InitialOutputSchema, TErrorMap> {
|
|
27
|
+
/**
|
|
28
|
+
* Private constructor to prevent direct instantiation.
|
|
29
|
+
* Use the static `create` method to initialize a new instance with a safe initial definition.
|
|
30
|
+
*/
|
|
31
|
+
private constructor();
|
|
32
|
+
/**
|
|
33
|
+
* Creates a fresh contract builder with an empty definition.
|
|
34
|
+
* Prefer the exported `oc` instance over calling this directly.
|
|
35
|
+
*
|
|
36
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
37
|
+
*/
|
|
38
|
+
static create(): ContractBuilder<Record<never, never>>;
|
|
39
|
+
/**
|
|
40
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
43
|
+
*/
|
|
44
|
+
meta(...plugins: MetaPlugin<InitialInputSchema, InitialOutputSchema, TErrorMap>[]): ContractBuilder<TErrorMap>;
|
|
45
|
+
/**
|
|
46
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
49
|
+
*/
|
|
50
|
+
errors<T extends ErrorMap>(errors: T): ContractBuilder<MergedErrorMap<TErrorMap, T>>;
|
|
51
|
+
/**
|
|
52
|
+
* Defines the input schema used to validate and type the procedure input.
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
55
|
+
*/
|
|
56
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInput<T, TErrorMap>;
|
|
57
|
+
/**
|
|
58
|
+
* Defines the output schema used to validate and type the procedure output.
|
|
59
|
+
*
|
|
60
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
61
|
+
*/
|
|
62
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithOutput<T, TErrorMap>;
|
|
63
|
+
/**
|
|
64
|
+
* Applies the builder's errors and metadata to every procedure contract in
|
|
65
|
+
* the given router contract.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link https://orpc.dev/docs/contract/router#extending-router | Router Contract - Extending Router}
|
|
68
|
+
*/
|
|
69
|
+
router<T extends RouterContract>(router: T): AugmentedContractRouter<T, TErrorMap>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The oRPC contract builder. Chain methods like `.input`, `.errors`, and
|
|
73
|
+
* `.output` to define procedure contracts, then compose them into router
|
|
74
|
+
* contracts.
|
|
75
|
+
*
|
|
76
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
77
|
+
*/
|
|
78
|
+
export declare const oc: ContractBuilder<Record<never, never>>;
|
package/dist/builder.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { toArray } from "@orpc/shared";
|
|
2
|
+
import { mergeErrorMap } from "./error-utils.mjs";
|
|
3
|
+
import { getHiddenMetaPlugins } from "./meta.mjs";
|
|
4
|
+
import { resolveMetaPlugins } from "./meta-utils.mjs";
|
|
5
|
+
import { ProcedureContract } from "./procedure.mjs";
|
|
6
|
+
import { augmentContractRouter } from "./router-utils.mjs";
|
|
7
|
+
|
|
8
|
+
export class ContractBuilder extends ProcedureContract {
|
|
9
|
+
|
|
10
|
+
constructor(definition) {
|
|
11
|
+
super(definition);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static create() {
|
|
15
|
+
return new ContractBuilder({
|
|
16
|
+
errorMap: {},
|
|
17
|
+
meta: {}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
meta(...plugins) {
|
|
22
|
+
const [meta, metaPlugins] = resolveMetaPlugins(this["~orpc"].meta, this["~orpc"].metaPlugins, plugins);
|
|
23
|
+
return new ContractBuilder({
|
|
24
|
+
...this["~orpc"],
|
|
25
|
+
meta,
|
|
26
|
+
metaPlugins
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
errors(errors) {
|
|
31
|
+
let result = new ContractBuilder({
|
|
32
|
+
...this["~orpc"],
|
|
33
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
34
|
+
});
|
|
35
|
+
const plugins = getHiddenMetaPlugins(errors);
|
|
36
|
+
if (plugins) {
|
|
37
|
+
result = result.meta(...plugins);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
input(schema) {
|
|
43
|
+
let result = new ContractBuilder({
|
|
44
|
+
...this["~orpc"],
|
|
45
|
+
inputSchemas: [...toArray(this["~orpc"].inputSchemas), schema]
|
|
46
|
+
});
|
|
47
|
+
const plugins = getHiddenMetaPlugins(schema);
|
|
48
|
+
if (plugins) {
|
|
49
|
+
result = result.meta(...plugins);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
output(schema) {
|
|
55
|
+
let result = new ContractBuilder({
|
|
56
|
+
...this["~orpc"],
|
|
57
|
+
outputSchemas: [...toArray(this["~orpc"].outputSchemas), schema]
|
|
58
|
+
});
|
|
59
|
+
const plugins = getHiddenMetaPlugins(schema);
|
|
60
|
+
if (plugins) {
|
|
61
|
+
result = result.meta(...plugins);
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
router(router) {
|
|
67
|
+
return augmentContractRouter(router, this["~orpc"]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const oc = /* @__PURE__ */ ContractBuilder.create();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ClientContext, ClientLink, ORPCClientOptions } from "@orpc/client";
|
|
2
|
+
import type { RouterContract } from "./router.mjs";
|
|
3
|
+
import type { RouterContractClient } from "./router-client.mjs";
|
|
4
|
+
export interface ContractClientFactory<TClientContext extends ClientContext> {
|
|
5
|
+
<T extends RouterContract>(contract: T): RouterContractClient<T, TClientContext>;
|
|
6
|
+
}
|
|
7
|
+
export interface ContractClientFactoryOptions<TClientContext extends ClientContext> extends Pick<ORPCClientOptions<RouterContractClient<RouterContract, TClientContext>>, "interceptors" | "scoped"> {
|
|
8
|
+
/**
|
|
9
|
+
* An optional reference to the root router-contract.
|
|
10
|
+
* When provided, the client factory will automatically register the passed contract
|
|
11
|
+
* into the router at the path defined by `meta.path`.
|
|
12
|
+
*/
|
|
13
|
+
contractRef?: RouterContract | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a client factory that builds a client from any procedure or router contract,
|
|
17
|
+
* so large projects can import individual contracts instead of a single root client.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* **Warning**: Every procedure contract passed to the factory must define `meta.path` matching its location in the root contract.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
|
|
23
|
+
*/
|
|
24
|
+
export declare function createContractClientFactory<TClientContext extends ClientContext>(link: ClientLink<TClientContext>, options?: ContractClientFactoryOptions<TClientContext>): ContractClientFactory<TClientContext>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createORPCClient } from "@orpc/client";
|
|
2
|
+
import { get, isTypescriptObject, set } from "@orpc/shared";
|
|
3
|
+
import { resolveBasePathMeta } from "./meta-built-in.mjs";
|
|
4
|
+
import { ProcedureContract } from "./procedure.mjs";
|
|
5
|
+
|
|
6
|
+
export function createContractClientFactory(link, options = {}) {
|
|
7
|
+
const factory = (contract) => {
|
|
8
|
+
const path = resolveBasePathMeta(contract);
|
|
9
|
+
if (path === undefined) {
|
|
10
|
+
throw new TypeError("ContractClientFactory: procedure contract must define `meta.path` that matches its path in the root router contract.");
|
|
11
|
+
}
|
|
12
|
+
const contractRef = options.contractRef;
|
|
13
|
+
if (contractRef) {
|
|
14
|
+
const register = (contract, path) => {
|
|
15
|
+
if (contract instanceof ProcedureContract) {
|
|
16
|
+
set(contractRef, [...path, "~orpc"], contract["~orpc"]);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (isTypescriptObject(contract)) {
|
|
20
|
+
for (const [key, value] of Object.entries(contract)) {
|
|
21
|
+
register(value, [...path, key]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
register(contract, path);
|
|
26
|
+
}
|
|
27
|
+
return createORPCClient(link, {
|
|
28
|
+
...options,
|
|
29
|
+
scoped: get(options.scoped, path),
|
|
30
|
+
path
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
return factory;
|
|
34
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ORPCErrorCode, ORPCErrorOptions } from "@orpc/client";
|
|
2
|
+
import type { MaybeOptionalOptions } from "@orpc/shared";
|
|
3
|
+
import type { ErrorMap, ErrorMapItem } from "./error.mjs";
|
|
4
|
+
import type { AnySchema, InferSchemaInput, Schema } from "./schema.mjs";
|
|
5
|
+
import { ORPCError } from "@orpc/client";
|
|
6
|
+
export interface ORPCErrorFactoryOptions<TData> {
|
|
7
|
+
/**
|
|
8
|
+
* Optional schema used to type and validate the error data.
|
|
9
|
+
* Must be a synchronous schema.
|
|
10
|
+
*/
|
|
11
|
+
data?: Schema<TData> | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Optional default message, can be overridden when constructing an error.
|
|
14
|
+
*/
|
|
15
|
+
message?: string | undefined;
|
|
16
|
+
}
|
|
17
|
+
export interface ORPCErrorFactory<
|
|
18
|
+
TCode extends ORPCErrorCode,
|
|
19
|
+
TData
|
|
20
|
+
> extends ErrorMapItem {
|
|
21
|
+
code: TCode;
|
|
22
|
+
data: Schema<TData>;
|
|
23
|
+
new (...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>): ORPCError<TCode, TData>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a reusable error class ({@link ORPCErrorFactory}) for the given code,
|
|
27
|
+
* default message, and data schema.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const RateLimitedError = error('RATE_LIMITED', {
|
|
32
|
+
* message: 'You are being rate limited',
|
|
33
|
+
* data: z.object({ retryAfter: z.number() }),
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* const procedure = os
|
|
37
|
+
* .errors({ [RateLimitedError.code]: RateLimitedError })
|
|
38
|
+
* .handler(() => {
|
|
39
|
+
* throw new RateLimitedError({ data: { retryAfter: 60 } })
|
|
40
|
+
* })
|
|
41
|
+
*
|
|
42
|
+
* try {
|
|
43
|
+
* const output = call(procedure)
|
|
44
|
+
* } catch (error) {
|
|
45
|
+
* if (error instanceof RateLimitedError) {
|
|
46
|
+
* console.log(error.data.retryAfter)
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @see {@link https://orpc.dev/docs/error-handling#error-factory | Error Handling - Error Factory}
|
|
52
|
+
*/
|
|
53
|
+
export declare function error<
|
|
54
|
+
TCode extends ORPCErrorCode,
|
|
55
|
+
TData = unknown
|
|
56
|
+
>(code: TCode, { data: dataSchema, message }?: ORPCErrorFactoryOptions<TData>): ORPCErrorFactory<TCode, TData>;
|
|
57
|
+
export type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, "status">;
|
|
58
|
+
export interface ORPCErrorConstructorMapItem<
|
|
59
|
+
TCode extends ORPCErrorCode,
|
|
60
|
+
TInData
|
|
61
|
+
> {
|
|
62
|
+
(...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>): ORPCError<TCode, TInData>;
|
|
63
|
+
}
|
|
64
|
+
export type ORPCErrorConstructorMap<T extends ErrorMap> = { [K in keyof T]: T[K] extends ErrorMapItem ? ORPCErrorConstructorMapItem<K & ORPCErrorCode, T[K]["data"] extends AnySchema ? InferSchemaInput<T[K]["data"]> : unknown> : never };
|
|
65
|
+
/**
|
|
66
|
+
* Creates a map of ORPC error constructors.
|
|
67
|
+
*
|
|
68
|
+
* The returned object is a `Proxy` that allows access to arbitrary error codes:
|
|
69
|
+
* - If the code exists in the provided `errorMap`, the corresponding constructor
|
|
70
|
+
* will create a **defined** `ORPCError`.
|
|
71
|
+
* - If the code does not exist, a fallback `ORPCError` constructor is returned.
|
|
72
|
+
*
|
|
73
|
+
* The `in` operator can be used to check whether an error code is explicitly
|
|
74
|
+
* defined in the map.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const errorMap = createORPCErrorConstructorMap({
|
|
79
|
+
* NOT_FOUND: {
|
|
80
|
+
* message: 'Not Found',
|
|
81
|
+
* },
|
|
82
|
+
* })
|
|
83
|
+
*
|
|
84
|
+
* throw errorMap.NOT_FOUND()
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function createORPCErrorConstructorMap<T extends ErrorMap>(errorMap: T): ORPCErrorConstructorMap<T>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ORPCError, RECURSIVE_CLIENT_UNWRAP_KEYS } from "@orpc/client";
|
|
2
|
+
import { getOwn, resolveMaybeOptionalOptions } from "@orpc/shared";
|
|
3
|
+
import { ValidationError } from "./error.mjs";
|
|
4
|
+
import { type } from "./schema-utils.mjs";
|
|
5
|
+
|
|
6
|
+
export function error(code, { data: dataSchema, message } = {}) {
|
|
7
|
+
const validateData = (schema, value) => {
|
|
8
|
+
const result = schema["~standard"].validate(value);
|
|
9
|
+
if (result instanceof Promise) {
|
|
10
|
+
throw new TypeError(`Error factory "${code}" does not support async data schemas.`);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
return class extends ORPCError {
|
|
15
|
+
static code = code;
|
|
16
|
+
static data = dataSchema ?? type();
|
|
17
|
+
static message = message;
|
|
18
|
+
constructor(...rest) {
|
|
19
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
20
|
+
let data = options.data;
|
|
21
|
+
if (dataSchema) {
|
|
22
|
+
const result = validateData(dataSchema, options.data);
|
|
23
|
+
if (result.issues) {
|
|
24
|
+
throw new ValidationError({
|
|
25
|
+
message: `Error factory "${code}" data validation failed`,
|
|
26
|
+
issues: result.issues,
|
|
27
|
+
invalidData: options.data
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
data = result.value;
|
|
31
|
+
}
|
|
32
|
+
super(code, {
|
|
33
|
+
...options,
|
|
34
|
+
message: options.message ?? message,
|
|
35
|
+
data
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
static [Symbol.hasInstance](instance) {
|
|
39
|
+
if (!(instance instanceof ORPCError)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (instance.code !== code) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (dataSchema && validateData(dataSchema, instance.data).issues) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createORPCErrorConstructorMap(errorMap) {
|
|
54
|
+
const proxy = new Proxy(errorMap, { get(target, code) {
|
|
55
|
+
if (typeof code !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(code)) {
|
|
56
|
+
return Reflect.get(target, code);
|
|
57
|
+
}
|
|
58
|
+
const item = (...rest) => {
|
|
59
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
60
|
+
const config = getOwn(errorMap, code);
|
|
61
|
+
const error = new ORPCError(code, {
|
|
62
|
+
message: options.message ?? config?.message,
|
|
63
|
+
data: options.data,
|
|
64
|
+
cause: options.cause
|
|
65
|
+
});
|
|
66
|
+
if (config) {
|
|
67
|
+
;
|
|
68
|
+
error.defined = true;
|
|
69
|
+
}
|
|
70
|
+
return error;
|
|
71
|
+
};
|
|
72
|
+
return item;
|
|
73
|
+
} });
|
|
74
|
+
return proxy;
|
|
75
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AnyORPCError } from "@orpc/client";
|
|
2
|
+
import type { ErrorMap } from "./error.mjs";
|
|
3
|
+
export type MergedErrorMap<
|
|
4
|
+
T1 extends ErrorMap,
|
|
5
|
+
T2 extends ErrorMap
|
|
6
|
+
> = keyof T1 extends never | keyof T2 ? T2 : Omit<T1, keyof T2> & T2;
|
|
7
|
+
export declare function mergeErrorMap<
|
|
8
|
+
T1 extends ErrorMap = object,
|
|
9
|
+
T2 extends ErrorMap = object
|
|
10
|
+
>(errorMap1: T1 | undefined, errorMap2: T2 | undefined): MergedErrorMap<T1, T2>;
|
|
11
|
+
export declare function reconcileORPCError(map: ErrorMap, error: AnyORPCError): Promise<AnyORPCError>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { cloneORPCError } from "@orpc/client";
|
|
2
|
+
import { getOwn } from "@orpc/shared";
|
|
3
|
+
export function mergeErrorMap(errorMap1, errorMap2) {
|
|
4
|
+
return {
|
|
5
|
+
...errorMap1,
|
|
6
|
+
...errorMap2
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export async function reconcileORPCError(map, error) {
|
|
10
|
+
const config = getOwn(map, error.code);
|
|
11
|
+
if (!config) {
|
|
12
|
+
if (!error.defined) {
|
|
13
|
+
return error;
|
|
14
|
+
}
|
|
15
|
+
const cloned = cloneORPCError(error);
|
|
16
|
+
cloned.defined = false;
|
|
17
|
+
return cloned;
|
|
18
|
+
}
|
|
19
|
+
if (!config.data) {
|
|
20
|
+
if (error.defined) {
|
|
21
|
+
return error;
|
|
22
|
+
}
|
|
23
|
+
const cloned = cloneORPCError(error);
|
|
24
|
+
cloned.defined = true;
|
|
25
|
+
return cloned;
|
|
26
|
+
}
|
|
27
|
+
const validated = await config.data["~standard"].validate(error.data);
|
|
28
|
+
if (validated.issues) {
|
|
29
|
+
if (!error.defined) {
|
|
30
|
+
return error;
|
|
31
|
+
}
|
|
32
|
+
const cloned = cloneORPCError(error);
|
|
33
|
+
cloned.defined = false;
|
|
34
|
+
return cloned;
|
|
35
|
+
}
|
|
36
|
+
if (error.data === validated.value && error.defined) {
|
|
37
|
+
return error;
|
|
38
|
+
}
|
|
39
|
+
const cloned = cloneORPCError(error);
|
|
40
|
+
cloned.data = validated.value;
|
|
41
|
+
cloned.defined = true;
|
|
42
|
+
return cloned;
|
|
43
|
+
}
|
package/dist/error.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ORPCError, ORPCErrorCode } from "@orpc/client";
|
|
2
|
+
import type { AnySchema, InferSchemaOutput, SchemaIssue } from "./schema.mjs";
|
|
3
|
+
export interface ErrorMapItem {
|
|
4
|
+
/**
|
|
5
|
+
* Default message, can be overridden when constructing an error.
|
|
6
|
+
*/
|
|
7
|
+
message?: string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Schema used to type and validate the error data.
|
|
10
|
+
*/
|
|
11
|
+
data?: AnySchema | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Map of error codes to their definitions, as passed to `.errors(...)`.
|
|
15
|
+
* Errors defined here remain properly typed on the client.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
18
|
+
*/
|
|
19
|
+
export type ErrorMap = { [key in ORPCErrorCode]?: ErrorMapItem };
|
|
20
|
+
export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = { [K in keyof TErrorMap]: TErrorMap[K] extends ErrorMapItem ? ORPCError<K & ORPCErrorCode, TErrorMap[K]["data"] extends AnySchema ? InferSchemaOutput<TErrorMap[K]["data"]> : unknown> : never }[keyof TErrorMap];
|
|
21
|
+
export interface ValidationErrorOptions extends ErrorOptions {
|
|
22
|
+
message: string;
|
|
23
|
+
issues: readonly SchemaIssue[];
|
|
24
|
+
invalidData: unknown;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Error thrown when input, output, or error data fails schema validation,
|
|
28
|
+
* carrying the standard-schema `issues` and the invalid data.
|
|
29
|
+
* Usually found as the `cause` of an `ORPCError`.
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/recipes/validation-customization | Validation Customization}
|
|
32
|
+
*/
|
|
33
|
+
export declare class ValidationError extends Error {
|
|
34
|
+
override name: string;
|
|
35
|
+
/**
|
|
36
|
+
* This array is readonly because the upstream Standard Schema returns readonly issues.
|
|
37
|
+
*/
|
|
38
|
+
issues: readonly SchemaIssue[];
|
|
39
|
+
invalidData: unknown;
|
|
40
|
+
constructor(options: ValidationErrorOptions);
|
|
41
|
+
}
|
package/dist/error.mjs
ADDED