@orpc/contract 0.0.0-next.44bdf93 → 0.0.0-next.47e44e3
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 +172 -399
- package/dist/src/builder-variants.d.ts +38 -0
- package/dist/src/builder.d.ts +30 -20
- 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 -13
- 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 -9
- 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 -58
- package/dist/src/error-orpc.d.ts +0 -109
- package/dist/src/procedure-builder-with-input.d.ts +0 -19
- package/dist/src/procedure-builder-with-output.d.ts +0 -19
- package/dist/src/procedure-builder.d.ts +0 -15
- package/dist/src/procedure-decorated.d.ts +0 -12
- package/dist/src/router-builder.d.ts +0 -23
- package/dist/src/schema-utils.d.ts +0 -5
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,404 +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
|
|
29
|
-
}
|
|
30
|
-
errors(errors) {
|
|
31
|
-
return new _DecoratedContractProcedure({
|
|
32
|
-
...this["~orpc"],
|
|
33
|
-
errorMap: {
|
|
34
|
-
...this["~orpc"].errorMap,
|
|
35
|
-
...errors
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
route(route) {
|
|
40
|
-
return new _DecoratedContractProcedure({
|
|
41
|
-
...this["~orpc"],
|
|
42
|
-
route: {
|
|
43
|
-
...this["~orpc"].route,
|
|
44
|
-
...route
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
prefix(prefix) {
|
|
49
|
-
return new _DecoratedContractProcedure({
|
|
50
|
-
...this["~orpc"],
|
|
51
|
-
...this["~orpc"].route?.path ? {
|
|
52
|
-
route: {
|
|
53
|
-
...this["~orpc"].route,
|
|
54
|
-
path: `${prefix}${this["~orpc"].route.path}`
|
|
55
|
-
}
|
|
56
|
-
} : void 0
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
unshiftTag(...tags) {
|
|
60
|
-
return new _DecoratedContractProcedure({
|
|
61
|
-
...this["~orpc"],
|
|
62
|
-
route: {
|
|
63
|
-
...this["~orpc"].route,
|
|
64
|
-
tags: [
|
|
65
|
-
...tags,
|
|
66
|
-
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
|
|
67
|
-
]
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// src/procedure-builder-with-input.ts
|
|
74
|
-
var ContractProcedureBuilderWithInput = class _ContractProcedureBuilderWithInput extends ContractProcedure {
|
|
75
|
-
errors(errors) {
|
|
76
|
-
const decorated = DecoratedContractProcedure.decorate(this).errors(errors);
|
|
77
|
-
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
78
|
-
}
|
|
79
|
-
route(route) {
|
|
80
|
-
const decorated = DecoratedContractProcedure.decorate(this).route(route);
|
|
81
|
-
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
82
|
-
}
|
|
83
|
-
prefix(prefix) {
|
|
84
|
-
const decorated = DecoratedContractProcedure.decorate(this).prefix(prefix);
|
|
85
|
-
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
86
|
-
}
|
|
87
|
-
unshiftTag(...tags) {
|
|
88
|
-
const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
|
|
89
|
-
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
90
|
-
}
|
|
91
|
-
output(schema, example) {
|
|
92
|
-
return new DecoratedContractProcedure({
|
|
93
|
-
...this["~orpc"],
|
|
94
|
-
OutputSchema: schema,
|
|
95
|
-
outputExample: example
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
// src/procedure-builder-with-output.ts
|
|
101
|
-
var ContractProcedureBuilderWithOutput = class _ContractProcedureBuilderWithOutput extends ContractProcedure {
|
|
102
|
-
errors(errors) {
|
|
103
|
-
const decorated = DecoratedContractProcedure.decorate(this).errors(errors);
|
|
104
|
-
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
105
|
-
}
|
|
106
|
-
route(route) {
|
|
107
|
-
const decorated = DecoratedContractProcedure.decorate(this).route(route);
|
|
108
|
-
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
109
|
-
}
|
|
110
|
-
prefix(prefix) {
|
|
111
|
-
const decorated = DecoratedContractProcedure.decorate(this).prefix(prefix);
|
|
112
|
-
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
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;
|
|
113
45
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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);
|
|
117
67
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
...this["~orpc"],
|
|
121
|
-
InputSchema: schema,
|
|
122
|
-
inputExample: example
|
|
123
|
-
});
|
|
68
|
+
if (options.tags) {
|
|
69
|
+
router = unshiftTagRoute(router, options.tags);
|
|
124
70
|
}
|
|
125
|
-
|
|
71
|
+
return router;
|
|
72
|
+
}
|
|
126
73
|
|
|
127
|
-
// src/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const decorated = DecoratedContractProcedure.decorate(this).route(route);
|
|
135
|
-
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
136
|
-
}
|
|
137
|
-
prefix(prefix) {
|
|
138
|
-
const decorated = DecoratedContractProcedure.decorate(this).prefix(prefix);
|
|
139
|
-
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
140
|
-
}
|
|
141
|
-
unshiftTag(...tags) {
|
|
142
|
-
const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
|
|
143
|
-
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
144
|
-
}
|
|
145
|
-
input(schema, example) {
|
|
146
|
-
return new ContractProcedureBuilderWithInput({
|
|
147
|
-
...this["~orpc"],
|
|
148
|
-
InputSchema: schema,
|
|
149
|
-
inputExample: example
|
|
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)
|
|
150
81
|
});
|
|
82
|
+
return adapted2;
|
|
151
83
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
OutputSchema: schema,
|
|
156
|
-
outputExample: example
|
|
157
|
-
});
|
|
84
|
+
const adapted = {};
|
|
85
|
+
for (const key in contract) {
|
|
86
|
+
adapted[key] = adaptContractRouter(contract[key], options);
|
|
158
87
|
}
|
|
159
|
-
|
|
88
|
+
return adapted;
|
|
89
|
+
}
|
|
160
90
|
|
|
161
|
-
// src/
|
|
162
|
-
var
|
|
163
|
-
"~type" = "ContractProcedure";
|
|
164
|
-
"~orpc";
|
|
91
|
+
// src/builder.ts
|
|
92
|
+
var ContractBuilder = class _ContractBuilder extends ContractProcedure {
|
|
165
93
|
constructor(def) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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({
|
|
170
103
|
...this["~orpc"],
|
|
171
|
-
|
|
104
|
+
meta: initialMeta
|
|
172
105
|
});
|
|
173
106
|
}
|
|
174
|
-
|
|
175
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Reset initial route
|
|
109
|
+
*/
|
|
110
|
+
$route(initialRoute) {
|
|
111
|
+
return new _ContractBuilder({
|
|
176
112
|
...this["~orpc"],
|
|
177
|
-
|
|
113
|
+
route: initialRoute
|
|
178
114
|
});
|
|
179
115
|
}
|
|
180
116
|
errors(errors) {
|
|
181
|
-
return new
|
|
117
|
+
return new _ContractBuilder({
|
|
182
118
|
...this["~orpc"],
|
|
183
|
-
errorMap:
|
|
184
|
-
...this["~orpc"].errorMap,
|
|
185
|
-
...errors
|
|
186
|
-
}
|
|
119
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
187
120
|
});
|
|
188
121
|
}
|
|
189
|
-
|
|
190
|
-
if (isContractProcedure(router)) {
|
|
191
|
-
let decorated = DecoratedContractProcedure.decorate(router);
|
|
192
|
-
if (this["~orpc"].tags) {
|
|
193
|
-
decorated = decorated.unshiftTag(...this["~orpc"].tags);
|
|
194
|
-
}
|
|
195
|
-
if (this["~orpc"].prefix) {
|
|
196
|
-
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
197
|
-
}
|
|
198
|
-
decorated = decorated.errors(this["~orpc"].errorMap);
|
|
199
|
-
return decorated;
|
|
200
|
-
}
|
|
201
|
-
const adapted = {};
|
|
202
|
-
for (const key in router) {
|
|
203
|
-
adapted[key] = this.router(router[key]);
|
|
204
|
-
}
|
|
205
|
-
return adapted;
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
// src/builder.ts
|
|
210
|
-
var ContractBuilder = class _ContractBuilder extends ContractProcedure {
|
|
211
|
-
errors(errors) {
|
|
122
|
+
meta(meta) {
|
|
212
123
|
return new _ContractBuilder({
|
|
213
124
|
...this["~orpc"],
|
|
214
|
-
|
|
215
|
-
...this["~orpc"].errorMap,
|
|
216
|
-
...errors
|
|
217
|
-
}
|
|
125
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
218
126
|
});
|
|
219
127
|
}
|
|
220
128
|
route(route) {
|
|
221
|
-
return new
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
OutputSchema: void 0,
|
|
225
|
-
errorMap: this["~orpc"].errorMap
|
|
129
|
+
return new _ContractBuilder({
|
|
130
|
+
...this["~orpc"],
|
|
131
|
+
route: mergeRoute(this["~orpc"].route, route)
|
|
226
132
|
});
|
|
227
133
|
}
|
|
228
|
-
input(schema
|
|
229
|
-
return new
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
OutputSchema: void 0,
|
|
233
|
-
errorMap: this["~orpc"].errorMap
|
|
134
|
+
input(schema) {
|
|
135
|
+
return new _ContractBuilder({
|
|
136
|
+
...this["~orpc"],
|
|
137
|
+
inputSchema: schema
|
|
234
138
|
});
|
|
235
139
|
}
|
|
236
|
-
output(schema
|
|
237
|
-
return new
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
InputSchema: void 0,
|
|
241
|
-
errorMap: this["~orpc"].errorMap
|
|
140
|
+
output(schema) {
|
|
141
|
+
return new _ContractBuilder({
|
|
142
|
+
...this["~orpc"],
|
|
143
|
+
outputSchema: schema
|
|
242
144
|
});
|
|
243
145
|
}
|
|
244
146
|
prefix(prefix) {
|
|
245
|
-
return new
|
|
246
|
-
|
|
247
|
-
|
|
147
|
+
return new _ContractBuilder({
|
|
148
|
+
...this["~orpc"],
|
|
149
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
248
150
|
});
|
|
249
151
|
}
|
|
250
152
|
tag(...tags) {
|
|
251
|
-
return new
|
|
252
|
-
|
|
253
|
-
|
|
153
|
+
return new _ContractBuilder({
|
|
154
|
+
...this["~orpc"],
|
|
155
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
|
254
156
|
});
|
|
255
157
|
}
|
|
256
158
|
router(router) {
|
|
257
|
-
return
|
|
258
|
-
errorMap: this["~orpc"].errorMap
|
|
259
|
-
}).router(router);
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
// src/error-orpc.ts
|
|
264
|
-
import { isPlainObject } from "@orpc/shared";
|
|
265
|
-
var COMMON_ORPC_ERROR_DEFS = {
|
|
266
|
-
BAD_REQUEST: {
|
|
267
|
-
status: 400,
|
|
268
|
-
message: "Bad Request"
|
|
269
|
-
},
|
|
270
|
-
UNAUTHORIZED: {
|
|
271
|
-
status: 401,
|
|
272
|
-
message: "Unauthorized"
|
|
273
|
-
},
|
|
274
|
-
FORBIDDEN: {
|
|
275
|
-
status: 403,
|
|
276
|
-
message: "Forbidden"
|
|
277
|
-
},
|
|
278
|
-
NOT_FOUND: {
|
|
279
|
-
status: 404,
|
|
280
|
-
message: "Not Found"
|
|
281
|
-
},
|
|
282
|
-
METHOD_NOT_SUPPORTED: {
|
|
283
|
-
status: 405,
|
|
284
|
-
message: "Method Not Supported"
|
|
285
|
-
},
|
|
286
|
-
NOT_ACCEPTABLE: {
|
|
287
|
-
status: 406,
|
|
288
|
-
message: "Not Acceptable"
|
|
289
|
-
},
|
|
290
|
-
TIMEOUT: {
|
|
291
|
-
status: 408,
|
|
292
|
-
message: "Request Timeout"
|
|
293
|
-
},
|
|
294
|
-
CONFLICT: {
|
|
295
|
-
status: 409,
|
|
296
|
-
message: "Conflict"
|
|
297
|
-
},
|
|
298
|
-
PRECONDITION_FAILED: {
|
|
299
|
-
status: 412,
|
|
300
|
-
message: "Precondition Failed"
|
|
301
|
-
},
|
|
302
|
-
PAYLOAD_TOO_LARGE: {
|
|
303
|
-
status: 413,
|
|
304
|
-
message: "Payload Too Large"
|
|
305
|
-
},
|
|
306
|
-
UNSUPPORTED_MEDIA_TYPE: {
|
|
307
|
-
status: 415,
|
|
308
|
-
message: "Unsupported Media Type"
|
|
309
|
-
},
|
|
310
|
-
UNPROCESSABLE_CONTENT: {
|
|
311
|
-
status: 422,
|
|
312
|
-
message: "Unprocessable Content"
|
|
313
|
-
},
|
|
314
|
-
TOO_MANY_REQUESTS: {
|
|
315
|
-
status: 429,
|
|
316
|
-
message: "Too Many Requests"
|
|
317
|
-
},
|
|
318
|
-
CLIENT_CLOSED_REQUEST: {
|
|
319
|
-
status: 499,
|
|
320
|
-
message: "Client Closed Request"
|
|
321
|
-
},
|
|
322
|
-
INTERNAL_SERVER_ERROR: {
|
|
323
|
-
status: 500,
|
|
324
|
-
message: "Internal Server Error"
|
|
325
|
-
},
|
|
326
|
-
NOT_IMPLEMENTED: {
|
|
327
|
-
status: 501,
|
|
328
|
-
message: "Not Implemented"
|
|
329
|
-
},
|
|
330
|
-
BAD_GATEWAY: {
|
|
331
|
-
status: 502,
|
|
332
|
-
message: "Bad Gateway"
|
|
333
|
-
},
|
|
334
|
-
SERVICE_UNAVAILABLE: {
|
|
335
|
-
status: 503,
|
|
336
|
-
message: "Service Unavailable"
|
|
337
|
-
},
|
|
338
|
-
GATEWAY_TIMEOUT: {
|
|
339
|
-
status: 504,
|
|
340
|
-
message: "Gateway Timeout"
|
|
159
|
+
return adaptContractRouter(router, this["~orpc"]);
|
|
341
160
|
}
|
|
342
161
|
};
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
defined;
|
|
351
|
-
code;
|
|
352
|
-
status;
|
|
353
|
-
data;
|
|
354
|
-
constructor(options) {
|
|
355
|
-
if (options.status && (options.status < 400 || options.status >= 600)) {
|
|
356
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
357
|
-
}
|
|
358
|
-
const message = fallbackORPCErrorMessage(options.code, options.message);
|
|
359
|
-
super(message, options);
|
|
360
|
-
this.code = options.code;
|
|
361
|
-
this.status = fallbackORPCErrorStatus(options.code, options.status);
|
|
362
|
-
this.defined = options.defined ?? false;
|
|
363
|
-
this.data = options.data;
|
|
364
|
-
}
|
|
365
|
-
toJSON() {
|
|
366
|
-
return {
|
|
367
|
-
defined: this.defined,
|
|
368
|
-
code: this.code,
|
|
369
|
-
status: this.status,
|
|
370
|
-
message: this.message,
|
|
371
|
-
data: this.data
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
static isValidJSON(json) {
|
|
375
|
-
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";
|
|
376
|
-
}
|
|
377
|
-
};
|
|
378
|
-
function isDefinedError(error) {
|
|
379
|
-
return error instanceof ORPCError && error.defined;
|
|
380
|
-
}
|
|
381
|
-
async function validateORPCError(map, error) {
|
|
382
|
-
const { code, status, message, data, cause, defined } = error;
|
|
383
|
-
const config = map?.[error.code];
|
|
384
|
-
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
|
385
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
386
|
-
}
|
|
387
|
-
if (!config.data) {
|
|
388
|
-
return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause });
|
|
389
|
-
}
|
|
390
|
-
const validated = await config.data["~standard"].validate(error.data);
|
|
391
|
-
if (validated.issues) {
|
|
392
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
393
|
-
}
|
|
394
|
-
return new ORPCError({
|
|
395
|
-
defined: true,
|
|
396
|
-
code,
|
|
397
|
-
status,
|
|
398
|
-
message,
|
|
399
|
-
data: validated.value,
|
|
400
|
-
cause
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// src/client-utils.ts
|
|
405
|
-
async function safe(promise) {
|
|
406
|
-
try {
|
|
407
|
-
const output = await promise;
|
|
408
|
-
return [output, void 0, false];
|
|
409
|
-
} catch (e) {
|
|
410
|
-
const error = e;
|
|
411
|
-
if (isDefinedError(error)) {
|
|
412
|
-
return [void 0, error, true];
|
|
413
|
-
}
|
|
414
|
-
return [void 0, error, false];
|
|
415
|
-
}
|
|
416
|
-
}
|
|
162
|
+
var oc = new ContractBuilder({
|
|
163
|
+
errorMap: {},
|
|
164
|
+
inputSchema: void 0,
|
|
165
|
+
outputSchema: void 0,
|
|
166
|
+
route: {},
|
|
167
|
+
meta: {}
|
|
168
|
+
});
|
|
417
169
|
|
|
418
170
|
// src/config.ts
|
|
419
171
|
var DEFAULT_CONFIG = {
|
|
@@ -423,34 +175,60 @@ var DEFAULT_CONFIG = {
|
|
|
423
175
|
defaultInputStructure: "compact",
|
|
424
176
|
defaultOutputStructure: "compact"
|
|
425
177
|
};
|
|
426
|
-
|
|
427
|
-
function configGlobal(config) {
|
|
428
|
-
if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) {
|
|
429
|
-
throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299");
|
|
430
|
-
}
|
|
431
|
-
GLOBAL_CONFIG_REF.value = config;
|
|
432
|
-
}
|
|
433
|
-
function fallbackToGlobalConfig(key, value) {
|
|
178
|
+
function fallbackContractConfig(key, value) {
|
|
434
179
|
if (value === void 0) {
|
|
435
|
-
|
|
436
|
-
if (fallback === void 0) {
|
|
437
|
-
return DEFAULT_CONFIG[key];
|
|
438
|
-
}
|
|
439
|
-
return fallback;
|
|
180
|
+
return DEFAULT_CONFIG[key];
|
|
440
181
|
}
|
|
441
182
|
return value;
|
|
442
183
|
}
|
|
443
184
|
|
|
444
|
-
// src/
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
185
|
+
// src/event-iterator.ts
|
|
186
|
+
import { mapEventIterator, ORPCError } from "@orpc/client";
|
|
187
|
+
import { isAsyncIteratorObject } from "@orpc/server-standard";
|
|
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;
|
|
450
227
|
}
|
|
451
|
-
|
|
228
|
+
return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL];
|
|
229
|
+
}
|
|
452
230
|
|
|
453
|
-
// src/schema
|
|
231
|
+
// src/schema.ts
|
|
454
232
|
function type(...[map]) {
|
|
455
233
|
return {
|
|
456
234
|
"~standard": {
|
|
@@ -467,31 +245,26 @@ function type(...[map]) {
|
|
|
467
245
|
}
|
|
468
246
|
|
|
469
247
|
// src/index.ts
|
|
470
|
-
|
|
471
|
-
errorMap: {},
|
|
472
|
-
InputSchema: void 0,
|
|
473
|
-
OutputSchema: void 0
|
|
474
|
-
});
|
|
248
|
+
import { ORPCError as ORPCError2 } from "@orpc/client";
|
|
475
249
|
export {
|
|
476
|
-
COMMON_ORPC_ERROR_DEFS,
|
|
477
250
|
ContractBuilder,
|
|
478
251
|
ContractProcedure,
|
|
479
|
-
|
|
480
|
-
ContractProcedureBuilderWithInput,
|
|
481
|
-
ContractProcedureBuilderWithOutput,
|
|
482
|
-
ContractRouterBuilder,
|
|
483
|
-
DecoratedContractProcedure,
|
|
484
|
-
ORPCError,
|
|
252
|
+
ORPCError2 as ORPCError,
|
|
485
253
|
ValidationError,
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
254
|
+
adaptContractRouter,
|
|
255
|
+
adaptRoute,
|
|
256
|
+
eventIterator,
|
|
257
|
+
fallbackContractConfig,
|
|
258
|
+
getEventIteratorSchemaDetails,
|
|
490
259
|
isContractProcedure,
|
|
491
|
-
|
|
260
|
+
mergeErrorMap,
|
|
261
|
+
mergeMeta,
|
|
262
|
+
mergePrefix,
|
|
263
|
+
mergeRoute,
|
|
264
|
+
mergeTags,
|
|
492
265
|
oc,
|
|
493
|
-
|
|
266
|
+
prefixRoute,
|
|
494
267
|
type,
|
|
495
|
-
|
|
268
|
+
unshiftTagRoute
|
|
496
269
|
};
|
|
497
270
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ErrorMap, MergedErrorMap } from './error';
|
|
2
|
+
import type { Meta } from './meta';
|
|
3
|
+
import type { ContractProcedure } from './procedure';
|
|
4
|
+
import type { HTTPPath, Route } from './route';
|
|
5
|
+
import type { AdaptContractRouterOptions, AdaptedContractRouter, ContractRouter } from './router';
|
|
6
|
+
import type { Schema } from './schema';
|
|
7
|
+
export interface ContractProcedureBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
8
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
9
|
+
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
10
|
+
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
11
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
12
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
13
|
+
}
|
|
14
|
+
export interface ContractProcedureBuilderWithInput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
15
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
16
|
+
meta(meta: TMeta): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
17
|
+
route(route: Route): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
18
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
19
|
+
}
|
|
20
|
+
export interface ContractProcedureBuilderWithOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
21
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
22
|
+
meta(meta: TMeta): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
23
|
+
route(route: Route): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
24
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
25
|
+
}
|
|
26
|
+
export interface ContractProcedureBuilderWithInputOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
27
|
+
errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
28
|
+
meta(meta: TMeta): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
29
|
+
route(route: Route): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
30
|
+
}
|
|
31
|
+
export interface ContractRouterBuilder<TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
32
|
+
'~orpc': AdaptContractRouterOptions<TErrorMap>;
|
|
33
|
+
'errors'<U extends ErrorMap>(errors: U): ContractRouterBuilder<MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
34
|
+
'prefix'(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
35
|
+
'tag'(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
36
|
+
'router'<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=builder-variants.d.ts.map
|