@orpc/server 0.11.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -45,13 +45,92 @@ function decorateMiddleware(middleware) {
45
45
  }
46
46
 
47
47
  // src/procedure-caller.ts
48
- import { value } from "@orpc/shared";
48
+ import { trim, value } from "@orpc/shared";
49
49
  import { ORPCError } from "@orpc/shared/error";
50
50
  import { OpenAPIDeserializer } from "@orpc/transformer";
51
+
52
+ // src/procedure.ts
53
+ import {
54
+ DecoratedContractProcedure,
55
+ isContractProcedure
56
+ } from "@orpc/contract";
57
+ var Procedure = class {
58
+ constructor(zz$p) {
59
+ this.zz$p = zz$p;
60
+ }
61
+ };
62
+ var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
63
+ function decorateProcedure(procedure) {
64
+ if (DECORATED_PROCEDURE_SYMBOL in procedure) {
65
+ return procedure;
66
+ }
67
+ return Object.assign(createProcedureCaller({
68
+ procedure,
69
+ context: void 0
70
+ }), {
71
+ [DECORATED_PROCEDURE_SYMBOL]: true,
72
+ zz$p: procedure.zz$p,
73
+ prefix(prefix) {
74
+ return decorateProcedure({
75
+ zz$p: {
76
+ ...procedure.zz$p,
77
+ contract: DecoratedContractProcedure.decorate(
78
+ procedure.zz$p.contract
79
+ ).prefix(prefix)
80
+ }
81
+ });
82
+ },
83
+ route(opts) {
84
+ return decorateProcedure({
85
+ zz$p: {
86
+ ...procedure.zz$p,
87
+ contract: DecoratedContractProcedure.decorate(
88
+ procedure.zz$p.contract
89
+ ).route(opts)
90
+ }
91
+ });
92
+ },
93
+ use(middleware, mapInput) {
94
+ const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
95
+ return decorateProcedure({
96
+ zz$p: {
97
+ ...procedure.zz$p,
98
+ middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
99
+ }
100
+ });
101
+ }
102
+ });
103
+ }
104
+ function isProcedure(item) {
105
+ if (item instanceof Procedure)
106
+ return true;
107
+ 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";
108
+ }
109
+
110
+ // src/procedure-caller.ts
51
111
  function createProcedureCaller(options) {
52
- const path = options.path ?? [];
53
- const procedure = options.procedure;
112
+ const loadProcedure = async () => {
113
+ let procedure;
114
+ if (isLazy(options.procedure)) {
115
+ procedure = (await loadLazy(options.procedure)).default;
116
+ } else {
117
+ procedure = options.procedure;
118
+ }
119
+ if (!isProcedure(procedure)) {
120
+ throw new ORPCError({
121
+ code: "NOT_FOUND",
122
+ message: "Not found",
123
+ cause: new Error(trim(`
124
+ This error should be caught by the typescript compiler.
125
+ But if you still see this error, it means that you trying to call a lazy router (expected to be a lazy procedure).
126
+ `))
127
+ });
128
+ }
129
+ return procedure;
130
+ };
54
131
  const caller = async (input) => {
132
+ const path = options.path ?? [];
133
+ const procedure = await loadProcedure();
55
134
  const input_ = (() => {
56
135
  if (!(input instanceof FormData)) {
57
136
  return input;
@@ -121,69 +200,68 @@ function createProcedureCaller(options) {
121
200
  return caller;
122
201
  }
123
202
 
124
- // src/procedure.ts
125
- import {
126
- DecoratedContractProcedure,
127
- isContractProcedure
128
- } from "@orpc/contract";
129
- var Procedure = class {
130
- constructor(zz$p) {
131
- this.zz$p = zz$p;
132
- }
133
- };
134
- var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
135
- function decorateProcedure(procedure) {
136
- if (DECORATED_PROCEDURE_SYMBOL in procedure) {
137
- return procedure;
138
- }
139
- return Object.assign(createProcedureCaller({
140
- procedure,
203
+ // src/lazy.ts
204
+ var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
205
+ function createLazy(loader) {
206
+ return {
207
+ [LAZY_LOADER_SYMBOL]: loader
208
+ };
209
+ }
210
+ function loadLazy(lazy) {
211
+ return lazy[LAZY_LOADER_SYMBOL]();
212
+ }
213
+ function isLazy(item) {
214
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
215
+ }
216
+ function createFlattenLazy(lazy) {
217
+ const flattenLoader = async () => {
218
+ let current = await loadLazy(lazy);
219
+ while (true) {
220
+ if (!isLazy(current.default)) {
221
+ break;
222
+ }
223
+ current = await loadLazy(current.default);
224
+ }
225
+ return current;
226
+ };
227
+ const flattenLazy = {
228
+ [LAZY_LOADER_SYMBOL]: flattenLoader
229
+ };
230
+ return flattenLazy;
231
+ }
232
+ function decorateLazy(lazy) {
233
+ const flattenLazy = createFlattenLazy(lazy);
234
+ const procedureCaller = createProcedureCaller({
235
+ procedure: flattenLazy,
141
236
  context: void 0
142
- }), {
143
- [DECORATED_PROCEDURE_SYMBOL]: true,
144
- zz$p: procedure.zz$p,
145
- prefix(prefix) {
146
- return decorateProcedure({
147
- zz$p: {
148
- ...procedure.zz$p,
149
- contract: DecoratedContractProcedure.decorate(
150
- procedure.zz$p.contract
151
- ).prefix(prefix)
152
- }
153
- });
154
- },
155
- route(opts) {
156
- return decorateProcedure({
157
- zz$p: {
158
- ...procedure.zz$p,
159
- contract: DecoratedContractProcedure.decorate(
160
- procedure.zz$p.contract
161
- ).route(opts)
162
- }
163
- });
164
- },
165
- use(middleware, mapInput) {
166
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
167
- return decorateProcedure({
168
- zz$p: {
169
- ...procedure.zz$p,
170
- middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
171
- }
172
- });
237
+ });
238
+ Object.assign(procedureCaller, flattenLazy);
239
+ const recursive = new Proxy(procedureCaller, {
240
+ get(target, key) {
241
+ if (typeof key !== "string") {
242
+ return Reflect.get(target, key);
243
+ }
244
+ return decorateLazy(createLazy(async () => {
245
+ const current = await loadLazy(flattenLazy);
246
+ return { default: current.default[key] };
247
+ }));
173
248
  }
174
249
  });
175
- }
176
- function isProcedure(item) {
177
- if (item instanceof Procedure)
178
- return true;
179
- 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";
250
+ return recursive;
180
251
  }
181
252
 
182
253
  export {
183
254
  mergeContext,
184
255
  decorateMiddleware,
256
+ LAZY_LOADER_SYMBOL,
257
+ createLazy,
258
+ loadLazy,
259
+ isLazy,
260
+ createFlattenLazy,
261
+ decorateLazy,
185
262
  createProcedureCaller,
186
263
  Procedure,
187
264
  decorateProcedure,
188
265
  isProcedure
189
266
  };
267
+ //# sourceMappingURL=chunk-FL4ZAGNE.js.map