@orpc/contract 0.0.0-next.ed15210 → 0.0.0-next.ee46dab
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 +114 -0
- package/dist/index.d.mts +234 -0
- package/dist/index.d.ts +234 -0
- package/dist/index.mjs +252 -0
- package/package.json +10 -13
- package/dist/index.js +0 -527
- package/dist/src/builder.d.ts +0 -32
- package/dist/src/client-utils.d.ts +0 -5
- package/dist/src/client.d.ts +0 -19
- package/dist/src/config.d.ts +0 -11
- 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 -23
- package/dist/src/procedure-builder-with-input.d.ts +0 -19
- package/dist/src/procedure-builder-with-output.d.ts +0 -19
- package/dist/src/procedure-builder.d.ts +0 -15
- package/dist/src/procedure-client.d.ts +0 -6
- package/dist/src/procedure-decorated.d.ts +0 -12
- package/dist/src/procedure.d.ts +0 -20
- package/dist/src/route.d.ts +0 -88
- package/dist/src/router-builder.d.ts +0 -24
- package/dist/src/router-client.d.ts +0 -7
- package/dist/src/router.d.ts +0 -13
- package/dist/src/schema-utils.d.ts +0 -5
- package/dist/src/types.d.ts +0 -7
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { isORPCErrorStatus, 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 && isORPCErrorStatus(def.route.successStatus)) {
|
|
24
|
+
throw new Error("[ContractProcedure] Invalid successStatus.");
|
|
25
|
+
}
|
|
26
|
+
if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
|
|
27
|
+
throw new Error("[ContractProcedure] Invalid error status code.");
|
|
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.ee46dab",
|
|
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.ee46dab",
|
|
29
|
+
"@orpc/client": "0.0.0-next.ee46dab",
|
|
30
|
+
"@orpc/standard-server": "0.0.0-next.ee46dab"
|
|
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
|
}
|