@orpc/contract 0.0.0-next.f22c7ec → 0.0.0-next.f56d2b3
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 +173 -177
- package/dist/src/builder-variants.d.ts +38 -0
- package/dist/src/builder.d.ts +30 -21
- package/dist/src/client-utils.d.ts +1 -1
- package/dist/src/client.d.ts +3 -1
- package/dist/src/config.d.ts +8 -34
- package/dist/src/error-map.d.ts +6 -50
- package/dist/src/error-orpc.d.ts +10 -10
- package/dist/src/error-utils.d.ts +14 -0
- package/dist/src/index.d.ts +5 -4
- package/dist/src/meta.d.ts +3 -0
- package/dist/src/procedure-client.d.ts +1 -1
- package/dist/src/procedure.d.ts +13 -78
- package/dist/src/route.d.ts +79 -0
- package/dist/src/router-client.d.ts +3 -3
- package/dist/src/router.d.ts +25 -9
- package/dist/src/schema.d.ts +8 -0
- package/dist/src/types.d.ts +0 -8
- package/package.json +3 -3
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/router-builder.d.ts +0 -23
package/dist/index.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
+
// src/error-map.ts
|
|
2
|
+
function mergeErrorMap(errorMap1, errorMap2) {
|
|
3
|
+
return { ...errorMap1, ...errorMap2 };
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/meta.ts
|
|
7
|
+
function mergeMeta(meta1, meta2) {
|
|
8
|
+
return { ...meta1, ...meta2 };
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
// src/procedure.ts
|
|
2
12
|
var ContractProcedure = class {
|
|
3
|
-
"~type" = "ContractProcedure";
|
|
4
13
|
"~orpc";
|
|
5
14
|
constructor(def) {
|
|
6
15
|
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
7
16
|
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
8
17
|
}
|
|
9
|
-
if (Object.values(def.errorMap
|
|
18
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
|
|
10
19
|
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
|
|
11
20
|
}
|
|
12
21
|
this["~orpc"] = def;
|
|
@@ -16,180 +25,140 @@ function isContractProcedure(item) {
|
|
|
16
25
|
if (item instanceof ContractProcedure) {
|
|
17
26
|
return true;
|
|
18
27
|
}
|
|
19
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~
|
|
28
|
+
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
29
|
}
|
|
21
30
|
|
|
22
|
-
// src/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
31
|
+
// src/route.ts
|
|
32
|
+
function mergeRoute(a, b) {
|
|
33
|
+
return { ...a, ...b };
|
|
34
|
+
}
|
|
35
|
+
function prefixRoute(route, prefix) {
|
|
36
|
+
if (!route.path) {
|
|
37
|
+
return route;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
...route,
|
|
41
|
+
path: `${prefix}${route.path}`
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function unshiftTagRoute(route, tags) {
|
|
45
|
+
return {
|
|
46
|
+
...route,
|
|
47
|
+
tags: [...tags, ...route.tags ?? []]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function mergePrefix(a, b) {
|
|
51
|
+
return a ? `${a}${b}` : b;
|
|
52
|
+
}
|
|
53
|
+
function mergeTags(a, b) {
|
|
54
|
+
return a ? [...a, ...b] : b;
|
|
55
|
+
}
|
|
56
|
+
function adaptRoute(route, options) {
|
|
57
|
+
let router = route;
|
|
58
|
+
if (options.prefix) {
|
|
59
|
+
router = prefixRoute(router, options.prefix);
|
|
29
60
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
...this["~orpc"],
|
|
33
|
-
route: {
|
|
34
|
-
...this["~orpc"].route,
|
|
35
|
-
...route
|
|
36
|
-
}
|
|
37
|
-
});
|
|
61
|
+
if (options.tags) {
|
|
62
|
+
router = unshiftTagRoute(router, options.tags);
|
|
38
63
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
return router;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/router.ts
|
|
68
|
+
function adaptContractRouter(contract, options) {
|
|
69
|
+
if (isContractProcedure(contract)) {
|
|
70
|
+
const adapted2 = new ContractProcedure({
|
|
71
|
+
...contract["~orpc"],
|
|
72
|
+
errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap),
|
|
73
|
+
route: adaptRoute(contract["~orpc"].route, options)
|
|
48
74
|
});
|
|
75
|
+
return adapted2;
|
|
49
76
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
route: {
|
|
54
|
-
...this["~orpc"].route,
|
|
55
|
-
tags: [
|
|
56
|
-
...tags,
|
|
57
|
-
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
|
|
58
|
-
]
|
|
59
|
-
}
|
|
60
|
-
});
|
|
77
|
+
const adapted = {};
|
|
78
|
+
for (const key in contract) {
|
|
79
|
+
adapted[key] = adaptContractRouter(contract[key], options);
|
|
61
80
|
}
|
|
62
|
-
|
|
63
|
-
|
|
81
|
+
return adapted;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/builder.ts
|
|
85
|
+
var ContractBuilder = class _ContractBuilder extends ContractProcedure {
|
|
86
|
+
constructor(def) {
|
|
87
|
+
super(def);
|
|
88
|
+
this["~orpc"].prefix = def.prefix;
|
|
89
|
+
this["~orpc"].tags = def.tags;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Reset initial meta
|
|
93
|
+
*/
|
|
94
|
+
$meta(initialMeta) {
|
|
95
|
+
return new _ContractBuilder({
|
|
64
96
|
...this["~orpc"],
|
|
65
|
-
|
|
66
|
-
inputExample: example
|
|
97
|
+
meta: initialMeta
|
|
67
98
|
});
|
|
68
99
|
}
|
|
69
|
-
|
|
70
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Reset initial route
|
|
102
|
+
*/
|
|
103
|
+
$route(initialRoute) {
|
|
104
|
+
return new _ContractBuilder({
|
|
71
105
|
...this["~orpc"],
|
|
72
|
-
|
|
73
|
-
outputExample: example
|
|
106
|
+
route: initialRoute
|
|
74
107
|
});
|
|
75
108
|
}
|
|
76
109
|
errors(errors) {
|
|
77
|
-
return new
|
|
110
|
+
return new _ContractBuilder({
|
|
78
111
|
...this["~orpc"],
|
|
79
|
-
errorMap:
|
|
80
|
-
...this["~orpc"].errorMap,
|
|
81
|
-
...errors
|
|
82
|
-
}
|
|
112
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
83
113
|
});
|
|
84
114
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// src/router-builder.ts
|
|
88
|
-
var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
89
|
-
"~type" = "ContractProcedure";
|
|
90
|
-
"~orpc";
|
|
91
|
-
constructor(def) {
|
|
92
|
-
this["~orpc"] = def;
|
|
93
|
-
}
|
|
94
|
-
prefix(prefix) {
|
|
95
|
-
return new _ContractRouterBuilder({
|
|
115
|
+
meta(meta) {
|
|
116
|
+
return new _ContractBuilder({
|
|
96
117
|
...this["~orpc"],
|
|
97
|
-
|
|
118
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
98
119
|
});
|
|
99
120
|
}
|
|
100
|
-
|
|
101
|
-
return new
|
|
121
|
+
route(route) {
|
|
122
|
+
return new _ContractBuilder({
|
|
102
123
|
...this["~orpc"],
|
|
103
|
-
|
|
124
|
+
route: mergeRoute(this["~orpc"].route, route)
|
|
104
125
|
});
|
|
105
126
|
}
|
|
106
|
-
|
|
107
|
-
return new
|
|
127
|
+
input(schema) {
|
|
128
|
+
return new _ContractBuilder({
|
|
108
129
|
...this["~orpc"],
|
|
109
|
-
|
|
110
|
-
...this["~orpc"].errorMap,
|
|
111
|
-
...errors
|
|
112
|
-
}
|
|
130
|
+
inputSchema: schema
|
|
113
131
|
});
|
|
114
132
|
}
|
|
115
|
-
|
|
116
|
-
if (isContractProcedure(router)) {
|
|
117
|
-
let decorated = DecoratedContractProcedure.decorate(router);
|
|
118
|
-
if (this["~orpc"].tags) {
|
|
119
|
-
decorated = decorated.unshiftTag(...this["~orpc"].tags);
|
|
120
|
-
}
|
|
121
|
-
if (this["~orpc"].prefix) {
|
|
122
|
-
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
123
|
-
}
|
|
124
|
-
decorated = decorated.errors(this["~orpc"].errorMap);
|
|
125
|
-
return decorated;
|
|
126
|
-
}
|
|
127
|
-
const adapted = {};
|
|
128
|
-
for (const key in router) {
|
|
129
|
-
adapted[key] = this.router(router[key]);
|
|
130
|
-
}
|
|
131
|
-
return adapted;
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
// src/builder.ts
|
|
136
|
-
var ContractBuilder = class _ContractBuilder {
|
|
137
|
-
"~type" = "ContractBuilder";
|
|
138
|
-
"~orpc";
|
|
139
|
-
constructor(def) {
|
|
140
|
-
this["~orpc"] = def;
|
|
141
|
-
}
|
|
142
|
-
errors(errors) {
|
|
133
|
+
output(schema) {
|
|
143
134
|
return new _ContractBuilder({
|
|
144
135
|
...this["~orpc"],
|
|
145
|
-
|
|
146
|
-
...this["~orpc"].errorMap,
|
|
147
|
-
...errors
|
|
148
|
-
}
|
|
136
|
+
outputSchema: schema
|
|
149
137
|
});
|
|
150
138
|
}
|
|
151
139
|
prefix(prefix) {
|
|
152
|
-
return new
|
|
153
|
-
|
|
154
|
-
|
|
140
|
+
return new _ContractBuilder({
|
|
141
|
+
...this["~orpc"],
|
|
142
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
155
143
|
});
|
|
156
144
|
}
|
|
157
145
|
tag(...tags) {
|
|
158
|
-
return new
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
route(route) {
|
|
164
|
-
return new DecoratedContractProcedure({
|
|
165
|
-
route,
|
|
166
|
-
InputSchema: void 0,
|
|
167
|
-
OutputSchema: void 0,
|
|
168
|
-
errorMap: this["~orpc"].errorMap
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
input(schema, example) {
|
|
172
|
-
return new DecoratedContractProcedure({
|
|
173
|
-
InputSchema: schema,
|
|
174
|
-
inputExample: example,
|
|
175
|
-
OutputSchema: void 0,
|
|
176
|
-
errorMap: this["~orpc"].errorMap
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
output(schema, example) {
|
|
180
|
-
return new DecoratedContractProcedure({
|
|
181
|
-
OutputSchema: schema,
|
|
182
|
-
outputExample: example,
|
|
183
|
-
InputSchema: void 0,
|
|
184
|
-
errorMap: this["~orpc"].errorMap
|
|
146
|
+
return new _ContractBuilder({
|
|
147
|
+
...this["~orpc"],
|
|
148
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
|
185
149
|
});
|
|
186
150
|
}
|
|
187
151
|
router(router) {
|
|
188
|
-
return
|
|
189
|
-
errorMap: this["~orpc"].errorMap
|
|
190
|
-
}).router(router);
|
|
152
|
+
return adaptContractRouter(router, this["~orpc"]);
|
|
191
153
|
}
|
|
192
154
|
};
|
|
155
|
+
var oc = new ContractBuilder({
|
|
156
|
+
errorMap: {},
|
|
157
|
+
inputSchema: void 0,
|
|
158
|
+
outputSchema: void 0,
|
|
159
|
+
route: {},
|
|
160
|
+
meta: {}
|
|
161
|
+
});
|
|
193
162
|
|
|
194
163
|
// src/error-orpc.ts
|
|
195
164
|
import { isPlainObject } from "@orpc/shared";
|
|
@@ -277,21 +246,21 @@ function fallbackORPCErrorStatus(code, status) {
|
|
|
277
246
|
function fallbackORPCErrorMessage(code, message) {
|
|
278
247
|
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
279
248
|
}
|
|
280
|
-
var ORPCError = class extends Error {
|
|
249
|
+
var ORPCError = class _ORPCError extends Error {
|
|
281
250
|
defined;
|
|
282
251
|
code;
|
|
283
252
|
status;
|
|
284
253
|
data;
|
|
285
|
-
constructor(options) {
|
|
286
|
-
if (options
|
|
254
|
+
constructor(code, ...[options]) {
|
|
255
|
+
if (options?.status && (options.status < 400 || options.status >= 600)) {
|
|
287
256
|
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
288
257
|
}
|
|
289
|
-
const message = fallbackORPCErrorMessage(
|
|
258
|
+
const message = fallbackORPCErrorMessage(code, options?.message);
|
|
290
259
|
super(message, options);
|
|
291
|
-
this.code =
|
|
292
|
-
this.status = fallbackORPCErrorStatus(
|
|
293
|
-
this.defined = options
|
|
294
|
-
this.data = options
|
|
260
|
+
this.code = code;
|
|
261
|
+
this.status = fallbackORPCErrorStatus(code, options?.status);
|
|
262
|
+
this.defined = options?.defined ?? false;
|
|
263
|
+
this.data = options?.data;
|
|
295
264
|
}
|
|
296
265
|
toJSON() {
|
|
297
266
|
return {
|
|
@@ -302,34 +271,53 @@ var ORPCError = class extends Error {
|
|
|
302
271
|
data: this.data
|
|
303
272
|
};
|
|
304
273
|
}
|
|
274
|
+
static fromJSON(json) {
|
|
275
|
+
return new _ORPCError(json.code, json);
|
|
276
|
+
}
|
|
305
277
|
static isValidJSON(json) {
|
|
306
278
|
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";
|
|
307
279
|
}
|
|
308
280
|
};
|
|
281
|
+
|
|
282
|
+
// src/error-utils.ts
|
|
309
283
|
function isDefinedError(error) {
|
|
310
284
|
return error instanceof ORPCError && error.defined;
|
|
311
285
|
}
|
|
286
|
+
function createORPCErrorConstructorMap(errors) {
|
|
287
|
+
const proxy = new Proxy(errors, {
|
|
288
|
+
get(target, code) {
|
|
289
|
+
if (typeof code !== "string") {
|
|
290
|
+
return Reflect.get(target, code);
|
|
291
|
+
}
|
|
292
|
+
const item = (...[options]) => {
|
|
293
|
+
const config = errors[code];
|
|
294
|
+
return new ORPCError(code, {
|
|
295
|
+
defined: Boolean(config),
|
|
296
|
+
status: config?.status,
|
|
297
|
+
message: options?.message ?? config?.message,
|
|
298
|
+
data: options?.data,
|
|
299
|
+
cause: options?.cause
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
return item;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
return proxy;
|
|
306
|
+
}
|
|
312
307
|
async function validateORPCError(map, error) {
|
|
313
308
|
const { code, status, message, data, cause, defined } = error;
|
|
314
309
|
const config = map?.[error.code];
|
|
315
310
|
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
|
316
|
-
return defined ? new ORPCError({ defined: false,
|
|
311
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
|
317
312
|
}
|
|
318
313
|
if (!config.data) {
|
|
319
|
-
return defined ? error : new ORPCError({ defined: true,
|
|
314
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
|
320
315
|
}
|
|
321
316
|
const validated = await config.data["~standard"].validate(error.data);
|
|
322
317
|
if (validated.issues) {
|
|
323
|
-
return defined ? new ORPCError({ defined: false,
|
|
318
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
|
324
319
|
}
|
|
325
|
-
return new ORPCError({
|
|
326
|
-
defined: true,
|
|
327
|
-
code,
|
|
328
|
-
status,
|
|
329
|
-
message,
|
|
330
|
-
data: validated.value,
|
|
331
|
-
cause
|
|
332
|
-
});
|
|
320
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
|
333
321
|
}
|
|
334
322
|
|
|
335
323
|
// src/client-utils.ts
|
|
@@ -354,20 +342,9 @@ var DEFAULT_CONFIG = {
|
|
|
354
342
|
defaultInputStructure: "compact",
|
|
355
343
|
defaultOutputStructure: "compact"
|
|
356
344
|
};
|
|
357
|
-
|
|
358
|
-
function configGlobal(config) {
|
|
359
|
-
if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) {
|
|
360
|
-
throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299");
|
|
361
|
-
}
|
|
362
|
-
GLOBAL_CONFIG_REF.value = config;
|
|
363
|
-
}
|
|
364
|
-
function fallbackToGlobalConfig(key, value) {
|
|
345
|
+
function fallbackContractConfig(key, value) {
|
|
365
346
|
if (value === void 0) {
|
|
366
|
-
|
|
367
|
-
if (fallback === void 0) {
|
|
368
|
-
return DEFAULT_CONFIG[key];
|
|
369
|
-
}
|
|
370
|
-
return fallback;
|
|
347
|
+
return DEFAULT_CONFIG[key];
|
|
371
348
|
}
|
|
372
349
|
return value;
|
|
373
350
|
}
|
|
@@ -381,26 +358,45 @@ var ValidationError = class extends Error {
|
|
|
381
358
|
}
|
|
382
359
|
};
|
|
383
360
|
|
|
384
|
-
// src/
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
361
|
+
// src/schema.ts
|
|
362
|
+
function type(...[map]) {
|
|
363
|
+
return {
|
|
364
|
+
"~standard": {
|
|
365
|
+
vendor: "custom",
|
|
366
|
+
version: 1,
|
|
367
|
+
async validate(value) {
|
|
368
|
+
if (map) {
|
|
369
|
+
return { value: await map(value) };
|
|
370
|
+
}
|
|
371
|
+
return { value };
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
388
376
|
export {
|
|
389
377
|
COMMON_ORPC_ERROR_DEFS,
|
|
390
378
|
ContractBuilder,
|
|
391
379
|
ContractProcedure,
|
|
392
|
-
ContractRouterBuilder,
|
|
393
|
-
DecoratedContractProcedure,
|
|
394
380
|
ORPCError,
|
|
395
381
|
ValidationError,
|
|
396
|
-
|
|
382
|
+
adaptContractRouter,
|
|
383
|
+
adaptRoute,
|
|
384
|
+
createORPCErrorConstructorMap,
|
|
385
|
+
fallbackContractConfig,
|
|
397
386
|
fallbackORPCErrorMessage,
|
|
398
387
|
fallbackORPCErrorStatus,
|
|
399
|
-
fallbackToGlobalConfig,
|
|
400
388
|
isContractProcedure,
|
|
401
389
|
isDefinedError,
|
|
390
|
+
mergeErrorMap,
|
|
391
|
+
mergeMeta,
|
|
392
|
+
mergePrefix,
|
|
393
|
+
mergeRoute,
|
|
394
|
+
mergeTags,
|
|
402
395
|
oc,
|
|
396
|
+
prefixRoute,
|
|
403
397
|
safe,
|
|
398
|
+
type,
|
|
399
|
+
unshiftTagRoute,
|
|
404
400
|
validateORPCError
|
|
405
401
|
};
|
|
406
402
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ErrorMap, MergedErrorMap } from './error-map';
|
|
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
|
package/dist/src/builder.d.ts
CHANGED
|
@@ -1,23 +1,32 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { ContractRouter } from './router';
|
|
4
|
-
import type {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
export declare class ContractBuilder<TErrorMap extends ErrorMap> {
|
|
12
|
-
'~type': "ContractBuilder";
|
|
13
|
-
'~orpc': ContractBuilderDef<TErrorMap>;
|
|
14
|
-
constructor(def: ContractBuilderDef<TErrorMap>);
|
|
15
|
-
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractBuilder<U & TErrorMap>;
|
|
16
|
-
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap>;
|
|
17
|
-
tag(...tags: string[]): ContractRouterBuilder<TErrorMap>;
|
|
18
|
-
route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined, TErrorMap>;
|
|
19
|
-
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined, TErrorMap>;
|
|
20
|
-
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U, TErrorMap>;
|
|
21
|
-
router<T extends ContractRouter<ErrorMap & Partial<StrictErrorMap<TErrorMap>>>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
1
|
+
import type { ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithOutput, ContractRouterBuilder } from './builder-variants';
|
|
2
|
+
import type { ContractProcedureDef } from './procedure';
|
|
3
|
+
import type { AdaptContractRouterOptions, AdaptedContractRouter, ContractRouter } from './router';
|
|
4
|
+
import type { Schema } from './schema';
|
|
5
|
+
import { type ErrorMap, type MergedErrorMap } from './error-map';
|
|
6
|
+
import { type Meta } from './meta';
|
|
7
|
+
import { ContractProcedure } from './procedure';
|
|
8
|
+
import { type HTTPPath, type Route } from './route';
|
|
9
|
+
export interface ContractBuilderDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, AdaptContractRouterOptions<TErrorMap> {
|
|
22
10
|
}
|
|
11
|
+
export declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
|
12
|
+
'~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
13
|
+
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
14
|
+
/**
|
|
15
|
+
* Reset initial meta
|
|
16
|
+
*/
|
|
17
|
+
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U>;
|
|
18
|
+
/**
|
|
19
|
+
* Reset initial route
|
|
20
|
+
*/
|
|
21
|
+
$route(initialRoute: Route): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
22
|
+
errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
|
|
23
|
+
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
24
|
+
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
25
|
+
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
|
|
26
|
+
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
|
|
27
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
28
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
|
|
29
|
+
router<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
30
|
+
}
|
|
31
|
+
export declare const oc: ContractBuilder<undefined, undefined, {}, {}>;
|
|
23
32
|
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ClientPromiseResult } from './client';
|
|
2
|
-
import {
|
|
2
|
+
import type { ORPCError } from './error-orpc';
|
|
3
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
4
|
export declare function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
|
|
5
5
|
//# sourceMappingURL=client-utils.d.ts.map
|
package/dist/src/client.d.ts
CHANGED
|
@@ -8,7 +8,9 @@ export type ClientOptions<TClientContext> = {
|
|
|
8
8
|
});
|
|
9
9
|
export type ClientRest<TClientContext, TInput> = [input: TInput, options: ClientOptions<TClientContext>] | (undefined extends TInput & TClientContext ? [] : never) | (undefined extends TClientContext ? [input: TInput] : never);
|
|
10
10
|
export type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
|
11
|
-
|
|
11
|
+
__error?: {
|
|
12
|
+
type: TError;
|
|
13
|
+
};
|
|
12
14
|
};
|
|
13
15
|
export interface Client<TClientContext, TInput, TOutput, TError extends Error> {
|
|
14
16
|
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
package/dist/src/config.d.ts
CHANGED
|
@@ -1,36 +1,10 @@
|
|
|
1
|
-
import type { HTTPMethod, InputStructure } from './
|
|
2
|
-
export interface
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @default 200
|
|
10
|
-
*/
|
|
11
|
-
defaultSuccessStatus?: number;
|
|
12
|
-
/**
|
|
13
|
-
*
|
|
14
|
-
* @default 'OK'
|
|
15
|
-
*/
|
|
16
|
-
defaultSuccessDescription?: string;
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
19
|
-
* @default 'compact'
|
|
20
|
-
*/
|
|
21
|
-
defaultInputStructure?: InputStructure;
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @default 'compact'
|
|
25
|
-
*/
|
|
26
|
-
defaultOutputStructure?: InputStructure;
|
|
1
|
+
import type { HTTPMethod, InputStructure } from './route';
|
|
2
|
+
export interface ContractConfig {
|
|
3
|
+
defaultMethod: HTTPMethod;
|
|
4
|
+
defaultSuccessStatus: number;
|
|
5
|
+
defaultSuccessDescription: string;
|
|
6
|
+
defaultInputStructure: InputStructure;
|
|
7
|
+
defaultOutputStructure: InputStructure;
|
|
27
8
|
}
|
|
28
|
-
|
|
29
|
-
* Set the global configuration, this configuration can effect entire project
|
|
30
|
-
*/
|
|
31
|
-
export declare function configGlobal(config: ORPCConfig): void;
|
|
32
|
-
/**
|
|
33
|
-
* Fallback the value to the global config if it is undefined
|
|
34
|
-
*/
|
|
35
|
-
export declare function fallbackToGlobalConfig<T extends keyof ORPCConfig>(key: T, value: ORPCConfig[T]): Exclude<ORPCConfig[T], undefined>;
|
|
9
|
+
export declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
36
10
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/src/error-map.d.ts
CHANGED
|
@@ -1,58 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Schema } from './
|
|
1
|
+
import type { ORPCErrorCode } from './error-orpc';
|
|
2
|
+
import type { Schema } from './schema';
|
|
3
3
|
export type ErrorMapItem<TDataSchema extends Schema> = {
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @default 500
|
|
7
|
-
*/
|
|
8
4
|
status?: number;
|
|
9
5
|
message?: string;
|
|
10
6
|
description?: string;
|
|
11
7
|
data?: TDataSchema;
|
|
12
8
|
};
|
|
13
|
-
export
|
|
14
|
-
[
|
|
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>>;
|
|
9
|
+
export type ErrorMap = {
|
|
10
|
+
[key in ORPCErrorCode]?: ErrorMapItem<Schema>;
|
|
57
11
|
};
|
|
12
|
+
export type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
|
|
13
|
+
export declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
|
|
58
14
|
//# sourceMappingURL=error-map.d.ts.map
|
package/dist/src/error-orpc.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ErrorMap, ErrorMapItem } from './error-map';
|
|
2
|
-
import type { SchemaOutput } from './
|
|
2
|
+
import type { SchemaOutput } from './schema';
|
|
3
3
|
export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
4
4
|
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never;
|
|
5
5
|
}[keyof TErrorMap];
|
|
@@ -82,9 +82,11 @@ export declare const COMMON_ORPC_ERROR_DEFS: {
|
|
|
82
82
|
};
|
|
83
83
|
};
|
|
84
84
|
export type CommonORPCErrorCode = keyof typeof COMMON_ORPC_ERROR_DEFS;
|
|
85
|
-
export type
|
|
85
|
+
export type ORPCErrorCode = CommonORPCErrorCode | (string & {});
|
|
86
|
+
export declare function fallbackORPCErrorStatus(code: ORPCErrorCode, status: number | undefined): number;
|
|
87
|
+
export declare function fallbackORPCErrorMessage(code: ORPCErrorCode, message: string | undefined): string;
|
|
88
|
+
export type ORPCErrorOptions<TData> = ErrorOptions & {
|
|
86
89
|
defined?: boolean;
|
|
87
|
-
code: TCode;
|
|
88
90
|
status?: number;
|
|
89
91
|
message?: string;
|
|
90
92
|
} & (undefined extends TData ? {
|
|
@@ -92,18 +94,16 @@ export type ORPCErrorOptions<TCode extends string, TData> = ErrorOptions & {
|
|
|
92
94
|
} : {
|
|
93
95
|
data: TData;
|
|
94
96
|
});
|
|
95
|
-
export
|
|
96
|
-
export declare
|
|
97
|
-
export declare class ORPCError<TCode extends CommonORPCErrorCode | (string & {}), TData> extends Error {
|
|
97
|
+
export type ORPCErrorOptionsRest<TData> = [options: ORPCErrorOptions<TData>] | (undefined extends TData ? [] : never);
|
|
98
|
+
export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
|
98
99
|
readonly defined: boolean;
|
|
99
100
|
readonly code: TCode;
|
|
100
101
|
readonly status: number;
|
|
101
102
|
readonly data: TData;
|
|
102
|
-
constructor(
|
|
103
|
+
constructor(code: TCode, ...[options]: ORPCErrorOptionsRest<TData>);
|
|
103
104
|
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
104
|
-
static
|
|
105
|
+
static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>): ORPCError<TCode, TData>;
|
|
106
|
+
static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
|
|
105
107
|
}
|
|
106
108
|
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
109
|
//# sourceMappingURL=error-orpc.d.ts.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapItem } from './error-map';
|
|
2
|
+
import type { ORPCErrorCode, ORPCErrorOptions } from './error-orpc';
|
|
3
|
+
import type { SchemaInput } from './schema';
|
|
4
|
+
import { ORPCError } from './error-orpc';
|
|
5
|
+
export declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
|
|
6
|
+
export type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
|
7
|
+
export type ORPCErrorConstructorMapItemRest<TData> = [options: ORPCErrorConstructorMapItemOptions<TData>] | (undefined extends TData ? [] : never);
|
|
8
|
+
export type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: ORPCErrorConstructorMapItemRest<TInData>) => ORPCError<TCode, TInData>;
|
|
9
|
+
export type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
10
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
|
|
11
|
+
};
|
|
12
|
+
export declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
|
13
|
+
export declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
|
14
|
+
//# sourceMappingURL=error-utils.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
/** unnoq */
|
|
2
|
-
import { ContractBuilder } from './builder';
|
|
3
2
|
export * from './builder';
|
|
3
|
+
export * from './builder-variants';
|
|
4
4
|
export * from './client';
|
|
5
5
|
export * from './client-utils';
|
|
6
6
|
export * from './config';
|
|
7
7
|
export * from './error';
|
|
8
8
|
export * from './error-map';
|
|
9
9
|
export * from './error-orpc';
|
|
10
|
+
export * from './error-utils';
|
|
11
|
+
export * from './meta';
|
|
10
12
|
export * from './procedure';
|
|
11
13
|
export * from './procedure-client';
|
|
12
|
-
export * from './
|
|
14
|
+
export * from './route';
|
|
13
15
|
export * from './router';
|
|
14
|
-
export * from './router-builder';
|
|
15
16
|
export * from './router-client';
|
|
17
|
+
export * from './schema';
|
|
16
18
|
export * from './types';
|
|
17
|
-
export declare const oc: ContractBuilder<Record<never, never>>;
|
|
18
19
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Client } from './client';
|
|
2
2
|
import type { ErrorFromErrorMap } from './error';
|
|
3
3
|
import type { ErrorMap } from './error-map';
|
|
4
|
-
import type { Schema, SchemaInput, SchemaOutput } from './
|
|
4
|
+
import type { Schema, SchemaInput, SchemaOutput } from './schema';
|
|
5
5
|
export type ContractProcedureClient<TClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
6
6
|
//# sourceMappingURL=procedure-client.d.ts.map
|
package/dist/src/procedure.d.ts
CHANGED
|
@@ -1,83 +1,18 @@
|
|
|
1
1
|
import type { ErrorMap } from './error-map';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
66
|
-
}
|
|
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>;
|
|
2
|
+
import type { Meta } from './meta';
|
|
3
|
+
import type { Route } from './route';
|
|
4
|
+
import type { Schema } from './schema';
|
|
5
|
+
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
6
|
+
meta: TMeta;
|
|
7
|
+
route: Route;
|
|
8
|
+
inputSchema: TInputSchema;
|
|
9
|
+
outputSchema: TOutputSchema;
|
|
73
10
|
errorMap: TErrorMap;
|
|
74
11
|
}
|
|
75
|
-
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
|
|
76
|
-
'~
|
|
77
|
-
|
|
78
|
-
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>);
|
|
12
|
+
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
13
|
+
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
14
|
+
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
79
15
|
}
|
|
80
|
-
export type
|
|
81
|
-
export
|
|
82
|
-
export declare function isContractProcedure(item: unknown): item is ANY_CONTRACT_PROCEDURE;
|
|
16
|
+
export type AnyContractProcedure = ContractProcedure<any, any, any, any>;
|
|
17
|
+
export declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
|
|
83
18
|
//# sourceMappingURL=procedure.d.ts.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export type HTTPPath = `/${string}`;
|
|
2
|
+
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
3
|
+
export type InputStructure = 'compact' | 'detailed';
|
|
4
|
+
export type OutputStructure = 'compact' | 'detailed';
|
|
5
|
+
export interface Route {
|
|
6
|
+
method?: HTTPMethod;
|
|
7
|
+
path?: HTTPPath;
|
|
8
|
+
summary?: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
deprecated?: boolean;
|
|
11
|
+
tags?: readonly string[];
|
|
12
|
+
/**
|
|
13
|
+
* The status code of the response when the procedure is successful.
|
|
14
|
+
*
|
|
15
|
+
* @default 200
|
|
16
|
+
*/
|
|
17
|
+
successStatus?: number;
|
|
18
|
+
/**
|
|
19
|
+
* The description of the response when the procedure is successful.
|
|
20
|
+
*
|
|
21
|
+
* @default 'OK'
|
|
22
|
+
*/
|
|
23
|
+
successDescription?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
|
|
26
|
+
*
|
|
27
|
+
* @option 'compact'
|
|
28
|
+
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
|
|
29
|
+
*
|
|
30
|
+
* @option 'detailed'
|
|
31
|
+
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
|
|
32
|
+
*
|
|
33
|
+
* Example:
|
|
34
|
+
* ```ts
|
|
35
|
+
* const input = {
|
|
36
|
+
* params: { id: 1 },
|
|
37
|
+
* query: { search: 'hello' },
|
|
38
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
39
|
+
* body: { name: 'John' },
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @default 'compact'
|
|
44
|
+
*/
|
|
45
|
+
inputStructure?: InputStructure;
|
|
46
|
+
/**
|
|
47
|
+
* Determines how the response should be structured based on the output.
|
|
48
|
+
*
|
|
49
|
+
* @option 'compact'
|
|
50
|
+
* Includes only the body data, encoded directly in the response.
|
|
51
|
+
*
|
|
52
|
+
* @option 'detailed'
|
|
53
|
+
* Separates the output into `headers` and `body` fields.
|
|
54
|
+
* - `headers`: Custom headers to merge with the response headers.
|
|
55
|
+
* - `body`: The response data.
|
|
56
|
+
*
|
|
57
|
+
* Example:
|
|
58
|
+
* ```ts
|
|
59
|
+
* const output = {
|
|
60
|
+
* headers: { 'x-custom-header': 'value' },
|
|
61
|
+
* body: { message: 'Hello, world!' },
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @default 'compact'
|
|
66
|
+
*/
|
|
67
|
+
outputStructure?: OutputStructure;
|
|
68
|
+
}
|
|
69
|
+
export declare function mergeRoute(a: Route, b: Route): Route;
|
|
70
|
+
export declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
|
|
71
|
+
export declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
|
|
72
|
+
export declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
|
|
73
|
+
export declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
|
|
74
|
+
export interface AdaptRouteOptions {
|
|
75
|
+
prefix?: HTTPPath;
|
|
76
|
+
tags?: readonly string[];
|
|
77
|
+
}
|
|
78
|
+
export declare function adaptRoute(route: Route, options: AdaptRouteOptions): Route;
|
|
79
|
+
//# sourceMappingURL=route.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ContractProcedure } from './procedure';
|
|
2
2
|
import type { ContractProcedureClient } from './procedure-client';
|
|
3
|
-
import type {
|
|
4
|
-
export type ContractRouterClient<TRouter extends
|
|
5
|
-
[K in keyof TRouter]: TRouter[K] extends
|
|
3
|
+
import type { AnyContractRouter } from './router';
|
|
4
|
+
export type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
|
|
5
|
+
[K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
6
6
|
};
|
|
7
7
|
//# sourceMappingURL=router-client.d.ts.map
|
package/dist/src/router.d.ts
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { Meta } from './meta';
|
|
2
|
+
import type { SchemaInput, SchemaOutput } from './schema';
|
|
3
|
+
import { type ErrorMap, type MergedErrorMap } from './error-map';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { type HTTPPath } from './route';
|
|
6
|
+
export type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
|
|
7
|
+
[k: string]: ContractRouter<TMeta>;
|
|
6
8
|
};
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
+
export type AnyContractRouter = ContractRouter<any>;
|
|
10
|
+
export type AdaptedContractRouter<TContract extends AnyContractRouter, TErrorMap extends ErrorMap> = {
|
|
11
|
+
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : TContract[K] extends AnyContractRouter ? AdaptedContractRouter<TContract[K], TErrorMap> : never;
|
|
9
12
|
};
|
|
10
|
-
export
|
|
11
|
-
|
|
13
|
+
export interface AdaptContractRouterOptions<TErrorMap extends ErrorMap> {
|
|
14
|
+
errorMap: TErrorMap;
|
|
15
|
+
prefix?: HTTPPath;
|
|
16
|
+
tags?: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
export declare function adaptContractRouter<TRouter extends ContractRouter<any>, TErrorMap extends ErrorMap>(contract: TRouter, options: AdaptContractRouterOptions<TErrorMap>): AdaptedContractRouter<TRouter, TErrorMap>;
|
|
19
|
+
export type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? SchemaInput<UInputSchema> : {
|
|
20
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
12
21
|
};
|
|
22
|
+
export type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? SchemaOutput<UOutputSchema> : {
|
|
23
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
24
|
+
};
|
|
25
|
+
export type ContractRouterToErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
|
|
26
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? ContractRouterToErrorMap<T[K]> : never;
|
|
27
|
+
}[keyof T];
|
|
28
|
+
export type ContractRouterToMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
|
|
13
29
|
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IsEqual, Promisable } from '@orpc/shared';
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
export type Schema = StandardSchemaV1 | undefined;
|
|
4
|
+
export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
|
|
5
|
+
export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
|
|
6
|
+
export type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
7
|
+
export declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): StandardSchemaV1<TInput, TOutput>;
|
|
8
|
+
//# sourceMappingURL=schema.d.ts.map
|
package/dist/src/types.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
1
|
import type { FindGlobalInstanceType } from '@orpc/shared';
|
|
2
|
-
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
-
export type HTTPPath = `/${string}`;
|
|
4
|
-
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
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
2
|
export type AbortSignal = FindGlobalInstanceType<'AbortSignal'>;
|
|
11
3
|
//# 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.f56d2b3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@standard-schema/spec": "1.0.0-
|
|
33
|
-
"@orpc/shared": "0.0.0-next.
|
|
32
|
+
"@standard-schema/spec": "1.0.0-rc.0",
|
|
33
|
+
"@orpc/shared": "0.0.0-next.f56d2b3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"arktype": "2.0.0-rc.26",
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
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
|
-
route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
8
|
-
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
9
|
-
unshiftTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
10
|
-
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, TOutputSchema, TErrorMap>;
|
|
11
|
-
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<TInputSchema, U, TErrorMap>;
|
|
12
|
-
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap & U>;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=procedure-decorated.d.ts.map
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
|
|
2
|
-
import type { ContractProcedure } from './procedure';
|
|
3
|
-
import type { ContractRouter } from './router';
|
|
4
|
-
import type { HTTPPath } from './types';
|
|
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>;
|
|
22
|
-
}
|
|
23
|
-
//# sourceMappingURL=router-builder.d.ts.map
|