@orpc/contract 0.0.0-next.db1f26d → 0.0.0-next.df024bb
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 +101 -0
- package/dist/index.d.mts +233 -0
- package/dist/index.d.ts +233 -0
- package/dist/index.mjs +240 -0
- package/package.json +14 -14
- package/dist/index.js +0 -179
- package/dist/src/builder.d.ts +0 -13
- package/dist/src/constants.d.ts +0 -3
- package/dist/src/index.d.ts +0 -10
- package/dist/src/procedure.d.ts +0 -47
- package/dist/src/router-builder.d.ts +0 -16
- package/dist/src/router.d.ts +0 -16
- package/dist/src/types.d.ts +0 -8
- package/dist/src/utils.d.ts +0 -4
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<h1></h1>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<a href="https://codecov.io/gh/unnoq/orpc">
|
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/@orpc/contract">
|
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fcontract?logo=npm" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
|
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
|
+
</a>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
|
+
|
|
24
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards, ensuring a smooth and enjoyable developer experience.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Highlights
|
|
29
|
+
|
|
30
|
+
- **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
|
|
31
|
+
- **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
|
|
32
|
+
- **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
|
|
33
|
+
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
|
|
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.
|
|
36
|
+
- **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
|
|
37
|
+
- **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
|
|
38
|
+
- **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
|
|
39
|
+
- **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
|
|
40
|
+
- **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
|
|
41
|
+
- **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
|
|
42
|
+
- **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
|
|
43
|
+
- **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
|
|
44
|
+
- **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
|
|
45
|
+
- **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
|
|
46
|
+
|
|
47
|
+
## Documentation
|
|
48
|
+
|
|
49
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
|
50
|
+
|
|
51
|
+
## Packages
|
|
52
|
+
|
|
53
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
56
|
+
- [@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
|
+
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
58
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
60
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
61
|
+
|
|
62
|
+
## `@orpc/contract`
|
|
63
|
+
|
|
64
|
+
Build your API contract. Read the [documentation](https://orpc.unnoq.com/docs/contract-first/define-contract) for more information.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
export const PlanetSchema = z.object({
|
|
68
|
+
id: z.number().int().min(1),
|
|
69
|
+
name: z.string(),
|
|
70
|
+
description: z.string().optional(),
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
export const listPlanetContract = oc
|
|
74
|
+
.input(
|
|
75
|
+
z.object({
|
|
76
|
+
limit: z.number().int().min(1).max(100).optional(),
|
|
77
|
+
cursor: z.number().int().min(0).default(0),
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
.output(z.array(PlanetSchema))
|
|
81
|
+
|
|
82
|
+
export const findPlanetContract = oc
|
|
83
|
+
.input(PlanetSchema.pick({ id: true }))
|
|
84
|
+
.output(PlanetSchema)
|
|
85
|
+
|
|
86
|
+
export const createPlanetContract = oc
|
|
87
|
+
.input(PlanetSchema.omit({ id: true }))
|
|
88
|
+
.output(PlanetSchema)
|
|
89
|
+
|
|
90
|
+
export const contract = {
|
|
91
|
+
planet: {
|
|
92
|
+
list: listPlanetContract,
|
|
93
|
+
find: findPlanetContract,
|
|
94
|
+
create: createPlanetContract,
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { ORPCErrorCode, ORPCError, ClientContext, Client } from '@orpc/client';
|
|
2
|
+
export { ORPCError } from '@orpc/client';
|
|
3
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
+
import { Promisable, IsEqual } from '@orpc/shared';
|
|
5
|
+
|
|
6
|
+
type Schema = StandardSchemaV1 | undefined;
|
|
7
|
+
type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
|
|
8
|
+
type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
|
|
9
|
+
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>): StandardSchemaV1<TInput, TOutput>;
|
|
11
|
+
|
|
12
|
+
interface ValidationErrorOptions extends ErrorOptions {
|
|
13
|
+
message: string;
|
|
14
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
15
|
+
}
|
|
16
|
+
declare class ValidationError extends Error {
|
|
17
|
+
readonly issues: readonly StandardSchemaV1.Issue[];
|
|
18
|
+
constructor(options: ValidationErrorOptions);
|
|
19
|
+
}
|
|
20
|
+
interface ErrorMapItem<TDataSchema extends Schema> {
|
|
21
|
+
status?: number;
|
|
22
|
+
message?: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
data?: TDataSchema;
|
|
25
|
+
}
|
|
26
|
+
type ErrorMap = {
|
|
27
|
+
[key in ORPCErrorCode]?: ErrorMapItem<Schema>;
|
|
28
|
+
};
|
|
29
|
+
type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
|
|
30
|
+
declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
|
|
31
|
+
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
32
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never;
|
|
33
|
+
}[keyof TErrorMap];
|
|
34
|
+
type ErrorFromErrorMap<TErrorMap extends ErrorMap> = Error | ORPCErrorFromErrorMap<TErrorMap>;
|
|
35
|
+
|
|
36
|
+
type Meta = Record<string, any>;
|
|
37
|
+
declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
|
|
38
|
+
|
|
39
|
+
type HTTPPath = `/${string}`;
|
|
40
|
+
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
41
|
+
type InputStructure = 'compact' | 'detailed';
|
|
42
|
+
type OutputStructure = 'compact' | 'detailed';
|
|
43
|
+
interface Route {
|
|
44
|
+
method?: HTTPMethod;
|
|
45
|
+
path?: HTTPPath;
|
|
46
|
+
summary?: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
deprecated?: boolean;
|
|
49
|
+
tags?: readonly string[];
|
|
50
|
+
/**
|
|
51
|
+
* The status code of the response when the procedure is successful.
|
|
52
|
+
*
|
|
53
|
+
* @default 200
|
|
54
|
+
*/
|
|
55
|
+
successStatus?: number;
|
|
56
|
+
/**
|
|
57
|
+
* The description of the response when the procedure is successful.
|
|
58
|
+
*
|
|
59
|
+
* @default 'OK'
|
|
60
|
+
*/
|
|
61
|
+
successDescription?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
|
|
64
|
+
*
|
|
65
|
+
* @option 'compact'
|
|
66
|
+
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
|
|
67
|
+
*
|
|
68
|
+
* @option 'detailed'
|
|
69
|
+
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
|
|
70
|
+
*
|
|
71
|
+
* Example:
|
|
72
|
+
* ```ts
|
|
73
|
+
* const input = {
|
|
74
|
+
* params: { id: 1 },
|
|
75
|
+
* query: { search: 'hello' },
|
|
76
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
77
|
+
* body: { name: 'John' },
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @default 'compact'
|
|
82
|
+
*/
|
|
83
|
+
inputStructure?: InputStructure;
|
|
84
|
+
/**
|
|
85
|
+
* Determines how the response should be structured based on the output.
|
|
86
|
+
*
|
|
87
|
+
* @option 'compact'
|
|
88
|
+
* Includes only the body data, encoded directly in the response.
|
|
89
|
+
*
|
|
90
|
+
* @option 'detailed'
|
|
91
|
+
* Separates the output into `headers` and `body` fields.
|
|
92
|
+
* - `headers`: Custom headers to merge with the response headers.
|
|
93
|
+
* - `body`: The response data.
|
|
94
|
+
*
|
|
95
|
+
* Example:
|
|
96
|
+
* ```ts
|
|
97
|
+
* const output = {
|
|
98
|
+
* headers: { 'x-custom-header': 'value' },
|
|
99
|
+
* body: { message: 'Hello, world!' },
|
|
100
|
+
* };
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @default 'compact'
|
|
104
|
+
*/
|
|
105
|
+
outputStructure?: OutputStructure;
|
|
106
|
+
}
|
|
107
|
+
declare function mergeRoute(a: Route, b: Route): Route;
|
|
108
|
+
declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
|
|
109
|
+
declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
|
|
110
|
+
declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
|
|
111
|
+
declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
|
|
112
|
+
interface AdaptRouteOptions {
|
|
113
|
+
prefix?: HTTPPath;
|
|
114
|
+
tags?: readonly string[];
|
|
115
|
+
}
|
|
116
|
+
declare function adaptRoute(route: Route, options: AdaptRouteOptions): Route;
|
|
117
|
+
|
|
118
|
+
interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
119
|
+
meta: TMeta;
|
|
120
|
+
route: Route;
|
|
121
|
+
inputSchema: TInputSchema;
|
|
122
|
+
outputSchema: TOutputSchema;
|
|
123
|
+
errorMap: TErrorMap;
|
|
124
|
+
}
|
|
125
|
+
declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
126
|
+
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
127
|
+
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
128
|
+
}
|
|
129
|
+
type AnyContractProcedure = ContractProcedure<any, any, any, any>;
|
|
130
|
+
declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
|
|
131
|
+
|
|
132
|
+
type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
|
|
133
|
+
[k: string]: ContractRouter<TMeta>;
|
|
134
|
+
};
|
|
135
|
+
type AnyContractRouter = ContractRouter<any>;
|
|
136
|
+
type AdaptedContractRouter<TContract extends AnyContractRouter, TErrorMap extends ErrorMap> = {
|
|
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> : {
|
|
146
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
147
|
+
};
|
|
148
|
+
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? SchemaOutput<UOutputSchema> : {
|
|
149
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
150
|
+
};
|
|
151
|
+
type ContractRouterToErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
|
|
152
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? ContractRouterToErrorMap<T[K]> : never;
|
|
153
|
+
}[keyof T];
|
|
154
|
+
type ContractRouterToMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
|
|
155
|
+
|
|
156
|
+
interface ContractProcedureBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
157
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
158
|
+
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
159
|
+
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
160
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
161
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
162
|
+
}
|
|
163
|
+
interface ContractProcedureBuilderWithInput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
164
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
165
|
+
meta(meta: TMeta): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
166
|
+
route(route: Route): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
167
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
168
|
+
}
|
|
169
|
+
interface ContractProcedureBuilderWithOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
170
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
171
|
+
meta(meta: TMeta): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
172
|
+
route(route: Route): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
173
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
174
|
+
}
|
|
175
|
+
interface ContractProcedureBuilderWithInputOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
176
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
177
|
+
meta(meta: TMeta): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
178
|
+
route(route: Route): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
179
|
+
}
|
|
180
|
+
interface ContractRouterBuilder<TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
181
|
+
'~orpc': AdaptContractRouterOptions<TErrorMap>;
|
|
182
|
+
'errors'<U extends ErrorMap>(errors: U): ContractRouterBuilder<MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
183
|
+
'prefix'(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
184
|
+
'tag'(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
185
|
+
'router'<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface ContractBuilderDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, AdaptContractRouterOptions<TErrorMap> {
|
|
189
|
+
}
|
|
190
|
+
declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
191
|
+
'~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
192
|
+
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
193
|
+
/**
|
|
194
|
+
* Reset initial meta
|
|
195
|
+
*/
|
|
196
|
+
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U>;
|
|
197
|
+
/**
|
|
198
|
+
* Reset initial route
|
|
199
|
+
*/
|
|
200
|
+
$route(initialRoute: Route): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
201
|
+
errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
202
|
+
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
203
|
+
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
204
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
205
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
206
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
207
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
208
|
+
router<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
209
|
+
}
|
|
210
|
+
declare const oc: ContractBuilder<undefined, undefined, {}, {}>;
|
|
211
|
+
|
|
212
|
+
interface ContractConfig {
|
|
213
|
+
defaultMethod: HTTPMethod;
|
|
214
|
+
defaultSuccessStatus: number;
|
|
215
|
+
defaultSuccessDescription: string;
|
|
216
|
+
defaultInputStructure: InputStructure;
|
|
217
|
+
defaultOutputStructure: InputStructure;
|
|
218
|
+
}
|
|
219
|
+
declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
220
|
+
|
|
221
|
+
declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: StandardSchemaV1<TYieldIn, TYieldOut>, returns?: StandardSchemaV1<TReturnIn, TReturnOut>): StandardSchemaV1<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
|
|
222
|
+
declare function getEventIteratorSchemaDetails(schema: Schema): undefined | {
|
|
223
|
+
yields: Schema;
|
|
224
|
+
returns: Schema;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
228
|
+
|
|
229
|
+
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
|
+
[K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export { type AdaptContractRouterOptions, type AdaptRouteOptions, type AdaptedContractRouter, type AnyContractProcedure, type AnyContractRouter, ContractBuilder, type ContractBuilderDef, type ContractConfig, ContractProcedure, type ContractProcedureBuilder, type ContractProcedureBuilderWithInput, type ContractProcedureBuilderWithInputOutput, type ContractProcedureBuilderWithOutput, type ContractProcedureClient, type ContractProcedureDef, type ContractRouter, type ContractRouterBuilder, type ContractRouterClient, type ContractRouterToErrorMap, type ContractRouterToMeta, type ErrorFromErrorMap, type ErrorMap, type ErrorMapItem, type HTTPMethod, type HTTPPath, type InferContractRouterInputs, type InferContractRouterOutputs, type InputStructure, type MergedErrorMap, type Meta, type ORPCErrorFromErrorMap, type OutputStructure, type Route, type Schema, type SchemaInput, type SchemaOutput, type TypeRest, ValidationError, type ValidationErrorOptions, adaptContractRouter, adaptRoute, eventIterator, fallbackContractConfig, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { ORPCErrorCode, ORPCError, ClientContext, Client } from '@orpc/client';
|
|
2
|
+
export { ORPCError } from '@orpc/client';
|
|
3
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
+
import { Promisable, IsEqual } from '@orpc/shared';
|
|
5
|
+
|
|
6
|
+
type Schema = StandardSchemaV1 | undefined;
|
|
7
|
+
type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
|
|
8
|
+
type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
|
|
9
|
+
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>): StandardSchemaV1<TInput, TOutput>;
|
|
11
|
+
|
|
12
|
+
interface ValidationErrorOptions extends ErrorOptions {
|
|
13
|
+
message: string;
|
|
14
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
15
|
+
}
|
|
16
|
+
declare class ValidationError extends Error {
|
|
17
|
+
readonly issues: readonly StandardSchemaV1.Issue[];
|
|
18
|
+
constructor(options: ValidationErrorOptions);
|
|
19
|
+
}
|
|
20
|
+
interface ErrorMapItem<TDataSchema extends Schema> {
|
|
21
|
+
status?: number;
|
|
22
|
+
message?: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
data?: TDataSchema;
|
|
25
|
+
}
|
|
26
|
+
type ErrorMap = {
|
|
27
|
+
[key in ORPCErrorCode]?: ErrorMapItem<Schema>;
|
|
28
|
+
};
|
|
29
|
+
type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
|
|
30
|
+
declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
|
|
31
|
+
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
32
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never;
|
|
33
|
+
}[keyof TErrorMap];
|
|
34
|
+
type ErrorFromErrorMap<TErrorMap extends ErrorMap> = Error | ORPCErrorFromErrorMap<TErrorMap>;
|
|
35
|
+
|
|
36
|
+
type Meta = Record<string, any>;
|
|
37
|
+
declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
|
|
38
|
+
|
|
39
|
+
type HTTPPath = `/${string}`;
|
|
40
|
+
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
41
|
+
type InputStructure = 'compact' | 'detailed';
|
|
42
|
+
type OutputStructure = 'compact' | 'detailed';
|
|
43
|
+
interface Route {
|
|
44
|
+
method?: HTTPMethod;
|
|
45
|
+
path?: HTTPPath;
|
|
46
|
+
summary?: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
deprecated?: boolean;
|
|
49
|
+
tags?: readonly string[];
|
|
50
|
+
/**
|
|
51
|
+
* The status code of the response when the procedure is successful.
|
|
52
|
+
*
|
|
53
|
+
* @default 200
|
|
54
|
+
*/
|
|
55
|
+
successStatus?: number;
|
|
56
|
+
/**
|
|
57
|
+
* The description of the response when the procedure is successful.
|
|
58
|
+
*
|
|
59
|
+
* @default 'OK'
|
|
60
|
+
*/
|
|
61
|
+
successDescription?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
|
|
64
|
+
*
|
|
65
|
+
* @option 'compact'
|
|
66
|
+
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
|
|
67
|
+
*
|
|
68
|
+
* @option 'detailed'
|
|
69
|
+
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
|
|
70
|
+
*
|
|
71
|
+
* Example:
|
|
72
|
+
* ```ts
|
|
73
|
+
* const input = {
|
|
74
|
+
* params: { id: 1 },
|
|
75
|
+
* query: { search: 'hello' },
|
|
76
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
77
|
+
* body: { name: 'John' },
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @default 'compact'
|
|
82
|
+
*/
|
|
83
|
+
inputStructure?: InputStructure;
|
|
84
|
+
/**
|
|
85
|
+
* Determines how the response should be structured based on the output.
|
|
86
|
+
*
|
|
87
|
+
* @option 'compact'
|
|
88
|
+
* Includes only the body data, encoded directly in the response.
|
|
89
|
+
*
|
|
90
|
+
* @option 'detailed'
|
|
91
|
+
* Separates the output into `headers` and `body` fields.
|
|
92
|
+
* - `headers`: Custom headers to merge with the response headers.
|
|
93
|
+
* - `body`: The response data.
|
|
94
|
+
*
|
|
95
|
+
* Example:
|
|
96
|
+
* ```ts
|
|
97
|
+
* const output = {
|
|
98
|
+
* headers: { 'x-custom-header': 'value' },
|
|
99
|
+
* body: { message: 'Hello, world!' },
|
|
100
|
+
* };
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @default 'compact'
|
|
104
|
+
*/
|
|
105
|
+
outputStructure?: OutputStructure;
|
|
106
|
+
}
|
|
107
|
+
declare function mergeRoute(a: Route, b: Route): Route;
|
|
108
|
+
declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
|
|
109
|
+
declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
|
|
110
|
+
declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
|
|
111
|
+
declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
|
|
112
|
+
interface AdaptRouteOptions {
|
|
113
|
+
prefix?: HTTPPath;
|
|
114
|
+
tags?: readonly string[];
|
|
115
|
+
}
|
|
116
|
+
declare function adaptRoute(route: Route, options: AdaptRouteOptions): Route;
|
|
117
|
+
|
|
118
|
+
interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
119
|
+
meta: TMeta;
|
|
120
|
+
route: Route;
|
|
121
|
+
inputSchema: TInputSchema;
|
|
122
|
+
outputSchema: TOutputSchema;
|
|
123
|
+
errorMap: TErrorMap;
|
|
124
|
+
}
|
|
125
|
+
declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
126
|
+
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
127
|
+
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
128
|
+
}
|
|
129
|
+
type AnyContractProcedure = ContractProcedure<any, any, any, any>;
|
|
130
|
+
declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
|
|
131
|
+
|
|
132
|
+
type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
|
|
133
|
+
[k: string]: ContractRouter<TMeta>;
|
|
134
|
+
};
|
|
135
|
+
type AnyContractRouter = ContractRouter<any>;
|
|
136
|
+
type AdaptedContractRouter<TContract extends AnyContractRouter, TErrorMap extends ErrorMap> = {
|
|
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> : {
|
|
146
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
147
|
+
};
|
|
148
|
+
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? SchemaOutput<UOutputSchema> : {
|
|
149
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
150
|
+
};
|
|
151
|
+
type ContractRouterToErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
|
|
152
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? ContractRouterToErrorMap<T[K]> : never;
|
|
153
|
+
}[keyof T];
|
|
154
|
+
type ContractRouterToMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
|
|
155
|
+
|
|
156
|
+
interface ContractProcedureBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
157
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
158
|
+
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
159
|
+
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
160
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
161
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
162
|
+
}
|
|
163
|
+
interface ContractProcedureBuilderWithInput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
164
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
165
|
+
meta(meta: TMeta): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
166
|
+
route(route: Route): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
167
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
168
|
+
}
|
|
169
|
+
interface ContractProcedureBuilderWithOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
170
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
171
|
+
meta(meta: TMeta): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
172
|
+
route(route: Route): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
173
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
174
|
+
}
|
|
175
|
+
interface ContractProcedureBuilderWithInputOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
176
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
177
|
+
meta(meta: TMeta): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
178
|
+
route(route: Route): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
179
|
+
}
|
|
180
|
+
interface ContractRouterBuilder<TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
181
|
+
'~orpc': AdaptContractRouterOptions<TErrorMap>;
|
|
182
|
+
'errors'<U extends ErrorMap>(errors: U): ContractRouterBuilder<MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
183
|
+
'prefix'(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
184
|
+
'tag'(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
185
|
+
'router'<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface ContractBuilderDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, AdaptContractRouterOptions<TErrorMap> {
|
|
189
|
+
}
|
|
190
|
+
declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
191
|
+
'~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
192
|
+
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
193
|
+
/**
|
|
194
|
+
* Reset initial meta
|
|
195
|
+
*/
|
|
196
|
+
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U>;
|
|
197
|
+
/**
|
|
198
|
+
* Reset initial route
|
|
199
|
+
*/
|
|
200
|
+
$route(initialRoute: Route): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
201
|
+
errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
202
|
+
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
203
|
+
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
204
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
205
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
206
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
207
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
208
|
+
router<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
209
|
+
}
|
|
210
|
+
declare const oc: ContractBuilder<undefined, undefined, {}, {}>;
|
|
211
|
+
|
|
212
|
+
interface ContractConfig {
|
|
213
|
+
defaultMethod: HTTPMethod;
|
|
214
|
+
defaultSuccessStatus: number;
|
|
215
|
+
defaultSuccessDescription: string;
|
|
216
|
+
defaultInputStructure: InputStructure;
|
|
217
|
+
defaultOutputStructure: InputStructure;
|
|
218
|
+
}
|
|
219
|
+
declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
220
|
+
|
|
221
|
+
declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: StandardSchemaV1<TYieldIn, TYieldOut>, returns?: StandardSchemaV1<TReturnIn, TReturnOut>): StandardSchemaV1<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
|
|
222
|
+
declare function getEventIteratorSchemaDetails(schema: Schema): undefined | {
|
|
223
|
+
yields: Schema;
|
|
224
|
+
returns: Schema;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
228
|
+
|
|
229
|
+
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
|
+
[K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export { type AdaptContractRouterOptions, type AdaptRouteOptions, type AdaptedContractRouter, type AnyContractProcedure, type AnyContractRouter, ContractBuilder, type ContractBuilderDef, type ContractConfig, ContractProcedure, type ContractProcedureBuilder, type ContractProcedureBuilderWithInput, type ContractProcedureBuilderWithInputOutput, type ContractProcedureBuilderWithOutput, type ContractProcedureClient, type ContractProcedureDef, type ContractRouter, type ContractRouterBuilder, type ContractRouterClient, type ContractRouterToErrorMap, type ContractRouterToMeta, type ErrorFromErrorMap, type ErrorMap, type ErrorMapItem, type HTTPMethod, type HTTPPath, type InferContractRouterInputs, type InferContractRouterOutputs, type InputStructure, type MergedErrorMap, type Meta, type ORPCErrorFromErrorMap, type OutputStructure, type Route, type Schema, type SchemaInput, type SchemaOutput, type TypeRest, ValidationError, type ValidationErrorOptions, adaptContractRouter, adaptRoute, eventIterator, fallbackContractConfig, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
|
+
export { ORPCError } from '@orpc/client';
|
|
3
|
+
import { isAsyncIteratorObject } from '@orpc/shared';
|
|
4
|
+
|
|
5
|
+
class ValidationError extends Error {
|
|
6
|
+
issues;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
super(options.message, options);
|
|
9
|
+
this.issues = options.issues;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function mergeErrorMap(errorMap1, errorMap2) {
|
|
13
|
+
return { ...errorMap1, ...errorMap2 };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function mergeMeta(meta1, meta2) {
|
|
17
|
+
return { ...meta1, ...meta2 };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class ContractProcedure {
|
|
21
|
+
"~orpc";
|
|
22
|
+
constructor(def) {
|
|
23
|
+
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
24
|
+
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
25
|
+
}
|
|
26
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
|
|
27
|
+
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
|
|
28
|
+
}
|
|
29
|
+
this["~orpc"] = def;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function isContractProcedure(item) {
|
|
33
|
+
if (item instanceof ContractProcedure) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "inputSchema" in item["~orpc"] && "outputSchema" in item["~orpc"] && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function mergeRoute(a, b) {
|
|
40
|
+
return { ...a, ...b };
|
|
41
|
+
}
|
|
42
|
+
function prefixRoute(route, prefix) {
|
|
43
|
+
if (!route.path) {
|
|
44
|
+
return route;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
...route,
|
|
48
|
+
path: `${prefix}${route.path}`
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function unshiftTagRoute(route, tags) {
|
|
52
|
+
return {
|
|
53
|
+
...route,
|
|
54
|
+
tags: [...tags, ...route.tags ?? []]
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function mergePrefix(a, b) {
|
|
58
|
+
return a ? `${a}${b}` : b;
|
|
59
|
+
}
|
|
60
|
+
function mergeTags(a, b) {
|
|
61
|
+
return a ? [...a, ...b] : b;
|
|
62
|
+
}
|
|
63
|
+
function adaptRoute(route, options) {
|
|
64
|
+
let router = route;
|
|
65
|
+
if (options.prefix) {
|
|
66
|
+
router = prefixRoute(router, options.prefix);
|
|
67
|
+
}
|
|
68
|
+
if (options.tags) {
|
|
69
|
+
router = unshiftTagRoute(router, options.tags);
|
|
70
|
+
}
|
|
71
|
+
return router;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function adaptContractRouter(contract, options) {
|
|
75
|
+
if (isContractProcedure(contract)) {
|
|
76
|
+
const adapted2 = new ContractProcedure({
|
|
77
|
+
...contract["~orpc"],
|
|
78
|
+
errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap),
|
|
79
|
+
route: adaptRoute(contract["~orpc"].route, options)
|
|
80
|
+
});
|
|
81
|
+
return adapted2;
|
|
82
|
+
}
|
|
83
|
+
const adapted = {};
|
|
84
|
+
for (const key in contract) {
|
|
85
|
+
adapted[key] = adaptContractRouter(contract[key], options);
|
|
86
|
+
}
|
|
87
|
+
return adapted;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
class ContractBuilder extends ContractProcedure {
|
|
91
|
+
constructor(def) {
|
|
92
|
+
super(def);
|
|
93
|
+
this["~orpc"].prefix = def.prefix;
|
|
94
|
+
this["~orpc"].tags = def.tags;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Reset initial meta
|
|
98
|
+
*/
|
|
99
|
+
$meta(initialMeta) {
|
|
100
|
+
return new ContractBuilder({
|
|
101
|
+
...this["~orpc"],
|
|
102
|
+
meta: initialMeta
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Reset initial route
|
|
107
|
+
*/
|
|
108
|
+
$route(initialRoute) {
|
|
109
|
+
return new ContractBuilder({
|
|
110
|
+
...this["~orpc"],
|
|
111
|
+
route: initialRoute
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
errors(errors) {
|
|
115
|
+
return new ContractBuilder({
|
|
116
|
+
...this["~orpc"],
|
|
117
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
meta(meta) {
|
|
121
|
+
return new ContractBuilder({
|
|
122
|
+
...this["~orpc"],
|
|
123
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
route(route) {
|
|
127
|
+
return new ContractBuilder({
|
|
128
|
+
...this["~orpc"],
|
|
129
|
+
route: mergeRoute(this["~orpc"].route, route)
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
input(schema) {
|
|
133
|
+
return new ContractBuilder({
|
|
134
|
+
...this["~orpc"],
|
|
135
|
+
inputSchema: schema
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
output(schema) {
|
|
139
|
+
return new ContractBuilder({
|
|
140
|
+
...this["~orpc"],
|
|
141
|
+
outputSchema: schema
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
prefix(prefix) {
|
|
145
|
+
return new ContractBuilder({
|
|
146
|
+
...this["~orpc"],
|
|
147
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
tag(...tags) {
|
|
151
|
+
return new ContractBuilder({
|
|
152
|
+
...this["~orpc"],
|
|
153
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
router(router) {
|
|
157
|
+
return adaptContractRouter(router, this["~orpc"]);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const oc = new ContractBuilder({
|
|
161
|
+
errorMap: {},
|
|
162
|
+
inputSchema: void 0,
|
|
163
|
+
outputSchema: void 0,
|
|
164
|
+
route: {},
|
|
165
|
+
meta: {}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const DEFAULT_CONFIG = {
|
|
169
|
+
defaultMethod: "POST",
|
|
170
|
+
defaultSuccessStatus: 200,
|
|
171
|
+
defaultSuccessDescription: "OK",
|
|
172
|
+
defaultInputStructure: "compact",
|
|
173
|
+
defaultOutputStructure: "compact"
|
|
174
|
+
};
|
|
175
|
+
function fallbackContractConfig(key, value) {
|
|
176
|
+
if (value === void 0) {
|
|
177
|
+
return DEFAULT_CONFIG[key];
|
|
178
|
+
}
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const EVENT_ITERATOR_SCHEMA_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_SCHEMA");
|
|
183
|
+
function eventIterator(yields, returns) {
|
|
184
|
+
return {
|
|
185
|
+
"~standard": {
|
|
186
|
+
[EVENT_ITERATOR_SCHEMA_SYMBOL]: { yields, returns },
|
|
187
|
+
vendor: "orpc",
|
|
188
|
+
version: 1,
|
|
189
|
+
validate(iterator) {
|
|
190
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
191
|
+
return { issues: [{ message: "Expect event iterator", path: [] }] };
|
|
192
|
+
}
|
|
193
|
+
const mapped = mapEventIterator(iterator, {
|
|
194
|
+
async value(value, done) {
|
|
195
|
+
const schema = done ? returns : yields;
|
|
196
|
+
if (!schema) {
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
const result = await schema["~standard"].validate(value);
|
|
200
|
+
if (result.issues) {
|
|
201
|
+
throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
|
|
202
|
+
message: "Event iterator validation failed",
|
|
203
|
+
cause: new ValidationError({
|
|
204
|
+
issues: result.issues,
|
|
205
|
+
message: "Event iterator validation failed"
|
|
206
|
+
})
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return result.value;
|
|
210
|
+
},
|
|
211
|
+
error: async (error) => error
|
|
212
|
+
});
|
|
213
|
+
return { value: mapped };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function getEventIteratorSchemaDetails(schema) {
|
|
219
|
+
if (schema === void 0) {
|
|
220
|
+
return void 0;
|
|
221
|
+
}
|
|
222
|
+
return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function type(...[map]) {
|
|
226
|
+
return {
|
|
227
|
+
"~standard": {
|
|
228
|
+
vendor: "custom",
|
|
229
|
+
version: 1,
|
|
230
|
+
async validate(value) {
|
|
231
|
+
if (map) {
|
|
232
|
+
return { value: await map(value) };
|
|
233
|
+
}
|
|
234
|
+
return { value };
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export { ContractBuilder, ContractProcedure, ValidationError, adaptContractRouter, adaptRoute, eventIterator, fallbackContractConfig, 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.df024bb",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,27 +15,27 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
21
|
-
},
|
|
22
|
-
"./🔒/*": {
|
|
23
|
-
"types": "./dist/src/*.d.ts"
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
24
21
|
}
|
|
25
22
|
},
|
|
26
23
|
"files": [
|
|
27
|
-
"!**/*.map",
|
|
28
|
-
"!**/*.tsbuildinfo",
|
|
29
24
|
"dist"
|
|
30
25
|
],
|
|
31
|
-
"peerDependencies": {
|
|
32
|
-
"zod": ">=3.23.0"
|
|
33
|
-
},
|
|
34
26
|
"dependencies": {
|
|
35
|
-
"@
|
|
27
|
+
"@standard-schema/spec": "^1.0.0",
|
|
28
|
+
"@orpc/client": "0.0.0-next.df024bb",
|
|
29
|
+
"@orpc/standard-server": "0.0.0-next.df024bb",
|
|
30
|
+
"@orpc/shared": "0.0.0-next.df024bb"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"arktype": "2.0.0-rc.26",
|
|
34
|
+
"valibot": "1.0.0-beta.9",
|
|
35
|
+
"zod": "^3.24.1"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
|
-
"build": "
|
|
38
|
+
"build": "unbuild",
|
|
39
39
|
"build:watch": "pnpm run build --watch",
|
|
40
40
|
"type:check": "tsc -b"
|
|
41
41
|
}
|
package/dist/index.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
// src/procedure.ts
|
|
2
|
-
var ContractProcedure = class {
|
|
3
|
-
constructor(zz$cp) {
|
|
4
|
-
this.zz$cp = zz$cp;
|
|
5
|
-
}
|
|
6
|
-
};
|
|
7
|
-
var DecoratedContractProcedure = class _DecoratedContractProcedure extends ContractProcedure {
|
|
8
|
-
static decorate(cp) {
|
|
9
|
-
if (cp instanceof _DecoratedContractProcedure)
|
|
10
|
-
return cp;
|
|
11
|
-
return new _DecoratedContractProcedure(cp.zz$cp);
|
|
12
|
-
}
|
|
13
|
-
route(opts) {
|
|
14
|
-
return new _DecoratedContractProcedure({
|
|
15
|
-
...this.zz$cp,
|
|
16
|
-
...opts,
|
|
17
|
-
method: opts.method,
|
|
18
|
-
path: opts.path
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
prefix(prefix) {
|
|
22
|
-
if (!this.zz$cp.path)
|
|
23
|
-
return this;
|
|
24
|
-
return new _DecoratedContractProcedure({
|
|
25
|
-
...this.zz$cp,
|
|
26
|
-
path: `${prefix}${this.zz$cp.path}`
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
addTags(...tags) {
|
|
30
|
-
if (!tags.length)
|
|
31
|
-
return this;
|
|
32
|
-
return new _DecoratedContractProcedure({
|
|
33
|
-
...this.zz$cp,
|
|
34
|
-
tags: [...this.zz$cp.tags ?? [], ...tags]
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
input(schema, example) {
|
|
38
|
-
return new _DecoratedContractProcedure({
|
|
39
|
-
...this.zz$cp,
|
|
40
|
-
InputSchema: schema,
|
|
41
|
-
inputExample: example
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
output(schema, example) {
|
|
45
|
-
return new _DecoratedContractProcedure({
|
|
46
|
-
...this.zz$cp,
|
|
47
|
-
OutputSchema: schema,
|
|
48
|
-
outputExample: example
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
function isContractProcedure(item) {
|
|
53
|
-
if (item instanceof ContractProcedure)
|
|
54
|
-
return true;
|
|
55
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "zz$cp" in item && typeof item.zz$cp === "object" && item.zz$cp !== null && "InputSchema" in item.zz$cp && "OutputSchema" in item.zz$cp;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/router-builder.ts
|
|
59
|
-
var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
60
|
-
constructor(zz$crb) {
|
|
61
|
-
this.zz$crb = zz$crb;
|
|
62
|
-
if (zz$crb.prefix && zz$crb.prefix.includes("{")) {
|
|
63
|
-
throw new Error('Prefix cannot contain "{" for dynamic routing');
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
prefix(prefix) {
|
|
67
|
-
return new _ContractRouterBuilder({
|
|
68
|
-
...this.zz$crb,
|
|
69
|
-
prefix: `${this.zz$crb.prefix ?? ""}${prefix}`
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
tags(...tags) {
|
|
73
|
-
if (!tags.length)
|
|
74
|
-
return this;
|
|
75
|
-
return new _ContractRouterBuilder({
|
|
76
|
-
...this.zz$crb,
|
|
77
|
-
tags: [...this.zz$crb.tags ?? [], ...tags]
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
router(router) {
|
|
81
|
-
const handled = {};
|
|
82
|
-
for (const key in router) {
|
|
83
|
-
const item = router[key];
|
|
84
|
-
if (isContractProcedure(item)) {
|
|
85
|
-
const decorated = DecoratedContractProcedure.decorate(item).addTags(
|
|
86
|
-
...this.zz$crb.tags ?? []
|
|
87
|
-
);
|
|
88
|
-
handled[key] = this.zz$crb.prefix ? decorated.prefix(this.zz$crb.prefix) : decorated;
|
|
89
|
-
} else {
|
|
90
|
-
handled[key] = this.router(item);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return handled;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// src/builder.ts
|
|
98
|
-
var ContractBuilder = class {
|
|
99
|
-
prefix(prefix) {
|
|
100
|
-
return new ContractRouterBuilder({
|
|
101
|
-
prefix
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
tags(...tags) {
|
|
105
|
-
return new ContractRouterBuilder({
|
|
106
|
-
tags
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
route(opts) {
|
|
110
|
-
return new DecoratedContractProcedure({
|
|
111
|
-
InputSchema: void 0,
|
|
112
|
-
OutputSchema: void 0,
|
|
113
|
-
...opts
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
input(schema, example) {
|
|
117
|
-
return new DecoratedContractProcedure({
|
|
118
|
-
InputSchema: schema,
|
|
119
|
-
inputExample: example,
|
|
120
|
-
OutputSchema: void 0
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
output(schema, example) {
|
|
124
|
-
return new DecoratedContractProcedure({
|
|
125
|
-
InputSchema: void 0,
|
|
126
|
-
OutputSchema: schema,
|
|
127
|
-
outputExample: example
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
router(router) {
|
|
131
|
-
return router;
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
// src/constants.ts
|
|
136
|
-
var ORPC_HEADER = "x-orpc-transformer";
|
|
137
|
-
var ORPC_HEADER_VALUE = "t";
|
|
138
|
-
|
|
139
|
-
// src/router.ts
|
|
140
|
-
function eachContractRouterLeaf(router, callback, prefix = []) {
|
|
141
|
-
for (const key in router) {
|
|
142
|
-
const item = router[key];
|
|
143
|
-
if (isContractProcedure(item)) {
|
|
144
|
-
callback(item, [...prefix, key]);
|
|
145
|
-
} else {
|
|
146
|
-
eachContractRouterLeaf(item, callback, [...prefix, key]);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/utils.ts
|
|
152
|
-
function standardizeHTTPPath(path) {
|
|
153
|
-
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
154
|
-
}
|
|
155
|
-
function prefixHTTPPath(prefix, path) {
|
|
156
|
-
const prefix_ = standardizeHTTPPath(prefix);
|
|
157
|
-
const path_ = standardizeHTTPPath(path);
|
|
158
|
-
if (prefix_ === "/")
|
|
159
|
-
return path_;
|
|
160
|
-
if (path_ === "/")
|
|
161
|
-
return prefix_;
|
|
162
|
-
return `${prefix_}${path_}`;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// src/index.ts
|
|
166
|
-
var oc = new ContractBuilder();
|
|
167
|
-
export {
|
|
168
|
-
ContractBuilder,
|
|
169
|
-
ContractProcedure,
|
|
170
|
-
DecoratedContractProcedure,
|
|
171
|
-
ORPC_HEADER,
|
|
172
|
-
ORPC_HEADER_VALUE,
|
|
173
|
-
eachContractRouterLeaf,
|
|
174
|
-
isContractProcedure,
|
|
175
|
-
oc,
|
|
176
|
-
prefixHTTPPath,
|
|
177
|
-
standardizeHTTPPath
|
|
178
|
-
};
|
|
179
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/builder.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { ContractRouter } from './router';
|
|
2
|
-
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
3
|
-
import { DecoratedContractProcedure, type RouteOptions } from './procedure';
|
|
4
|
-
import { ContractRouterBuilder } from './router-builder';
|
|
5
|
-
export declare class ContractBuilder {
|
|
6
|
-
prefix(prefix: HTTPPath): ContractRouterBuilder;
|
|
7
|
-
tags(...tags: string[]): ContractRouterBuilder;
|
|
8
|
-
route(opts: RouteOptions): DecoratedContractProcedure<undefined, undefined>;
|
|
9
|
-
input<USchema extends Schema>(schema: USchema, example?: SchemaInput<USchema>): DecoratedContractProcedure<USchema, undefined>;
|
|
10
|
-
output<USchema extends Schema>(schema: USchema, example?: SchemaOutput<USchema>): DecoratedContractProcedure<undefined, USchema>;
|
|
11
|
-
router<T extends ContractRouter>(router: T): T;
|
|
12
|
-
}
|
|
13
|
-
//# sourceMappingURL=builder.d.ts.map
|
package/dist/src/constants.d.ts
DELETED
package/dist/src/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/** unnoq */
|
|
2
|
-
import { ContractBuilder } from './builder';
|
|
3
|
-
export * from './builder';
|
|
4
|
-
export * from './constants';
|
|
5
|
-
export * from './procedure';
|
|
6
|
-
export * from './router';
|
|
7
|
-
export * from './types';
|
|
8
|
-
export * from './utils';
|
|
9
|
-
export declare const oc: ContractBuilder;
|
|
10
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/procedure.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { HTTPMethod, HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
2
|
-
export interface RouteOptions {
|
|
3
|
-
method?: HTTPMethod;
|
|
4
|
-
path?: HTTPPath;
|
|
5
|
-
summary?: string;
|
|
6
|
-
description?: string;
|
|
7
|
-
deprecated?: boolean;
|
|
8
|
-
tags?: string[];
|
|
9
|
-
}
|
|
10
|
-
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema> {
|
|
11
|
-
zz$cp: {
|
|
12
|
-
path?: HTTPPath;
|
|
13
|
-
method?: HTTPMethod;
|
|
14
|
-
summary?: string;
|
|
15
|
-
description?: string;
|
|
16
|
-
deprecated?: boolean;
|
|
17
|
-
tags?: string[];
|
|
18
|
-
InputSchema: TInputSchema;
|
|
19
|
-
inputExample?: SchemaOutput<TInputSchema>;
|
|
20
|
-
OutputSchema: TOutputSchema;
|
|
21
|
-
outputExample?: SchemaOutput<TOutputSchema>;
|
|
22
|
-
};
|
|
23
|
-
constructor(zz$cp: {
|
|
24
|
-
path?: HTTPPath;
|
|
25
|
-
method?: HTTPMethod;
|
|
26
|
-
summary?: string;
|
|
27
|
-
description?: string;
|
|
28
|
-
deprecated?: boolean;
|
|
29
|
-
tags?: string[];
|
|
30
|
-
InputSchema: TInputSchema;
|
|
31
|
-
inputExample?: SchemaOutput<TInputSchema>;
|
|
32
|
-
OutputSchema: TOutputSchema;
|
|
33
|
-
outputExample?: SchemaOutput<TOutputSchema>;
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema>;
|
|
37
|
-
export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema> extends ContractProcedure<TInputSchema, TOutputSchema> {
|
|
38
|
-
static decorate<TInputSchema extends Schema, TOutputSchema extends Schema>(cp: ContractProcedure<TInputSchema, TOutputSchema>): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
39
|
-
route(opts: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
40
|
-
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
41
|
-
addTags(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
42
|
-
input<USchema extends Schema>(schema: USchema, example?: SchemaInput<USchema>): DecoratedContractProcedure<USchema, TOutputSchema>;
|
|
43
|
-
output<USchema extends Schema>(schema: USchema, example?: SchemaOutput<USchema>): DecoratedContractProcedure<TInputSchema, USchema>;
|
|
44
|
-
}
|
|
45
|
-
export type WELL_DEFINED_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema>;
|
|
46
|
-
export declare function isContractProcedure(item: unknown): item is WELL_DEFINED_CONTRACT_PROCEDURE;
|
|
47
|
-
//# sourceMappingURL=procedure.d.ts.map
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { ContractRouter, HandledContractRouter } from './router';
|
|
2
|
-
import type { HTTPPath } from './types';
|
|
3
|
-
export declare class ContractRouterBuilder {
|
|
4
|
-
zz$crb: {
|
|
5
|
-
prefix?: HTTPPath;
|
|
6
|
-
tags?: string[];
|
|
7
|
-
};
|
|
8
|
-
constructor(zz$crb: {
|
|
9
|
-
prefix?: HTTPPath;
|
|
10
|
-
tags?: string[];
|
|
11
|
-
});
|
|
12
|
-
prefix(prefix: HTTPPath): ContractRouterBuilder;
|
|
13
|
-
tags(...tags: string[]): ContractRouterBuilder;
|
|
14
|
-
router<T extends ContractRouter>(router: T): HandledContractRouter<T>;
|
|
15
|
-
}
|
|
16
|
-
//# sourceMappingURL=router-builder.d.ts.map
|
package/dist/src/router.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { SchemaInput, SchemaOutput } from './types';
|
|
2
|
-
import { type ContractProcedure, type DecoratedContractProcedure, type WELL_DEFINED_CONTRACT_PROCEDURE } from './procedure';
|
|
3
|
-
export interface ContractRouter {
|
|
4
|
-
[k: string]: ContractProcedure<any, any> | ContractRouter;
|
|
5
|
-
}
|
|
6
|
-
export type HandledContractRouter<TContract extends ContractRouter> = {
|
|
7
|
-
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? DecoratedContractProcedure<UInputSchema, UOutputSchema> : TContract[K] extends ContractRouter ? HandledContractRouter<TContract[K]> : never;
|
|
8
|
-
};
|
|
9
|
-
export declare function eachContractRouterLeaf(router: ContractRouter, callback: (item: WELL_DEFINED_CONTRACT_PROCEDURE, path: string[]) => void, prefix?: string[]): void;
|
|
10
|
-
export type InferContractRouterInputs<T extends ContractRouter> = {
|
|
11
|
-
[K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
12
|
-
};
|
|
13
|
-
export type InferContractRouterOutputs<T extends ContractRouter> = {
|
|
14
|
-
[K in keyof T]: T[K] extends ContractProcedure<any, infer UOutputSchema> ? SchemaOutput<UOutputSchema> : T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
15
|
-
};
|
|
16
|
-
//# sourceMappingURL=router.d.ts.map
|
package/dist/src/types.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { input, output, ZodType } from 'zod';
|
|
2
|
-
export type HTTPPath = `/${string}`;
|
|
3
|
-
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
4
|
-
export type HTTPStatus = number;
|
|
5
|
-
export type Schema = ZodType<any, any, any> | undefined;
|
|
6
|
-
export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends ZodType<any, any, any> ? input<TSchema> : TFallback;
|
|
7
|
-
export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends ZodType<any, any, any> ? output<TSchema> : TFallback;
|
|
8
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/src/utils.d.ts
DELETED