@orpc/contract 0.0.0-next.a055cad → 0.0.0-next.a2e4a58
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 +420 -96
- package/dist/src/builder.d.ts +24 -8
- package/dist/src/client-utils.d.ts +5 -0
- package/dist/src/client.d.ts +19 -0
- package/dist/src/config.d.ts +10 -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 +15 -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 +73 -37
- package/dist/src/router-builder.d.ts +20 -13
- 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 +8 -5
- package/package.json +8 -5
- package/dist/src/constants.d.ts +0 -3
- package/dist/src/utils.d.ts +0 -4
package/dist/index.js
CHANGED
|
@@ -1,179 +1,503 @@
|
|
|
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"];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/procedure-decorated.ts
|
|
7
23
|
var DecoratedContractProcedure = class _DecoratedContractProcedure extends ContractProcedure {
|
|
8
|
-
static decorate(
|
|
9
|
-
if (
|
|
10
|
-
return
|
|
11
|
-
|
|
24
|
+
static decorate(procedure) {
|
|
25
|
+
if (procedure instanceof _DecoratedContractProcedure) {
|
|
26
|
+
return procedure;
|
|
27
|
+
}
|
|
28
|
+
return new _DecoratedContractProcedure(procedure["~orpc"]);
|
|
29
|
+
}
|
|
30
|
+
errors(errors) {
|
|
31
|
+
return new _DecoratedContractProcedure({
|
|
32
|
+
...this["~orpc"],
|
|
33
|
+
errorMap: {
|
|
34
|
+
...this["~orpc"].errorMap,
|
|
35
|
+
...errors
|
|
36
|
+
}
|
|
37
|
+
});
|
|
12
38
|
}
|
|
13
|
-
route(
|
|
39
|
+
route(route) {
|
|
14
40
|
return new _DecoratedContractProcedure({
|
|
15
|
-
...this
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
41
|
+
...this["~orpc"],
|
|
42
|
+
route: {
|
|
43
|
+
...this["~orpc"].route,
|
|
44
|
+
...route
|
|
45
|
+
}
|
|
19
46
|
});
|
|
20
47
|
}
|
|
21
48
|
prefix(prefix) {
|
|
22
|
-
if (!this.zz$cp.path)
|
|
23
|
-
return this;
|
|
24
49
|
return new _DecoratedContractProcedure({
|
|
25
|
-
...this
|
|
26
|
-
path
|
|
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
|
|
27
57
|
});
|
|
28
58
|
}
|
|
29
|
-
|
|
30
|
-
if (!tags.length)
|
|
31
|
-
return this;
|
|
59
|
+
unshiftTag(...tags) {
|
|
32
60
|
return new _DecoratedContractProcedure({
|
|
33
|
-
...this
|
|
34
|
-
|
|
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
|
|
35
96
|
});
|
|
36
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"]);
|
|
113
|
+
}
|
|
114
|
+
unshiftTag(...tags) {
|
|
115
|
+
const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
|
|
116
|
+
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
117
|
+
}
|
|
37
118
|
input(schema, example) {
|
|
38
|
-
return new
|
|
39
|
-
...this
|
|
119
|
+
return new DecoratedContractProcedure({
|
|
120
|
+
...this["~orpc"],
|
|
121
|
+
InputSchema: schema,
|
|
122
|
+
inputExample: example
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/procedure-builder.ts
|
|
128
|
+
var ContractProcedureBuilder = class _ContractProcedureBuilder extends ContractProcedure {
|
|
129
|
+
errors(errors) {
|
|
130
|
+
const decorated = DecoratedContractProcedure.decorate(this).errors(errors);
|
|
131
|
+
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
132
|
+
}
|
|
133
|
+
route(route) {
|
|
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"],
|
|
40
148
|
InputSchema: schema,
|
|
41
149
|
inputExample: example
|
|
42
150
|
});
|
|
43
151
|
}
|
|
44
152
|
output(schema, example) {
|
|
45
|
-
return new
|
|
46
|
-
...this
|
|
153
|
+
return new ContractProcedureBuilderWithOutput({
|
|
154
|
+
...this["~orpc"],
|
|
47
155
|
OutputSchema: schema,
|
|
48
156
|
outputExample: example
|
|
49
157
|
});
|
|
50
158
|
}
|
|
51
159
|
};
|
|
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
160
|
|
|
58
161
|
// src/router-builder.ts
|
|
59
162
|
var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
163
|
+
"~type" = "ContractProcedure";
|
|
164
|
+
"~orpc";
|
|
165
|
+
constructor(def) {
|
|
166
|
+
this["~orpc"] = def;
|
|
65
167
|
}
|
|
66
168
|
prefix(prefix) {
|
|
67
169
|
return new _ContractRouterBuilder({
|
|
68
|
-
...this
|
|
69
|
-
prefix: `${this.
|
|
170
|
+
...this["~orpc"],
|
|
171
|
+
prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
|
|
70
172
|
});
|
|
71
173
|
}
|
|
72
|
-
|
|
73
|
-
if (!tags.length)
|
|
74
|
-
return this;
|
|
174
|
+
tag(...tags) {
|
|
75
175
|
return new _ContractRouterBuilder({
|
|
76
|
-
...this
|
|
77
|
-
tags: [...this.
|
|
176
|
+
...this["~orpc"],
|
|
177
|
+
tags: [...this["~orpc"].tags ?? [], ...tags]
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
errors(errors) {
|
|
181
|
+
return new _ContractRouterBuilder({
|
|
182
|
+
...this["~orpc"],
|
|
183
|
+
errorMap: {
|
|
184
|
+
...this["~orpc"].errorMap,
|
|
185
|
+
...errors
|
|
186
|
+
}
|
|
78
187
|
});
|
|
79
188
|
}
|
|
80
189
|
router(router) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
88
|
-
handled[key] = this.zz$crb.prefix ? decorated.prefix(this.zz$crb.prefix) : decorated;
|
|
89
|
-
} else {
|
|
90
|
-
handled[key] = this.router(item);
|
|
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);
|
|
91
197
|
}
|
|
198
|
+
decorated = decorated.errors(this["~orpc"].errorMap);
|
|
199
|
+
return decorated;
|
|
92
200
|
}
|
|
93
|
-
|
|
201
|
+
const adapted = {};
|
|
202
|
+
for (const key in router) {
|
|
203
|
+
adapted[key] = this.router(router[key]);
|
|
204
|
+
}
|
|
205
|
+
return adapted;
|
|
94
206
|
}
|
|
95
207
|
};
|
|
96
208
|
|
|
97
209
|
// src/builder.ts
|
|
98
|
-
var ContractBuilder = class {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
210
|
+
var ContractBuilder = class _ContractBuilder extends ContractProcedure {
|
|
211
|
+
constructor(def) {
|
|
212
|
+
super(def);
|
|
213
|
+
}
|
|
214
|
+
config(config) {
|
|
215
|
+
return new _ContractBuilder({
|
|
216
|
+
...this["~orpc"],
|
|
217
|
+
config: {
|
|
218
|
+
...this["~orpc"].config,
|
|
219
|
+
...config
|
|
220
|
+
}
|
|
102
221
|
});
|
|
103
222
|
}
|
|
104
|
-
|
|
105
|
-
return new
|
|
106
|
-
|
|
223
|
+
errors(errors) {
|
|
224
|
+
return new _ContractBuilder({
|
|
225
|
+
...this["~orpc"],
|
|
226
|
+
errorMap: {
|
|
227
|
+
...this["~orpc"].errorMap,
|
|
228
|
+
...errors
|
|
229
|
+
}
|
|
107
230
|
});
|
|
108
231
|
}
|
|
109
|
-
route(
|
|
110
|
-
return new
|
|
232
|
+
route(route) {
|
|
233
|
+
return new ContractProcedureBuilder({
|
|
234
|
+
route: {
|
|
235
|
+
...this["~orpc"].config.initialRoute,
|
|
236
|
+
...route
|
|
237
|
+
},
|
|
111
238
|
InputSchema: void 0,
|
|
112
239
|
OutputSchema: void 0,
|
|
113
|
-
|
|
240
|
+
errorMap: this["~orpc"].errorMap
|
|
114
241
|
});
|
|
115
242
|
}
|
|
116
243
|
input(schema, example) {
|
|
117
|
-
return new
|
|
244
|
+
return new ContractProcedureBuilderWithInput({
|
|
245
|
+
route: this["~orpc"].config.initialRoute,
|
|
118
246
|
InputSchema: schema,
|
|
119
247
|
inputExample: example,
|
|
120
|
-
OutputSchema: void 0
|
|
248
|
+
OutputSchema: void 0,
|
|
249
|
+
errorMap: this["~orpc"].errorMap
|
|
121
250
|
});
|
|
122
251
|
}
|
|
123
252
|
output(schema, example) {
|
|
124
|
-
return new
|
|
125
|
-
|
|
253
|
+
return new ContractProcedureBuilderWithOutput({
|
|
254
|
+
route: this["~orpc"].config.initialRoute,
|
|
126
255
|
OutputSchema: schema,
|
|
127
|
-
outputExample: example
|
|
256
|
+
outputExample: example,
|
|
257
|
+
InputSchema: void 0,
|
|
258
|
+
errorMap: this["~orpc"].errorMap
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
prefix(prefix) {
|
|
262
|
+
return new ContractRouterBuilder({
|
|
263
|
+
prefix,
|
|
264
|
+
errorMap: this["~orpc"].errorMap
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
tag(...tags) {
|
|
268
|
+
return new ContractRouterBuilder({
|
|
269
|
+
tags,
|
|
270
|
+
errorMap: this["~orpc"].errorMap
|
|
128
271
|
});
|
|
129
272
|
}
|
|
130
273
|
router(router) {
|
|
131
|
-
return
|
|
274
|
+
return new ContractRouterBuilder({
|
|
275
|
+
errorMap: this["~orpc"].errorMap
|
|
276
|
+
}).router(router);
|
|
132
277
|
}
|
|
133
278
|
};
|
|
134
279
|
|
|
135
|
-
// src/
|
|
136
|
-
|
|
137
|
-
var
|
|
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"
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
function fallbackORPCErrorStatus(code, status) {
|
|
361
|
+
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
362
|
+
}
|
|
363
|
+
function fallbackORPCErrorMessage(code, message) {
|
|
364
|
+
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
365
|
+
}
|
|
366
|
+
var ORPCError = class extends Error {
|
|
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
|
+
}
|
|
138
420
|
|
|
139
|
-
// src/
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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];
|
|
147
430
|
}
|
|
431
|
+
return [void 0, error, false];
|
|
148
432
|
}
|
|
149
433
|
}
|
|
150
434
|
|
|
151
|
-
// src/
|
|
152
|
-
|
|
153
|
-
|
|
435
|
+
// src/config.ts
|
|
436
|
+
var DEFAULT_CONFIG = {
|
|
437
|
+
defaultMethod: "POST",
|
|
438
|
+
defaultSuccessStatus: 200,
|
|
439
|
+
defaultSuccessDescription: "OK",
|
|
440
|
+
defaultInputStructure: "compact",
|
|
441
|
+
defaultOutputStructure: "compact"
|
|
442
|
+
};
|
|
443
|
+
function fallbackContractConfig(key, value) {
|
|
444
|
+
if (value === void 0) {
|
|
445
|
+
return DEFAULT_CONFIG[key];
|
|
446
|
+
}
|
|
447
|
+
return value;
|
|
154
448
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
449
|
+
|
|
450
|
+
// src/error.ts
|
|
451
|
+
var ValidationError = class extends Error {
|
|
452
|
+
issues;
|
|
453
|
+
constructor(options) {
|
|
454
|
+
super(options.message, options);
|
|
455
|
+
this.issues = options.issues;
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// src/schema-utils.ts
|
|
460
|
+
function type(...[map]) {
|
|
461
|
+
return {
|
|
462
|
+
"~standard": {
|
|
463
|
+
vendor: "custom",
|
|
464
|
+
version: 1,
|
|
465
|
+
async validate(value) {
|
|
466
|
+
if (map) {
|
|
467
|
+
return { value: await map(value) };
|
|
468
|
+
}
|
|
469
|
+
return { value };
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
};
|
|
163
473
|
}
|
|
164
474
|
|
|
165
475
|
// src/index.ts
|
|
166
|
-
var oc = new ContractBuilder(
|
|
476
|
+
var oc = new ContractBuilder({
|
|
477
|
+
errorMap: {},
|
|
478
|
+
InputSchema: void 0,
|
|
479
|
+
OutputSchema: void 0,
|
|
480
|
+
config: {}
|
|
481
|
+
});
|
|
167
482
|
export {
|
|
483
|
+
COMMON_ORPC_ERROR_DEFS,
|
|
168
484
|
ContractBuilder,
|
|
169
485
|
ContractProcedure,
|
|
486
|
+
ContractProcedureBuilder,
|
|
487
|
+
ContractProcedureBuilderWithInput,
|
|
488
|
+
ContractProcedureBuilderWithOutput,
|
|
489
|
+
ContractRouterBuilder,
|
|
170
490
|
DecoratedContractProcedure,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
491
|
+
ORPCError,
|
|
492
|
+
ValidationError,
|
|
493
|
+
fallbackContractConfig,
|
|
494
|
+
fallbackORPCErrorMessage,
|
|
495
|
+
fallbackORPCErrorStatus,
|
|
174
496
|
isContractProcedure,
|
|
497
|
+
isDefinedError,
|
|
175
498
|
oc,
|
|
176
|
-
|
|
177
|
-
|
|
499
|
+
safe,
|
|
500
|
+
type,
|
|
501
|
+
validateORPCError
|
|
178
502
|
};
|
|
179
503
|
//# sourceMappingURL=index.js.map
|
package/dist/src/builder.d.ts
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
|
|
2
|
+
import type { ContractProcedureDef, RouteOptions } from './procedure';
|
|
1
3
|
import type { ContractRouter } from './router';
|
|
4
|
+
import type { AdaptedContractRouter } from './router-builder';
|
|
2
5
|
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
3
|
-
import {
|
|
6
|
+
import { ContractProcedure } from './procedure';
|
|
7
|
+
import { ContractProcedureBuilder } from './procedure-builder';
|
|
8
|
+
import { ContractProcedureBuilderWithInput } from './procedure-builder-with-input';
|
|
9
|
+
import { ContractProcedureBuilderWithOutput } from './procedure-builder-with-output';
|
|
4
10
|
import { ContractRouterBuilder } from './router-builder';
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export interface ContractBuilderConfig {
|
|
12
|
+
initialRoute?: RouteOptions;
|
|
13
|
+
}
|
|
14
|
+
export interface ContractBuilderDef<TErrorMap extends ErrorMap> extends ContractProcedureDef<undefined, undefined, TErrorMap> {
|
|
15
|
+
config: ContractBuilderConfig;
|
|
16
|
+
}
|
|
17
|
+
export declare class ContractBuilder<TErrorMap extends ErrorMap> extends ContractProcedure<undefined, undefined, TErrorMap> {
|
|
18
|
+
'~orpc': ContractBuilderDef<TErrorMap>;
|
|
19
|
+
constructor(def: ContractBuilderDef<TErrorMap>);
|
|
20
|
+
config(config: ContractBuilderConfig): ContractBuilder<TErrorMap>;
|
|
21
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractBuilder<U & TErrorMap>;
|
|
22
|
+
route(route: RouteOptions): ContractProcedureBuilder<TErrorMap>;
|
|
23
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): ContractProcedureBuilderWithInput<U, TErrorMap>;
|
|
24
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): ContractProcedureBuilderWithOutput<U, TErrorMap>;
|
|
25
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap>;
|
|
26
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap>;
|
|
27
|
+
router<T extends ContractRouter<ErrorMap & Partial<StrictErrorMap<TErrorMap>>>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
12
28
|
}
|
|
13
29
|
//# 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
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HTTPMethod, InputStructure } from './types';
|
|
2
|
+
export interface ContractConfig {
|
|
3
|
+
defaultMethod: HTTPMethod;
|
|
4
|
+
defaultSuccessStatus: number;
|
|
5
|
+
defaultSuccessDescription: string;
|
|
6
|
+
defaultInputStructure: InputStructure;
|
|
7
|
+
defaultOutputStructure: InputStructure;
|
|
8
|
+
}
|
|
9
|
+
export declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
10
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { CommonORPCErrorCode } from './error-orpc';
|
|
2
|
+
import type { Schema } from './types';
|
|
3
|
+
export type ErrorMapItem<TDataSchema extends Schema> = {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @default 500
|
|
7
|
+
*/
|
|
8
|
+
status?: number;
|
|
9
|
+
message?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
data?: TDataSchema;
|
|
12
|
+
};
|
|
13
|
+
export interface ErrorMap {
|
|
14
|
+
[k: string]: ErrorMapItem<Schema>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions
|
|
18
|
+
*
|
|
19
|
+
* Purpose:
|
|
20
|
+
* - Helps `U` suggest `CommonORPCErrorCode` to the user when typing.
|
|
21
|
+
*
|
|
22
|
+
* Why not replace `ErrorMap` with `ErrorMapSuggestions`?
|
|
23
|
+
* - `ErrorMapSuggestions` has a drawback: it allows `undefined` values for items.
|
|
24
|
+
* - `ErrorMapGuard<TErrorMap>` uses `Partial`, which can introduce `undefined` values.
|
|
25
|
+
*
|
|
26
|
+
* This could lead to unintended behavior where `undefined` values override `TErrorMap`,
|
|
27
|
+
* potentially resulting in a `never` type after merging.
|
|
28
|
+
*
|
|
29
|
+
* Recommendation:
|
|
30
|
+
* - Use `ErrorMapSuggestions` to assist users in typing correctly but do not replace `ErrorMap`.
|
|
31
|
+
* - Ensure `ErrorMapGuard<TErrorMap>` is adjusted to prevent `undefined` values.
|
|
32
|
+
*/
|
|
33
|
+
export type ErrorMapSuggestions = {
|
|
34
|
+
[key in CommonORPCErrorCode | (string & {})]?: ErrorMapItem<Schema>;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* `U` extends `ErrorMap` & `ErrorMapGuard<TErrorMap>`
|
|
38
|
+
*
|
|
39
|
+
* `ErrorMapGuard` is a utility type that ensures `U` cannot redefine the structure of `TErrorMap`.
|
|
40
|
+
* It achieves this by setting each key in `TErrorMap` to `never`, effectively preventing any redefinition.
|
|
41
|
+
*
|
|
42
|
+
* Why not just use `Partial<TErrorMap>`?
|
|
43
|
+
* - Allowing users to redefine existing error map items would require using `StrictErrorMap`.
|
|
44
|
+
* - However, I prefer not to use `StrictErrorMap` frequently, due to perceived performance concerns,
|
|
45
|
+
* though this has not been benchmarked and is based on personal preference.
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export type ErrorMapGuard<TErrorMap extends ErrorMap> = {
|
|
49
|
+
[K in keyof TErrorMap]?: never;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Since `undefined` has a specific meaning (it use default value),
|
|
53
|
+
* we ensure all additional properties in each item of the ErrorMap are explicitly set to `undefined`.
|
|
54
|
+
*/
|
|
55
|
+
export type StrictErrorMap<T extends ErrorMap> = {
|
|
56
|
+
[K in keyof T]: T[K] & Partial<Record<Exclude<keyof ErrorMapItem<any>, keyof T[K]>, undefined>>;
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=error-map.d.ts.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapItem } from './error-map';
|
|
2
|
+
import type { SchemaOutput } from './types';
|
|
3
|
+
export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
4
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never;
|
|
5
|
+
}[keyof TErrorMap];
|
|
6
|
+
export declare const COMMON_ORPC_ERROR_DEFS: {
|
|
7
|
+
readonly BAD_REQUEST: {
|
|
8
|
+
readonly status: 400;
|
|
9
|
+
readonly message: "Bad Request";
|
|
10
|
+
};
|
|
11
|
+
readonly UNAUTHORIZED: {
|
|
12
|
+
readonly status: 401;
|
|
13
|
+
readonly message: "Unauthorized";
|
|
14
|
+
};
|
|
15
|
+
readonly FORBIDDEN: {
|
|
16
|
+
readonly status: 403;
|
|
17
|
+
readonly message: "Forbidden";
|
|
18
|
+
};
|
|
19
|
+
readonly NOT_FOUND: {
|
|
20
|
+
readonly status: 404;
|
|
21
|
+
readonly message: "Not Found";
|
|
22
|
+
};
|
|
23
|
+
readonly METHOD_NOT_SUPPORTED: {
|
|
24
|
+
readonly status: 405;
|
|
25
|
+
readonly message: "Method Not Supported";
|
|
26
|
+
};
|
|
27
|
+
readonly NOT_ACCEPTABLE: {
|
|
28
|
+
readonly status: 406;
|
|
29
|
+
readonly message: "Not Acceptable";
|
|
30
|
+
};
|
|
31
|
+
readonly TIMEOUT: {
|
|
32
|
+
readonly status: 408;
|
|
33
|
+
readonly message: "Request Timeout";
|
|
34
|
+
};
|
|
35
|
+
readonly CONFLICT: {
|
|
36
|
+
readonly status: 409;
|
|
37
|
+
readonly message: "Conflict";
|
|
38
|
+
};
|
|
39
|
+
readonly PRECONDITION_FAILED: {
|
|
40
|
+
readonly status: 412;
|
|
41
|
+
readonly message: "Precondition Failed";
|
|
42
|
+
};
|
|
43
|
+
readonly PAYLOAD_TOO_LARGE: {
|
|
44
|
+
readonly status: 413;
|
|
45
|
+
readonly message: "Payload Too Large";
|
|
46
|
+
};
|
|
47
|
+
readonly UNSUPPORTED_MEDIA_TYPE: {
|
|
48
|
+
readonly status: 415;
|
|
49
|
+
readonly message: "Unsupported Media Type";
|
|
50
|
+
};
|
|
51
|
+
readonly UNPROCESSABLE_CONTENT: {
|
|
52
|
+
readonly status: 422;
|
|
53
|
+
readonly message: "Unprocessable Content";
|
|
54
|
+
};
|
|
55
|
+
readonly TOO_MANY_REQUESTS: {
|
|
56
|
+
readonly status: 429;
|
|
57
|
+
readonly message: "Too Many Requests";
|
|
58
|
+
};
|
|
59
|
+
readonly CLIENT_CLOSED_REQUEST: {
|
|
60
|
+
readonly status: 499;
|
|
61
|
+
readonly message: "Client Closed Request";
|
|
62
|
+
};
|
|
63
|
+
readonly INTERNAL_SERVER_ERROR: {
|
|
64
|
+
readonly status: 500;
|
|
65
|
+
readonly message: "Internal Server Error";
|
|
66
|
+
};
|
|
67
|
+
readonly NOT_IMPLEMENTED: {
|
|
68
|
+
readonly status: 501;
|
|
69
|
+
readonly message: "Not Implemented";
|
|
70
|
+
};
|
|
71
|
+
readonly BAD_GATEWAY: {
|
|
72
|
+
readonly status: 502;
|
|
73
|
+
readonly message: "Bad Gateway";
|
|
74
|
+
};
|
|
75
|
+
readonly SERVICE_UNAVAILABLE: {
|
|
76
|
+
readonly status: 503;
|
|
77
|
+
readonly message: "Service Unavailable";
|
|
78
|
+
};
|
|
79
|
+
readonly GATEWAY_TIMEOUT: {
|
|
80
|
+
readonly status: 504;
|
|
81
|
+
readonly message: "Gateway Timeout";
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export type CommonORPCErrorCode = keyof typeof COMMON_ORPC_ERROR_DEFS;
|
|
85
|
+
export type ORPCErrorOptions<TCode extends string, TData> = ErrorOptions & {
|
|
86
|
+
defined?: boolean;
|
|
87
|
+
code: TCode;
|
|
88
|
+
status?: number;
|
|
89
|
+
message?: string;
|
|
90
|
+
} & (undefined extends TData ? {
|
|
91
|
+
data?: TData;
|
|
92
|
+
} : {
|
|
93
|
+
data: TData;
|
|
94
|
+
});
|
|
95
|
+
export declare function fallbackORPCErrorStatus(code: CommonORPCErrorCode | (string & {}), status: number | undefined): number;
|
|
96
|
+
export declare function fallbackORPCErrorMessage(code: CommonORPCErrorCode | (string & {}), message: string | undefined): string;
|
|
97
|
+
export declare class ORPCError<TCode extends CommonORPCErrorCode | (string & {}), TData> extends Error {
|
|
98
|
+
readonly defined: boolean;
|
|
99
|
+
readonly code: TCode;
|
|
100
|
+
readonly status: number;
|
|
101
|
+
readonly data: TData;
|
|
102
|
+
constructor(options: ORPCErrorOptions<TCode, TData>);
|
|
103
|
+
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
104
|
+
static isValidJSON(json: unknown): json is ORPCErrorJSON<string, unknown>;
|
|
105
|
+
}
|
|
106
|
+
export type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
|
|
107
|
+
export declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
|
|
108
|
+
export declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
|
109
|
+
//# sourceMappingURL=error-orpc.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import type { ErrorMap } from './error-map';
|
|
3
|
+
import type { ORPCErrorFromErrorMap } from './error-orpc';
|
|
4
|
+
export type ErrorFromErrorMap<TErrorMap extends ErrorMap> = Error | ORPCErrorFromErrorMap<TErrorMap>;
|
|
5
|
+
export interface ValidationErrorOptions extends ErrorOptions {
|
|
6
|
+
message: string;
|
|
7
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
8
|
+
}
|
|
9
|
+
export declare class ValidationError extends Error {
|
|
10
|
+
readonly issues: readonly StandardSchemaV1.Issue[];
|
|
11
|
+
constructor(options: ValidationErrorOptions);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=error.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
/** unnoq */
|
|
2
2
|
import { ContractBuilder } from './builder';
|
|
3
3
|
export * from './builder';
|
|
4
|
-
export * from './
|
|
4
|
+
export * from './client';
|
|
5
|
+
export * from './client-utils';
|
|
6
|
+
export * from './config';
|
|
7
|
+
export * from './error';
|
|
8
|
+
export * from './error-map';
|
|
9
|
+
export * from './error-orpc';
|
|
5
10
|
export * from './procedure';
|
|
11
|
+
export * from './procedure-builder';
|
|
12
|
+
export * from './procedure-builder-with-input';
|
|
13
|
+
export * from './procedure-builder-with-output';
|
|
14
|
+
export * from './procedure-client';
|
|
15
|
+
export * from './procedure-decorated';
|
|
6
16
|
export * from './router';
|
|
17
|
+
export * from './router-builder';
|
|
18
|
+
export * from './router-client';
|
|
19
|
+
export * from './schema-utils';
|
|
7
20
|
export * from './types';
|
|
8
|
-
export
|
|
9
|
-
export declare const oc: ContractBuilder;
|
|
21
|
+
export declare const oc: ContractBuilder<Record<never, never>>;
|
|
10
22
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema, SchemaOutput } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
6
|
+
/**
|
|
7
|
+
* `ContractProcedureBuilderWithInput` is a branch of `ContractProcedureBuilder` which it has input schema.
|
|
8
|
+
*
|
|
9
|
+
* Why?
|
|
10
|
+
* - prevents override input schema after .input
|
|
11
|
+
*/
|
|
12
|
+
export declare class ContractProcedureBuilderWithInput<TInputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<TInputSchema, undefined, TErrorMap> {
|
|
13
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap & U>;
|
|
14
|
+
route(route: RouteOptions): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap>;
|
|
15
|
+
prefix(prefix: HTTPPath): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap>;
|
|
16
|
+
unshiftTag(...tags: string[]): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap>;
|
|
17
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<TInputSchema, U, TErrorMap>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=procedure-builder-with-input.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema, SchemaInput } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
6
|
+
/**
|
|
7
|
+
* `ContractProcedureBuilderWithOutput` is a branch of `ContractProcedureBuilder` which it has output schema.
|
|
8
|
+
*
|
|
9
|
+
* Why?
|
|
10
|
+
* - prevents override output schema after .output
|
|
11
|
+
*/
|
|
12
|
+
export declare class ContractProcedureBuilderWithOutput<TOutputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<undefined, TOutputSchema, TErrorMap> {
|
|
13
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap & U>;
|
|
14
|
+
route(route: RouteOptions): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
15
|
+
prefix(prefix: HTTPPath): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
16
|
+
unshiftTag(...tags: string[]): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
17
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, TOutputSchema, TErrorMap>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=procedure-builder-with-output.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { ContractProcedureBuilderWithInput } from './procedure-builder-with-input';
|
|
6
|
+
import { ContractProcedureBuilderWithOutput } from './procedure-builder-with-output';
|
|
7
|
+
export declare class ContractProcedureBuilder<TErrorMap extends ErrorMap> extends ContractProcedure<undefined, undefined, TErrorMap> {
|
|
8
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractProcedureBuilder<TErrorMap & U>;
|
|
9
|
+
route(route: RouteOptions): ContractProcedureBuilder<TErrorMap>;
|
|
10
|
+
prefix(prefix: HTTPPath): ContractProcedureBuilder<TErrorMap>;
|
|
11
|
+
unshiftTag(...tags: string[]): ContractProcedureBuilder<TErrorMap>;
|
|
12
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): ContractProcedureBuilderWithInput<U, TErrorMap>;
|
|
13
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): ContractProcedureBuilderWithOutput<U, TErrorMap>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=procedure-builder.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Client } from './client';
|
|
2
|
+
import type { ErrorFromErrorMap } from './error';
|
|
3
|
+
import type { ErrorMap } from './error-map';
|
|
4
|
+
import type { Schema, SchemaInput, SchemaOutput } from './types';
|
|
5
|
+
export type ContractProcedureClient<TClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
6
|
+
//# sourceMappingURL=procedure-client.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap> {
|
|
6
|
+
static decorate<UInputSchema extends Schema, UOutputSchema extends Schema, TErrorMap extends ErrorMap>(procedure: ContractProcedure<UInputSchema, UOutputSchema, TErrorMap>): DecoratedContractProcedure<UInputSchema, UOutputSchema, TErrorMap>;
|
|
7
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap & U>;
|
|
8
|
+
route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
9
|
+
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
10
|
+
unshiftTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=procedure-decorated.d.ts.map
|
package/dist/src/procedure.d.ts
CHANGED
|
@@ -1,47 +1,83 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ErrorMap } from './error-map';
|
|
2
|
+
import type { HTTPMethod, HTTPPath, InputStructure, OutputStructure, Schema, SchemaOutput } from './types';
|
|
2
3
|
export interface RouteOptions {
|
|
3
4
|
method?: HTTPMethod;
|
|
4
5
|
path?: HTTPPath;
|
|
5
6
|
summary?: string;
|
|
6
7
|
description?: string;
|
|
7
8
|
deprecated?: boolean;
|
|
8
|
-
tags?: string[];
|
|
9
|
+
tags?: readonly string[];
|
|
10
|
+
/**
|
|
11
|
+
* The status code of the response when the procedure is successful.
|
|
12
|
+
*
|
|
13
|
+
* @default 200
|
|
14
|
+
*/
|
|
15
|
+
successStatus?: number;
|
|
16
|
+
/**
|
|
17
|
+
* The description of the response when the procedure is successful.
|
|
18
|
+
*
|
|
19
|
+
* @default 'OK'
|
|
20
|
+
*/
|
|
21
|
+
successDescription?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
|
|
24
|
+
*
|
|
25
|
+
* @option 'compact'
|
|
26
|
+
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
|
|
27
|
+
*
|
|
28
|
+
* @option 'detailed'
|
|
29
|
+
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
|
|
30
|
+
*
|
|
31
|
+
* Example:
|
|
32
|
+
* ```ts
|
|
33
|
+
* const input = {
|
|
34
|
+
* params: { id: 1 },
|
|
35
|
+
* query: { search: 'hello' },
|
|
36
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
37
|
+
* body: { name: 'John' },
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @default 'compact'
|
|
42
|
+
*/
|
|
43
|
+
inputStructure?: InputStructure;
|
|
44
|
+
/**
|
|
45
|
+
* Determines how the response should be structured based on the output.
|
|
46
|
+
*
|
|
47
|
+
* @option 'compact'
|
|
48
|
+
* Includes only the body data, encoded directly in the response.
|
|
49
|
+
*
|
|
50
|
+
* @option 'detailed'
|
|
51
|
+
* Separates the output into `headers` and `body` fields.
|
|
52
|
+
* - `headers`: Custom headers to merge with the response headers.
|
|
53
|
+
* - `body`: The response data.
|
|
54
|
+
*
|
|
55
|
+
* Example:
|
|
56
|
+
* ```ts
|
|
57
|
+
* const output = {
|
|
58
|
+
* headers: { 'x-custom-header': 'value' },
|
|
59
|
+
* body: { message: 'Hello, world!' },
|
|
60
|
+
* };
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @default 'compact'
|
|
64
|
+
*/
|
|
65
|
+
outputStructure?: OutputStructure;
|
|
9
66
|
}
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
tags?: string[];
|
|
18
|
-
InputSchema: TInputSchema;
|
|
19
|
-
inputExample?: SchemaOutput<TInputSchema>;
|
|
20
|
-
OutputSchema: TOutputSchema;
|
|
21
|
-
outputExample?: SchemaOutput<TOutputSchema>;
|
|
22
|
-
};
|
|
23
|
-
constructor(zz$cp: {
|
|
24
|
-
path?: HTTPPath;
|
|
25
|
-
method?: HTTPMethod;
|
|
26
|
-
summary?: string;
|
|
27
|
-
description?: string;
|
|
28
|
-
deprecated?: boolean;
|
|
29
|
-
tags?: string[];
|
|
30
|
-
InputSchema: TInputSchema;
|
|
31
|
-
inputExample?: SchemaOutput<TInputSchema>;
|
|
32
|
-
OutputSchema: TOutputSchema;
|
|
33
|
-
outputExample?: SchemaOutput<TOutputSchema>;
|
|
34
|
-
});
|
|
67
|
+
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
|
|
68
|
+
route?: RouteOptions;
|
|
69
|
+
InputSchema: TInputSchema;
|
|
70
|
+
inputExample?: SchemaOutput<TInputSchema>;
|
|
71
|
+
OutputSchema: TOutputSchema;
|
|
72
|
+
outputExample?: SchemaOutput<TOutputSchema>;
|
|
73
|
+
errorMap: TErrorMap;
|
|
35
74
|
}
|
|
36
|
-
export
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
41
|
-
addTags(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
42
|
-
input<USchema extends Schema>(schema: USchema, example?: SchemaInput<USchema>): DecoratedContractProcedure<USchema, TOutputSchema>;
|
|
43
|
-
output<USchema extends Schema>(schema: USchema, example?: SchemaOutput<USchema>): DecoratedContractProcedure<TInputSchema, USchema>;
|
|
75
|
+
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
|
|
76
|
+
'~type': "ContractProcedure";
|
|
77
|
+
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>;
|
|
78
|
+
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>);
|
|
44
79
|
}
|
|
45
|
-
export type
|
|
46
|
-
export
|
|
80
|
+
export type ANY_CONTRACT_PROCEDURE = ContractProcedure<any, any, any>;
|
|
81
|
+
export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema, ErrorMap>;
|
|
82
|
+
export declare function isContractProcedure(item: unknown): item is ANY_CONTRACT_PROCEDURE;
|
|
47
83
|
//# sourceMappingURL=procedure.d.ts.map
|
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
|
|
2
|
+
import type { ContractProcedure } from './procedure';
|
|
3
|
+
import type { ContractRouter } from './router';
|
|
2
4
|
import type { HTTPPath } from './types';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
6
|
+
export type AdaptedContractRouter<TContract extends ContractRouter<any>, TErrorMapExtra extends ErrorMap> = {
|
|
7
|
+
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors> ? DecoratedContractProcedure<UInputSchema, UOutputSchema, UErrors & TErrorMapExtra> : TContract[K] extends ContractRouter<any> ? AdaptedContractRouter<TContract[K], TErrorMapExtra> : never;
|
|
8
|
+
};
|
|
9
|
+
export interface ContractRouterBuilderDef<TErrorMap extends ErrorMap> {
|
|
10
|
+
prefix?: HTTPPath;
|
|
11
|
+
tags?: string[];
|
|
12
|
+
errorMap: TErrorMap;
|
|
13
|
+
}
|
|
14
|
+
export declare class ContractRouterBuilder<TErrorMap extends ErrorMap> {
|
|
15
|
+
'~type': "ContractProcedure";
|
|
16
|
+
'~orpc': ContractRouterBuilderDef<TErrorMap>;
|
|
17
|
+
constructor(def: ContractRouterBuilderDef<TErrorMap>);
|
|
18
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap>;
|
|
19
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap>;
|
|
20
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractRouterBuilder<U & TErrorMap>;
|
|
21
|
+
router<T extends ContractRouter<ErrorMap & Partial<StrictErrorMap<TErrorMap>>>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
15
22
|
}
|
|
16
23
|
//# sourceMappingURL=router-builder.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ContractProcedure } from './procedure';
|
|
2
|
+
import type { ContractProcedureClient } from './procedure-client';
|
|
3
|
+
import type { ContractRouter } from './router';
|
|
4
|
+
export type ContractRouterClient<TRouter extends ContractRouter<any>, TClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
|
|
5
|
+
[K in keyof TRouter]: TRouter[K] extends ContractRouter<any> ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=router-client.d.ts.map
|
package/dist/src/router.d.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
+
import type { ErrorMap } from './error-map';
|
|
2
|
+
import type { ContractProcedure } from './procedure';
|
|
1
3
|
import type { SchemaInput, SchemaOutput } from './types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
[k: string]: ContractProcedure<any, any> | ContractRouter;
|
|
5
|
-
}
|
|
6
|
-
export type HandledContractRouter<TContract extends ContractRouter> = {
|
|
7
|
-
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? DecoratedContractProcedure<UInputSchema, UOutputSchema> : TContract[K] extends ContractRouter ? HandledContractRouter<TContract[K]> : never;
|
|
4
|
+
export type ContractRouter<T extends ErrorMap> = ContractProcedure<any, any, T> | {
|
|
5
|
+
[k: string]: ContractRouter<T>;
|
|
8
6
|
};
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
[K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
7
|
+
export type InferContractRouterInputs<T extends ContractRouter<any>> = T extends ContractProcedure<infer UInputSchema, any, any> ? SchemaInput<UInputSchema> : {
|
|
8
|
+
[K in keyof T]: T[K] extends ContractRouter<any> ? InferContractRouterInputs<T[K]> : never;
|
|
12
9
|
};
|
|
13
|
-
export type InferContractRouterOutputs<T extends ContractRouter
|
|
14
|
-
[K in keyof T]: T[K] extends
|
|
10
|
+
export type InferContractRouterOutputs<T extends ContractRouter<any>> = T extends ContractProcedure<any, infer UOutputSchema, any> ? SchemaOutput<UOutputSchema> : {
|
|
11
|
+
[K in keyof T]: T[K] extends ContractRouter<any> ? InferContractRouterOutputs<T[K]> : never;
|
|
15
12
|
};
|
|
16
13
|
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { IsEqual, Promisable } from '@orpc/shared';
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
export type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
4
|
+
export declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): StandardSchemaV1<TInput, TOutput>;
|
|
5
|
+
//# sourceMappingURL=schema-utils.d.ts.map
|
package/dist/src/types.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FindGlobalInstanceType } from '@orpc/shared';
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
3
|
export type HTTPPath = `/${string}`;
|
|
3
4
|
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
7
|
-
export type
|
|
5
|
+
export type InputStructure = 'compact' | 'detailed';
|
|
6
|
+
export type OutputStructure = 'compact' | 'detailed';
|
|
7
|
+
export type Schema = StandardSchemaV1 | undefined;
|
|
8
|
+
export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
|
|
9
|
+
export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
|
|
10
|
+
export type AbortSignal = FindGlobalInstanceType<'AbortSignal'>;
|
|
8
11
|
//# sourceMappingURL=types.d.ts.map
|
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.a2e4a58",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -28,11 +28,14 @@
|
|
|
28
28
|
"!**/*.tsbuildinfo",
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
|
-
"peerDependencies": {
|
|
32
|
-
"zod": ">=3.23.0"
|
|
33
|
-
},
|
|
34
31
|
"dependencies": {
|
|
35
|
-
"@
|
|
32
|
+
"@standard-schema/spec": "1.0.0-beta.4",
|
|
33
|
+
"@orpc/shared": "0.0.0-next.a2e4a58"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"arktype": "2.0.0-rc.26",
|
|
37
|
+
"valibot": "1.0.0-beta.9",
|
|
38
|
+
"zod": "3.24.1"
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
41
|
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
package/dist/src/constants.d.ts
DELETED
package/dist/src/utils.d.ts
DELETED