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