@orpc/contract 0.0.0-next.cba521d → 0.0.0-next.cc4cb21
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.js +194 -304
- package/dist/src/builder-variants.d.ts +38 -0
- package/dist/src/builder.d.ts +30 -14
- package/dist/src/config.d.ts +8 -34
- package/dist/src/error.d.ts +17 -3
- package/dist/src/event-iterator.d.ts +8 -0
- package/dist/src/index.d.ts +6 -9
- package/dist/src/meta.d.ts +3 -0
- package/dist/src/procedure-client.d.ts +4 -5
- package/dist/src/procedure.d.ts +14 -79
- package/dist/src/route.d.ts +79 -0
- package/dist/src/router-client.d.ts +4 -3
- package/dist/src/router.d.ts +25 -8
- package/dist/src/{types.d.ts → schema.d.ts} +4 -7
- package/package.json +6 -4
- package/dist/src/client-utils.d.ts +0 -5
- package/dist/src/client.d.ts +0 -19
- package/dist/src/error-map.d.ts +0 -16
- package/dist/src/error-orpc.d.ts +0 -109
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/router-builder.d.ts +0 -20
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 />
|
|
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.js
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
|
+
// src/error.ts
|
|
2
|
+
var ValidationError = class extends Error {
|
|
3
|
+
issues;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super(options.message, options);
|
|
6
|
+
this.issues = options.issues;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function mergeErrorMap(errorMap1, errorMap2) {
|
|
10
|
+
return { ...errorMap1, ...errorMap2 };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/meta.ts
|
|
14
|
+
function mergeMeta(meta1, meta2) {
|
|
15
|
+
return { ...meta1, ...meta2 };
|
|
16
|
+
}
|
|
17
|
+
|
|
1
18
|
// src/procedure.ts
|
|
2
19
|
var ContractProcedure = class {
|
|
3
|
-
"~type" = "ContractProcedure";
|
|
4
20
|
"~orpc";
|
|
5
21
|
constructor(def) {
|
|
6
22
|
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
7
23
|
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
8
24
|
}
|
|
9
|
-
if (Object.values(def.errorMap
|
|
25
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
|
|
10
26
|
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
|
|
11
27
|
}
|
|
12
28
|
this["~orpc"] = def;
|
|
@@ -16,311 +32,140 @@ function isContractProcedure(item) {
|
|
|
16
32
|
if (item instanceof ContractProcedure) {
|
|
17
33
|
return true;
|
|
18
34
|
}
|
|
19
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~
|
|
35
|
+
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"];
|
|
20
36
|
}
|
|
21
37
|
|
|
22
|
-
// src/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
38
|
+
// src/route.ts
|
|
39
|
+
function mergeRoute(a, b) {
|
|
40
|
+
return { ...a, ...b };
|
|
41
|
+
}
|
|
42
|
+
function prefixRoute(route, prefix) {
|
|
43
|
+
if (!route.path) {
|
|
44
|
+
return route;
|
|
29
45
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
// src/router.ts
|
|
75
|
+
function adaptContractRouter(contract, options) {
|
|
76
|
+
if (isContractProcedure(contract)) {
|
|
77
|
+
const adapted2 = new ContractProcedure({
|
|
78
|
+
...contract["~orpc"],
|
|
79
|
+
errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap),
|
|
80
|
+
route: adaptRoute(contract["~orpc"].route, options)
|
|
37
81
|
});
|
|
82
|
+
return adapted2;
|
|
38
83
|
}
|
|
39
|
-
|
|
40
|
-
|
|
84
|
+
const adapted = {};
|
|
85
|
+
for (const key in contract) {
|
|
86
|
+
adapted[key] = adaptContractRouter(contract[key], options);
|
|
87
|
+
}
|
|
88
|
+
return adapted;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/builder.ts
|
|
92
|
+
var ContractBuilder = class _ContractBuilder extends ContractProcedure {
|
|
93
|
+
constructor(def) {
|
|
94
|
+
super(def);
|
|
95
|
+
this["~orpc"].prefix = def.prefix;
|
|
96
|
+
this["~orpc"].tags = def.tags;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Reset initial meta
|
|
100
|
+
*/
|
|
101
|
+
$meta(initialMeta) {
|
|
102
|
+
return new _ContractBuilder({
|
|
41
103
|
...this["~orpc"],
|
|
42
|
-
|
|
43
|
-
route: {
|
|
44
|
-
...this["~orpc"].route,
|
|
45
|
-
path: `${prefix}${this["~orpc"].route.path}`
|
|
46
|
-
}
|
|
47
|
-
} : void 0
|
|
104
|
+
meta: initialMeta
|
|
48
105
|
});
|
|
49
106
|
}
|
|
50
|
-
|
|
51
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Reset initial route
|
|
109
|
+
*/
|
|
110
|
+
$route(initialRoute) {
|
|
111
|
+
return new _ContractBuilder({
|
|
52
112
|
...this["~orpc"],
|
|
53
|
-
route:
|
|
54
|
-
...this["~orpc"].route,
|
|
55
|
-
tags: [
|
|
56
|
-
...tags,
|
|
57
|
-
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
|
|
58
|
-
]
|
|
59
|
-
}
|
|
113
|
+
route: initialRoute
|
|
60
114
|
});
|
|
61
115
|
}
|
|
62
|
-
|
|
63
|
-
return new
|
|
116
|
+
errors(errors) {
|
|
117
|
+
return new _ContractBuilder({
|
|
64
118
|
...this["~orpc"],
|
|
65
|
-
|
|
66
|
-
inputExample: example
|
|
119
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
67
120
|
});
|
|
68
121
|
}
|
|
69
|
-
|
|
70
|
-
return new
|
|
122
|
+
meta(meta) {
|
|
123
|
+
return new _ContractBuilder({
|
|
71
124
|
...this["~orpc"],
|
|
72
|
-
|
|
73
|
-
outputExample: example
|
|
125
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
74
126
|
});
|
|
75
127
|
}
|
|
76
|
-
|
|
77
|
-
return new
|
|
128
|
+
route(route) {
|
|
129
|
+
return new _ContractBuilder({
|
|
78
130
|
...this["~orpc"],
|
|
79
|
-
|
|
131
|
+
route: mergeRoute(this["~orpc"].route, route)
|
|
80
132
|
});
|
|
81
133
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// src/router-builder.ts
|
|
85
|
-
var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
86
|
-
"~type" = "ContractProcedure";
|
|
87
|
-
"~orpc";
|
|
88
|
-
constructor(def) {
|
|
89
|
-
this["~orpc"] = def;
|
|
90
|
-
}
|
|
91
|
-
prefix(prefix) {
|
|
92
|
-
return new _ContractRouterBuilder({
|
|
134
|
+
input(schema) {
|
|
135
|
+
return new _ContractBuilder({
|
|
93
136
|
...this["~orpc"],
|
|
94
|
-
|
|
137
|
+
inputSchema: schema
|
|
95
138
|
});
|
|
96
139
|
}
|
|
97
|
-
|
|
98
|
-
return new
|
|
140
|
+
output(schema) {
|
|
141
|
+
return new _ContractBuilder({
|
|
99
142
|
...this["~orpc"],
|
|
100
|
-
|
|
143
|
+
outputSchema: schema
|
|
101
144
|
});
|
|
102
145
|
}
|
|
103
|
-
router(router) {
|
|
104
|
-
if (isContractProcedure(router)) {
|
|
105
|
-
let decorated = DecoratedContractProcedure.decorate(router);
|
|
106
|
-
if (this["~orpc"].tags) {
|
|
107
|
-
decorated = decorated.unshiftTag(...this["~orpc"].tags);
|
|
108
|
-
}
|
|
109
|
-
if (this["~orpc"].prefix) {
|
|
110
|
-
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
111
|
-
}
|
|
112
|
-
return decorated;
|
|
113
|
-
}
|
|
114
|
-
const adapted = {};
|
|
115
|
-
for (const key in router) {
|
|
116
|
-
adapted[key] = this.router(router[key]);
|
|
117
|
-
}
|
|
118
|
-
return adapted;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// src/builder.ts
|
|
123
|
-
var ContractBuilder = class {
|
|
124
146
|
prefix(prefix) {
|
|
125
|
-
return new
|
|
126
|
-
|
|
147
|
+
return new _ContractBuilder({
|
|
148
|
+
...this["~orpc"],
|
|
149
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
127
150
|
});
|
|
128
151
|
}
|
|
129
152
|
tag(...tags) {
|
|
130
|
-
return new
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
route(route) {
|
|
135
|
-
return new DecoratedContractProcedure({
|
|
136
|
-
route,
|
|
137
|
-
InputSchema: void 0,
|
|
138
|
-
OutputSchema: void 0,
|
|
139
|
-
errorMap: void 0
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
input(schema, example) {
|
|
143
|
-
return new DecoratedContractProcedure({
|
|
144
|
-
InputSchema: schema,
|
|
145
|
-
inputExample: example,
|
|
146
|
-
OutputSchema: void 0,
|
|
147
|
-
errorMap: void 0
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
output(schema, example) {
|
|
151
|
-
return new DecoratedContractProcedure({
|
|
152
|
-
OutputSchema: schema,
|
|
153
|
-
outputExample: example,
|
|
154
|
-
InputSchema: void 0,
|
|
155
|
-
errorMap: void 0
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
errors(errorMap) {
|
|
159
|
-
return new DecoratedContractProcedure({
|
|
160
|
-
InputSchema: void 0,
|
|
161
|
-
OutputSchema: void 0,
|
|
162
|
-
errorMap
|
|
153
|
+
return new _ContractBuilder({
|
|
154
|
+
...this["~orpc"],
|
|
155
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
|
163
156
|
});
|
|
164
157
|
}
|
|
165
158
|
router(router) {
|
|
166
|
-
return router;
|
|
159
|
+
return adaptContractRouter(router, this["~orpc"]);
|
|
167
160
|
}
|
|
168
161
|
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
},
|
|
177
|
-
UNAUTHORIZED: {
|
|
178
|
-
status: 401,
|
|
179
|
-
message: "Unauthorized"
|
|
180
|
-
},
|
|
181
|
-
FORBIDDEN: {
|
|
182
|
-
status: 403,
|
|
183
|
-
message: "Forbidden"
|
|
184
|
-
},
|
|
185
|
-
NOT_FOUND: {
|
|
186
|
-
status: 404,
|
|
187
|
-
message: "Not Found"
|
|
188
|
-
},
|
|
189
|
-
METHOD_NOT_SUPPORTED: {
|
|
190
|
-
status: 405,
|
|
191
|
-
message: "Method Not Supported"
|
|
192
|
-
},
|
|
193
|
-
NOT_ACCEPTABLE: {
|
|
194
|
-
status: 406,
|
|
195
|
-
message: "Not Acceptable"
|
|
196
|
-
},
|
|
197
|
-
TIMEOUT: {
|
|
198
|
-
status: 408,
|
|
199
|
-
message: "Request Timeout"
|
|
200
|
-
},
|
|
201
|
-
CONFLICT: {
|
|
202
|
-
status: 409,
|
|
203
|
-
message: "Conflict"
|
|
204
|
-
},
|
|
205
|
-
PRECONDITION_FAILED: {
|
|
206
|
-
status: 412,
|
|
207
|
-
message: "Precondition Failed"
|
|
208
|
-
},
|
|
209
|
-
PAYLOAD_TOO_LARGE: {
|
|
210
|
-
status: 413,
|
|
211
|
-
message: "Payload Too Large"
|
|
212
|
-
},
|
|
213
|
-
UNSUPPORTED_MEDIA_TYPE: {
|
|
214
|
-
status: 415,
|
|
215
|
-
message: "Unsupported Media Type"
|
|
216
|
-
},
|
|
217
|
-
UNPROCESSABLE_CONTENT: {
|
|
218
|
-
status: 422,
|
|
219
|
-
message: "Unprocessable Content"
|
|
220
|
-
},
|
|
221
|
-
TOO_MANY_REQUESTS: {
|
|
222
|
-
status: 429,
|
|
223
|
-
message: "Too Many Requests"
|
|
224
|
-
},
|
|
225
|
-
CLIENT_CLOSED_REQUEST: {
|
|
226
|
-
status: 499,
|
|
227
|
-
message: "Client Closed Request"
|
|
228
|
-
},
|
|
229
|
-
INTERNAL_SERVER_ERROR: {
|
|
230
|
-
status: 500,
|
|
231
|
-
message: "Internal Server Error"
|
|
232
|
-
},
|
|
233
|
-
NOT_IMPLEMENTED: {
|
|
234
|
-
status: 501,
|
|
235
|
-
message: "Not Implemented"
|
|
236
|
-
},
|
|
237
|
-
BAD_GATEWAY: {
|
|
238
|
-
status: 502,
|
|
239
|
-
message: "Bad Gateway"
|
|
240
|
-
},
|
|
241
|
-
SERVICE_UNAVAILABLE: {
|
|
242
|
-
status: 503,
|
|
243
|
-
message: "Service Unavailable"
|
|
244
|
-
},
|
|
245
|
-
GATEWAY_TIMEOUT: {
|
|
246
|
-
status: 504,
|
|
247
|
-
message: "Gateway Timeout"
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
function fallbackORPCErrorStatus(code, status) {
|
|
251
|
-
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
252
|
-
}
|
|
253
|
-
function fallbackORPCErrorMessage(code, message) {
|
|
254
|
-
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
255
|
-
}
|
|
256
|
-
var ORPCError = class extends Error {
|
|
257
|
-
defined;
|
|
258
|
-
code;
|
|
259
|
-
status;
|
|
260
|
-
data;
|
|
261
|
-
constructor(options) {
|
|
262
|
-
if (options.status && (options.status < 400 || options.status >= 600)) {
|
|
263
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
264
|
-
}
|
|
265
|
-
const message = fallbackORPCErrorMessage(options.code, options.message);
|
|
266
|
-
super(message, options);
|
|
267
|
-
this.code = options.code;
|
|
268
|
-
this.status = fallbackORPCErrorStatus(options.code, options.status);
|
|
269
|
-
this.defined = options.defined ?? false;
|
|
270
|
-
this.data = options.data;
|
|
271
|
-
}
|
|
272
|
-
toJSON() {
|
|
273
|
-
return {
|
|
274
|
-
defined: this.defined,
|
|
275
|
-
code: this.code,
|
|
276
|
-
status: this.status,
|
|
277
|
-
message: this.message,
|
|
278
|
-
data: this.data
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
static isValidJSON(json) {
|
|
282
|
-
return isPlainObject(json) && "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
function isDefinedError(error) {
|
|
286
|
-
return error instanceof ORPCError && error.defined;
|
|
287
|
-
}
|
|
288
|
-
async function validateORPCError(map, error) {
|
|
289
|
-
const { code, status, message, data, cause, defined } = error;
|
|
290
|
-
const config = map?.[error.code];
|
|
291
|
-
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
|
292
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
293
|
-
}
|
|
294
|
-
if (!config.data) {
|
|
295
|
-
return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause });
|
|
296
|
-
}
|
|
297
|
-
const validated = await config.data["~standard"].validate(error.data);
|
|
298
|
-
if (validated.issues) {
|
|
299
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
300
|
-
}
|
|
301
|
-
return new ORPCError({
|
|
302
|
-
defined: true,
|
|
303
|
-
code,
|
|
304
|
-
status,
|
|
305
|
-
message,
|
|
306
|
-
data: validated.value,
|
|
307
|
-
cause
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// src/client-utils.ts
|
|
312
|
-
async function safe(promise) {
|
|
313
|
-
try {
|
|
314
|
-
const output = await promise;
|
|
315
|
-
return [output, void 0, false];
|
|
316
|
-
} catch (e) {
|
|
317
|
-
const error = e;
|
|
318
|
-
if (isDefinedError(error)) {
|
|
319
|
-
return [void 0, error, true];
|
|
320
|
-
}
|
|
321
|
-
return [void 0, error, false];
|
|
322
|
-
}
|
|
323
|
-
}
|
|
162
|
+
var oc = new ContractBuilder({
|
|
163
|
+
errorMap: {},
|
|
164
|
+
inputSchema: void 0,
|
|
165
|
+
outputSchema: void 0,
|
|
166
|
+
route: {},
|
|
167
|
+
meta: {}
|
|
168
|
+
});
|
|
324
169
|
|
|
325
170
|
// src/config.ts
|
|
326
171
|
var DEFAULT_CONFIG = {
|
|
@@ -330,51 +175,96 @@ var DEFAULT_CONFIG = {
|
|
|
330
175
|
defaultInputStructure: "compact",
|
|
331
176
|
defaultOutputStructure: "compact"
|
|
332
177
|
};
|
|
333
|
-
|
|
334
|
-
function configGlobal(config) {
|
|
335
|
-
if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) {
|
|
336
|
-
throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299");
|
|
337
|
-
}
|
|
338
|
-
GLOBAL_CONFIG_REF.value = config;
|
|
339
|
-
}
|
|
340
|
-
function fallbackToGlobalConfig(key, value) {
|
|
178
|
+
function fallbackContractConfig(key, value) {
|
|
341
179
|
if (value === void 0) {
|
|
342
|
-
|
|
343
|
-
if (fallback === void 0) {
|
|
344
|
-
return DEFAULT_CONFIG[key];
|
|
345
|
-
}
|
|
346
|
-
return fallback;
|
|
180
|
+
return DEFAULT_CONFIG[key];
|
|
347
181
|
}
|
|
348
182
|
return value;
|
|
349
183
|
}
|
|
350
184
|
|
|
351
|
-
// src/
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
185
|
+
// src/event-iterator.ts
|
|
186
|
+
import { mapEventIterator, ORPCError } from "@orpc/client";
|
|
187
|
+
import { isAsyncIteratorObject } from "@orpc/shared";
|
|
188
|
+
var EVENT_ITERATOR_SCHEMA_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_SCHEMA");
|
|
189
|
+
function eventIterator(yields, returns) {
|
|
190
|
+
return {
|
|
191
|
+
"~standard": {
|
|
192
|
+
[EVENT_ITERATOR_SCHEMA_SYMBOL]: { yields, returns },
|
|
193
|
+
vendor: "orpc",
|
|
194
|
+
version: 1,
|
|
195
|
+
validate(iterator) {
|
|
196
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
197
|
+
return { issues: [{ message: "Expect event source iterator", path: [] }] };
|
|
198
|
+
}
|
|
199
|
+
const mapped = mapEventIterator(iterator, {
|
|
200
|
+
async value(value, done) {
|
|
201
|
+
const schema = done ? returns : yields;
|
|
202
|
+
if (!schema) {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
const result = await schema["~standard"].validate(value);
|
|
206
|
+
if (result.issues) {
|
|
207
|
+
throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
|
|
208
|
+
message: "Event source iterator validation failed",
|
|
209
|
+
cause: new ValidationError({
|
|
210
|
+
issues: result.issues,
|
|
211
|
+
message: "Event source iterator validation failed"
|
|
212
|
+
})
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return result.value;
|
|
216
|
+
},
|
|
217
|
+
error: async (error) => error
|
|
218
|
+
});
|
|
219
|
+
return { value: mapped };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function getEventIteratorSchemaDetails(schema) {
|
|
225
|
+
if (schema === void 0) {
|
|
226
|
+
return void 0;
|
|
357
227
|
}
|
|
358
|
-
|
|
228
|
+
return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/schema.ts
|
|
232
|
+
function type(...[map]) {
|
|
233
|
+
return {
|
|
234
|
+
"~standard": {
|
|
235
|
+
vendor: "custom",
|
|
236
|
+
version: 1,
|
|
237
|
+
async validate(value) {
|
|
238
|
+
if (map) {
|
|
239
|
+
return { value: await map(value) };
|
|
240
|
+
}
|
|
241
|
+
return { value };
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
359
246
|
|
|
360
247
|
// src/index.ts
|
|
361
|
-
|
|
248
|
+
import { ORPCError as ORPCError2 } from "@orpc/client";
|
|
362
249
|
export {
|
|
363
|
-
COMMON_ORPC_ERROR_DEFS,
|
|
364
250
|
ContractBuilder,
|
|
365
251
|
ContractProcedure,
|
|
366
|
-
|
|
367
|
-
DecoratedContractProcedure,
|
|
368
|
-
ORPCError,
|
|
252
|
+
ORPCError2 as ORPCError,
|
|
369
253
|
ValidationError,
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
254
|
+
adaptContractRouter,
|
|
255
|
+
adaptRoute,
|
|
256
|
+
eventIterator,
|
|
257
|
+
fallbackContractConfig,
|
|
258
|
+
getEventIteratorSchemaDetails,
|
|
374
259
|
isContractProcedure,
|
|
375
|
-
|
|
260
|
+
mergeErrorMap,
|
|
261
|
+
mergeMeta,
|
|
262
|
+
mergePrefix,
|
|
263
|
+
mergeRoute,
|
|
264
|
+
mergeTags,
|
|
376
265
|
oc,
|
|
377
|
-
|
|
378
|
-
|
|
266
|
+
prefixRoute,
|
|
267
|
+
type,
|
|
268
|
+
unshiftTagRoute
|
|
379
269
|
};
|
|
380
270
|
//# sourceMappingURL=index.js.map
|