@orpc/server 0.29.0 → 0.31.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/{chunk-XDC42C3C.js → chunk-GK2Z6B6W.js} +34 -31
- package/dist/{chunk-VY6NXNQT.js → chunk-SXUFCJBY.js} +2 -2
- package/dist/fetch.js +2 -2
- package/dist/hono.js +2 -2
- package/dist/index.js +525 -88
- package/dist/next.js +2 -2
- package/dist/node.js +2 -2
- package/dist/src/builder-with-errors-middlewares.d.ts +50 -0
- package/dist/src/builder-with-errors.d.ts +49 -0
- package/dist/src/builder-with-middlewares.d.ts +47 -0
- package/dist/src/builder.d.ts +28 -21
- package/dist/src/context.d.ts +11 -0
- package/dist/src/error.d.ts +1 -1
- package/dist/src/hidden.d.ts +2 -2
- package/dist/src/implementer-chainable.d.ts +7 -3
- package/dist/src/index.d.ts +2 -2
- package/dist/src/middleware-decorated.d.ts +2 -1
- package/dist/src/middleware.d.ts +1 -0
- package/dist/src/procedure-builder-with-input.d.ts +34 -0
- package/dist/src/procedure-builder-with-output.d.ts +33 -0
- package/dist/src/procedure-builder.d.ts +19 -16
- package/dist/src/procedure-decorated.d.ts +8 -8
- package/dist/src/procedure-implementer.d.ts +6 -3
- package/dist/src/procedure.d.ts +4 -2
- package/dist/src/router-builder.d.ts +18 -14
- package/dist/src/router-implementer.d.ts +7 -6
- package/dist/src/router.d.ts +2 -2
- package/package.json +3 -3
@@ -36,9 +36,6 @@ function isProcedure(item) {
|
|
36
36
|
import { ORPCError } from "@orpc/contract";
|
37
37
|
function createORPCErrorConstructorMap(errors) {
|
38
38
|
const constructors = {};
|
39
|
-
if (!errors) {
|
40
|
-
return constructors;
|
41
|
-
}
|
42
39
|
for (const code in errors) {
|
43
40
|
const config = errors[code];
|
44
41
|
if (!config) {
|
@@ -86,6 +83,11 @@ function flatLazy(lazied) {
|
|
86
83
|
return lazy(flattenLoader);
|
87
84
|
}
|
88
85
|
|
86
|
+
// src/middleware.ts
|
87
|
+
function middlewareOutputFn(output) {
|
88
|
+
return { output, context: void 0 };
|
89
|
+
}
|
90
|
+
|
89
91
|
// src/procedure-client.ts
|
90
92
|
import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
|
91
93
|
import { executeWithHooks, toError, value } from "@orpc/shared";
|
@@ -94,30 +96,22 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
94
96
|
const path = options?.path ?? [];
|
95
97
|
const { default: procedure } = await unlazy(lazyableProcedure);
|
96
98
|
const context = await value(options?.context, callerOptions?.context);
|
97
|
-
const
|
99
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap);
|
100
|
+
const executeOptions = {
|
101
|
+
input,
|
102
|
+
context,
|
103
|
+
errors,
|
98
104
|
path,
|
99
105
|
procedure,
|
100
106
|
signal: callerOptions?.signal
|
101
107
|
};
|
102
|
-
const executeWithValidation = async () => {
|
103
|
-
const validInput = await validateInput(procedure, input);
|
104
|
-
const output = await executeMiddlewareChain({
|
105
|
-
context,
|
106
|
-
input: validInput,
|
107
|
-
path,
|
108
|
-
procedure,
|
109
|
-
signal: callerOptions?.signal,
|
110
|
-
errors: createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap)
|
111
|
-
});
|
112
|
-
return validateOutput(procedure, output);
|
113
|
-
};
|
114
108
|
try {
|
115
109
|
const output = await executeWithHooks({
|
116
110
|
hooks: options,
|
117
111
|
input,
|
118
112
|
context,
|
119
|
-
meta,
|
120
|
-
execute:
|
113
|
+
meta: executeOptions,
|
114
|
+
execute: () => executeProcedureInternal(procedure, executeOptions)
|
121
115
|
});
|
122
116
|
return output;
|
123
117
|
} catch (e) {
|
@@ -160,21 +154,29 @@ async function validateOutput(procedure, output) {
|
|
160
154
|
}
|
161
155
|
return result.value;
|
162
156
|
}
|
163
|
-
async function
|
164
|
-
const middlewares =
|
165
|
-
|
166
|
-
|
157
|
+
async function executeProcedureInternal(procedure, options) {
|
158
|
+
const middlewares = procedure["~orpc"].middlewares;
|
159
|
+
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
160
|
+
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
161
|
+
let currentIndex = 0;
|
162
|
+
let currentContext = options.context;
|
163
|
+
let currentInput = options.input;
|
167
164
|
const next = async (nextOptions) => {
|
168
|
-
const
|
169
|
-
|
165
|
+
const index = currentIndex;
|
166
|
+
currentIndex += 1;
|
170
167
|
currentContext = mergeContext(currentContext, nextOptions.context);
|
171
|
-
if (
|
172
|
-
|
168
|
+
if (index === inputValidationIndex) {
|
169
|
+
currentInput = await validateInput(procedure, currentInput);
|
170
|
+
}
|
171
|
+
const mid = middlewares[index];
|
172
|
+
const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
|
173
|
+
if (index === outputValidationIndex) {
|
174
|
+
const validatedOutput = await validateOutput(procedure, result.output);
|
175
|
+
return {
|
176
|
+
...result,
|
177
|
+
output: validatedOutput
|
178
|
+
};
|
173
179
|
}
|
174
|
-
const result = {
|
175
|
-
output: await opt.procedure["~orpc"].handler({ ...opt, context: currentContext }),
|
176
|
-
context: currentContext
|
177
|
-
};
|
178
180
|
return result;
|
179
181
|
};
|
180
182
|
return (await next({})).output;
|
@@ -221,7 +223,8 @@ export {
|
|
221
223
|
isLazy,
|
222
224
|
unlazy,
|
223
225
|
flatLazy,
|
226
|
+
middlewareOutputFn,
|
224
227
|
createProcedureClient,
|
225
228
|
getRouterChild
|
226
229
|
};
|
227
|
-
//# sourceMappingURL=chunk-
|
230
|
+
//# sourceMappingURL=chunk-GK2Z6B6W.js.map
|
@@ -4,7 +4,7 @@ import {
|
|
4
4
|
getRouterChild,
|
5
5
|
isProcedure,
|
6
6
|
unlazy
|
7
|
-
} from "./chunk-
|
7
|
+
} from "./chunk-GK2Z6B6W.js";
|
8
8
|
|
9
9
|
// src/adapters/fetch/super-json.ts
|
10
10
|
var super_json_exports = {};
|
@@ -298,4 +298,4 @@ export {
|
|
298
298
|
ORPCProcedureMatcher,
|
299
299
|
RPCHandler
|
300
300
|
};
|
301
|
-
//# sourceMappingURL=chunk-
|
301
|
+
//# sourceMappingURL=chunk-SXUFCJBY.js.map
|
package/dist/fetch.js
CHANGED
package/dist/hono.js
CHANGED
@@ -4,8 +4,8 @@ import {
|
|
4
4
|
ORPCProcedureMatcher,
|
5
5
|
RPCHandler,
|
6
6
|
super_json_exports
|
7
|
-
} from "./chunk-
|
8
|
-
import "./chunk-
|
7
|
+
} from "./chunk-SXUFCJBY.js";
|
8
|
+
import "./chunk-GK2Z6B6W.js";
|
9
9
|
|
10
10
|
// src/adapters/hono/middleware.ts
|
11
11
|
import { value } from "@orpc/shared";
|