@orpc/contract 2.0.0-beta.48 → 2.0.0-beta.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/builder-variants.d.ts +103 -0
- package/dist/builder.d.ts +78 -0
- package/dist/builder.js +112 -0
- package/dist/client-factory.d.ts +24 -0
- package/dist/client-factory.js +38 -0
- package/dist/error-factory.d.ts +80 -0
- package/dist/error-factory.js +121 -0
- package/dist/error-utils.d.ts +5 -0
- package/dist/error-utils.js +40 -0
- package/dist/error.d.ts +45 -0
- package/dist/error.js +20 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +31 -0
- package/dist/meta-built-in.d.ts +30 -0
- package/dist/meta-built-in.js +54 -0
- package/dist/meta-utils.d.ts +58 -0
- package/dist/meta-utils.js +76 -0
- package/dist/meta.d.ts +53 -0
- package/dist/meta.js +13 -0
- package/dist/plugins/index.d.ts +12 -0
- package/dist/plugins/index.js +12 -0
- package/dist/plugins/request-validation.d.ts +28 -0
- package/dist/plugins/request-validation.js +55 -0
- package/dist/plugins/response-validation.d.ts +15 -0
- package/dist/plugins/response-validation.js +72 -0
- package/dist/procedure-client.d.ts +5 -0
- package/dist/procedure.d.ts +28 -0
- package/dist/procedure.js +28 -0
- package/dist/router-client.d.ts +13 -0
- package/dist/router-utils.d.ts +30 -0
- package/dist/router-utils.js +79 -0
- package/dist/router.d.ts +52 -0
- package/dist/schema-built-in.d.ts +23 -0
- package/dist/schema-built-in.js +76 -0
- package/dist/schema-utils.d.ts +20 -0
- package/dist/schema-utils.js +44 -0
- package/dist/schema.d.ts +25 -0
- package/package.json +28 -30
- package/dist/builder-variants.d.mts +0 -113
- package/dist/builder.d.mts +0 -78
- package/dist/builder.mjs +0 -71
- package/dist/client-factory.d.mts +0 -24
- package/dist/client-factory.mjs +0 -34
- package/dist/error-factory.d.mts +0 -87
- package/dist/error-factory.mjs +0 -75
- package/dist/error-utils.d.mts +0 -11
- package/dist/error-utils.mjs +0 -43
- package/dist/error.d.mts +0 -41
- package/dist/error.mjs +0 -12
- package/dist/index.d.mts +0 -62
- package/dist/index.mjs +0 -25
- package/dist/meta-built-in.d.mts +0 -38
- package/dist/meta-built-in.mjs +0 -40
- package/dist/meta-utils.d.mts +0 -62
- package/dist/meta-utils.mjs +0 -33
- package/dist/meta.d.mts +0 -61
- package/dist/meta.mjs +0 -11
- package/dist/plugins/index.d.mts +0 -12
- package/dist/plugins/index.mjs +0 -8
- package/dist/plugins/request-validation.d.mts +0 -28
- package/dist/plugins/request-validation.mjs +0 -46
- package/dist/plugins/response-validation.d.mts +0 -15
- package/dist/plugins/response-validation.mjs +0 -53
- package/dist/procedure-client.d.mts +0 -10
- package/dist/procedure.d.mts +0 -36
- package/dist/procedure.mjs +0 -19
- package/dist/router-client.d.mts +0 -14
- package/dist/router-utils.d.mts +0 -34
- package/dist/router-utils.mjs +0 -67
- package/dist/router.d.mts +0 -42
- package/dist/schema-built-in.d.mts +0 -28
- package/dist/schema-built-in.mjs +0 -73
- package/dist/schema-utils.d.mts +0 -26
- package/dist/schema-utils.mjs +0 -29
- package/dist/schema.d.mts +0 -31
- /package/dist/{builder-variants.mjs → builder-variants.js} +0 -0
- /package/dist/{procedure-client.mjs → procedure-client.js} +0 -0
- /package/dist/{router-client.mjs → router-client.js} +0 -0
- /package/dist/{router.mjs → router.js} +0 -0
- /package/dist/{schema.mjs → schema.js} +0 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/contract">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fcontract?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
208
209
|
<tr>
|
|
209
210
|
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
211
|
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
-
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
212
212
|
</tr>
|
|
213
213
|
</table>
|
|
214
214
|
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { InitialInputSchema, InitialOutputSchema } from './builder.ts';
|
|
2
|
+
import type { MergedErrorMap } from './error-utils.ts';
|
|
3
|
+
import type { ErrorMap } from './error.ts';
|
|
4
|
+
import type { MetaPlugin } from './meta.ts';
|
|
5
|
+
import type { ProcedureContract } from './procedure.ts';
|
|
6
|
+
import type { AnySchema, MergedSchema } from './schema.ts';
|
|
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<TInputSchema extends AnySchema, TErrorMap extends ErrorMap> extends ProcedureContract<TInputSchema, InitialOutputSchema, TErrorMap> {
|
|
14
|
+
/**
|
|
15
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
18
|
+
*/
|
|
19
|
+
meta(...plugins: MetaPlugin<TInputSchema, InitialOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithInput<TInputSchema, TErrorMap>;
|
|
20
|
+
/**
|
|
21
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
24
|
+
*/
|
|
25
|
+
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithInput<TInputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
26
|
+
/**
|
|
27
|
+
* Adds an additional input schema, merged with the previously defined one.
|
|
28
|
+
*
|
|
29
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
30
|
+
*/
|
|
31
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInput<MergedSchema<T, TInputSchema>, TErrorMap>;
|
|
32
|
+
/**
|
|
33
|
+
* Defines the output schema used to validate and type the procedure output.
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
36
|
+
*/
|
|
37
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<TInputSchema, T, TErrorMap>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The contract builder variant returned after `.output` is called.
|
|
41
|
+
* It is already a complete procedure contract that can still be refined.
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
44
|
+
*/
|
|
45
|
+
export interface ProcedureContractBuilderWithOutput<TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends ProcedureContract<InitialInputSchema, TOutputSchema, TErrorMap> {
|
|
46
|
+
/**
|
|
47
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
48
|
+
*
|
|
49
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
50
|
+
*/
|
|
51
|
+
meta(...plugins: MetaPlugin<InitialInputSchema, TOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
52
|
+
/**
|
|
53
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
56
|
+
*/
|
|
57
|
+
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithOutput<TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
58
|
+
/**
|
|
59
|
+
* Defines the input schema used to validate and type the procedure input.
|
|
60
|
+
*
|
|
61
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
62
|
+
*/
|
|
63
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<T, TOutputSchema, TErrorMap>;
|
|
64
|
+
/**
|
|
65
|
+
* Adds an additional output schema, merged with the previously defined one.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
68
|
+
*/
|
|
69
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithOutput<MergedSchema<T, TOutputSchema>, TErrorMap>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The contract builder variant returned after both `.input` and `.output`
|
|
73
|
+
* are called. It is already a complete procedure contract that can still be
|
|
74
|
+
* refined.
|
|
75
|
+
*
|
|
76
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
77
|
+
*/
|
|
78
|
+
export interface ProcedureContractBuilderWithInputOutput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends ProcedureContract<TInputSchema, TOutputSchema, TErrorMap> {
|
|
79
|
+
/**
|
|
80
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
81
|
+
*
|
|
82
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
83
|
+
*/
|
|
84
|
+
meta(...plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap>;
|
|
85
|
+
/**
|
|
86
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
87
|
+
*
|
|
88
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
89
|
+
*/
|
|
90
|
+
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
91
|
+
/**
|
|
92
|
+
* Adds an additional input schema, merged with the previously defined one.
|
|
93
|
+
*
|
|
94
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
95
|
+
*/
|
|
96
|
+
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<MergedSchema<T, TInputSchema>, TOutputSchema, TErrorMap>;
|
|
97
|
+
/**
|
|
98
|
+
* Adds an additional output schema, merged with the previously defined one.
|
|
99
|
+
*
|
|
100
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
101
|
+
*/
|
|
102
|
+
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<TInputSchema, MergedSchema<T, TOutputSchema>, TErrorMap>;
|
|
103
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ProcedureContractBuilderWithInput, ProcedureContractBuilderWithOutput } from './builder-variants.ts';
|
|
2
|
+
import type { MergedErrorMap } from './error-utils.ts';
|
|
3
|
+
import type { ErrorMap } from './error.ts';
|
|
4
|
+
import type { MetaPlugin } from './meta.ts';
|
|
5
|
+
import { ProcedureContract } from './procedure.ts';
|
|
6
|
+
import type { AugmentedContractRouter } from './router-utils.ts';
|
|
7
|
+
import type { RouterContract } from './router.ts';
|
|
8
|
+
import type { AnySchema, Schema } from './schema.ts';
|
|
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.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { toArray } from '@orpc/shared';
|
|
2
|
+
import { mergeErrorMap } from './error-utils.js';
|
|
3
|
+
import { resolveMetaPlugins } from './meta-utils.js';
|
|
4
|
+
import { getHiddenMetaPlugins } from './meta.js';
|
|
5
|
+
import { ProcedureContract } from './procedure.js';
|
|
6
|
+
import { augmentContractRouter } from './router-utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* The contract builder behind `oc`. Chain its methods to define procedure
|
|
9
|
+
* contracts — metadata, errors, and input/output schemas — without any
|
|
10
|
+
* business logic.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
13
|
+
*/
|
|
14
|
+
export class ContractBuilder extends ProcedureContract {
|
|
15
|
+
/**
|
|
16
|
+
* Private constructor to prevent direct instantiation.
|
|
17
|
+
* Use the static `create` method to initialize a new instance with a safe initial definition.
|
|
18
|
+
*/
|
|
19
|
+
constructor(definition) {
|
|
20
|
+
super(definition);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a fresh contract builder with an empty definition.
|
|
24
|
+
* Prefer the exported `oc` instance over calling this directly.
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
27
|
+
*/
|
|
28
|
+
static create() {
|
|
29
|
+
return new ContractBuilder({
|
|
30
|
+
errorMap: {},
|
|
31
|
+
meta: {},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Applies metadata plugins to contracts built from this builder.
|
|
36
|
+
*
|
|
37
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
38
|
+
*/
|
|
39
|
+
meta(...plugins) {
|
|
40
|
+
const [meta, metaPlugins] = resolveMetaPlugins(this['~orpc'].meta, this['~orpc'].metaPlugins, plugins);
|
|
41
|
+
return new ContractBuilder({
|
|
42
|
+
...this['~orpc'],
|
|
43
|
+
meta,
|
|
44
|
+
metaPlugins,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Defines typesafe errors that implementations of this contract can throw.
|
|
49
|
+
*
|
|
50
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
51
|
+
*/
|
|
52
|
+
errors(errors) {
|
|
53
|
+
let result = new ContractBuilder({
|
|
54
|
+
...this['~orpc'],
|
|
55
|
+
errorMap: mergeErrorMap(this['~orpc'].errorMap, errors),
|
|
56
|
+
});
|
|
57
|
+
const plugins = getHiddenMetaPlugins(errors);
|
|
58
|
+
if (plugins) {
|
|
59
|
+
result = result.meta(...plugins);
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Defines the input schema used to validate and type the procedure input.
|
|
65
|
+
*
|
|
66
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
67
|
+
*/
|
|
68
|
+
input(schema) {
|
|
69
|
+
let result = new ContractBuilder({
|
|
70
|
+
...this['~orpc'],
|
|
71
|
+
inputSchemas: [...toArray(this['~orpc'].inputSchemas), schema],
|
|
72
|
+
});
|
|
73
|
+
const plugins = getHiddenMetaPlugins(schema);
|
|
74
|
+
if (plugins) {
|
|
75
|
+
result = result.meta(...plugins);
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Defines the output schema used to validate and type the procedure output.
|
|
81
|
+
*
|
|
82
|
+
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
83
|
+
*/
|
|
84
|
+
output(schema) {
|
|
85
|
+
let result = new ContractBuilder({
|
|
86
|
+
...this['~orpc'],
|
|
87
|
+
outputSchemas: [...toArray(this['~orpc'].outputSchemas), schema],
|
|
88
|
+
});
|
|
89
|
+
const plugins = getHiddenMetaPlugins(schema);
|
|
90
|
+
if (plugins) {
|
|
91
|
+
result = result.meta(...plugins);
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Applies the builder's errors and metadata to every procedure contract in
|
|
97
|
+
* the given router contract.
|
|
98
|
+
*
|
|
99
|
+
* @see {@link https://orpc.dev/docs/contract/router#extending-router | Router Contract - Extending Router}
|
|
100
|
+
*/
|
|
101
|
+
router(router) {
|
|
102
|
+
return augmentContractRouter(router, this['~orpc']);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The oRPC contract builder. Chain methods like `.input`, `.errors`, and
|
|
107
|
+
* `.output` to define procedure contracts, then compose them into router
|
|
108
|
+
* contracts.
|
|
109
|
+
*
|
|
110
|
+
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
111
|
+
*/
|
|
112
|
+
export const oc = /* @__PURE__ */ ContractBuilder.create();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ClientContext, ClientLink, ORPCClientOptions } from '@orpc/client';
|
|
2
|
+
import type { RouterContractClient } from './router-client.ts';
|
|
3
|
+
import type { RouterContract } from './router.ts';
|
|
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,38 @@
|
|
|
1
|
+
import { createORPCClient } from '@orpc/client';
|
|
2
|
+
import { get, isNonPrimitive, set } from '@orpc/shared';
|
|
3
|
+
import { resolveBasePathMeta } from './meta-built-in.js';
|
|
4
|
+
import { ProcedureContract } from './procedure.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a client factory that builds a client from any procedure or router contract,
|
|
7
|
+
* so large projects can import individual contracts instead of a single root client.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* **Warning**: Every procedure contract passed to the factory must define `meta.path` matching its location in the root contract.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
|
|
13
|
+
*/
|
|
14
|
+
export function createContractClientFactory(link, options = {}) {
|
|
15
|
+
const factory = (contract) => {
|
|
16
|
+
const path = resolveBasePathMeta(contract);
|
|
17
|
+
if (path === undefined) {
|
|
18
|
+
throw new TypeError('ContractClientFactory: procedure contract must define `meta.path` that matches its path in the root router contract.');
|
|
19
|
+
}
|
|
20
|
+
const contractRef = options.contractRef;
|
|
21
|
+
if (contractRef) {
|
|
22
|
+
const register = (contract, path) => {
|
|
23
|
+
if (contract instanceof ProcedureContract) {
|
|
24
|
+
set(contractRef, [...path, '~orpc'], contract['~orpc']);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (isNonPrimitive(contract)) {
|
|
28
|
+
for (const [key, value] of Object.entries(contract)) {
|
|
29
|
+
register(value, [...path, key]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
register(contract, path);
|
|
34
|
+
}
|
|
35
|
+
return createORPCClient(link, { ...options, scoped: get(options.scoped, path), path });
|
|
36
|
+
};
|
|
37
|
+
return factory;
|
|
38
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ORPCErrorCode, ORPCErrorOptions } from '@orpc/client';
|
|
2
|
+
import { ORPCError } from '@orpc/client';
|
|
3
|
+
import type { MaybeOptionalOptions } from '@orpc/shared';
|
|
4
|
+
import type { ErrorMap, ErrorMapItem } from './error.ts';
|
|
5
|
+
import type { AnySchema, InferSchemaInput, Schema } from './schema.ts';
|
|
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<TCode extends ORPCErrorCode, TData> extends ErrorMapItem {
|
|
18
|
+
code: TCode;
|
|
19
|
+
data: Schema<TData>;
|
|
20
|
+
new (...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>): ORPCError<TCode, TData>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a reusable error class ({@link ORPCErrorFactory}) for the given code,
|
|
24
|
+
* default message, and data schema.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const RateLimitedError = error('RATE_LIMITED', {
|
|
29
|
+
* message: 'You are being rate limited',
|
|
30
|
+
* data: z.object({ retryAfter: z.number() }),
|
|
31
|
+
* })
|
|
32
|
+
*
|
|
33
|
+
* const procedure = os
|
|
34
|
+
* .errors({ [RateLimitedError.code]: RateLimitedError })
|
|
35
|
+
* .handler(() => {
|
|
36
|
+
* throw new RateLimitedError({ data: { retryAfter: 60 } })
|
|
37
|
+
* })
|
|
38
|
+
*
|
|
39
|
+
* try {
|
|
40
|
+
* const output = call(procedure)
|
|
41
|
+
* } catch (error) {
|
|
42
|
+
* if (error instanceof RateLimitedError) {
|
|
43
|
+
* console.log(error.data.retryAfter)
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/error-handling#error-factory | Error Handling - Error Factory}
|
|
49
|
+
*/
|
|
50
|
+
export declare function error<TCode extends ORPCErrorCode, TData = unknown>(code: TCode, { data: dataSchema, message }?: ORPCErrorFactoryOptions<TData>): ORPCErrorFactory<TCode, TData>;
|
|
51
|
+
export type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'status'>;
|
|
52
|
+
export interface ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> {
|
|
53
|
+
(...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>): ORPCError<TCode, TInData>;
|
|
54
|
+
}
|
|
55
|
+
export type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
56
|
+
[K in keyof T]: T[K] extends ErrorMapItem ? ORPCErrorConstructorMapItem<K & ORPCErrorCode, T[K]['data'] extends AnySchema ? InferSchemaInput<T[K]['data']> : unknown> : never;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Creates a map of ORPC error constructors.
|
|
60
|
+
*
|
|
61
|
+
* The returned object is a `Proxy` that allows access to arbitrary error codes:
|
|
62
|
+
* - If the code exists in the provided `errorMap`, the corresponding constructor
|
|
63
|
+
* will create a **defined** `ORPCError`.
|
|
64
|
+
* - If the code does not exist, a fallback `ORPCError` constructor is returned.
|
|
65
|
+
*
|
|
66
|
+
* The `in` operator can be used to check whether an error code is explicitly
|
|
67
|
+
* defined in the map.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const errorMap = createORPCErrorConstructorMap({
|
|
72
|
+
* NOT_FOUND: {
|
|
73
|
+
* message: 'Not Found',
|
|
74
|
+
* },
|
|
75
|
+
* })
|
|
76
|
+
*
|
|
77
|
+
* throw errorMap.NOT_FOUND()
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function createORPCErrorConstructorMap<T extends ErrorMap>(errorMap: T): ORPCErrorConstructorMap<T>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { ORPCError, RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
|
|
2
|
+
import { getOwn, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
3
|
+
import { ValidationError } from './error.js';
|
|
4
|
+
import { type } from './schema-utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a reusable error class ({@link ORPCErrorFactory}) for the given code,
|
|
7
|
+
* default message, and data schema.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const RateLimitedError = error('RATE_LIMITED', {
|
|
12
|
+
* message: 'You are being rate limited',
|
|
13
|
+
* data: z.object({ retryAfter: z.number() }),
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* const procedure = os
|
|
17
|
+
* .errors({ [RateLimitedError.code]: RateLimitedError })
|
|
18
|
+
* .handler(() => {
|
|
19
|
+
* throw new RateLimitedError({ data: { retryAfter: 60 } })
|
|
20
|
+
* })
|
|
21
|
+
*
|
|
22
|
+
* try {
|
|
23
|
+
* const output = call(procedure)
|
|
24
|
+
* } catch (error) {
|
|
25
|
+
* if (error instanceof RateLimitedError) {
|
|
26
|
+
* console.log(error.data.retryAfter)
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/error-handling#error-factory | Error Handling - Error Factory}
|
|
32
|
+
*/
|
|
33
|
+
export function error(code, { data: dataSchema, message } = {}) {
|
|
34
|
+
const validateData = (schema, value) => {
|
|
35
|
+
const result = schema['~standard'].validate(value);
|
|
36
|
+
if (result instanceof Promise) {
|
|
37
|
+
throw new TypeError(`Error factory "${code}" does not support async data schemas.`);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
return class extends ORPCError {
|
|
42
|
+
static code = code;
|
|
43
|
+
static data = dataSchema ?? type();
|
|
44
|
+
static message = message;
|
|
45
|
+
constructor(...rest) {
|
|
46
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
47
|
+
let data = options.data;
|
|
48
|
+
if (dataSchema) {
|
|
49
|
+
const result = validateData(dataSchema, options.data);
|
|
50
|
+
if (result.issues) {
|
|
51
|
+
throw new ValidationError({
|
|
52
|
+
message: `Error factory "${code}" data validation failed`,
|
|
53
|
+
issues: result.issues,
|
|
54
|
+
invalidData: options.data,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
data = result.value;
|
|
58
|
+
}
|
|
59
|
+
super(code, { ...options, message: options.message ?? message, data });
|
|
60
|
+
}
|
|
61
|
+
static [Symbol.hasInstance](instance) {
|
|
62
|
+
if (!(instance instanceof ORPCError)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (instance.code !== code) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (dataSchema && validateData(dataSchema, instance.data).issues) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates a map of ORPC error constructors.
|
|
77
|
+
*
|
|
78
|
+
* The returned object is a `Proxy` that allows access to arbitrary error codes:
|
|
79
|
+
* - If the code exists in the provided `errorMap`, the corresponding constructor
|
|
80
|
+
* will create a **defined** `ORPCError`.
|
|
81
|
+
* - If the code does not exist, a fallback `ORPCError` constructor is returned.
|
|
82
|
+
*
|
|
83
|
+
* The `in` operator can be used to check whether an error code is explicitly
|
|
84
|
+
* defined in the map.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const errorMap = createORPCErrorConstructorMap({
|
|
89
|
+
* NOT_FOUND: {
|
|
90
|
+
* message: 'Not Found',
|
|
91
|
+
* },
|
|
92
|
+
* })
|
|
93
|
+
*
|
|
94
|
+
* throw errorMap.NOT_FOUND()
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export function createORPCErrorConstructorMap(errorMap) {
|
|
98
|
+
const proxy = new Proxy(errorMap, {
|
|
99
|
+
get(target, code) {
|
|
100
|
+
if (typeof code !== 'string' || RECURSIVE_CLIENT_UNWRAP_KEYS.has(code)) {
|
|
101
|
+
return Reflect.get(target, code);
|
|
102
|
+
}
|
|
103
|
+
const item = (...rest) => {
|
|
104
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
105
|
+
const config = getOwn(errorMap, code);
|
|
106
|
+
const error = new ORPCError(code, {
|
|
107
|
+
message: options.message ?? config?.message,
|
|
108
|
+
data: options.data,
|
|
109
|
+
cause: options.cause,
|
|
110
|
+
});
|
|
111
|
+
if (config) {
|
|
112
|
+
;
|
|
113
|
+
error.defined = true;
|
|
114
|
+
}
|
|
115
|
+
return error;
|
|
116
|
+
};
|
|
117
|
+
return item;
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
return proxy;
|
|
121
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AnyORPCError } from '@orpc/client';
|
|
2
|
+
import type { ErrorMap } from './error.ts';
|
|
3
|
+
export type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = keyof T1 extends never | keyof T2 ? T2 : Omit<T1, keyof T2> & T2;
|
|
4
|
+
export declare function mergeErrorMap<T1 extends ErrorMap = object, T2 extends ErrorMap = object>(errorMap1: T1 | undefined, errorMap2: T2 | undefined): MergedErrorMap<T1, T2>;
|
|
5
|
+
export declare function reconcileORPCError(map: ErrorMap, error: AnyORPCError): Promise<AnyORPCError>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { cloneORPCError } from '@orpc/client';
|
|
2
|
+
import { getOwn } from '@orpc/shared';
|
|
3
|
+
export function mergeErrorMap(errorMap1, errorMap2) {
|
|
4
|
+
return { ...errorMap1, ...errorMap2 };
|
|
5
|
+
}
|
|
6
|
+
export async function reconcileORPCError(map, error) {
|
|
7
|
+
const config = getOwn(map, error.code);
|
|
8
|
+
if (!config) {
|
|
9
|
+
if (!error.defined) {
|
|
10
|
+
return error;
|
|
11
|
+
}
|
|
12
|
+
const cloned = cloneORPCError(error);
|
|
13
|
+
cloned.defined = false;
|
|
14
|
+
return cloned;
|
|
15
|
+
}
|
|
16
|
+
if (!config.data) {
|
|
17
|
+
if (error.defined) {
|
|
18
|
+
return error;
|
|
19
|
+
}
|
|
20
|
+
const cloned = cloneORPCError(error);
|
|
21
|
+
cloned.defined = true;
|
|
22
|
+
return cloned;
|
|
23
|
+
}
|
|
24
|
+
const validated = await config.data['~standard'].validate(error.data);
|
|
25
|
+
if (validated.issues) {
|
|
26
|
+
if (!error.defined) {
|
|
27
|
+
return error;
|
|
28
|
+
}
|
|
29
|
+
const cloned = cloneORPCError(error);
|
|
30
|
+
cloned.defined = false;
|
|
31
|
+
return cloned;
|
|
32
|
+
}
|
|
33
|
+
if (error.data === validated.value && error.defined) {
|
|
34
|
+
return error;
|
|
35
|
+
}
|
|
36
|
+
const cloned = cloneORPCError(error);
|
|
37
|
+
cloned.data = validated.value;
|
|
38
|
+
cloned.defined = true;
|
|
39
|
+
return cloned;
|
|
40
|
+
}
|