@orpc/server 0.31.0 → 0.33.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/{chunk-SXUFCJBY.js → chunk-ESTRJAOX.js} +5 -7
- package/dist/{chunk-GK2Z6B6W.js → chunk-KK4SDLC7.js} +158 -68
- package/dist/fetch.js +2 -2
- package/dist/hono.js +3 -3
- package/dist/index.js +191 -787
- package/dist/next.js +3 -3
- package/dist/node.js +6 -6
- package/dist/src/adapters/fetch/orpc-handler.d.ts +1 -1
- package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +4 -4
- package/dist/src/adapters/fetch/types.d.ts +4 -4
- package/dist/src/adapters/hono/middleware.d.ts +3 -3
- package/dist/src/adapters/next/serve.d.ts +8 -8
- package/dist/src/adapters/node/orpc-handler.d.ts +2 -2
- package/dist/src/adapters/node/types.d.ts +5 -5
- package/dist/src/builder-variants.d.ts +74 -0
- package/dist/src/builder.d.ts +50 -36
- package/dist/src/config.d.ts +6 -0
- package/dist/src/context.d.ts +8 -10
- package/dist/src/hidden.d.ts +5 -3
- package/dist/src/implementer-procedure.d.ts +30 -0
- package/dist/src/implementer-variants.d.ts +16 -0
- package/dist/src/implementer.d.ts +27 -0
- package/dist/src/index.d.ts +9 -13
- package/dist/src/lazy-utils.d.ts +4 -2
- package/dist/src/lazy.d.ts +9 -5
- package/dist/src/middleware-decorated.d.ts +7 -7
- package/dist/src/middleware-utils.d.ts +5 -0
- package/dist/src/middleware.d.ts +22 -21
- package/dist/src/procedure-client.d.ts +6 -8
- package/dist/src/procedure-decorated.d.ts +10 -15
- package/dist/src/procedure-utils.d.ts +2 -2
- package/dist/src/procedure.d.ts +17 -33
- package/dist/src/router-accessible-lazy.d.ts +8 -0
- package/dist/src/router-client.d.ts +6 -10
- package/dist/src/router.d.ts +25 -12
- package/package.json +3 -3
- package/dist/src/builder-with-errors-middlewares.d.ts +0 -50
- package/dist/src/builder-with-errors.d.ts +0 -49
- package/dist/src/builder-with-middlewares.d.ts +0 -47
- package/dist/src/error.d.ts +0 -10
- package/dist/src/implementer-chainable.d.ts +0 -14
- package/dist/src/lazy-decorated.d.ts +0 -7
- package/dist/src/procedure-builder-with-input.d.ts +0 -34
- package/dist/src/procedure-builder-with-output.d.ts +0 -33
- package/dist/src/procedure-builder.d.ts +0 -27
- package/dist/src/procedure-implementer.d.ts +0 -22
- package/dist/src/router-builder.d.ts +0 -33
- package/dist/src/router-implementer.d.ts +0 -22
- package/dist/src/types.d.ts +0 -14
- package/dist/src/utils.d.ts +0 -3
@@ -4,7 +4,7 @@ import {
|
|
4
4
|
getRouterChild,
|
5
5
|
isProcedure,
|
6
6
|
unlazy
|
7
|
-
} from "./chunk-
|
7
|
+
} from "./chunk-KK4SDLC7.js";
|
8
8
|
|
9
9
|
// src/adapters/fetch/super-json.ts
|
10
10
|
var super_json_exports = {};
|
@@ -206,8 +206,7 @@ var ORPCPayloadCodec = class {
|
|
206
206
|
const json = await re.json();
|
207
207
|
return deserialize(json);
|
208
208
|
} catch (e) {
|
209
|
-
throw new ORPCError({
|
210
|
-
code: "BAD_REQUEST",
|
209
|
+
throw new ORPCError("BAD_REQUEST", {
|
211
210
|
message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
|
212
211
|
cause: e
|
213
212
|
});
|
@@ -247,7 +246,7 @@ var RPCHandler = class {
|
|
247
246
|
procedureMatcher;
|
248
247
|
payloadCodec;
|
249
248
|
async handle(request, ...[options]) {
|
250
|
-
const context = options?.context;
|
249
|
+
const context = options?.context ?? {};
|
251
250
|
const execute = async () => {
|
252
251
|
const url = new URL(request.url);
|
253
252
|
const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
@@ -277,8 +276,7 @@ var RPCHandler = class {
|
|
277
276
|
});
|
278
277
|
return result;
|
279
278
|
} catch (e) {
|
280
|
-
const error = e instanceof ORPCError2 ? e : new ORPCError2({
|
281
|
-
code: "INTERNAL_SERVER_ERROR",
|
279
|
+
const error = e instanceof ORPCError2 ? e : new ORPCError2("INTERNAL_SERVER_ERROR", {
|
282
280
|
message: "Internal server error",
|
283
281
|
cause: e
|
284
282
|
});
|
@@ -298,4 +296,4 @@ export {
|
|
298
296
|
ORPCProcedureMatcher,
|
299
297
|
RPCHandler
|
300
298
|
};
|
301
|
-
//# sourceMappingURL=chunk-
|
299
|
+
//# sourceMappingURL=chunk-ESTRJAOX.js.map
|
@@ -4,22 +4,23 @@ var __export = (target, all) => {
|
|
4
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
5
5
|
};
|
6
6
|
|
7
|
-
// src/
|
8
|
-
|
9
|
-
|
10
|
-
return b;
|
11
|
-
if (!b)
|
12
|
-
return a;
|
7
|
+
// src/lazy.ts
|
8
|
+
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
9
|
+
function lazy(loader) {
|
13
10
|
return {
|
14
|
-
|
15
|
-
...b
|
11
|
+
[LAZY_LOADER_SYMBOL]: loader
|
16
12
|
};
|
17
13
|
}
|
14
|
+
function isLazy(item) {
|
15
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
16
|
+
}
|
17
|
+
function unlazy(lazied) {
|
18
|
+
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
19
|
+
}
|
18
20
|
|
19
21
|
// src/procedure.ts
|
20
22
|
import { isContractProcedure } from "@orpc/contract";
|
21
23
|
var Procedure = class {
|
22
|
-
"~type" = "Procedure";
|
23
24
|
"~orpc";
|
24
25
|
constructor(def) {
|
25
26
|
this["~orpc"] = def;
|
@@ -29,46 +30,10 @@ function isProcedure(item) {
|
|
29
30
|
if (item instanceof Procedure) {
|
30
31
|
return true;
|
31
32
|
}
|
32
|
-
return (
|
33
|
-
}
|
34
|
-
|
35
|
-
// src/error.ts
|
36
|
-
import { ORPCError } from "@orpc/contract";
|
37
|
-
function createORPCErrorConstructorMap(errors) {
|
38
|
-
const constructors = {};
|
39
|
-
for (const code in errors) {
|
40
|
-
const config = errors[code];
|
41
|
-
if (!config) {
|
42
|
-
continue;
|
43
|
-
}
|
44
|
-
const constructor = (...[options]) => {
|
45
|
-
return new ORPCError({
|
46
|
-
code,
|
47
|
-
defined: true,
|
48
|
-
status: config.status,
|
49
|
-
message: options?.message ?? config.message,
|
50
|
-
data: options?.data,
|
51
|
-
cause: options?.cause
|
52
|
-
});
|
53
|
-
};
|
54
|
-
constructors[code] = constructor;
|
55
|
-
}
|
56
|
-
return constructors;
|
33
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
57
34
|
}
|
58
35
|
|
59
|
-
// src/lazy.ts
|
60
|
-
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
61
|
-
function lazy(loader) {
|
62
|
-
return {
|
63
|
-
[LAZY_LOADER_SYMBOL]: loader
|
64
|
-
};
|
65
|
-
}
|
66
|
-
function isLazy(item) {
|
67
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
68
|
-
}
|
69
|
-
function unlazy(lazied) {
|
70
|
-
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
71
|
-
}
|
36
|
+
// src/lazy-utils.ts
|
72
37
|
function flatLazy(lazied) {
|
73
38
|
const flattenLoader = async () => {
|
74
39
|
let current = await unlazy(lazied);
|
@@ -82,21 +47,35 @@ function flatLazy(lazied) {
|
|
82
47
|
};
|
83
48
|
return lazy(flattenLoader);
|
84
49
|
}
|
50
|
+
function createLazyProcedureFormAnyLazy(lazied) {
|
51
|
+
const lazyProcedure = lazy(async () => {
|
52
|
+
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
53
|
+
if (!isProcedure(maybeProcedure)) {
|
54
|
+
throw new Error(`
|
55
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
56
|
+
This should be caught by TypeScript compilation.
|
57
|
+
Please report this issue if this makes you feel uncomfortable.
|
58
|
+
`);
|
59
|
+
}
|
60
|
+
return { default: maybeProcedure };
|
61
|
+
});
|
62
|
+
return lazyProcedure;
|
63
|
+
}
|
85
64
|
|
86
65
|
// src/middleware.ts
|
87
66
|
function middlewareOutputFn(output) {
|
88
|
-
return { output, context:
|
67
|
+
return { output, context: {} };
|
89
68
|
}
|
90
69
|
|
91
70
|
// src/procedure-client.ts
|
92
|
-
import { ORPCError
|
71
|
+
import { createORPCErrorConstructorMap, ORPCError, validateORPCError, ValidationError } from "@orpc/contract";
|
93
72
|
import { executeWithHooks, toError, value } from "@orpc/shared";
|
94
73
|
function createProcedureClient(lazyableProcedure, ...[options]) {
|
95
74
|
return async (...[input, callerOptions]) => {
|
96
75
|
const path = options?.path ?? [];
|
97
76
|
const { default: procedure } = await unlazy(lazyableProcedure);
|
98
|
-
const context = await value(options?.context, callerOptions?.context);
|
99
|
-
const errors = createORPCErrorConstructorMap(procedure["~orpc"].
|
77
|
+
const context = await value(options?.context ?? {}, callerOptions?.context);
|
78
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
100
79
|
const executeOptions = {
|
101
80
|
input,
|
102
81
|
context,
|
@@ -115,23 +94,23 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
115
94
|
});
|
116
95
|
return output;
|
117
96
|
} catch (e) {
|
118
|
-
if (!(e instanceof
|
97
|
+
if (!(e instanceof ORPCError)) {
|
119
98
|
throw toError(e);
|
120
99
|
}
|
121
|
-
const validated = await validateORPCError(procedure["~orpc"].
|
100
|
+
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
122
101
|
throw validated;
|
123
102
|
}
|
124
103
|
};
|
125
104
|
}
|
126
105
|
async function validateInput(procedure, input) {
|
127
|
-
const schema = procedure["~orpc"].
|
128
|
-
if (!schema)
|
106
|
+
const schema = procedure["~orpc"].inputSchema;
|
107
|
+
if (!schema) {
|
129
108
|
return input;
|
109
|
+
}
|
130
110
|
const result = await schema["~standard"].validate(input);
|
131
111
|
if (result.issues) {
|
132
|
-
throw new
|
112
|
+
throw new ORPCError("BAD_REQUEST", {
|
133
113
|
message: "Input validation failed",
|
134
|
-
code: "BAD_REQUEST",
|
135
114
|
data: {
|
136
115
|
issues: result.issues
|
137
116
|
},
|
@@ -141,14 +120,14 @@ async function validateInput(procedure, input) {
|
|
141
120
|
return result.value;
|
142
121
|
}
|
143
122
|
async function validateOutput(procedure, output) {
|
144
|
-
const schema = procedure["~orpc"].
|
145
|
-
if (!schema)
|
123
|
+
const schema = procedure["~orpc"].outputSchema;
|
124
|
+
if (!schema) {
|
146
125
|
return output;
|
126
|
+
}
|
147
127
|
const result = await schema["~standard"].validate(output);
|
148
128
|
if (result.issues) {
|
149
|
-
throw new
|
129
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
150
130
|
message: "Output validation failed",
|
151
|
-
code: "INTERNAL_SERVER_ERROR",
|
152
131
|
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
153
132
|
});
|
154
133
|
}
|
@@ -161,10 +140,10 @@ async function executeProcedureInternal(procedure, options) {
|
|
161
140
|
let currentIndex = 0;
|
162
141
|
let currentContext = options.context;
|
163
142
|
let currentInput = options.input;
|
164
|
-
const next = async (nextOptions) => {
|
143
|
+
const next = async (...[nextOptions]) => {
|
165
144
|
const index = currentIndex;
|
166
145
|
currentIndex += 1;
|
167
|
-
currentContext =
|
146
|
+
currentContext = { ...currentContext, ...nextOptions?.context };
|
168
147
|
if (index === inputValidationIndex) {
|
169
148
|
currentInput = await validateInput(procedure, currentInput);
|
170
149
|
}
|
@@ -182,7 +161,97 @@ async function executeProcedureInternal(procedure, options) {
|
|
182
161
|
return (await next({})).output;
|
183
162
|
}
|
184
163
|
|
164
|
+
// src/hidden.ts
|
165
|
+
var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
|
166
|
+
function setRouterContract(obj, contract) {
|
167
|
+
return new Proxy(obj, {
|
168
|
+
get(target, key) {
|
169
|
+
if (key === ROUTER_CONTRACT_SYMBOL) {
|
170
|
+
return contract;
|
171
|
+
}
|
172
|
+
return Reflect.get(target, key);
|
173
|
+
}
|
174
|
+
});
|
175
|
+
}
|
176
|
+
function getRouterContract(obj) {
|
177
|
+
return obj[ROUTER_CONTRACT_SYMBOL];
|
178
|
+
}
|
179
|
+
var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
180
|
+
function deepSetLazyRouterPrefix(router, prefix) {
|
181
|
+
return new Proxy(router, {
|
182
|
+
get(target, key) {
|
183
|
+
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
184
|
+
const val = Reflect.get(target, key);
|
185
|
+
if (isLazy(val)) {
|
186
|
+
return deepSetLazyRouterPrefix(val, prefix);
|
187
|
+
}
|
188
|
+
return val;
|
189
|
+
}
|
190
|
+
return prefix;
|
191
|
+
}
|
192
|
+
});
|
193
|
+
}
|
194
|
+
function getLazyRouterPrefix(obj) {
|
195
|
+
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
196
|
+
}
|
197
|
+
|
185
198
|
// src/router.ts
|
199
|
+
import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
|
200
|
+
|
201
|
+
// src/middleware-utils.ts
|
202
|
+
function dedupeMiddlewares(compare, middlewares) {
|
203
|
+
let min = 0;
|
204
|
+
for (let i = 0; i < middlewares.length; i++) {
|
205
|
+
const index = compare.indexOf(middlewares[i], min);
|
206
|
+
if (index === -1) {
|
207
|
+
return middlewares.slice(i);
|
208
|
+
}
|
209
|
+
min = index + 1;
|
210
|
+
}
|
211
|
+
return [];
|
212
|
+
}
|
213
|
+
function mergeMiddlewares(first, second) {
|
214
|
+
return [...first, ...dedupeMiddlewares(first, second)];
|
215
|
+
}
|
216
|
+
function addMiddleware(middlewares, addition) {
|
217
|
+
return [...middlewares, addition];
|
218
|
+
}
|
219
|
+
|
220
|
+
// src/router.ts
|
221
|
+
function adaptRouter(router, options) {
|
222
|
+
if (isLazy(router)) {
|
223
|
+
const adapted2 = lazy(async () => {
|
224
|
+
const unlaziedRouter = (await unlazy(router)).default;
|
225
|
+
const adapted3 = adaptRouter(unlaziedRouter, options);
|
226
|
+
return { default: adapted3 };
|
227
|
+
});
|
228
|
+
const accessible = createAccessibleLazyRouter(adapted2);
|
229
|
+
const currentPrefix = getLazyRouterPrefix(router);
|
230
|
+
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
231
|
+
if (prefix) {
|
232
|
+
return deepSetLazyRouterPrefix(accessible, prefix);
|
233
|
+
}
|
234
|
+
return accessible;
|
235
|
+
}
|
236
|
+
if (isProcedure(router)) {
|
237
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
238
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
239
|
+
const adapted2 = new Procedure({
|
240
|
+
...router["~orpc"],
|
241
|
+
route: adaptRoute(router["~orpc"].route, options),
|
242
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
243
|
+
middlewares: newMiddlewares,
|
244
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
245
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
246
|
+
});
|
247
|
+
return adapted2;
|
248
|
+
}
|
249
|
+
const adapted = {};
|
250
|
+
for (const key in router) {
|
251
|
+
adapted[key] = adaptRouter(router[key], options);
|
252
|
+
}
|
253
|
+
return adapted;
|
254
|
+
}
|
186
255
|
function getRouterChild(router, ...path) {
|
187
256
|
let current = router;
|
188
257
|
for (let i = 0; i < path.length; i++) {
|
@@ -212,19 +281,40 @@ function getRouterChild(router, ...path) {
|
|
212
281
|
return current;
|
213
282
|
}
|
214
283
|
|
284
|
+
// src/router-accessible-lazy.ts
|
285
|
+
function createAccessibleLazyRouter(lazied) {
|
286
|
+
const flattenLazy = flatLazy(lazied);
|
287
|
+
const recursive = new Proxy(flattenLazy, {
|
288
|
+
get(target, key) {
|
289
|
+
if (typeof key !== "string") {
|
290
|
+
return Reflect.get(target, key);
|
291
|
+
}
|
292
|
+
const next = getRouterChild(flattenLazy, key);
|
293
|
+
return createAccessibleLazyRouter(next);
|
294
|
+
}
|
295
|
+
});
|
296
|
+
return recursive;
|
297
|
+
}
|
298
|
+
|
215
299
|
export {
|
216
300
|
__export,
|
217
|
-
mergeContext,
|
218
|
-
Procedure,
|
219
|
-
isProcedure,
|
220
|
-
createORPCErrorConstructorMap,
|
221
301
|
LAZY_LOADER_SYMBOL,
|
222
302
|
lazy,
|
223
303
|
isLazy,
|
224
304
|
unlazy,
|
305
|
+
Procedure,
|
306
|
+
isProcedure,
|
225
307
|
flatLazy,
|
308
|
+
createLazyProcedureFormAnyLazy,
|
309
|
+
addMiddleware,
|
226
310
|
middlewareOutputFn,
|
227
311
|
createProcedureClient,
|
312
|
+
setRouterContract,
|
313
|
+
getRouterContract,
|
314
|
+
deepSetLazyRouterPrefix,
|
315
|
+
getLazyRouterPrefix,
|
316
|
+
createAccessibleLazyRouter,
|
317
|
+
adaptRouter,
|
228
318
|
getRouterChild
|
229
319
|
};
|
230
|
-
//# sourceMappingURL=chunk-
|
320
|
+
//# sourceMappingURL=chunk-KK4SDLC7.js.map
|
package/dist/fetch.js
CHANGED
package/dist/hono.js
CHANGED
@@ -4,14 +4,14 @@ import {
|
|
4
4
|
ORPCProcedureMatcher,
|
5
5
|
RPCHandler,
|
6
6
|
super_json_exports
|
7
|
-
} from "./chunk-
|
8
|
-
import "./chunk-
|
7
|
+
} from "./chunk-ESTRJAOX.js";
|
8
|
+
import "./chunk-KK4SDLC7.js";
|
9
9
|
|
10
10
|
// src/adapters/hono/middleware.ts
|
11
11
|
import { value } from "@orpc/shared";
|
12
12
|
function createMiddleware(handler, ...[options]) {
|
13
13
|
return async (c, next) => {
|
14
|
-
const context = await value(options?.context, c);
|
14
|
+
const context = await value(options?.context ?? {}, c);
|
15
15
|
const { matched, response } = await handler.handle(c.req.raw, { ...options, context });
|
16
16
|
if (matched) {
|
17
17
|
c.res = response;
|