@orpc/contract 0.0.0-next.f22c7ec → 0.0.0-next.f4d410a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -0
- package/dist/index.d.mts +236 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.mjs +252 -0
- package/package.json +10 -13
- package/dist/index.js +0 -406
- package/dist/src/builder.d.ts +0 -23
- package/dist/src/client-utils.d.ts +0 -5
- package/dist/src/client.d.ts +0 -19
- package/dist/src/config.d.ts +0 -36
- package/dist/src/error-map.d.ts +0 -58
- package/dist/src/error-orpc.d.ts +0 -109
- package/dist/src/error.d.ts +0 -13
- package/dist/src/index.d.ts +0 -18
- package/dist/src/procedure-client.d.ts +0 -6
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/procedure.d.ts +0 -83
- package/dist/src/router-builder.d.ts +0 -23
- package/dist/src/router-client.d.ts +0 -7
- package/dist/src/router.d.ts +0 -13
- package/dist/src/types.d.ts +0 -11
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
|
+
export { ORPCError } from '@orpc/client';
|
|
3
|
+
import { isAsyncIteratorObject } from '@orpc/shared';
|
|
4
|
+
|
|
5
|
+
class ValidationError extends Error {
|
|
6
|
+
issues;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
super(options.message, options);
|
|
9
|
+
this.issues = options.issues;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function mergeErrorMap(errorMap1, errorMap2) {
|
|
13
|
+
return { ...errorMap1, ...errorMap2 };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function mergeMeta(meta1, meta2) {
|
|
17
|
+
return { ...meta1, ...meta2 };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class ContractProcedure {
|
|
21
|
+
"~orpc";
|
|
22
|
+
constructor(def) {
|
|
23
|
+
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
24
|
+
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
25
|
+
}
|
|
26
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
|
|
27
|
+
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
|
|
28
|
+
}
|
|
29
|
+
this["~orpc"] = def;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function isContractProcedure(item) {
|
|
33
|
+
if (item instanceof ContractProcedure) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function mergeRoute(a, b) {
|
|
40
|
+
return { ...a, ...b };
|
|
41
|
+
}
|
|
42
|
+
function prefixRoute(route, prefix) {
|
|
43
|
+
if (!route.path) {
|
|
44
|
+
return route;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
...route,
|
|
48
|
+
path: `${prefix}${route.path}`
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function unshiftTagRoute(route, tags) {
|
|
52
|
+
return {
|
|
53
|
+
...route,
|
|
54
|
+
tags: [...tags, ...route.tags ?? []]
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function mergePrefix(a, b) {
|
|
58
|
+
return a ? `${a}${b}` : b;
|
|
59
|
+
}
|
|
60
|
+
function mergeTags(a, b) {
|
|
61
|
+
return a ? [...a, ...b] : b;
|
|
62
|
+
}
|
|
63
|
+
function enhanceRoute(route, options) {
|
|
64
|
+
let router = route;
|
|
65
|
+
if (options.prefix) {
|
|
66
|
+
router = prefixRoute(router, options.prefix);
|
|
67
|
+
}
|
|
68
|
+
if (options.tags?.length) {
|
|
69
|
+
router = unshiftTagRoute(router, options.tags);
|
|
70
|
+
}
|
|
71
|
+
return router;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getContractRouter(router, path) {
|
|
75
|
+
let current = router;
|
|
76
|
+
for (let i = 0; i < path.length; i++) {
|
|
77
|
+
const segment = path[i];
|
|
78
|
+
if (!current) {
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
if (isContractProcedure(current)) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
current = current[segment];
|
|
85
|
+
}
|
|
86
|
+
return current;
|
|
87
|
+
}
|
|
88
|
+
function enhanceContractRouter(router, options) {
|
|
89
|
+
if (isContractProcedure(router)) {
|
|
90
|
+
const enhanced2 = new ContractProcedure({
|
|
91
|
+
...router["~orpc"],
|
|
92
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
93
|
+
route: enhanceRoute(router["~orpc"].route, options)
|
|
94
|
+
});
|
|
95
|
+
return enhanced2;
|
|
96
|
+
}
|
|
97
|
+
const enhanced = {};
|
|
98
|
+
for (const key in router) {
|
|
99
|
+
enhanced[key] = enhanceContractRouter(router[key], options);
|
|
100
|
+
}
|
|
101
|
+
return enhanced;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class ContractBuilder extends ContractProcedure {
|
|
105
|
+
constructor(def) {
|
|
106
|
+
super(def);
|
|
107
|
+
this["~orpc"].prefix = def.prefix;
|
|
108
|
+
this["~orpc"].tags = def.tags;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reset initial meta
|
|
112
|
+
*/
|
|
113
|
+
$meta(initialMeta) {
|
|
114
|
+
return new ContractBuilder({
|
|
115
|
+
...this["~orpc"],
|
|
116
|
+
meta: initialMeta
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Reset initial route
|
|
121
|
+
*/
|
|
122
|
+
$route(initialRoute) {
|
|
123
|
+
return new ContractBuilder({
|
|
124
|
+
...this["~orpc"],
|
|
125
|
+
route: initialRoute
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
errors(errors) {
|
|
129
|
+
return new ContractBuilder({
|
|
130
|
+
...this["~orpc"],
|
|
131
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
meta(meta) {
|
|
135
|
+
return new ContractBuilder({
|
|
136
|
+
...this["~orpc"],
|
|
137
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
route(route) {
|
|
141
|
+
return new ContractBuilder({
|
|
142
|
+
...this["~orpc"],
|
|
143
|
+
route: mergeRoute(this["~orpc"].route, route)
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
input(schema) {
|
|
147
|
+
return new ContractBuilder({
|
|
148
|
+
...this["~orpc"],
|
|
149
|
+
inputSchema: schema
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
output(schema) {
|
|
153
|
+
return new ContractBuilder({
|
|
154
|
+
...this["~orpc"],
|
|
155
|
+
outputSchema: schema
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
prefix(prefix) {
|
|
159
|
+
return new ContractBuilder({
|
|
160
|
+
...this["~orpc"],
|
|
161
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
tag(...tags) {
|
|
165
|
+
return new ContractBuilder({
|
|
166
|
+
...this["~orpc"],
|
|
167
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
router(router) {
|
|
171
|
+
return enhanceContractRouter(router, this["~orpc"]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const oc = new ContractBuilder({
|
|
175
|
+
errorMap: {},
|
|
176
|
+
route: {},
|
|
177
|
+
meta: {}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const DEFAULT_CONFIG = {
|
|
181
|
+
defaultMethod: "POST",
|
|
182
|
+
defaultSuccessStatus: 200,
|
|
183
|
+
defaultSuccessDescription: "OK",
|
|
184
|
+
defaultInputStructure: "compact",
|
|
185
|
+
defaultOutputStructure: "compact"
|
|
186
|
+
};
|
|
187
|
+
function fallbackContractConfig(key, value) {
|
|
188
|
+
if (value === void 0) {
|
|
189
|
+
return DEFAULT_CONFIG[key];
|
|
190
|
+
}
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const EVENT_ITERATOR_DETAILS_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_DETAILS");
|
|
195
|
+
function eventIterator(yields, returns) {
|
|
196
|
+
return {
|
|
197
|
+
"~standard": {
|
|
198
|
+
[EVENT_ITERATOR_DETAILS_SYMBOL]: { yields, returns },
|
|
199
|
+
vendor: "orpc",
|
|
200
|
+
version: 1,
|
|
201
|
+
validate(iterator) {
|
|
202
|
+
if (!isAsyncIteratorObject(iterator)) {
|
|
203
|
+
return { issues: [{ message: "Expect event iterator", path: [] }] };
|
|
204
|
+
}
|
|
205
|
+
const mapped = mapEventIterator(iterator, {
|
|
206
|
+
async value(value, done) {
|
|
207
|
+
const schema = done ? returns : yields;
|
|
208
|
+
if (!schema) {
|
|
209
|
+
return value;
|
|
210
|
+
}
|
|
211
|
+
const result = await schema["~standard"].validate(value);
|
|
212
|
+
if (result.issues) {
|
|
213
|
+
throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
|
|
214
|
+
message: "Event iterator validation failed",
|
|
215
|
+
cause: new ValidationError({
|
|
216
|
+
issues: result.issues,
|
|
217
|
+
message: "Event iterator validation failed"
|
|
218
|
+
})
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return result.value;
|
|
222
|
+
},
|
|
223
|
+
error: async (error) => error
|
|
224
|
+
});
|
|
225
|
+
return { value: mapped };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function getEventIteratorSchemaDetails(schema) {
|
|
231
|
+
if (schema === void 0) {
|
|
232
|
+
return void 0;
|
|
233
|
+
}
|
|
234
|
+
return schema["~standard"][EVENT_ITERATOR_DETAILS_SYMBOL];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function type(...[map]) {
|
|
238
|
+
return {
|
|
239
|
+
"~standard": {
|
|
240
|
+
vendor: "custom",
|
|
241
|
+
version: 1,
|
|
242
|
+
async validate(value) {
|
|
243
|
+
if (map) {
|
|
244
|
+
return { value: await map(value) };
|
|
245
|
+
}
|
|
246
|
+
return { value };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
|
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.f4d410a",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,30 +15,27 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
21
|
-
},
|
|
22
|
-
"./🔒/*": {
|
|
23
|
-
"types": "./dist/src/*.d.ts"
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
24
21
|
}
|
|
25
22
|
},
|
|
26
23
|
"files": [
|
|
27
|
-
"!**/*.map",
|
|
28
|
-
"!**/*.tsbuildinfo",
|
|
29
24
|
"dist"
|
|
30
25
|
],
|
|
31
26
|
"dependencies": {
|
|
32
|
-
"@standard-schema/spec": "1.0.0
|
|
33
|
-
"@orpc/shared": "0.0.0-next.
|
|
27
|
+
"@standard-schema/spec": "^1.0.0",
|
|
28
|
+
"@orpc/shared": "0.0.0-next.f4d410a",
|
|
29
|
+
"@orpc/standard-server": "0.0.0-next.f4d410a",
|
|
30
|
+
"@orpc/client": "0.0.0-next.f4d410a"
|
|
34
31
|
},
|
|
35
32
|
"devDependencies": {
|
|
36
33
|
"arktype": "2.0.0-rc.26",
|
|
37
34
|
"valibot": "1.0.0-beta.9",
|
|
38
|
-
"zod": "3.24.
|
|
35
|
+
"zod": "^3.24.2"
|
|
39
36
|
},
|
|
40
37
|
"scripts": {
|
|
41
|
-
"build": "
|
|
38
|
+
"build": "unbuild",
|
|
42
39
|
"build:watch": "pnpm run build --watch",
|
|
43
40
|
"type:check": "tsc -b"
|
|
44
41
|
}
|
package/dist/index.js
DELETED
|
@@ -1,406 +0,0 @@
|
|
|
1
|
-
// src/procedure.ts
|
|
2
|
-
var ContractProcedure = class {
|
|
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;
|
|
13
|
-
}
|
|
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
|
|
23
|
-
var DecoratedContractProcedure = class _DecoratedContractProcedure extends ContractProcedure {
|
|
24
|
-
static decorate(procedure) {
|
|
25
|
-
if (procedure instanceof _DecoratedContractProcedure) {
|
|
26
|
-
return procedure;
|
|
27
|
-
}
|
|
28
|
-
return new _DecoratedContractProcedure(procedure["~orpc"]);
|
|
29
|
-
}
|
|
30
|
-
route(route) {
|
|
31
|
-
return new _DecoratedContractProcedure({
|
|
32
|
-
...this["~orpc"],
|
|
33
|
-
route: {
|
|
34
|
-
...this["~orpc"].route,
|
|
35
|
-
...route
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
prefix(prefix) {
|
|
40
|
-
return new _DecoratedContractProcedure({
|
|
41
|
-
...this["~orpc"],
|
|
42
|
-
...this["~orpc"].route?.path ? {
|
|
43
|
-
route: {
|
|
44
|
-
...this["~orpc"].route,
|
|
45
|
-
path: `${prefix}${this["~orpc"].route.path}`
|
|
46
|
-
}
|
|
47
|
-
} : void 0
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
unshiftTag(...tags) {
|
|
51
|
-
return new _DecoratedContractProcedure({
|
|
52
|
-
...this["~orpc"],
|
|
53
|
-
route: {
|
|
54
|
-
...this["~orpc"].route,
|
|
55
|
-
tags: [
|
|
56
|
-
...tags,
|
|
57
|
-
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
|
|
58
|
-
]
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
input(schema, example) {
|
|
63
|
-
return new _DecoratedContractProcedure({
|
|
64
|
-
...this["~orpc"],
|
|
65
|
-
InputSchema: schema,
|
|
66
|
-
inputExample: example
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
output(schema, example) {
|
|
70
|
-
return new _DecoratedContractProcedure({
|
|
71
|
-
...this["~orpc"],
|
|
72
|
-
OutputSchema: schema,
|
|
73
|
-
outputExample: example
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
errors(errors) {
|
|
77
|
-
return new _DecoratedContractProcedure({
|
|
78
|
-
...this["~orpc"],
|
|
79
|
-
errorMap: {
|
|
80
|
-
...this["~orpc"].errorMap,
|
|
81
|
-
...errors
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
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({
|
|
96
|
-
...this["~orpc"],
|
|
97
|
-
prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
tag(...tags) {
|
|
101
|
-
return new _ContractRouterBuilder({
|
|
102
|
-
...this["~orpc"],
|
|
103
|
-
tags: [...this["~orpc"].tags ?? [], ...tags]
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
errors(errors) {
|
|
107
|
-
return new _ContractRouterBuilder({
|
|
108
|
-
...this["~orpc"],
|
|
109
|
-
errorMap: {
|
|
110
|
-
...this["~orpc"].errorMap,
|
|
111
|
-
...errors
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
router(router) {
|
|
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) {
|
|
143
|
-
return new _ContractBuilder({
|
|
144
|
-
...this["~orpc"],
|
|
145
|
-
errorMap: {
|
|
146
|
-
...this["~orpc"].errorMap,
|
|
147
|
-
...errors
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
prefix(prefix) {
|
|
152
|
-
return new ContractRouterBuilder({
|
|
153
|
-
prefix,
|
|
154
|
-
errorMap: this["~orpc"].errorMap
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
tag(...tags) {
|
|
158
|
-
return new ContractRouterBuilder({
|
|
159
|
-
tags,
|
|
160
|
-
errorMap: this["~orpc"].errorMap
|
|
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
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
router(router) {
|
|
188
|
-
return new ContractRouterBuilder({
|
|
189
|
-
errorMap: this["~orpc"].errorMap
|
|
190
|
-
}).router(router);
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
// src/error-orpc.ts
|
|
195
|
-
import { isPlainObject } from "@orpc/shared";
|
|
196
|
-
var COMMON_ORPC_ERROR_DEFS = {
|
|
197
|
-
BAD_REQUEST: {
|
|
198
|
-
status: 400,
|
|
199
|
-
message: "Bad Request"
|
|
200
|
-
},
|
|
201
|
-
UNAUTHORIZED: {
|
|
202
|
-
status: 401,
|
|
203
|
-
message: "Unauthorized"
|
|
204
|
-
},
|
|
205
|
-
FORBIDDEN: {
|
|
206
|
-
status: 403,
|
|
207
|
-
message: "Forbidden"
|
|
208
|
-
},
|
|
209
|
-
NOT_FOUND: {
|
|
210
|
-
status: 404,
|
|
211
|
-
message: "Not Found"
|
|
212
|
-
},
|
|
213
|
-
METHOD_NOT_SUPPORTED: {
|
|
214
|
-
status: 405,
|
|
215
|
-
message: "Method Not Supported"
|
|
216
|
-
},
|
|
217
|
-
NOT_ACCEPTABLE: {
|
|
218
|
-
status: 406,
|
|
219
|
-
message: "Not Acceptable"
|
|
220
|
-
},
|
|
221
|
-
TIMEOUT: {
|
|
222
|
-
status: 408,
|
|
223
|
-
message: "Request Timeout"
|
|
224
|
-
},
|
|
225
|
-
CONFLICT: {
|
|
226
|
-
status: 409,
|
|
227
|
-
message: "Conflict"
|
|
228
|
-
},
|
|
229
|
-
PRECONDITION_FAILED: {
|
|
230
|
-
status: 412,
|
|
231
|
-
message: "Precondition Failed"
|
|
232
|
-
},
|
|
233
|
-
PAYLOAD_TOO_LARGE: {
|
|
234
|
-
status: 413,
|
|
235
|
-
message: "Payload Too Large"
|
|
236
|
-
},
|
|
237
|
-
UNSUPPORTED_MEDIA_TYPE: {
|
|
238
|
-
status: 415,
|
|
239
|
-
message: "Unsupported Media Type"
|
|
240
|
-
},
|
|
241
|
-
UNPROCESSABLE_CONTENT: {
|
|
242
|
-
status: 422,
|
|
243
|
-
message: "Unprocessable Content"
|
|
244
|
-
},
|
|
245
|
-
TOO_MANY_REQUESTS: {
|
|
246
|
-
status: 429,
|
|
247
|
-
message: "Too Many Requests"
|
|
248
|
-
},
|
|
249
|
-
CLIENT_CLOSED_REQUEST: {
|
|
250
|
-
status: 499,
|
|
251
|
-
message: "Client Closed Request"
|
|
252
|
-
},
|
|
253
|
-
INTERNAL_SERVER_ERROR: {
|
|
254
|
-
status: 500,
|
|
255
|
-
message: "Internal Server Error"
|
|
256
|
-
},
|
|
257
|
-
NOT_IMPLEMENTED: {
|
|
258
|
-
status: 501,
|
|
259
|
-
message: "Not Implemented"
|
|
260
|
-
},
|
|
261
|
-
BAD_GATEWAY: {
|
|
262
|
-
status: 502,
|
|
263
|
-
message: "Bad Gateway"
|
|
264
|
-
},
|
|
265
|
-
SERVICE_UNAVAILABLE: {
|
|
266
|
-
status: 503,
|
|
267
|
-
message: "Service Unavailable"
|
|
268
|
-
},
|
|
269
|
-
GATEWAY_TIMEOUT: {
|
|
270
|
-
status: 504,
|
|
271
|
-
message: "Gateway Timeout"
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
function fallbackORPCErrorStatus(code, status) {
|
|
275
|
-
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
276
|
-
}
|
|
277
|
-
function fallbackORPCErrorMessage(code, message) {
|
|
278
|
-
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
279
|
-
}
|
|
280
|
-
var ORPCError = class extends Error {
|
|
281
|
-
defined;
|
|
282
|
-
code;
|
|
283
|
-
status;
|
|
284
|
-
data;
|
|
285
|
-
constructor(options) {
|
|
286
|
-
if (options.status && (options.status < 400 || options.status >= 600)) {
|
|
287
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
288
|
-
}
|
|
289
|
-
const message = fallbackORPCErrorMessage(options.code, options.message);
|
|
290
|
-
super(message, options);
|
|
291
|
-
this.code = options.code;
|
|
292
|
-
this.status = fallbackORPCErrorStatus(options.code, options.status);
|
|
293
|
-
this.defined = options.defined ?? false;
|
|
294
|
-
this.data = options.data;
|
|
295
|
-
}
|
|
296
|
-
toJSON() {
|
|
297
|
-
return {
|
|
298
|
-
defined: this.defined,
|
|
299
|
-
code: this.code,
|
|
300
|
-
status: this.status,
|
|
301
|
-
message: this.message,
|
|
302
|
-
data: this.data
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
static isValidJSON(json) {
|
|
306
|
-
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
|
-
}
|
|
308
|
-
};
|
|
309
|
-
function isDefinedError(error) {
|
|
310
|
-
return error instanceof ORPCError && error.defined;
|
|
311
|
-
}
|
|
312
|
-
async function validateORPCError(map, error) {
|
|
313
|
-
const { code, status, message, data, cause, defined } = error;
|
|
314
|
-
const config = map?.[error.code];
|
|
315
|
-
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
|
316
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
317
|
-
}
|
|
318
|
-
if (!config.data) {
|
|
319
|
-
return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause });
|
|
320
|
-
}
|
|
321
|
-
const validated = await config.data["~standard"].validate(error.data);
|
|
322
|
-
if (validated.issues) {
|
|
323
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
324
|
-
}
|
|
325
|
-
return new ORPCError({
|
|
326
|
-
defined: true,
|
|
327
|
-
code,
|
|
328
|
-
status,
|
|
329
|
-
message,
|
|
330
|
-
data: validated.value,
|
|
331
|
-
cause
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// src/client-utils.ts
|
|
336
|
-
async function safe(promise) {
|
|
337
|
-
try {
|
|
338
|
-
const output = await promise;
|
|
339
|
-
return [output, void 0, false];
|
|
340
|
-
} catch (e) {
|
|
341
|
-
const error = e;
|
|
342
|
-
if (isDefinedError(error)) {
|
|
343
|
-
return [void 0, error, true];
|
|
344
|
-
}
|
|
345
|
-
return [void 0, error, false];
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// src/config.ts
|
|
350
|
-
var DEFAULT_CONFIG = {
|
|
351
|
-
defaultMethod: "POST",
|
|
352
|
-
defaultSuccessStatus: 200,
|
|
353
|
-
defaultSuccessDescription: "OK",
|
|
354
|
-
defaultInputStructure: "compact",
|
|
355
|
-
defaultOutputStructure: "compact"
|
|
356
|
-
};
|
|
357
|
-
var GLOBAL_CONFIG_REF = { value: DEFAULT_CONFIG };
|
|
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) {
|
|
365
|
-
if (value === void 0) {
|
|
366
|
-
const fallback = GLOBAL_CONFIG_REF.value[key];
|
|
367
|
-
if (fallback === void 0) {
|
|
368
|
-
return DEFAULT_CONFIG[key];
|
|
369
|
-
}
|
|
370
|
-
return fallback;
|
|
371
|
-
}
|
|
372
|
-
return value;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
// src/error.ts
|
|
376
|
-
var ValidationError = class extends Error {
|
|
377
|
-
issues;
|
|
378
|
-
constructor(options) {
|
|
379
|
-
super(options.message, options);
|
|
380
|
-
this.issues = options.issues;
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
// src/index.ts
|
|
385
|
-
var oc = new ContractBuilder({
|
|
386
|
-
errorMap: {}
|
|
387
|
-
});
|
|
388
|
-
export {
|
|
389
|
-
COMMON_ORPC_ERROR_DEFS,
|
|
390
|
-
ContractBuilder,
|
|
391
|
-
ContractProcedure,
|
|
392
|
-
ContractRouterBuilder,
|
|
393
|
-
DecoratedContractProcedure,
|
|
394
|
-
ORPCError,
|
|
395
|
-
ValidationError,
|
|
396
|
-
configGlobal,
|
|
397
|
-
fallbackORPCErrorMessage,
|
|
398
|
-
fallbackORPCErrorStatus,
|
|
399
|
-
fallbackToGlobalConfig,
|
|
400
|
-
isContractProcedure,
|
|
401
|
-
isDefinedError,
|
|
402
|
-
oc,
|
|
403
|
-
safe,
|
|
404
|
-
validateORPCError
|
|
405
|
-
};
|
|
406
|
-
//# sourceMappingURL=index.js.map
|