@temporary-name/server 1.9.3-alpha.8dcf0da5d97e7b89ab5ce50c8d1733c158e629a8 → 1.9.3-alpha.907c7c78d0193d34752279de92d699e50d6bc3a1
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.
- package/dist/adapters/aws-lambda/index.d.mts +4 -6
- package/dist/adapters/aws-lambda/index.d.ts +4 -6
- package/dist/adapters/aws-lambda/index.mjs +4 -4
- package/dist/adapters/fetch/index.d.mts +8 -86
- package/dist/adapters/fetch/index.d.ts +8 -86
- package/dist/adapters/fetch/index.mjs +16 -155
- package/dist/adapters/node/index.d.mts +8 -63
- package/dist/adapters/node/index.d.ts +8 -63
- package/dist/adapters/node/index.mjs +14 -120
- package/dist/adapters/standard/index.d.mts +5 -7
- package/dist/adapters/standard/index.d.ts +5 -7
- package/dist/adapters/standard/index.mjs +4 -4
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +104 -182
- package/dist/index.d.ts +104 -182
- package/dist/index.mjs +126 -104
- package/dist/openapi/index.d.mts +11 -27
- package/dist/openapi/index.d.ts +11 -27
- package/dist/openapi/index.mjs +8 -75
- package/dist/shared/server.Bj_UpI5O.d.mts +372 -0
- package/dist/shared/server.Bj_UpI5O.d.ts +372 -0
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.C6VnFy4S.d.mts +41 -0
- package/dist/shared/server.CQIFwyhc.mjs +40 -0
- package/dist/shared/server.ChOv1yG3.mjs +319 -0
- package/dist/shared/server.CnzXkSjj.d.ts +41 -0
- package/dist/shared/server.DXzEGRE2.mjs +403 -0
- package/dist/shared/server.DgzAlPjF.mjs +160 -0
- package/dist/shared/server.YUvuxHty.mjs +48 -0
- package/package.json +10 -27
- package/dist/plugins/index.d.mts +0 -160
- package/dist/plugins/index.d.ts +0 -160
- package/dist/plugins/index.mjs +0 -288
- package/dist/shared/server.BSJugGGA.d.mts +0 -56
- package/dist/shared/server.Bs8EZATl.d.ts +0 -23
- package/dist/shared/server.CHV9AQHl.mjs +0 -412
- package/dist/shared/server.CWWP8ypC.d.mts +0 -239
- package/dist/shared/server.CWWP8ypC.d.ts +0 -239
- package/dist/shared/server.Cn7WsRHl.mjs +0 -254
- package/dist/shared/server.DrMgIiBr.d.mts +0 -23
- package/dist/shared/server.EKwDe0d2.d.ts +0 -56
- package/dist/shared/server.JtIZ8YG7.mjs +0 -237
|
@@ -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 };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { HTTPPath, StandardResponse, StandardLazyRequest } from '@temporary-name/shared';
|
|
2
|
+
import { C as Context, m as Router } from './server.Bj_UpI5O.js';
|
|
3
|
+
|
|
4
|
+
interface StandardHandleOptions<T extends Context> {
|
|
5
|
+
prefix?: HTTPPath;
|
|
6
|
+
context: T;
|
|
7
|
+
}
|
|
8
|
+
type StandardHandleResult = {
|
|
9
|
+
matched: true;
|
|
10
|
+
response: StandardResponse;
|
|
11
|
+
} | {
|
|
12
|
+
matched: false;
|
|
13
|
+
response: undefined;
|
|
14
|
+
};
|
|
15
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
|
16
|
+
}
|
|
17
|
+
declare class StandardHandler<T extends Context> {
|
|
18
|
+
private readonly matcher;
|
|
19
|
+
constructor(router: Router<T>, _options: NoInfer<StandardHandlerOptions<T>>);
|
|
20
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
24
|
+
context?: T;
|
|
25
|
+
} : {
|
|
26
|
+
context: T;
|
|
27
|
+
});
|
|
28
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
29
|
+
/**
|
|
30
|
+
* {@link https://github.com/unjs/rou3}
|
|
31
|
+
*
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
declare function toRou3Pattern(path: HTTPPath): string;
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
declare function decodeParams(params: Record<string, string>): Record<string, string>;
|
|
39
|
+
|
|
40
|
+
export { StandardHandler as c, decodeParams as d, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
41
|
+
export type { FriendlyStandardHandleOptions as F, StandardHandleOptions as S, StandardHandleResult as a, StandardHandlerOptions as b };
|