@orpc/server 0.0.0-next.1d55ec0 → 0.0.0-next.2f8ca7f

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.
@@ -0,0 +1,274 @@
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/middleware.ts
14
+ var decoratedMiddlewareSymbol = Symbol("\u{1F512}decoratedMiddleware");
15
+ function decorateMiddleware(middleware) {
16
+ if (Reflect.get(middleware, decoratedMiddlewareSymbol)) {
17
+ return middleware;
18
+ }
19
+ const concat = (concatMiddleware, mapInput2) => {
20
+ const concatMiddleware_ = mapInput2 ? decorateMiddleware(concatMiddleware).mapInput(mapInput2) : concatMiddleware;
21
+ return decorateMiddleware(async (input, context, meta, ...rest) => {
22
+ const input_ = input;
23
+ const context_ = context;
24
+ const meta_ = meta;
25
+ const next = async (options) => {
26
+ return concatMiddleware_(input_, mergeContext(context_, options.context), meta_, ...rest);
27
+ };
28
+ const m1 = await middleware(input_, context_, {
29
+ ...meta_,
30
+ next
31
+ }, ...rest);
32
+ return m1;
33
+ });
34
+ };
35
+ const mapInput = (map) => {
36
+ return decorateMiddleware(
37
+ (input, ...rest) => middleware(map(input), ...rest)
38
+ );
39
+ };
40
+ return Object.assign(middleware, {
41
+ [decoratedMiddlewareSymbol]: true,
42
+ concat,
43
+ mapInput
44
+ });
45
+ }
46
+
47
+ // src/procedure-caller.ts
48
+ import { executeWithHooks, trim, value } from "@orpc/shared";
49
+ import { ORPCError } from "@orpc/shared/error";
50
+
51
+ // src/procedure.ts
52
+ import {
53
+ DecoratedContractProcedure,
54
+ isContractProcedure
55
+ } from "@orpc/contract";
56
+ var Procedure = class {
57
+ constructor(zz$p) {
58
+ this.zz$p = zz$p;
59
+ }
60
+ };
61
+ var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
62
+ function decorateProcedure(procedure) {
63
+ if (DECORATED_PROCEDURE_SYMBOL in procedure) {
64
+ return procedure;
65
+ }
66
+ return Object.assign(createProcedureCaller({
67
+ procedure,
68
+ context: void 0
69
+ }), {
70
+ [DECORATED_PROCEDURE_SYMBOL]: true,
71
+ zz$p: procedure.zz$p,
72
+ prefix(prefix) {
73
+ return decorateProcedure({
74
+ zz$p: {
75
+ ...procedure.zz$p,
76
+ contract: DecoratedContractProcedure.decorate(
77
+ procedure.zz$p.contract
78
+ ).prefix(prefix)
79
+ }
80
+ });
81
+ },
82
+ route(opts) {
83
+ return decorateProcedure({
84
+ zz$p: {
85
+ ...procedure.zz$p,
86
+ contract: DecoratedContractProcedure.decorate(
87
+ procedure.zz$p.contract
88
+ ).route(opts)
89
+ }
90
+ });
91
+ },
92
+ use(middleware, mapInput) {
93
+ const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
94
+ return decorateProcedure({
95
+ zz$p: {
96
+ ...procedure.zz$p,
97
+ middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
98
+ }
99
+ });
100
+ }
101
+ });
102
+ }
103
+ function isProcedure(item) {
104
+ if (item instanceof Procedure)
105
+ return true;
106
+ return (typeof item === "object" || typeof item === "function") && item !== null && "zz$p" in item && typeof item.zz$p === "object" && item.zz$p !== null && "contract" in item.zz$p && isContractProcedure(item.zz$p.contract) && "func" in item.zz$p && typeof item.zz$p.func === "function";
107
+ }
108
+
109
+ // src/procedure-caller.ts
110
+ function createProcedureCaller(options) {
111
+ const caller = async (...args) => {
112
+ const [input, callerOptions] = args;
113
+ const path = options.path ?? [];
114
+ const procedure = await loadProcedure(options.procedure);
115
+ const context = await value(options.context);
116
+ const execute = async () => {
117
+ const validInput = (() => {
118
+ const schema = procedure.zz$p.contract.zz$cp.InputSchema;
119
+ if (!schema) {
120
+ return input;
121
+ }
122
+ try {
123
+ return schema.parse(input);
124
+ } catch (e) {
125
+ throw new ORPCError({
126
+ message: "Validation input failed",
127
+ code: "BAD_REQUEST",
128
+ cause: e
129
+ });
130
+ }
131
+ })();
132
+ const meta = {
133
+ path,
134
+ procedure,
135
+ signal: callerOptions?.signal
136
+ };
137
+ const middlewares = procedure.zz$p.middlewares ?? [];
138
+ let currentMidIndex = 0;
139
+ let currentContext = context;
140
+ const next = async (nextOptions) => {
141
+ const mid = middlewares[currentMidIndex];
142
+ currentMidIndex += 1;
143
+ currentContext = mergeContext(currentContext, nextOptions.context);
144
+ if (mid) {
145
+ return await mid(validInput, currentContext, {
146
+ ...meta,
147
+ next,
148
+ output: (output3) => ({ output: output3, context: void 0 })
149
+ });
150
+ } else {
151
+ return {
152
+ output: await await procedure.zz$p.func(validInput, currentContext, meta),
153
+ context: currentContext
154
+ };
155
+ }
156
+ };
157
+ const output2 = (await next({})).output;
158
+ const validOutput = await (async () => {
159
+ const schema = procedure.zz$p.contract.zz$cp.OutputSchema;
160
+ if (!schema) {
161
+ return output2;
162
+ }
163
+ const result = await schema.safeParseAsync(output2);
164
+ if (result.error) {
165
+ throw new ORPCError({
166
+ message: "Validation output failed",
167
+ code: "INTERNAL_SERVER_ERROR",
168
+ cause: result.error
169
+ });
170
+ }
171
+ return result.data;
172
+ })();
173
+ return validOutput;
174
+ };
175
+ const output = await executeWithHooks({
176
+ hooks: options,
177
+ input,
178
+ context,
179
+ meta: {
180
+ path,
181
+ procedure
182
+ },
183
+ execute
184
+ });
185
+ return output;
186
+ };
187
+ return caller;
188
+ }
189
+ async function loadProcedure(procedure) {
190
+ let loadedProcedure;
191
+ if (isLazy(procedure)) {
192
+ loadedProcedure = (await loadLazy(procedure)).default;
193
+ } else {
194
+ loadedProcedure = procedure;
195
+ }
196
+ if (!isProcedure(loadedProcedure)) {
197
+ throw new ORPCError({
198
+ code: "NOT_FOUND",
199
+ message: "Not found",
200
+ cause: new Error(trim(`
201
+ This error should be caught by the typescript compiler.
202
+ But if you still see this error, it means that you trying to call a lazy router (expected to be a lazy procedure).
203
+ `))
204
+ });
205
+ }
206
+ return loadedProcedure;
207
+ }
208
+
209
+ // src/lazy.ts
210
+ var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
211
+ function createLazy(loader) {
212
+ return {
213
+ [LAZY_LOADER_SYMBOL]: loader
214
+ };
215
+ }
216
+ function loadLazy(lazy) {
217
+ return lazy[LAZY_LOADER_SYMBOL]();
218
+ }
219
+ function isLazy(item) {
220
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
221
+ }
222
+ function createFlattenLazy(lazy) {
223
+ const flattenLoader = async () => {
224
+ let current = await loadLazy(lazy);
225
+ while (true) {
226
+ if (!isLazy(current.default)) {
227
+ break;
228
+ }
229
+ current = await loadLazy(current.default);
230
+ }
231
+ return current;
232
+ };
233
+ const flattenLazy = {
234
+ [LAZY_LOADER_SYMBOL]: flattenLoader
235
+ };
236
+ return flattenLazy;
237
+ }
238
+ function decorateLazy(lazy) {
239
+ const flattenLazy = createFlattenLazy(lazy);
240
+ const procedureCaller = createProcedureCaller({
241
+ procedure: flattenLazy,
242
+ context: void 0
243
+ });
244
+ Object.assign(procedureCaller, flattenLazy);
245
+ const recursive = new Proxy(procedureCaller, {
246
+ get(target, key) {
247
+ if (typeof key !== "string") {
248
+ return Reflect.get(target, key);
249
+ }
250
+ return decorateLazy(createLazy(async () => {
251
+ const current = await loadLazy(flattenLazy);
252
+ return { default: current.default[key] };
253
+ }));
254
+ }
255
+ });
256
+ return recursive;
257
+ }
258
+
259
+ export {
260
+ mergeContext,
261
+ decorateMiddleware,
262
+ LAZY_LOADER_SYMBOL,
263
+ createLazy,
264
+ loadLazy,
265
+ isLazy,
266
+ createFlattenLazy,
267
+ decorateLazy,
268
+ createProcedureCaller,
269
+ loadProcedure,
270
+ Procedure,
271
+ decorateProcedure,
272
+ isProcedure
273
+ };
274
+ //# sourceMappingURL=chunk-3JMSDC5L.js.map