@orpc/server 0.0.0-next.1d55ec0 → 0.0.0-next.2281fcb

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ // src/utils.ts
2
+ function mergeContext(a, b) {
3
+ if (!a)
4
+ return b;
5
+ if (!b)
6
+ return a;
7
+ return {
8
+ ...a,
9
+ ...b
10
+ };
11
+ }
12
+
13
+ // src/procedure.ts
14
+ import { isContractProcedure } from "@orpc/contract";
15
+ var Procedure = class {
16
+ "~type" = "Procedure";
17
+ "~orpc";
18
+ constructor(def) {
19
+ this["~orpc"] = def;
20
+ }
21
+ };
22
+ function isProcedure(item) {
23
+ if (item instanceof Procedure) {
24
+ return true;
25
+ }
26
+ return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "Procedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "contract" in item["~orpc"] && isContractProcedure(item["~orpc"].contract) && "func" in item["~orpc"] && typeof item["~orpc"].func === "function";
27
+ }
28
+
29
+ // src/lazy.ts
30
+ var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
31
+ function lazy(loader) {
32
+ return {
33
+ [LAZY_LOADER_SYMBOL]: loader
34
+ };
35
+ }
36
+ function isLazy(item) {
37
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
38
+ }
39
+ function unlazy(lazied) {
40
+ return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
41
+ }
42
+ function flatLazy(lazied) {
43
+ const flattenLoader = async () => {
44
+ let current = await unlazy(lazied);
45
+ while (true) {
46
+ if (!isLazy(current.default)) {
47
+ break;
48
+ }
49
+ current = await unlazy(current.default);
50
+ }
51
+ return current;
52
+ };
53
+ return lazy(flattenLoader);
54
+ }
55
+
56
+ // src/procedure-client.ts
57
+ import { executeWithHooks, value } from "@orpc/shared";
58
+ import { ORPCError } from "@orpc/shared/error";
59
+ function createProcedureClient(options) {
60
+ return async (...[input, callerOptions]) => {
61
+ const path = options.path ?? [];
62
+ const { default: procedure } = await unlazy(options.procedure);
63
+ const context = await value(options.context);
64
+ const meta = {
65
+ path,
66
+ procedure,
67
+ signal: callerOptions?.signal
68
+ };
69
+ const executeWithValidation = async () => {
70
+ const validInput = await validateInput(procedure, input);
71
+ const output = await executeMiddlewareChain(
72
+ procedure,
73
+ validInput,
74
+ context,
75
+ meta
76
+ );
77
+ return validateOutput(procedure, output);
78
+ };
79
+ return executeWithHooks({
80
+ hooks: options,
81
+ input,
82
+ context,
83
+ meta,
84
+ execute: executeWithValidation
85
+ });
86
+ };
87
+ }
88
+ async function validateInput(procedure, input) {
89
+ const schema = procedure["~orpc"].contract["~orpc"].InputSchema;
90
+ if (!schema)
91
+ return input;
92
+ const result = await schema["~standard"].validate(input);
93
+ if (result.issues) {
94
+ throw new ORPCError({
95
+ message: "Input validation failed",
96
+ code: "BAD_REQUEST",
97
+ issues: result.issues
98
+ });
99
+ }
100
+ return result.value;
101
+ }
102
+ async function validateOutput(procedure, output) {
103
+ const schema = procedure["~orpc"].contract["~orpc"].OutputSchema;
104
+ if (!schema)
105
+ return output;
106
+ const result = await schema["~standard"].validate(output);
107
+ if (result.issues) {
108
+ throw new ORPCError({
109
+ message: "Output validation failed",
110
+ code: "INTERNAL_SERVER_ERROR",
111
+ issues: result.issues
112
+ });
113
+ }
114
+ return result.value;
115
+ }
116
+ async function executeMiddlewareChain(procedure, input, context, meta) {
117
+ const middlewares = procedure["~orpc"].middlewares ?? [];
118
+ let currentMidIndex = 0;
119
+ let currentContext = context;
120
+ const next = async (nextOptions) => {
121
+ const mid = middlewares[currentMidIndex];
122
+ currentMidIndex += 1;
123
+ currentContext = mergeContext(currentContext, nextOptions.context);
124
+ if (mid) {
125
+ return await mid(input, currentContext, {
126
+ ...meta,
127
+ next,
128
+ output: (output) => ({ output, context: void 0 })
129
+ });
130
+ }
131
+ const result = {
132
+ output: await procedure["~orpc"].func(input, currentContext, meta),
133
+ context: currentContext
134
+ };
135
+ return result;
136
+ };
137
+ return (await next({})).output;
138
+ }
139
+
140
+ // src/router.ts
141
+ function getRouterChild(router, ...path) {
142
+ let current = router;
143
+ for (let i = 0; i < path.length; i++) {
144
+ const segment = path[i];
145
+ if (!current) {
146
+ return void 0;
147
+ }
148
+ if (isProcedure(current)) {
149
+ return void 0;
150
+ }
151
+ if (!isLazy(current)) {
152
+ current = current[segment];
153
+ continue;
154
+ }
155
+ const lazied = current;
156
+ const rest = path.slice(i);
157
+ const newLazy = lazy(async () => {
158
+ const unwrapped = await unlazy(lazied);
159
+ if (!unwrapped.default) {
160
+ return unwrapped;
161
+ }
162
+ const next = getRouterChild(unwrapped.default, ...rest);
163
+ return { default: next };
164
+ });
165
+ return flatLazy(newLazy);
166
+ }
167
+ return current;
168
+ }
169
+
170
+ export {
171
+ mergeContext,
172
+ Procedure,
173
+ isProcedure,
174
+ LAZY_LOADER_SYMBOL,
175
+ lazy,
176
+ isLazy,
177
+ unlazy,
178
+ flatLazy,
179
+ createProcedureClient,
180
+ getRouterChild
181
+ };
182
+ //# sourceMappingURL=chunk-FN62GL22.js.map