@orpc/contract 0.0.0-next.df024bb → 0.0.0-next.e0f01a5
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 +14 -1
- package/dist/index.d.mts +63 -61
- package/dist/index.d.ts +63 -61
- package/dist/index.mjs +38 -26
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
- **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
|
|
33
33
|
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
|
|
34
34
|
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
|
|
35
|
-
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue), Pinia Colada, and more.
|
|
35
|
+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
|
|
36
36
|
- **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
|
|
37
37
|
- **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
|
|
38
38
|
- **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
|
|
@@ -53,11 +53,16 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
53
53
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
54
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
55
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
56
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
56
57
|
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
57
58
|
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
59
|
+
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
60
|
+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
|
58
61
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
62
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
60
63
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
64
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
65
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
66
|
|
|
62
67
|
## `@orpc/contract`
|
|
63
68
|
|
|
@@ -96,6 +101,14 @@ export const contract = {
|
|
|
96
101
|
}
|
|
97
102
|
```
|
|
98
103
|
|
|
104
|
+
## Sponsors
|
|
105
|
+
|
|
106
|
+
<p align="center">
|
|
107
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
108
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
109
|
+
</a>
|
|
110
|
+
</p>
|
|
111
|
+
|
|
99
112
|
## License
|
|
100
113
|
|
|
101
114
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { ORPCErrorCode, ORPCError, ClientContext, Client } from '@orpc/client';
|
|
2
|
-
export { ORPCError } from '@orpc/client';
|
|
1
|
+
import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath, ClientContext, Client } from '@orpc/client';
|
|
2
|
+
export { HTTPMethod, HTTPPath, ORPCError } from '@orpc/client';
|
|
3
|
+
import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
|
|
4
|
+
export { Registry, ThrowableError } from '@orpc/shared';
|
|
3
5
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
-
import { Promisable, IsEqual } from '@orpc/shared';
|
|
5
6
|
|
|
6
|
-
type Schema = StandardSchemaV1
|
|
7
|
-
type
|
|
8
|
-
type
|
|
7
|
+
type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
|
|
8
|
+
type AnySchema = Schema<any, any>;
|
|
9
|
+
type SchemaIssue = StandardSchemaV1.Issue;
|
|
10
|
+
type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
|
|
11
|
+
type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
|
|
9
12
|
type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
10
|
-
declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>):
|
|
13
|
+
declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
|
|
11
14
|
|
|
12
15
|
interface ValidationErrorOptions extends ErrorOptions {
|
|
13
16
|
message: string;
|
|
14
|
-
issues: readonly
|
|
17
|
+
issues: readonly SchemaIssue[];
|
|
15
18
|
}
|
|
16
19
|
declare class ValidationError extends Error {
|
|
17
|
-
readonly issues: readonly
|
|
20
|
+
readonly issues: readonly SchemaIssue[];
|
|
18
21
|
constructor(options: ValidationErrorOptions);
|
|
19
22
|
}
|
|
20
|
-
interface ErrorMapItem<TDataSchema extends
|
|
23
|
+
interface ErrorMapItem<TDataSchema extends AnySchema> {
|
|
21
24
|
status?: number;
|
|
22
25
|
message?: string;
|
|
23
|
-
description?: string;
|
|
24
26
|
data?: TDataSchema;
|
|
25
27
|
}
|
|
26
28
|
type ErrorMap = {
|
|
27
|
-
[key in ORPCErrorCode]?: ErrorMapItem<
|
|
29
|
+
[key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
|
|
28
30
|
};
|
|
29
31
|
type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
|
|
30
32
|
declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
|
|
31
33
|
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
32
|
-
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema
|
|
34
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
|
|
33
35
|
}[keyof TErrorMap];
|
|
34
|
-
type ErrorFromErrorMap<TErrorMap extends ErrorMap> =
|
|
36
|
+
type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
|
|
35
37
|
|
|
36
38
|
type Meta = Record<string, any>;
|
|
37
39
|
declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
|
|
38
40
|
|
|
39
|
-
type HTTPPath = `/${string}`;
|
|
40
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
41
41
|
type InputStructure = 'compact' | 'detailed';
|
|
42
42
|
type OutputStructure = 'compact' | 'detailed';
|
|
43
43
|
interface Route {
|
|
@@ -109,20 +109,20 @@ declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
|
|
|
109
109
|
declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
|
|
110
110
|
declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
|
|
111
111
|
declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
|
|
112
|
-
interface
|
|
112
|
+
interface EnhanceRouteOptions {
|
|
113
113
|
prefix?: HTTPPath;
|
|
114
114
|
tags?: readonly string[];
|
|
115
115
|
}
|
|
116
|
-
declare function
|
|
116
|
+
declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
|
|
117
117
|
|
|
118
|
-
interface ContractProcedureDef<TInputSchema extends
|
|
118
|
+
interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
119
119
|
meta: TMeta;
|
|
120
120
|
route: Route;
|
|
121
|
-
inputSchema
|
|
122
|
-
outputSchema
|
|
121
|
+
inputSchema?: TInputSchema;
|
|
122
|
+
outputSchema?: TOutputSchema;
|
|
123
123
|
errorMap: TErrorMap;
|
|
124
124
|
}
|
|
125
|
-
declare class ContractProcedure<TInputSchema extends
|
|
125
|
+
declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
126
126
|
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
127
127
|
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
128
128
|
}
|
|
@@ -133,67 +133,67 @@ type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta
|
|
|
133
133
|
[k: string]: ContractRouter<TMeta>;
|
|
134
134
|
};
|
|
135
135
|
type AnyContractRouter = ContractRouter<any>;
|
|
136
|
-
type
|
|
137
|
-
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : TContract[K] extends AnyContractRouter ? AdaptedContractRouter<TContract[K], TErrorMap> : never;
|
|
138
|
-
};
|
|
139
|
-
interface AdaptContractRouterOptions<TErrorMap extends ErrorMap> {
|
|
140
|
-
errorMap: TErrorMap;
|
|
141
|
-
prefix?: HTTPPath;
|
|
142
|
-
tags?: readonly string[];
|
|
143
|
-
}
|
|
144
|
-
declare function adaptContractRouter<TRouter extends ContractRouter<any>, TErrorMap extends ErrorMap>(contract: TRouter, options: AdaptContractRouterOptions<TErrorMap>): AdaptedContractRouter<TRouter, TErrorMap>;
|
|
145
|
-
type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? SchemaInput<UInputSchema> : {
|
|
136
|
+
type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
146
137
|
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
147
138
|
};
|
|
148
|
-
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ?
|
|
139
|
+
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
149
140
|
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
150
141
|
};
|
|
151
|
-
type
|
|
152
|
-
[K in keyof T]: T[K] extends AnyContractRouter ?
|
|
142
|
+
type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
|
|
143
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
|
|
153
144
|
}[keyof T];
|
|
154
|
-
type
|
|
145
|
+
type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
|
|
155
146
|
|
|
156
|
-
|
|
147
|
+
declare function getContractRouter(router: AnyContractRouter, path: readonly string[]): AnyContractRouter | undefined;
|
|
148
|
+
type EnhancedContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : {
|
|
149
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? EnhancedContractRouter<T[K], TErrorMap> : never;
|
|
150
|
+
};
|
|
151
|
+
interface EnhanceContractRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
|
152
|
+
errorMap: TErrorMap;
|
|
153
|
+
}
|
|
154
|
+
declare function enhanceContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap>(router: T, options: EnhanceContractRouterOptions<TErrorMap>): EnhancedContractRouter<T, TErrorMap>;
|
|
155
|
+
|
|
156
|
+
interface ContractProcedureBuilder<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
157
157
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
158
158
|
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
159
159
|
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
160
|
-
input<U extends
|
|
161
|
-
output<U extends
|
|
160
|
+
input<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
161
|
+
output<U extends AnySchema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
162
162
|
}
|
|
163
|
-
interface ContractProcedureBuilderWithInput<TInputSchema extends
|
|
163
|
+
interface ContractProcedureBuilderWithInput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
164
164
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
165
165
|
meta(meta: TMeta): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
166
166
|
route(route: Route): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
167
|
-
output<U extends
|
|
167
|
+
output<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInputOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
168
168
|
}
|
|
169
|
-
interface ContractProcedureBuilderWithOutput<TInputSchema extends
|
|
169
|
+
interface ContractProcedureBuilderWithOutput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
170
170
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
171
171
|
meta(meta: TMeta): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
172
172
|
route(route: Route): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
173
|
-
input<U extends
|
|
173
|
+
input<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInputOutput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
174
174
|
}
|
|
175
|
-
interface ContractProcedureBuilderWithInputOutput<TInputSchema extends
|
|
175
|
+
interface ContractProcedureBuilderWithInputOutput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
176
176
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
177
177
|
meta(meta: TMeta): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
178
178
|
route(route: Route): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
179
179
|
}
|
|
180
180
|
interface ContractRouterBuilder<TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
181
|
-
'~orpc':
|
|
181
|
+
'~orpc': EnhanceContractRouterOptions<TErrorMap>;
|
|
182
182
|
'errors'<U extends ErrorMap>(errors: U): ContractRouterBuilder<MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
183
183
|
'prefix'(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
184
184
|
'tag'(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
185
|
-
'router'<T extends ContractRouter<TMeta>>(router: T):
|
|
185
|
+
'router'<T extends ContractRouter<TMeta>>(router: T): EnhancedContractRouter<T, TErrorMap>;
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
interface ContractBuilderDef<TInputSchema extends
|
|
188
|
+
interface ContractBuilderDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, EnhanceContractRouterOptions<TErrorMap> {
|
|
189
189
|
}
|
|
190
|
-
declare class ContractBuilder<TInputSchema extends
|
|
190
|
+
declare class ContractBuilder<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
191
191
|
'~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
192
192
|
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
193
193
|
/**
|
|
194
194
|
* Reset initial meta
|
|
195
195
|
*/
|
|
196
|
-
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U
|
|
196
|
+
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U & Record<never, never>>;
|
|
197
197
|
/**
|
|
198
198
|
* Reset initial route
|
|
199
199
|
*/
|
|
@@ -201,33 +201,35 @@ declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends
|
|
|
201
201
|
errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
202
202
|
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
203
203
|
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
204
|
-
input<U extends
|
|
205
|
-
output<U extends
|
|
204
|
+
input<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
205
|
+
output<U extends AnySchema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
206
206
|
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
207
207
|
tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
208
|
-
router<T extends ContractRouter<TMeta>>(router: T):
|
|
208
|
+
router<T extends ContractRouter<TMeta>>(router: T): EnhancedContractRouter<T, TErrorMap>;
|
|
209
209
|
}
|
|
210
|
-
declare const oc: ContractBuilder<
|
|
210
|
+
declare const oc: ContractBuilder<Schema<unknown, unknown>, Schema<unknown, unknown>, Record<never, never>, Record<never, never>>;
|
|
211
211
|
|
|
212
212
|
interface ContractConfig {
|
|
213
213
|
defaultMethod: HTTPMethod;
|
|
214
214
|
defaultSuccessStatus: number;
|
|
215
215
|
defaultSuccessDescription: string;
|
|
216
216
|
defaultInputStructure: InputStructure;
|
|
217
|
-
defaultOutputStructure:
|
|
217
|
+
defaultOutputStructure: OutputStructure;
|
|
218
218
|
}
|
|
219
219
|
declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
220
220
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
221
|
+
interface EventIteratorSchemaDetails {
|
|
222
|
+
yields: AnySchema;
|
|
223
|
+
returns?: AnySchema;
|
|
224
|
+
}
|
|
225
|
+
declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
|
|
226
|
+
declare function getEventIteratorSchemaDetails(schema: AnySchema | undefined): undefined | EventIteratorSchemaDetails;
|
|
226
227
|
|
|
227
|
-
type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends
|
|
228
|
+
type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
228
229
|
|
|
229
230
|
type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext extends ClientContext = Record<never, never>> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
|
|
230
231
|
[K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
231
232
|
};
|
|
232
233
|
|
|
233
|
-
export {
|
|
234
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
|
|
235
|
+
export type { AnyContractProcedure, AnyContractRouter, AnySchema, ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractProcedureDef, ContractRouter, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhanceRouteOptions, EnhancedContractRouter, ErrorFromErrorMap, ErrorMap, ErrorMapItem, EventIteratorSchemaDetails, InferContractRouterErrorMap, InferContractRouterInputs, InferContractRouterMeta, InferContractRouterOutputs, InferSchemaInput, InferSchemaOutput, InputStructure, MergedErrorMap, Meta, ORPCErrorFromErrorMap, OutputStructure, Route, Schema, SchemaIssue, TypeRest, ValidationErrorOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { ORPCErrorCode, ORPCError, ClientContext, Client } from '@orpc/client';
|
|
2
|
-
export { ORPCError } from '@orpc/client';
|
|
1
|
+
import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath, ClientContext, Client } from '@orpc/client';
|
|
2
|
+
export { HTTPMethod, HTTPPath, ORPCError } from '@orpc/client';
|
|
3
|
+
import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
|
|
4
|
+
export { Registry, ThrowableError } from '@orpc/shared';
|
|
3
5
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
-
import { Promisable, IsEqual } from '@orpc/shared';
|
|
5
6
|
|
|
6
|
-
type Schema = StandardSchemaV1
|
|
7
|
-
type
|
|
8
|
-
type
|
|
7
|
+
type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
|
|
8
|
+
type AnySchema = Schema<any, any>;
|
|
9
|
+
type SchemaIssue = StandardSchemaV1.Issue;
|
|
10
|
+
type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
|
|
11
|
+
type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
|
|
9
12
|
type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
10
|
-
declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>):
|
|
13
|
+
declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
|
|
11
14
|
|
|
12
15
|
interface ValidationErrorOptions extends ErrorOptions {
|
|
13
16
|
message: string;
|
|
14
|
-
issues: readonly
|
|
17
|
+
issues: readonly SchemaIssue[];
|
|
15
18
|
}
|
|
16
19
|
declare class ValidationError extends Error {
|
|
17
|
-
readonly issues: readonly
|
|
20
|
+
readonly issues: readonly SchemaIssue[];
|
|
18
21
|
constructor(options: ValidationErrorOptions);
|
|
19
22
|
}
|
|
20
|
-
interface ErrorMapItem<TDataSchema extends
|
|
23
|
+
interface ErrorMapItem<TDataSchema extends AnySchema> {
|
|
21
24
|
status?: number;
|
|
22
25
|
message?: string;
|
|
23
|
-
description?: string;
|
|
24
26
|
data?: TDataSchema;
|
|
25
27
|
}
|
|
26
28
|
type ErrorMap = {
|
|
27
|
-
[key in ORPCErrorCode]?: ErrorMapItem<
|
|
29
|
+
[key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
|
|
28
30
|
};
|
|
29
31
|
type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
|
|
30
32
|
declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
|
|
31
33
|
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
32
|
-
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema
|
|
34
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
|
|
33
35
|
}[keyof TErrorMap];
|
|
34
|
-
type ErrorFromErrorMap<TErrorMap extends ErrorMap> =
|
|
36
|
+
type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
|
|
35
37
|
|
|
36
38
|
type Meta = Record<string, any>;
|
|
37
39
|
declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
|
|
38
40
|
|
|
39
|
-
type HTTPPath = `/${string}`;
|
|
40
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
41
41
|
type InputStructure = 'compact' | 'detailed';
|
|
42
42
|
type OutputStructure = 'compact' | 'detailed';
|
|
43
43
|
interface Route {
|
|
@@ -109,20 +109,20 @@ declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
|
|
|
109
109
|
declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
|
|
110
110
|
declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
|
|
111
111
|
declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
|
|
112
|
-
interface
|
|
112
|
+
interface EnhanceRouteOptions {
|
|
113
113
|
prefix?: HTTPPath;
|
|
114
114
|
tags?: readonly string[];
|
|
115
115
|
}
|
|
116
|
-
declare function
|
|
116
|
+
declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
|
|
117
117
|
|
|
118
|
-
interface ContractProcedureDef<TInputSchema extends
|
|
118
|
+
interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
119
119
|
meta: TMeta;
|
|
120
120
|
route: Route;
|
|
121
|
-
inputSchema
|
|
122
|
-
outputSchema
|
|
121
|
+
inputSchema?: TInputSchema;
|
|
122
|
+
outputSchema?: TOutputSchema;
|
|
123
123
|
errorMap: TErrorMap;
|
|
124
124
|
}
|
|
125
|
-
declare class ContractProcedure<TInputSchema extends
|
|
125
|
+
declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
126
126
|
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
127
127
|
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
128
128
|
}
|
|
@@ -133,67 +133,67 @@ type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta
|
|
|
133
133
|
[k: string]: ContractRouter<TMeta>;
|
|
134
134
|
};
|
|
135
135
|
type AnyContractRouter = ContractRouter<any>;
|
|
136
|
-
type
|
|
137
|
-
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : TContract[K] extends AnyContractRouter ? AdaptedContractRouter<TContract[K], TErrorMap> : never;
|
|
138
|
-
};
|
|
139
|
-
interface AdaptContractRouterOptions<TErrorMap extends ErrorMap> {
|
|
140
|
-
errorMap: TErrorMap;
|
|
141
|
-
prefix?: HTTPPath;
|
|
142
|
-
tags?: readonly string[];
|
|
143
|
-
}
|
|
144
|
-
declare function adaptContractRouter<TRouter extends ContractRouter<any>, TErrorMap extends ErrorMap>(contract: TRouter, options: AdaptContractRouterOptions<TErrorMap>): AdaptedContractRouter<TRouter, TErrorMap>;
|
|
145
|
-
type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? SchemaInput<UInputSchema> : {
|
|
136
|
+
type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
146
137
|
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
147
138
|
};
|
|
148
|
-
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ?
|
|
139
|
+
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
149
140
|
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
150
141
|
};
|
|
151
|
-
type
|
|
152
|
-
[K in keyof T]: T[K] extends AnyContractRouter ?
|
|
142
|
+
type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
|
|
143
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
|
|
153
144
|
}[keyof T];
|
|
154
|
-
type
|
|
145
|
+
type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
|
|
155
146
|
|
|
156
|
-
|
|
147
|
+
declare function getContractRouter(router: AnyContractRouter, path: readonly string[]): AnyContractRouter | undefined;
|
|
148
|
+
type EnhancedContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : {
|
|
149
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? EnhancedContractRouter<T[K], TErrorMap> : never;
|
|
150
|
+
};
|
|
151
|
+
interface EnhanceContractRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
|
152
|
+
errorMap: TErrorMap;
|
|
153
|
+
}
|
|
154
|
+
declare function enhanceContractRouter<T extends AnyContractRouter, TErrorMap extends ErrorMap>(router: T, options: EnhanceContractRouterOptions<TErrorMap>): EnhancedContractRouter<T, TErrorMap>;
|
|
155
|
+
|
|
156
|
+
interface ContractProcedureBuilder<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
157
157
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
158
158
|
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
159
159
|
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
160
|
-
input<U extends
|
|
161
|
-
output<U extends
|
|
160
|
+
input<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
161
|
+
output<U extends AnySchema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
162
162
|
}
|
|
163
|
-
interface ContractProcedureBuilderWithInput<TInputSchema extends
|
|
163
|
+
interface ContractProcedureBuilderWithInput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
164
164
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
165
165
|
meta(meta: TMeta): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
166
166
|
route(route: Route): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
167
|
-
output<U extends
|
|
167
|
+
output<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInputOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
168
168
|
}
|
|
169
|
-
interface ContractProcedureBuilderWithOutput<TInputSchema extends
|
|
169
|
+
interface ContractProcedureBuilderWithOutput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
170
170
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
171
171
|
meta(meta: TMeta): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
172
172
|
route(route: Route): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
173
|
-
input<U extends
|
|
173
|
+
input<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInputOutput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
174
174
|
}
|
|
175
|
-
interface ContractProcedureBuilderWithInputOutput<TInputSchema extends
|
|
175
|
+
interface ContractProcedureBuilderWithInputOutput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
176
176
|
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
177
177
|
meta(meta: TMeta): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
178
178
|
route(route: Route): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
179
179
|
}
|
|
180
180
|
interface ContractRouterBuilder<TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
181
|
-
'~orpc':
|
|
181
|
+
'~orpc': EnhanceContractRouterOptions<TErrorMap>;
|
|
182
182
|
'errors'<U extends ErrorMap>(errors: U): ContractRouterBuilder<MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
183
183
|
'prefix'(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
184
184
|
'tag'(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
185
|
-
'router'<T extends ContractRouter<TMeta>>(router: T):
|
|
185
|
+
'router'<T extends ContractRouter<TMeta>>(router: T): EnhancedContractRouter<T, TErrorMap>;
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
interface ContractBuilderDef<TInputSchema extends
|
|
188
|
+
interface ContractBuilderDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, EnhanceContractRouterOptions<TErrorMap> {
|
|
189
189
|
}
|
|
190
|
-
declare class ContractBuilder<TInputSchema extends
|
|
190
|
+
declare class ContractBuilder<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
191
191
|
'~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
192
192
|
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
193
193
|
/**
|
|
194
194
|
* Reset initial meta
|
|
195
195
|
*/
|
|
196
|
-
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U
|
|
196
|
+
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U & Record<never, never>>;
|
|
197
197
|
/**
|
|
198
198
|
* Reset initial route
|
|
199
199
|
*/
|
|
@@ -201,33 +201,35 @@ declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends
|
|
|
201
201
|
errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
202
202
|
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
203
203
|
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
204
|
-
input<U extends
|
|
205
|
-
output<U extends
|
|
204
|
+
input<U extends AnySchema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
205
|
+
output<U extends AnySchema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
206
206
|
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
207
207
|
tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
208
|
-
router<T extends ContractRouter<TMeta>>(router: T):
|
|
208
|
+
router<T extends ContractRouter<TMeta>>(router: T): EnhancedContractRouter<T, TErrorMap>;
|
|
209
209
|
}
|
|
210
|
-
declare const oc: ContractBuilder<
|
|
210
|
+
declare const oc: ContractBuilder<Schema<unknown, unknown>, Schema<unknown, unknown>, Record<never, never>, Record<never, never>>;
|
|
211
211
|
|
|
212
212
|
interface ContractConfig {
|
|
213
213
|
defaultMethod: HTTPMethod;
|
|
214
214
|
defaultSuccessStatus: number;
|
|
215
215
|
defaultSuccessDescription: string;
|
|
216
216
|
defaultInputStructure: InputStructure;
|
|
217
|
-
defaultOutputStructure:
|
|
217
|
+
defaultOutputStructure: OutputStructure;
|
|
218
218
|
}
|
|
219
219
|
declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
220
220
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
221
|
+
interface EventIteratorSchemaDetails {
|
|
222
|
+
yields: AnySchema;
|
|
223
|
+
returns?: AnySchema;
|
|
224
|
+
}
|
|
225
|
+
declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
|
|
226
|
+
declare function getEventIteratorSchemaDetails(schema: AnySchema | undefined): undefined | EventIteratorSchemaDetails;
|
|
226
227
|
|
|
227
|
-
type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends
|
|
228
|
+
type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
228
229
|
|
|
229
230
|
type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext extends ClientContext = Record<never, never>> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
|
|
230
231
|
[K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
231
232
|
};
|
|
232
233
|
|
|
233
|
-
export {
|
|
234
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
|
|
235
|
+
export type { AnyContractProcedure, AnyContractRouter, AnySchema, ContractBuilderDef, ContractConfig, ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithInputOutput, ContractProcedureBuilderWithOutput, ContractProcedureClient, ContractProcedureDef, ContractRouter, ContractRouterBuilder, ContractRouterClient, EnhanceContractRouterOptions, EnhanceRouteOptions, EnhancedContractRouter, ErrorFromErrorMap, ErrorMap, ErrorMapItem, EventIteratorSchemaDetails, InferContractRouterErrorMap, InferContractRouterInputs, InferContractRouterMeta, InferContractRouterOutputs, InferSchemaInput, InferSchemaOutput, InputStructure, MergedErrorMap, Meta, ORPCErrorFromErrorMap, OutputStructure, Route, Schema, SchemaIssue, TypeRest, ValidationErrorOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
1
|
+
import { isORPCErrorStatus, mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
2
|
export { ORPCError } from '@orpc/client';
|
|
3
3
|
import { isAsyncIteratorObject } from '@orpc/shared';
|
|
4
4
|
|
|
@@ -20,11 +20,11 @@ function mergeMeta(meta1, meta2) {
|
|
|
20
20
|
class ContractProcedure {
|
|
21
21
|
"~orpc";
|
|
22
22
|
constructor(def) {
|
|
23
|
-
if (def.route?.successStatus && (def.route.successStatus
|
|
24
|
-
throw new Error("[ContractProcedure]
|
|
23
|
+
if (def.route?.successStatus && isORPCErrorStatus(def.route.successStatus)) {
|
|
24
|
+
throw new Error("[ContractProcedure] Invalid successStatus.");
|
|
25
25
|
}
|
|
26
|
-
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status
|
|
27
|
-
throw new Error("[ContractProcedure]
|
|
26
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
|
|
27
|
+
throw new Error("[ContractProcedure] Invalid error status code.");
|
|
28
28
|
}
|
|
29
29
|
this["~orpc"] = def;
|
|
30
30
|
}
|
|
@@ -33,7 +33,7 @@ function isContractProcedure(item) {
|
|
|
33
33
|
if (item instanceof ContractProcedure) {
|
|
34
34
|
return true;
|
|
35
35
|
}
|
|
36
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "
|
|
36
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
function mergeRoute(a, b) {
|
|
@@ -60,31 +60,45 @@ function mergePrefix(a, b) {
|
|
|
60
60
|
function mergeTags(a, b) {
|
|
61
61
|
return a ? [...a, ...b] : b;
|
|
62
62
|
}
|
|
63
|
-
function
|
|
63
|
+
function enhanceRoute(route, options) {
|
|
64
64
|
let router = route;
|
|
65
65
|
if (options.prefix) {
|
|
66
66
|
router = prefixRoute(router, options.prefix);
|
|
67
67
|
}
|
|
68
|
-
if (options.tags) {
|
|
68
|
+
if (options.tags?.length) {
|
|
69
69
|
router = unshiftTagRoute(router, options.tags);
|
|
70
70
|
}
|
|
71
71
|
return router;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
74
|
+
function getContractRouter(router, path) {
|
|
75
|
+
let current = router;
|
|
76
|
+
for (let i = 0; i < path.length; i++) {
|
|
77
|
+
const segment = path[i];
|
|
78
|
+
if (!current) {
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
if (isContractProcedure(current)) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
current = current[segment];
|
|
85
|
+
}
|
|
86
|
+
return current;
|
|
87
|
+
}
|
|
88
|
+
function enhanceContractRouter(router, options) {
|
|
89
|
+
if (isContractProcedure(router)) {
|
|
90
|
+
const enhanced2 = new ContractProcedure({
|
|
91
|
+
...router["~orpc"],
|
|
92
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
93
|
+
route: enhanceRoute(router["~orpc"].route, options)
|
|
80
94
|
});
|
|
81
|
-
return
|
|
95
|
+
return enhanced2;
|
|
82
96
|
}
|
|
83
|
-
const
|
|
84
|
-
for (const key in
|
|
85
|
-
|
|
97
|
+
const enhanced = {};
|
|
98
|
+
for (const key in router) {
|
|
99
|
+
enhanced[key] = enhanceContractRouter(router[key], options);
|
|
86
100
|
}
|
|
87
|
-
return
|
|
101
|
+
return enhanced;
|
|
88
102
|
}
|
|
89
103
|
|
|
90
104
|
class ContractBuilder extends ContractProcedure {
|
|
@@ -154,13 +168,11 @@ class ContractBuilder extends ContractProcedure {
|
|
|
154
168
|
});
|
|
155
169
|
}
|
|
156
170
|
router(router) {
|
|
157
|
-
return
|
|
171
|
+
return enhanceContractRouter(router, this["~orpc"]);
|
|
158
172
|
}
|
|
159
173
|
}
|
|
160
174
|
const oc = new ContractBuilder({
|
|
161
175
|
errorMap: {},
|
|
162
|
-
inputSchema: void 0,
|
|
163
|
-
outputSchema: void 0,
|
|
164
176
|
route: {},
|
|
165
177
|
meta: {}
|
|
166
178
|
});
|
|
@@ -179,11 +191,11 @@ function fallbackContractConfig(key, value) {
|
|
|
179
191
|
return value;
|
|
180
192
|
}
|
|
181
193
|
|
|
182
|
-
const
|
|
194
|
+
const EVENT_ITERATOR_DETAILS_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_DETAILS");
|
|
183
195
|
function eventIterator(yields, returns) {
|
|
184
196
|
return {
|
|
185
197
|
"~standard": {
|
|
186
|
-
[
|
|
198
|
+
[EVENT_ITERATOR_DETAILS_SYMBOL]: { yields, returns },
|
|
187
199
|
vendor: "orpc",
|
|
188
200
|
version: 1,
|
|
189
201
|
validate(iterator) {
|
|
@@ -219,7 +231,7 @@ function getEventIteratorSchemaDetails(schema) {
|
|
|
219
231
|
if (schema === void 0) {
|
|
220
232
|
return void 0;
|
|
221
233
|
}
|
|
222
|
-
return schema["~standard"][
|
|
234
|
+
return schema["~standard"][EVENT_ITERATOR_DETAILS_SYMBOL];
|
|
223
235
|
}
|
|
224
236
|
|
|
225
237
|
function type(...[map]) {
|
|
@@ -237,4 +249,4 @@ function type(...[map]) {
|
|
|
237
249
|
};
|
|
238
250
|
}
|
|
239
251
|
|
|
240
|
-
export { ContractBuilder, ContractProcedure, ValidationError,
|
|
252
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.e0f01a5",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -25,14 +25,13 @@
|
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@standard-schema/spec": "^1.0.0",
|
|
28
|
-
"@orpc/client": "0.0.0-next.
|
|
29
|
-
"@orpc/
|
|
30
|
-
"@orpc/shared": "0.0.0-next.df024bb"
|
|
28
|
+
"@orpc/client": "0.0.0-next.e0f01a5",
|
|
29
|
+
"@orpc/shared": "0.0.0-next.e0f01a5"
|
|
31
30
|
},
|
|
32
31
|
"devDependencies": {
|
|
33
|
-
"arktype": "2.
|
|
34
|
-
"valibot": "1.0.0
|
|
35
|
-
"zod": "^3.24.
|
|
32
|
+
"arktype": "2.1.19",
|
|
33
|
+
"valibot": "1.0.0",
|
|
34
|
+
"zod": "^3.24.2"
|
|
36
35
|
},
|
|
37
36
|
"scripts": {
|
|
38
37
|
"build": "unbuild",
|