@orpc/contract 0.0.0-next.e7ee5a9 → 0.0.0-next.e82d760
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 +28 -20
- package/dist/index.d.mts +375 -68
- package/dist/index.d.ts +375 -68
- package/dist/index.mjs +147 -32
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
1
|
+
import { isORPCErrorStatus, mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
2
|
export { ORPCError } from '@orpc/client';
|
|
3
|
-
import { isAsyncIteratorObject } from '@orpc/shared';
|
|
3
|
+
import { isAsyncIteratorObject, get, isTypescriptObject, isPropertyKey } from '@orpc/shared';
|
|
4
4
|
|
|
5
5
|
class ValidationError extends Error {
|
|
6
6
|
issues;
|
|
@@ -18,13 +18,16 @@ function mergeMeta(meta1, meta2) {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
class ContractProcedure {
|
|
21
|
+
/**
|
|
22
|
+
* This property holds the defined options for the contract procedure.
|
|
23
|
+
*/
|
|
21
24
|
"~orpc";
|
|
22
25
|
constructor(def) {
|
|
23
|
-
if (def.route?.successStatus && (def.route.successStatus
|
|
24
|
-
throw new Error("[ContractProcedure]
|
|
26
|
+
if (def.route?.successStatus && isORPCErrorStatus(def.route.successStatus)) {
|
|
27
|
+
throw new Error("[ContractProcedure] Invalid successStatus.");
|
|
25
28
|
}
|
|
26
|
-
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status
|
|
27
|
-
throw new Error("[ContractProcedure]
|
|
29
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
|
|
30
|
+
throw new Error("[ContractProcedure] Invalid error status code.");
|
|
28
31
|
}
|
|
29
32
|
this["~orpc"] = def;
|
|
30
33
|
}
|
|
@@ -33,7 +36,7 @@ function isContractProcedure(item) {
|
|
|
33
36
|
if (item instanceof ContractProcedure) {
|
|
34
37
|
return true;
|
|
35
38
|
}
|
|
36
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "
|
|
39
|
+
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
40
|
}
|
|
38
41
|
|
|
39
42
|
function mergeRoute(a, b) {
|
|
@@ -60,31 +63,62 @@ function mergePrefix(a, b) {
|
|
|
60
63
|
function mergeTags(a, b) {
|
|
61
64
|
return a ? [...a, ...b] : b;
|
|
62
65
|
}
|
|
63
|
-
function
|
|
66
|
+
function enhanceRoute(route, options) {
|
|
64
67
|
let router = route;
|
|
65
68
|
if (options.prefix) {
|
|
66
69
|
router = prefixRoute(router, options.prefix);
|
|
67
70
|
}
|
|
68
|
-
if (options.tags) {
|
|
71
|
+
if (options.tags?.length) {
|
|
69
72
|
router = unshiftTagRoute(router, options.tags);
|
|
70
73
|
}
|
|
71
74
|
return router;
|
|
72
75
|
}
|
|
73
76
|
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
function getContractRouter(router, path) {
|
|
78
|
+
let current = router;
|
|
79
|
+
for (let i = 0; i < path.length; i++) {
|
|
80
|
+
const segment = path[i];
|
|
81
|
+
if (!current) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
if (isContractProcedure(current)) {
|
|
85
|
+
return void 0;
|
|
86
|
+
}
|
|
87
|
+
current = current[segment];
|
|
88
|
+
}
|
|
89
|
+
return current;
|
|
90
|
+
}
|
|
91
|
+
function enhanceContractRouter(router, options) {
|
|
92
|
+
if (isContractProcedure(router)) {
|
|
93
|
+
const enhanced2 = new ContractProcedure({
|
|
94
|
+
...router["~orpc"],
|
|
95
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
96
|
+
route: enhanceRoute(router["~orpc"].route, options)
|
|
80
97
|
});
|
|
81
|
-
return
|
|
98
|
+
return enhanced2;
|
|
99
|
+
}
|
|
100
|
+
const enhanced = {};
|
|
101
|
+
for (const key in router) {
|
|
102
|
+
enhanced[key] = enhanceContractRouter(router[key], options);
|
|
103
|
+
}
|
|
104
|
+
return enhanced;
|
|
105
|
+
}
|
|
106
|
+
function minifyContractRouter(router) {
|
|
107
|
+
if (isContractProcedure(router)) {
|
|
108
|
+
const procedure = {
|
|
109
|
+
"~orpc": {
|
|
110
|
+
errorMap: {},
|
|
111
|
+
meta: router["~orpc"].meta,
|
|
112
|
+
route: router["~orpc"].route
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
return procedure;
|
|
82
116
|
}
|
|
83
|
-
const
|
|
84
|
-
for (const key in
|
|
85
|
-
|
|
117
|
+
const json = {};
|
|
118
|
+
for (const key in router) {
|
|
119
|
+
json[key] = minifyContractRouter(router[key]);
|
|
86
120
|
}
|
|
87
|
-
return
|
|
121
|
+
return json;
|
|
88
122
|
}
|
|
89
123
|
|
|
90
124
|
class ContractBuilder extends ContractProcedure {
|
|
@@ -94,7 +128,9 @@ class ContractBuilder extends ContractProcedure {
|
|
|
94
128
|
this["~orpc"].tags = def.tags;
|
|
95
129
|
}
|
|
96
130
|
/**
|
|
97
|
-
*
|
|
131
|
+
* Sets or overrides the initial meta.
|
|
132
|
+
*
|
|
133
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
98
134
|
*/
|
|
99
135
|
$meta(initialMeta) {
|
|
100
136
|
return new ContractBuilder({
|
|
@@ -103,7 +139,11 @@ class ContractBuilder extends ContractProcedure {
|
|
|
103
139
|
});
|
|
104
140
|
}
|
|
105
141
|
/**
|
|
106
|
-
*
|
|
142
|
+
* Sets or overrides the initial route.
|
|
143
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
144
|
+
*
|
|
145
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
146
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
107
147
|
*/
|
|
108
148
|
$route(initialRoute) {
|
|
109
149
|
return new ContractBuilder({
|
|
@@ -111,56 +151,103 @@ class ContractBuilder extends ContractProcedure {
|
|
|
111
151
|
route: initialRoute
|
|
112
152
|
});
|
|
113
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Adds type-safe custom errors to the contract.
|
|
156
|
+
* The provided errors are spared-merged with any existing errors in the contract.
|
|
157
|
+
*
|
|
158
|
+
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
159
|
+
*/
|
|
114
160
|
errors(errors) {
|
|
115
161
|
return new ContractBuilder({
|
|
116
162
|
...this["~orpc"],
|
|
117
163
|
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
118
164
|
});
|
|
119
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Sets or updates the metadata for the contract.
|
|
168
|
+
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
169
|
+
*
|
|
170
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
171
|
+
*/
|
|
120
172
|
meta(meta) {
|
|
121
173
|
return new ContractBuilder({
|
|
122
174
|
...this["~orpc"],
|
|
123
175
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
124
176
|
});
|
|
125
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Sets or updates the route definition for the contract.
|
|
180
|
+
* The provided route is spared-merged with any existing route in the contract.
|
|
181
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
182
|
+
*
|
|
183
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
184
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
185
|
+
*/
|
|
126
186
|
route(route) {
|
|
127
187
|
return new ContractBuilder({
|
|
128
188
|
...this["~orpc"],
|
|
129
189
|
route: mergeRoute(this["~orpc"].route, route)
|
|
130
190
|
});
|
|
131
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Defines the input validation schema for the contract.
|
|
194
|
+
*
|
|
195
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
|
|
196
|
+
*/
|
|
132
197
|
input(schema) {
|
|
133
198
|
return new ContractBuilder({
|
|
134
199
|
...this["~orpc"],
|
|
135
200
|
inputSchema: schema
|
|
136
201
|
});
|
|
137
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Defines the output validation schema for the contract.
|
|
205
|
+
*
|
|
206
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
|
|
207
|
+
*/
|
|
138
208
|
output(schema) {
|
|
139
209
|
return new ContractBuilder({
|
|
140
210
|
...this["~orpc"],
|
|
141
211
|
outputSchema: schema
|
|
142
212
|
});
|
|
143
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Prefixes all procedures in the contract router.
|
|
216
|
+
* The provided prefix is post-appended to any existing router prefix.
|
|
217
|
+
*
|
|
218
|
+
* @note This option does not affect procedures that do not define a path in their route definition.
|
|
219
|
+
*
|
|
220
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
221
|
+
*/
|
|
144
222
|
prefix(prefix) {
|
|
145
223
|
return new ContractBuilder({
|
|
146
224
|
...this["~orpc"],
|
|
147
225
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
148
226
|
});
|
|
149
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Adds tags to all procedures in the contract router.
|
|
230
|
+
* This helpful when you want to group procedures together in the OpenAPI specification.
|
|
231
|
+
*
|
|
232
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
233
|
+
*/
|
|
150
234
|
tag(...tags) {
|
|
151
235
|
return new ContractBuilder({
|
|
152
236
|
...this["~orpc"],
|
|
153
237
|
tags: mergeTags(this["~orpc"].tags, tags)
|
|
154
238
|
});
|
|
155
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Applies all of the previously defined options to the specified contract router.
|
|
242
|
+
*
|
|
243
|
+
* @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
|
|
244
|
+
*/
|
|
156
245
|
router(router) {
|
|
157
|
-
return
|
|
246
|
+
return enhanceContractRouter(router, this["~orpc"]);
|
|
158
247
|
}
|
|
159
248
|
}
|
|
160
249
|
const oc = new ContractBuilder({
|
|
161
250
|
errorMap: {},
|
|
162
|
-
inputSchema: void 0,
|
|
163
|
-
outputSchema: void 0,
|
|
164
251
|
route: {},
|
|
165
252
|
meta: {}
|
|
166
253
|
});
|
|
@@ -179,16 +266,16 @@ function fallbackContractConfig(key, value) {
|
|
|
179
266
|
return value;
|
|
180
267
|
}
|
|
181
268
|
|
|
182
|
-
const
|
|
269
|
+
const EVENT_ITERATOR_DETAILS_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_DETAILS");
|
|
183
270
|
function eventIterator(yields, returns) {
|
|
184
271
|
return {
|
|
185
272
|
"~standard": {
|
|
186
|
-
[
|
|
273
|
+
[EVENT_ITERATOR_DETAILS_SYMBOL]: { yields, returns },
|
|
187
274
|
vendor: "orpc",
|
|
188
275
|
version: 1,
|
|
189
276
|
validate(iterator) {
|
|
190
277
|
if (!isAsyncIteratorObject(iterator)) {
|
|
191
|
-
return { issues: [{ message: "Expect event
|
|
278
|
+
return { issues: [{ message: "Expect event iterator", path: [] }] };
|
|
192
279
|
}
|
|
193
280
|
const mapped = mapEventIterator(iterator, {
|
|
194
281
|
async value(value, done) {
|
|
@@ -199,10 +286,10 @@ function eventIterator(yields, returns) {
|
|
|
199
286
|
const result = await schema["~standard"].validate(value);
|
|
200
287
|
if (result.issues) {
|
|
201
288
|
throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
|
|
202
|
-
message: "Event
|
|
289
|
+
message: "Event iterator validation failed",
|
|
203
290
|
cause: new ValidationError({
|
|
204
291
|
issues: result.issues,
|
|
205
|
-
message: "Event
|
|
292
|
+
message: "Event iterator validation failed"
|
|
206
293
|
})
|
|
207
294
|
});
|
|
208
295
|
}
|
|
@@ -219,7 +306,20 @@ function getEventIteratorSchemaDetails(schema) {
|
|
|
219
306
|
if (schema === void 0) {
|
|
220
307
|
return void 0;
|
|
221
308
|
}
|
|
222
|
-
return schema["~standard"][
|
|
309
|
+
return schema["~standard"][EVENT_ITERATOR_DETAILS_SYMBOL];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function inferRPCMethodFromContractRouter(contract) {
|
|
313
|
+
return (_, path) => {
|
|
314
|
+
const procedure = get(contract, path);
|
|
315
|
+
if (!isContractProcedure(procedure)) {
|
|
316
|
+
throw new Error(
|
|
317
|
+
`[inferRPCMethodFromContractRouter] No valid procedure found at path "${path.join(".")}". This may happen when the contract router is not properly configured.`
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
321
|
+
return method === "HEAD" ? "GET" : method;
|
|
322
|
+
};
|
|
223
323
|
}
|
|
224
324
|
|
|
225
325
|
function type(...[map]) {
|
|
@@ -237,4 +337,19 @@ function type(...[map]) {
|
|
|
237
337
|
};
|
|
238
338
|
}
|
|
239
339
|
|
|
240
|
-
|
|
340
|
+
function isSchemaIssue(issue) {
|
|
341
|
+
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
if (issue.path !== void 0) {
|
|
345
|
+
if (!Array.isArray(issue.path)) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
if (!issue.path.every((segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key))) {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return true;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, 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.e82d760",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -25,14 +25,14 @@
|
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@standard-schema/spec": "^1.0.0",
|
|
28
|
-
"
|
|
29
|
-
"@orpc/
|
|
30
|
-
"@orpc/shared": "0.0.0-next.
|
|
28
|
+
"openapi-types": "^12.1.3",
|
|
29
|
+
"@orpc/client": "0.0.0-next.e82d760",
|
|
30
|
+
"@orpc/shared": "0.0.0-next.e82d760"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"arktype": "2.
|
|
34
|
-
"valibot": "1.
|
|
35
|
-
"zod": "^3.
|
|
33
|
+
"arktype": "2.1.20",
|
|
34
|
+
"valibot": "^1.1.0",
|
|
35
|
+
"zod": "^3.25.67"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "unbuild",
|