@temporary-name/server 1.9.3-alpha.65222302f1b71807a849530b3fe0fa0326d3c1a2 → 1.9.3-alpha.6d3c10e7b535aec2de4b812161c8001a916134b9

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.
Files changed (42) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +4 -6
  2. package/dist/adapters/aws-lambda/index.d.ts +4 -6
  3. package/dist/adapters/aws-lambda/index.mjs +5 -7
  4. package/dist/adapters/fetch/index.d.mts +8 -86
  5. package/dist/adapters/fetch/index.d.ts +8 -86
  6. package/dist/adapters/fetch/index.mjs +17 -158
  7. package/dist/adapters/node/index.d.mts +8 -63
  8. package/dist/adapters/node/index.d.ts +8 -63
  9. package/dist/adapters/node/index.mjs +15 -123
  10. package/dist/adapters/standard/index.d.mts +20 -33
  11. package/dist/adapters/standard/index.d.ts +20 -33
  12. package/dist/adapters/standard/index.mjs +5 -7
  13. package/dist/helpers/index.mjs +3 -29
  14. package/dist/index.d.mts +122 -267
  15. package/dist/index.d.ts +122 -267
  16. package/dist/index.mjs +214 -380
  17. package/dist/openapi/index.d.mts +12 -28
  18. package/dist/openapi/index.d.ts +12 -28
  19. package/dist/openapi/index.mjs +66 -150
  20. package/dist/shared/server.BYV-qTJt.d.ts +41 -0
  21. package/dist/shared/server.C1RJffw4.mjs +30 -0
  22. package/dist/shared/server.CQIFwyhc.mjs +40 -0
  23. package/dist/shared/server.CWb33y9B.d.mts +41 -0
  24. package/dist/shared/server.ChOv1yG3.mjs +319 -0
  25. package/dist/shared/server.DXzEGRE2.mjs +403 -0
  26. package/dist/shared/server.DgzAlPjF.mjs +160 -0
  27. package/dist/shared/server.DimTvmOQ.d.mts +373 -0
  28. package/dist/shared/server.DimTvmOQ.d.ts +373 -0
  29. package/dist/shared/server.YUvuxHty.mjs +48 -0
  30. package/package.json +10 -26
  31. package/dist/plugins/index.d.mts +0 -160
  32. package/dist/plugins/index.d.ts +0 -160
  33. package/dist/plugins/index.mjs +0 -288
  34. package/dist/shared/server.BEHw7Eyx.mjs +0 -247
  35. package/dist/shared/server.BKSOrA6h.d.mts +0 -192
  36. package/dist/shared/server.BKSOrA6h.d.ts +0 -192
  37. package/dist/shared/server.BKh8I1Ny.mjs +0 -239
  38. package/dist/shared/server.BeuTpcmO.d.mts +0 -23
  39. package/dist/shared/server.C1fnTLq0.d.mts +0 -57
  40. package/dist/shared/server.CQyYNJ1H.d.ts +0 -57
  41. package/dist/shared/server.DLsti1Pv.mjs +0 -293
  42. package/dist/shared/server.SLLuK6_v.d.ts +0 -23
@@ -0,0 +1,319 @@
1
+ import { HTTPMethods } from '@temporary-name/shared';
2
+ import * as z from '@temporary-name/zod';
3
+
4
+ function isStartWithMiddlewares(middlewares, compare) {
5
+ if (compare.length > middlewares.length) {
6
+ return false;
7
+ }
8
+ for (let i = 0; i < middlewares.length; i++) {
9
+ if (compare[i] === void 0) {
10
+ return true;
11
+ }
12
+ if (middlewares[i] !== compare[i]) {
13
+ return false;
14
+ }
15
+ }
16
+ return true;
17
+ }
18
+ function mergeMiddlewares(first, second, options) {
19
+ if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
20
+ return second;
21
+ }
22
+ return [...first, ...second];
23
+ }
24
+ function addMiddleware(middlewares, addition) {
25
+ return [...middlewares, addition];
26
+ }
27
+
28
+ class Contract {
29
+ /**
30
+ * This property holds the defined options.
31
+ */
32
+ "~orpc";
33
+ constructor(def) {
34
+ this["~orpc"] = def;
35
+ }
36
+ }
37
+ class Procedure extends Contract {
38
+ }
39
+ function isProcedure(item) {
40
+ return item instanceof Procedure || // This is so we'll return true for Proxy-wrapped Procedures e.g. as returned by `callable`
41
+ (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "route" in item["~orpc"] && "meta" in item["~orpc"] && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
42
+ }
43
+
44
+ class ValidationError extends Error {
45
+ issues;
46
+ data;
47
+ constructor(options) {
48
+ super(options.message, options);
49
+ this.issues = options.issues;
50
+ this.data = options.data;
51
+ }
52
+ }
53
+
54
+ function mergeRoute(a, b) {
55
+ return { ...a, ...b };
56
+ }
57
+ function prefixRoute(route, prefix) {
58
+ if (!route.path) {
59
+ return route;
60
+ }
61
+ return {
62
+ ...route,
63
+ path: `${prefix}${route.path}`
64
+ };
65
+ }
66
+ function unshiftTagRoute(route, tags) {
67
+ return {
68
+ ...route,
69
+ tags: [...tags, ...route.tags ?? []]
70
+ };
71
+ }
72
+ function mergePrefix(a, b) {
73
+ return a ? `${a}${b}` : b;
74
+ }
75
+ function mergeTags(a, b) {
76
+ return a ? [...a, ...b] : b;
77
+ }
78
+ function enhanceRoute(route, options) {
79
+ let router = route;
80
+ if (options.prefix) {
81
+ router = prefixRoute(router, options.prefix);
82
+ }
83
+ if (options.tags?.length) {
84
+ router = unshiftTagRoute(router, options.tags);
85
+ }
86
+ return router;
87
+ }
88
+
89
+ function getRouter(router, path) {
90
+ let current = router;
91
+ for (let i = 0; i < path.length; i++) {
92
+ const segment = path[i];
93
+ if (!current) {
94
+ return void 0;
95
+ }
96
+ if (isProcedure(current)) {
97
+ return void 0;
98
+ }
99
+ if (!isLazy(current)) {
100
+ current = current[segment];
101
+ continue;
102
+ }
103
+ const lazied = current;
104
+ const rest = path.slice(i);
105
+ return lazyInternal(async () => {
106
+ const unwrapped = await unlazy(lazied);
107
+ const next = getRouter(unwrapped.default, rest);
108
+ return unlazy(next);
109
+ }, getLazyMeta(lazied));
110
+ }
111
+ return current;
112
+ }
113
+ function createAccessibleLazyRouter(lazied) {
114
+ const recursive = new Proxy(lazied, {
115
+ get(target, key) {
116
+ if (typeof key !== "string") {
117
+ return Reflect.get(target, key);
118
+ }
119
+ const next = getRouter(lazied, [key]);
120
+ return createAccessibleLazyRouter(next);
121
+ }
122
+ });
123
+ return recursive;
124
+ }
125
+ function enhanceRouter(router, options) {
126
+ if (isLazy(router)) {
127
+ const laziedMeta = getLazyMeta(router);
128
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
129
+ const enhanced2 = lazyInternal(
130
+ async () => {
131
+ const { default: unlaziedRouter } = await unlazy(router);
132
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
133
+ return unlazy(enhanced3);
134
+ },
135
+ {
136
+ ...laziedMeta,
137
+ prefix: enhancedPrefix
138
+ }
139
+ );
140
+ const accessible = createAccessibleLazyRouter(enhanced2);
141
+ return accessible;
142
+ }
143
+ if (isProcedure(router)) {
144
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
145
+ dedupeLeading: options.dedupeLeadingMiddlewares
146
+ });
147
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
148
+ const enhanced2 = new Procedure({
149
+ ...router["~orpc"],
150
+ route: enhanceRoute(router["~orpc"].route, options),
151
+ middlewares: newMiddlewares,
152
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
153
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
154
+ });
155
+ return enhanced2;
156
+ }
157
+ const enhanced = {};
158
+ for (const key in router) {
159
+ enhanced[key] = enhanceRouter(router[key], options);
160
+ }
161
+ return enhanced;
162
+ }
163
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
164
+ const currentRouter = options.router;
165
+ if (isLazy(currentRouter)) {
166
+ lazyOptions.push({
167
+ router: currentRouter,
168
+ path: options.path
169
+ });
170
+ } else if (currentRouter instanceof Contract) {
171
+ callback({
172
+ contract: currentRouter,
173
+ path: options.path
174
+ });
175
+ } else if (typeof currentRouter === "string") {
176
+ throw new Error("Unexpected: got string instead of router");
177
+ } else {
178
+ for (const key in currentRouter) {
179
+ traverseContractProcedures(
180
+ {
181
+ router: currentRouter[key],
182
+ path: [...options.path, key]
183
+ },
184
+ callback,
185
+ lazyOptions
186
+ );
187
+ }
188
+ }
189
+ return lazyOptions;
190
+ }
191
+ async function resolveContractProcedures(options, callback) {
192
+ const pending = [options];
193
+ for (const options2 of pending) {
194
+ const lazyOptions = traverseContractProcedures(options2, callback);
195
+ for (const options3 of lazyOptions) {
196
+ const { default: router } = await unlazy(options3.router);
197
+ pending.push({
198
+ router,
199
+ path: options3.path
200
+ });
201
+ }
202
+ }
203
+ }
204
+ async function unlazyRouter(router) {
205
+ if (isProcedure(router)) {
206
+ return router;
207
+ }
208
+ const unlazied = {};
209
+ for (const key in router) {
210
+ const item = router[key];
211
+ const { default: unlaziedRouter } = await unlazy(item);
212
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
213
+ }
214
+ return unlazied;
215
+ }
216
+
217
+ const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
218
+ function lazyInternal(loader, meta = {}) {
219
+ return {
220
+ [LAZY_SYMBOL]: {
221
+ loader,
222
+ meta
223
+ }
224
+ };
225
+ }
226
+ function lazy(prefix, loader) {
227
+ return enhanceRouter(lazyInternal(loader), {
228
+ middlewares: [],
229
+ dedupeLeadingMiddlewares: true,
230
+ prefix
231
+ });
232
+ }
233
+ function isLazy(item) {
234
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
235
+ }
236
+ function getLazyMeta(lazied) {
237
+ return lazied[LAZY_SYMBOL].meta;
238
+ }
239
+ function unlazy(lazied) {
240
+ return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
241
+ }
242
+
243
+ const endpointRegex = new RegExp(`^(${HTTPMethods.join("|")})`);
244
+ function standardizeHTTPPath(path) {
245
+ return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
246
+ }
247
+ function getDynamicParams(path) {
248
+ return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
249
+ raw: v,
250
+ name: v.match(/\{\+?([^}]+)\}/)[1]
251
+ })) : void 0;
252
+ }
253
+ function parseEndpointDefinition(stringsOrEndpoint, values) {
254
+ let method, path, pathSchema;
255
+ if (stringsOrEndpoint instanceof Array) {
256
+ let endpoint = stringsOrEndpoint[0];
257
+ if (endpoint === void 0 || !endpointRegex.test(endpoint)) {
258
+ throw new Error(".endpoint() must start with a valid HTTP endpoint string.");
259
+ }
260
+ const shape = {};
261
+ for (let i = 1; i < stringsOrEndpoint.length; i++) {
262
+ const str = stringsOrEndpoint[i];
263
+ const value = values[i - 1];
264
+ if (typeof value !== "object" || value instanceof z.core.$ZodType) {
265
+ throw new Error(
266
+ `Each template value for .endpoint must be an object with a single key, whose value is a ZodType.`
267
+ );
268
+ }
269
+ const valueEntries = Object.entries(value);
270
+ if (valueEntries.length !== 1) {
271
+ throw new Error(
272
+ `Each template value for .endpoint must be an object with a single key, whose value is a ZodType.`
273
+ );
274
+ }
275
+ const [key, schema] = valueEntries[0];
276
+ endpoint += `{${key}}${str}`;
277
+ if (key in schema) {
278
+ throw new Error(`Duplicate path parameter name "${key}" in endpoint.`);
279
+ }
280
+ shape[key] = schema;
281
+ }
282
+ [method, path] = endpoint.split(" ", 2);
283
+ pathSchema = z.object(shape);
284
+ } else if (values.length <= 1) {
285
+ const endpoint = stringsOrEndpoint;
286
+ const schema = values[0];
287
+ [method, path] = endpoint.split(" ", 2);
288
+ const pathParamNames = getDynamicParams(path)?.map((p) => p.name) ?? [];
289
+ let schemaKeys;
290
+ if (schema instanceof z.core.$ZodType) {
291
+ if (schema instanceof z.core.$ZodObject) {
292
+ schemaKeys = Object.keys(schema._zod.def.shape);
293
+ pathSchema = schema;
294
+ } else {
295
+ throw new Error(
296
+ `Path schema for endpoint "${endpoint}" must be a ZodObject schema (or object where each value is a ZodType).`
297
+ );
298
+ }
299
+ } else if (typeof schema === "object") {
300
+ schemaKeys = Object.keys(schema);
301
+ pathSchema = z.object(schema);
302
+ } else if (schema !== void 0) {
303
+ throw new Error(
304
+ `Path schema for endpoint "${endpoint}" must be a ZodObject schema (or object where each value is a ZodType).`
305
+ );
306
+ } else {
307
+ schemaKeys = [];
308
+ pathSchema = z.object({});
309
+ }
310
+ if (pathParamNames.length !== schemaKeys.length || !pathParamNames.every((name) => schemaKeys.includes(name))) {
311
+ throw new Error(`Path schema keys do not match dynamic parameters in endpoint "${endpoint}".`);
312
+ }
313
+ } else {
314
+ throw new Error("Invalid arguments for .endpoint() method.");
315
+ }
316
+ return { method, path, pathSchema };
317
+ }
318
+
319
+ export { Contract as C, LAZY_SYMBOL as L, Procedure as P, ValidationError as V, mergeTags as a, mergeRoute as b, prefixRoute as c, addMiddleware as d, enhanceRouter as e, getLazyMeta as f, getDynamicParams as g, isLazy as h, isProcedure as i, getRouter as j, lazy as k, lazyInternal as l, mergePrefix as m, isStartWithMiddlewares as n, mergeMiddlewares as o, parseEndpointDefinition as p, createAccessibleLazyRouter as q, resolveContractProcedures as r, standardizeHTTPPath as s, traverseContractProcedures as t, unlazy as u, unlazyRouter as v, endpointRegex as w };