@orpc/contract 0.0.0-next.4e27480 → 0.0.0-next.4f63ec1
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/dist/index.js +167 -400
- package/dist/src/builder-variants.d.ts +38 -0
- package/dist/src/builder.d.ts +28 -25
- package/dist/src/config.d.ts +1 -1
- 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,421 +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
|
-
|
|
94
|
+
super(def);
|
|
95
|
+
this["~orpc"].prefix = def.prefix;
|
|
96
|
+
this["~orpc"].tags = def.tags;
|
|
167
97
|
}
|
|
168
|
-
|
|
169
|
-
|
|
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 _ContractRouterBuilder({
|
|
182
|
-
...this["~orpc"],
|
|
183
|
-
errorMap: {
|
|
184
|
-
...this["~orpc"].errorMap,
|
|
185
|
-
...errors
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
router(router) {
|
|
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
|
-
constructor(def) {
|
|
212
|
-
super(def);
|
|
213
|
-
}
|
|
214
|
-
config(config) {
|
|
215
117
|
return new _ContractBuilder({
|
|
216
118
|
...this["~orpc"],
|
|
217
|
-
|
|
218
|
-
...this["~orpc"].config,
|
|
219
|
-
...config
|
|
220
|
-
}
|
|
119
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
221
120
|
});
|
|
222
121
|
}
|
|
223
|
-
|
|
122
|
+
meta(meta) {
|
|
224
123
|
return new _ContractBuilder({
|
|
225
124
|
...this["~orpc"],
|
|
226
|
-
|
|
227
|
-
...this["~orpc"].errorMap,
|
|
228
|
-
...errors
|
|
229
|
-
}
|
|
125
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
230
126
|
});
|
|
231
127
|
}
|
|
232
128
|
route(route) {
|
|
233
|
-
return new
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
...route
|
|
237
|
-
},
|
|
238
|
-
InputSchema: void 0,
|
|
239
|
-
OutputSchema: void 0,
|
|
240
|
-
errorMap: this["~orpc"].errorMap
|
|
129
|
+
return new _ContractBuilder({
|
|
130
|
+
...this["~orpc"],
|
|
131
|
+
route: mergeRoute(this["~orpc"].route, route)
|
|
241
132
|
});
|
|
242
133
|
}
|
|
243
|
-
input(schema
|
|
244
|
-
return new
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
inputExample: example,
|
|
248
|
-
OutputSchema: void 0,
|
|
249
|
-
errorMap: this["~orpc"].errorMap
|
|
134
|
+
input(schema) {
|
|
135
|
+
return new _ContractBuilder({
|
|
136
|
+
...this["~orpc"],
|
|
137
|
+
inputSchema: schema
|
|
250
138
|
});
|
|
251
139
|
}
|
|
252
|
-
output(schema
|
|
253
|
-
return new
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
outputExample: example,
|
|
257
|
-
InputSchema: void 0,
|
|
258
|
-
errorMap: this["~orpc"].errorMap
|
|
140
|
+
output(schema) {
|
|
141
|
+
return new _ContractBuilder({
|
|
142
|
+
...this["~orpc"],
|
|
143
|
+
outputSchema: schema
|
|
259
144
|
});
|
|
260
145
|
}
|
|
261
146
|
prefix(prefix) {
|
|
262
|
-
return new
|
|
263
|
-
|
|
264
|
-
|
|
147
|
+
return new _ContractBuilder({
|
|
148
|
+
...this["~orpc"],
|
|
149
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
265
150
|
});
|
|
266
151
|
}
|
|
267
152
|
tag(...tags) {
|
|
268
|
-
return new
|
|
269
|
-
|
|
270
|
-
|
|
153
|
+
return new _ContractBuilder({
|
|
154
|
+
...this["~orpc"],
|
|
155
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
|
271
156
|
});
|
|
272
157
|
}
|
|
273
158
|
router(router) {
|
|
274
|
-
return
|
|
275
|
-
errorMap: this["~orpc"].errorMap
|
|
276
|
-
}).router(router);
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
// src/error-orpc.ts
|
|
281
|
-
import { isPlainObject } from "@orpc/shared";
|
|
282
|
-
var COMMON_ORPC_ERROR_DEFS = {
|
|
283
|
-
BAD_REQUEST: {
|
|
284
|
-
status: 400,
|
|
285
|
-
message: "Bad Request"
|
|
286
|
-
},
|
|
287
|
-
UNAUTHORIZED: {
|
|
288
|
-
status: 401,
|
|
289
|
-
message: "Unauthorized"
|
|
290
|
-
},
|
|
291
|
-
FORBIDDEN: {
|
|
292
|
-
status: 403,
|
|
293
|
-
message: "Forbidden"
|
|
294
|
-
},
|
|
295
|
-
NOT_FOUND: {
|
|
296
|
-
status: 404,
|
|
297
|
-
message: "Not Found"
|
|
298
|
-
},
|
|
299
|
-
METHOD_NOT_SUPPORTED: {
|
|
300
|
-
status: 405,
|
|
301
|
-
message: "Method Not Supported"
|
|
302
|
-
},
|
|
303
|
-
NOT_ACCEPTABLE: {
|
|
304
|
-
status: 406,
|
|
305
|
-
message: "Not Acceptable"
|
|
306
|
-
},
|
|
307
|
-
TIMEOUT: {
|
|
308
|
-
status: 408,
|
|
309
|
-
message: "Request Timeout"
|
|
310
|
-
},
|
|
311
|
-
CONFLICT: {
|
|
312
|
-
status: 409,
|
|
313
|
-
message: "Conflict"
|
|
314
|
-
},
|
|
315
|
-
PRECONDITION_FAILED: {
|
|
316
|
-
status: 412,
|
|
317
|
-
message: "Precondition Failed"
|
|
318
|
-
},
|
|
319
|
-
PAYLOAD_TOO_LARGE: {
|
|
320
|
-
status: 413,
|
|
321
|
-
message: "Payload Too Large"
|
|
322
|
-
},
|
|
323
|
-
UNSUPPORTED_MEDIA_TYPE: {
|
|
324
|
-
status: 415,
|
|
325
|
-
message: "Unsupported Media Type"
|
|
326
|
-
},
|
|
327
|
-
UNPROCESSABLE_CONTENT: {
|
|
328
|
-
status: 422,
|
|
329
|
-
message: "Unprocessable Content"
|
|
330
|
-
},
|
|
331
|
-
TOO_MANY_REQUESTS: {
|
|
332
|
-
status: 429,
|
|
333
|
-
message: "Too Many Requests"
|
|
334
|
-
},
|
|
335
|
-
CLIENT_CLOSED_REQUEST: {
|
|
336
|
-
status: 499,
|
|
337
|
-
message: "Client Closed Request"
|
|
338
|
-
},
|
|
339
|
-
INTERNAL_SERVER_ERROR: {
|
|
340
|
-
status: 500,
|
|
341
|
-
message: "Internal Server Error"
|
|
342
|
-
},
|
|
343
|
-
NOT_IMPLEMENTED: {
|
|
344
|
-
status: 501,
|
|
345
|
-
message: "Not Implemented"
|
|
346
|
-
},
|
|
347
|
-
BAD_GATEWAY: {
|
|
348
|
-
status: 502,
|
|
349
|
-
message: "Bad Gateway"
|
|
350
|
-
},
|
|
351
|
-
SERVICE_UNAVAILABLE: {
|
|
352
|
-
status: 503,
|
|
353
|
-
message: "Service Unavailable"
|
|
354
|
-
},
|
|
355
|
-
GATEWAY_TIMEOUT: {
|
|
356
|
-
status: 504,
|
|
357
|
-
message: "Gateway Timeout"
|
|
159
|
+
return adaptContractRouter(router, this["~orpc"]);
|
|
358
160
|
}
|
|
359
161
|
};
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
defined;
|
|
368
|
-
code;
|
|
369
|
-
status;
|
|
370
|
-
data;
|
|
371
|
-
constructor(options) {
|
|
372
|
-
if (options.status && (options.status < 400 || options.status >= 600)) {
|
|
373
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
374
|
-
}
|
|
375
|
-
const message = fallbackORPCErrorMessage(options.code, options.message);
|
|
376
|
-
super(message, options);
|
|
377
|
-
this.code = options.code;
|
|
378
|
-
this.status = fallbackORPCErrorStatus(options.code, options.status);
|
|
379
|
-
this.defined = options.defined ?? false;
|
|
380
|
-
this.data = options.data;
|
|
381
|
-
}
|
|
382
|
-
toJSON() {
|
|
383
|
-
return {
|
|
384
|
-
defined: this.defined,
|
|
385
|
-
code: this.code,
|
|
386
|
-
status: this.status,
|
|
387
|
-
message: this.message,
|
|
388
|
-
data: this.data
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
static isValidJSON(json) {
|
|
392
|
-
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";
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
function isDefinedError(error) {
|
|
396
|
-
return error instanceof ORPCError && error.defined;
|
|
397
|
-
}
|
|
398
|
-
async function validateORPCError(map, error) {
|
|
399
|
-
const { code, status, message, data, cause, defined } = error;
|
|
400
|
-
const config = map?.[error.code];
|
|
401
|
-
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
|
402
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
403
|
-
}
|
|
404
|
-
if (!config.data) {
|
|
405
|
-
return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause });
|
|
406
|
-
}
|
|
407
|
-
const validated = await config.data["~standard"].validate(error.data);
|
|
408
|
-
if (validated.issues) {
|
|
409
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
410
|
-
}
|
|
411
|
-
return new ORPCError({
|
|
412
|
-
defined: true,
|
|
413
|
-
code,
|
|
414
|
-
status,
|
|
415
|
-
message,
|
|
416
|
-
data: validated.value,
|
|
417
|
-
cause
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// src/client-utils.ts
|
|
422
|
-
async function safe(promise) {
|
|
423
|
-
try {
|
|
424
|
-
const output = await promise;
|
|
425
|
-
return [output, void 0, false];
|
|
426
|
-
} catch (e) {
|
|
427
|
-
const error = e;
|
|
428
|
-
if (isDefinedError(error)) {
|
|
429
|
-
return [void 0, error, true];
|
|
430
|
-
}
|
|
431
|
-
return [void 0, error, false];
|
|
432
|
-
}
|
|
433
|
-
}
|
|
162
|
+
var oc = new ContractBuilder({
|
|
163
|
+
errorMap: {},
|
|
164
|
+
inputSchema: void 0,
|
|
165
|
+
outputSchema: void 0,
|
|
166
|
+
route: {},
|
|
167
|
+
meta: {}
|
|
168
|
+
});
|
|
434
169
|
|
|
435
170
|
// src/config.ts
|
|
436
171
|
var DEFAULT_CONFIG = {
|
|
@@ -447,16 +182,53 @@ function fallbackContractConfig(key, value) {
|
|
|
447
182
|
return value;
|
|
448
183
|
}
|
|
449
184
|
|
|
450
|
-
// src/
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
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;
|
|
456
227
|
}
|
|
457
|
-
|
|
228
|
+
return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL];
|
|
229
|
+
}
|
|
458
230
|
|
|
459
|
-
// src/schema
|
|
231
|
+
// src/schema.ts
|
|
460
232
|
function type(...[map]) {
|
|
461
233
|
return {
|
|
462
234
|
"~standard": {
|
|
@@ -473,31 +245,26 @@ function type(...[map]) {
|
|
|
473
245
|
}
|
|
474
246
|
|
|
475
247
|
// src/index.ts
|
|
476
|
-
|
|
477
|
-
errorMap: {},
|
|
478
|
-
InputSchema: void 0,
|
|
479
|
-
OutputSchema: void 0,
|
|
480
|
-
config: {}
|
|
481
|
-
});
|
|
248
|
+
import { ORPCError as ORPCError2 } from "@orpc/client";
|
|
482
249
|
export {
|
|
483
|
-
COMMON_ORPC_ERROR_DEFS,
|
|
484
250
|
ContractBuilder,
|
|
485
251
|
ContractProcedure,
|
|
486
|
-
|
|
487
|
-
ContractProcedureBuilderWithInput,
|
|
488
|
-
ContractProcedureBuilderWithOutput,
|
|
489
|
-
ContractRouterBuilder,
|
|
490
|
-
DecoratedContractProcedure,
|
|
491
|
-
ORPCError,
|
|
252
|
+
ORPCError2 as ORPCError,
|
|
492
253
|
ValidationError,
|
|
254
|
+
adaptContractRouter,
|
|
255
|
+
adaptRoute,
|
|
256
|
+
eventIterator,
|
|
493
257
|
fallbackContractConfig,
|
|
494
|
-
|
|
495
|
-
fallbackORPCErrorStatus,
|
|
258
|
+
getEventIteratorSchemaDetails,
|
|
496
259
|
isContractProcedure,
|
|
497
|
-
|
|
260
|
+
mergeErrorMap,
|
|
261
|
+
mergeMeta,
|
|
262
|
+
mergePrefix,
|
|
263
|
+
mergeRoute,
|
|
264
|
+
mergeTags,
|
|
498
265
|
oc,
|
|
499
|
-
|
|
266
|
+
prefixRoute,
|
|
500
267
|
type,
|
|
501
|
-
|
|
268
|
+
unshiftTagRoute
|
|
502
269
|
};
|
|
503
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
|