@aura-stack/router 0.6.0-rc.2 → 0.6.0
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/assert.cjs +3 -0
- package/dist/assert.d.ts +1 -2
- package/dist/assert.js +1 -1
- package/dist/chunk-3ATEFJBU.js +87 -0
- package/dist/chunk-FS3EN7NZ.js +114 -0
- package/dist/{chunk-SY4MM2AG.js → chunk-HHL2LY22.js} +1 -1
- package/dist/{chunk-SP2NSNB2.js → chunk-ISIML7WX.js} +7 -4
- package/dist/{chunk-6CIHAUKJ.js → chunk-JAYQXZDB.js} +5 -5
- package/dist/{chunk-3X2BFSRT.js → chunk-NFASFT4W.js} +3 -0
- package/dist/chunk-VRGPOTTV.js +42 -0
- package/dist/{chunk-5F6FUKTB.js → chunk-ZTWC6PXG.js} +1 -1
- package/dist/client.cjs +7 -4
- package/dist/client.d.ts +0 -1
- package/dist/client.js +1 -1
- package/dist/context.d.ts +0 -1
- package/dist/context.js +2 -2
- package/dist/endpoint.cjs +3 -0
- package/dist/endpoint.d.ts +3 -4
- package/dist/endpoint.js +2 -2
- package/dist/index.cjs +167 -100
- package/dist/index.d.ts +1 -2
- package/dist/index.js +8 -6
- package/dist/middlewares.cjs +5 -5
- package/dist/middlewares.d.ts +3 -4
- package/dist/middlewares.js +1 -1
- package/dist/on-error.cjs +144 -0
- package/dist/on-error.d.ts +9 -0
- package/dist/on-error.js +8 -0
- package/dist/router.cjs +178 -120
- package/dist/router.d.ts +2 -24
- package/dist/router.js +8 -12
- package/dist/trie.cjs +203 -0
- package/dist/trie.d.ts +34 -0
- package/dist/trie.js +9 -0
- package/dist/types.d.ts +91 -14
- package/package.json +4 -1
- package/dist/chunk-WJXMLTGP.js +0 -163
package/dist/index.cjs
CHANGED
|
@@ -113,6 +113,9 @@ var InvalidZodSchemaError = class {
|
|
|
113
113
|
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
|
|
114
114
|
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
115
115
|
var isSupportedMethod = (method) => {
|
|
116
|
+
if (Array.isArray(method)) {
|
|
117
|
+
return method.every((meth) => supportedMethods.has(meth));
|
|
118
|
+
}
|
|
116
119
|
return supportedMethods.has(method);
|
|
117
120
|
};
|
|
118
121
|
var isSupportedBodyMethod = (method) => {
|
|
@@ -150,6 +153,143 @@ function createEndpointConfig(...args) {
|
|
|
150
153
|
return args[0];
|
|
151
154
|
}
|
|
152
155
|
|
|
156
|
+
// src/trie.ts
|
|
157
|
+
var TrieNode = class {
|
|
158
|
+
param;
|
|
159
|
+
statics = /* @__PURE__ */ new Map();
|
|
160
|
+
endpoints = /* @__PURE__ */ new Map();
|
|
161
|
+
};
|
|
162
|
+
var TrieRouter = class {
|
|
163
|
+
root;
|
|
164
|
+
statics;
|
|
165
|
+
methods;
|
|
166
|
+
constructor() {
|
|
167
|
+
this.statics = /* @__PURE__ */ new Map();
|
|
168
|
+
this.methods = /* @__PURE__ */ new Set();
|
|
169
|
+
this.root = new TrieNode();
|
|
170
|
+
}
|
|
171
|
+
add(endpoint) {
|
|
172
|
+
const isDynamic = endpoint.route.includes(":");
|
|
173
|
+
const methods = Array.isArray(endpoint.method) ? endpoint.method : [endpoint.method];
|
|
174
|
+
if (!isDynamic) {
|
|
175
|
+
for (const method of methods) {
|
|
176
|
+
this.statics.set(`${method} ${endpoint.route}`, endpoint);
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
let node = this.root;
|
|
180
|
+
const route = endpoint.route;
|
|
181
|
+
const routeLength = route.length;
|
|
182
|
+
let prev = 0;
|
|
183
|
+
while (prev < routeLength) {
|
|
184
|
+
const curr = route.indexOf("/", prev);
|
|
185
|
+
const end = curr === -1 ? routeLength : curr;
|
|
186
|
+
if (end > prev) {
|
|
187
|
+
const segment = route.slice(prev, end);
|
|
188
|
+
if (segment[0] === ":") {
|
|
189
|
+
const name = segment.slice(1);
|
|
190
|
+
let param = node.param;
|
|
191
|
+
if (!param) {
|
|
192
|
+
param = { name, node: new TrieNode() };
|
|
193
|
+
node.param = param;
|
|
194
|
+
} else if (param.name !== name) {
|
|
195
|
+
throw new RouterError(
|
|
196
|
+
"BAD_REQUEST",
|
|
197
|
+
`Conflicting in the route by the dynamic segment "${param.name}" and "${name}"`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
node = param.node;
|
|
201
|
+
} else {
|
|
202
|
+
let child = node.statics.get(segment);
|
|
203
|
+
if (!child) {
|
|
204
|
+
child = new TrieNode();
|
|
205
|
+
node.statics.set(segment, child);
|
|
206
|
+
}
|
|
207
|
+
node = child;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (curr === -1) {
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
prev = curr + 1;
|
|
214
|
+
}
|
|
215
|
+
for (const method of methods) {
|
|
216
|
+
node.endpoints.set(method, endpoint);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const method of methods) {
|
|
220
|
+
this.methods.add(method);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
match(method, pathname) {
|
|
224
|
+
const staticEndpoint = this.statics.get(`${method} ${pathname}`);
|
|
225
|
+
if (staticEndpoint) {
|
|
226
|
+
return { endpoint: staticEndpoint, params: {} };
|
|
227
|
+
}
|
|
228
|
+
let node = this.root;
|
|
229
|
+
const params = {};
|
|
230
|
+
const pathLength = pathname.length;
|
|
231
|
+
let prev = 0;
|
|
232
|
+
while (prev < pathLength) {
|
|
233
|
+
const curr = pathname.indexOf("/", prev);
|
|
234
|
+
const end = curr === -1 ? pathLength : curr;
|
|
235
|
+
if (end > prev) {
|
|
236
|
+
const segment = pathname.slice(prev, end);
|
|
237
|
+
const staticNode = node.statics.get(segment);
|
|
238
|
+
if (staticNode) {
|
|
239
|
+
node = staticNode;
|
|
240
|
+
} else {
|
|
241
|
+
const param = node.param;
|
|
242
|
+
if (!param) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
params[param.name] = decodeURIComponent(segment);
|
|
246
|
+
node = param.node;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (curr === -1) {
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
prev = curr + 1;
|
|
253
|
+
}
|
|
254
|
+
const endpoint = node.endpoints.get(method);
|
|
255
|
+
if (!endpoint) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return { endpoint, params };
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// src/on-error.ts
|
|
263
|
+
var onError = async (error, request, config) => {
|
|
264
|
+
if (config.onError) {
|
|
265
|
+
try {
|
|
266
|
+
const response = await config.onError(error, request);
|
|
267
|
+
return response;
|
|
268
|
+
} catch {
|
|
269
|
+
return Response.json(
|
|
270
|
+
{ message: "A critical failure occurred during error handling" },
|
|
271
|
+
{ status: 500, statusText: statusText.INTERNAL_SERVER_ERROR }
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (isInvalidZodSchemaError(error)) {
|
|
276
|
+
const { errors, status, statusText: statusText2 } = error;
|
|
277
|
+
return Response.json(
|
|
278
|
+
{
|
|
279
|
+
message: "Invalid request data",
|
|
280
|
+
error: "validation_error",
|
|
281
|
+
details: errors
|
|
282
|
+
},
|
|
283
|
+
{ status, statusText: statusText2 }
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
if (isRouterError(error)) {
|
|
287
|
+
const { message, status, statusText: statusText2 } = error;
|
|
288
|
+
return Response.json({ message }, { status, statusText: statusText2 });
|
|
289
|
+
}
|
|
290
|
+
return Response.json({ message: "Internal Server Error" }, { status: 500, statusText: statusText.INTERNAL_SERVER_ERROR });
|
|
291
|
+
};
|
|
292
|
+
|
|
153
293
|
// src/headers.ts
|
|
154
294
|
var import_cookie = require("cookie");
|
|
155
295
|
var HeadersBuilder = class {
|
|
@@ -263,9 +403,9 @@ var createContentTypeRegex = (contentTypes, contenType) => {
|
|
|
263
403
|
};
|
|
264
404
|
|
|
265
405
|
// src/middlewares.ts
|
|
266
|
-
var executeGlobalMiddlewares = async (context,
|
|
267
|
-
if (!
|
|
268
|
-
for (const middleware of
|
|
406
|
+
var executeGlobalMiddlewares = async (context, use) => {
|
|
407
|
+
if (!use) return context;
|
|
408
|
+
for (const middleware of use) {
|
|
269
409
|
if (typeof middleware !== "function") {
|
|
270
410
|
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
271
411
|
}
|
|
@@ -280,10 +420,10 @@ var executeGlobalMiddlewares = async (context, middlewares) => {
|
|
|
280
420
|
}
|
|
281
421
|
return context;
|
|
282
422
|
};
|
|
283
|
-
var executeMiddlewares = async (context,
|
|
423
|
+
var executeMiddlewares = async (context, use = []) => {
|
|
284
424
|
try {
|
|
285
425
|
let ctx = context;
|
|
286
|
-
for (const middleware of
|
|
426
|
+
for (const middleware of use) {
|
|
287
427
|
if (typeof middleware !== "function") {
|
|
288
428
|
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
289
429
|
}
|
|
@@ -296,103 +436,24 @@ var executeMiddlewares = async (context, middlewares = []) => {
|
|
|
296
436
|
};
|
|
297
437
|
|
|
298
438
|
// src/router.ts
|
|
299
|
-
var
|
|
300
|
-
statics: /* @__PURE__ */ new Map(),
|
|
301
|
-
endpoints: /* @__PURE__ */ new Map()
|
|
302
|
-
});
|
|
303
|
-
var insert = (root, endpoint) => {
|
|
304
|
-
if (!root || !endpoint) return;
|
|
305
|
-
let node = root;
|
|
306
|
-
const segments = endpoint.route === "/" ? [] : endpoint.route.split("/").filter(Boolean);
|
|
307
|
-
for (const segment of segments) {
|
|
308
|
-
if (segment.startsWith(":")) {
|
|
309
|
-
const name = segment.slice(1);
|
|
310
|
-
if (!node.param) {
|
|
311
|
-
node.param = { name, node: createNode() };
|
|
312
|
-
} else if (node.param.name !== name) {
|
|
313
|
-
throw new RouterError(
|
|
314
|
-
"BAD_REQUEST",
|
|
315
|
-
`Conflicting in the route by the dynamic segment "${node.param.name}" and "${name}"`
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
node = node.param.node;
|
|
319
|
-
} else {
|
|
320
|
-
if (!node.statics.has(segment)) {
|
|
321
|
-
node.statics.set(segment, createNode());
|
|
322
|
-
}
|
|
323
|
-
node = node.statics.get(segment);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
if (node.endpoints.has(endpoint.method)) {
|
|
327
|
-
throw new RouterError("BAD_REQUEST", `Duplicate endpoint for ${endpoint?.method} ${endpoint?.route}`);
|
|
328
|
-
}
|
|
329
|
-
node.endpoints.set(endpoint.method, endpoint);
|
|
330
|
-
};
|
|
331
|
-
var search = (method, root, pathname) => {
|
|
332
|
-
let node = root;
|
|
333
|
-
const params = {};
|
|
334
|
-
const segments = pathname === "/" ? [] : pathname.split("/").filter(Boolean);
|
|
335
|
-
for (const segment of segments) {
|
|
336
|
-
if (node?.statics.has(segment)) {
|
|
337
|
-
node = node.statics.get(segment);
|
|
338
|
-
} else if (node?.param) {
|
|
339
|
-
params[node.param.name] = decodeURIComponent(segment);
|
|
340
|
-
node = node.param.node;
|
|
341
|
-
} else {
|
|
342
|
-
throw new RouterError("NOT_FOUND", `No route found for path: ${pathname}`);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
if (!node.endpoints.has(method)) {
|
|
346
|
-
throw new RouterError("NOT_FOUND", `No route found for path: ${pathname}`);
|
|
347
|
-
}
|
|
348
|
-
return { endpoint: node.endpoints.get(method), params };
|
|
349
|
-
};
|
|
350
|
-
var handleError = async (error, request, config) => {
|
|
351
|
-
if (config.onError) {
|
|
352
|
-
try {
|
|
353
|
-
const response = await config.onError(error, request);
|
|
354
|
-
return response;
|
|
355
|
-
} catch {
|
|
356
|
-
return Response.json(
|
|
357
|
-
{ message: "A critical failure occurred during error handling" },
|
|
358
|
-
{ status: 500, statusText: statusText.INTERNAL_SERVER_ERROR }
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
if (isInvalidZodSchemaError(error)) {
|
|
363
|
-
const { errors, status, statusText: statusText2 } = error;
|
|
364
|
-
return Response.json(
|
|
365
|
-
{
|
|
366
|
-
message: "Invalid request data",
|
|
367
|
-
error: "validation_error",
|
|
368
|
-
details: errors
|
|
369
|
-
},
|
|
370
|
-
{ status, statusText: statusText2 }
|
|
371
|
-
);
|
|
372
|
-
}
|
|
373
|
-
if (isRouterError(error)) {
|
|
374
|
-
const { message, status, statusText: statusText2 } = error;
|
|
375
|
-
return Response.json({ message }, { status, statusText: statusText2 });
|
|
376
|
-
}
|
|
377
|
-
return Response.json({ message: "Internal Server Error" }, { status: 500, statusText: statusText.INTERNAL_SERVER_ERROR });
|
|
378
|
-
};
|
|
379
|
-
var handleRequest = async (method, request, config, root) => {
|
|
439
|
+
var handleRequest = async (method, request, config, router) => {
|
|
380
440
|
try {
|
|
381
441
|
if (!isSupportedMethod(request.method)) {
|
|
382
442
|
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${request.method}' is not supported`);
|
|
383
443
|
}
|
|
384
444
|
const globalContext = { request, context: config.context ?? {} };
|
|
385
|
-
const globalRequestContext = await executeGlobalMiddlewares(globalContext, config.
|
|
445
|
+
const globalRequestContext = await executeGlobalMiddlewares(globalContext, config.use);
|
|
386
446
|
if (globalRequestContext instanceof Response) return globalRequestContext;
|
|
387
447
|
const url = new URL(globalRequestContext.request.url);
|
|
388
448
|
const pathnameWithBase = url.pathname;
|
|
389
449
|
if (globalRequestContext.request.method !== method) {
|
|
390
450
|
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${globalRequestContext.request.method}' is not allowed`);
|
|
391
451
|
}
|
|
392
|
-
const
|
|
393
|
-
if (
|
|
394
|
-
throw new RouterError("
|
|
452
|
+
const node = router.match(method, pathnameWithBase);
|
|
453
|
+
if (!node) {
|
|
454
|
+
throw new RouterError("NOT_FOUND", `No route found for path: ${pathnameWithBase}`);
|
|
395
455
|
}
|
|
456
|
+
const { endpoint, params } = node;
|
|
396
457
|
const dynamicParams = getRouteParams(params, endpoint.config);
|
|
397
458
|
const body = await getBody(globalRequestContext.request, endpoint.config);
|
|
398
459
|
const searchParams = getSearchParams(globalRequestContext.request.url, endpoint.config);
|
|
@@ -408,31 +469,35 @@ var handleRequest = async (method, request, config, root) => {
|
|
|
408
469
|
route: endpoint.route,
|
|
409
470
|
context: config.context ?? {}
|
|
410
471
|
};
|
|
411
|
-
context = await executeMiddlewares(context, endpoint.config.
|
|
472
|
+
context = await executeMiddlewares(context, endpoint.config.use);
|
|
412
473
|
const response = await endpoint.handler(context);
|
|
413
474
|
return response;
|
|
414
475
|
} catch (error) {
|
|
415
|
-
return
|
|
476
|
+
return onError(error, request, config);
|
|
416
477
|
}
|
|
417
478
|
};
|
|
418
479
|
var createRouter = (endpoints, config = {}) => {
|
|
419
|
-
const
|
|
480
|
+
const router = new TrieRouter();
|
|
420
481
|
const server = {};
|
|
421
482
|
const methods = /* @__PURE__ */ new Set();
|
|
422
483
|
for (const endpoint of endpoints) {
|
|
423
484
|
const withBasePath = config.basePath ? `${config.basePath}${endpoint.route}` : endpoint.route;
|
|
424
|
-
|
|
425
|
-
|
|
485
|
+
router.add({ ...endpoint, route: withBasePath });
|
|
486
|
+
const endpointMethods = Array.isArray(endpoint.method) ? endpoint.method : [endpoint.method];
|
|
487
|
+
for (const method of endpointMethods) {
|
|
488
|
+
methods.add(method);
|
|
489
|
+
}
|
|
426
490
|
}
|
|
427
491
|
for (const method of methods) {
|
|
428
|
-
server[method] = (request) => handleRequest(method, request, config,
|
|
492
|
+
server[method] = (request) => handleRequest(method, request, config, router);
|
|
429
493
|
}
|
|
430
494
|
return server;
|
|
431
495
|
};
|
|
432
496
|
|
|
433
497
|
// src/client.ts
|
|
434
498
|
function createClient(options) {
|
|
435
|
-
const { baseURL, headers: defaultHeaders } = options;
|
|
499
|
+
const { baseURL, basePath, headers: defaultHeaders, fetch: customFetch, ...clientOptions } = options;
|
|
500
|
+
const fetchFn = customFetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
436
501
|
return new Proxy(
|
|
437
502
|
{},
|
|
438
503
|
{
|
|
@@ -440,7 +505,7 @@ function createClient(options) {
|
|
|
440
505
|
const method = prop.toString().toUpperCase();
|
|
441
506
|
return async (path, ctx) => {
|
|
442
507
|
const searchParams = new URLSearchParams(ctx?.searchParams);
|
|
443
|
-
let resolvedPath = `${
|
|
508
|
+
let resolvedPath = `${basePath ?? ""}${path}`;
|
|
444
509
|
for (const [key, value] of Object.entries(ctx?.params ?? {})) {
|
|
445
510
|
resolvedPath = resolvedPath.replace(`:${key}`, String(value));
|
|
446
511
|
}
|
|
@@ -449,11 +514,13 @@ function createClient(options) {
|
|
|
449
514
|
url.search = searchParams.toString();
|
|
450
515
|
}
|
|
451
516
|
const { params: _p, searchParams: _s, ...requestInit } = ctx ?? {};
|
|
452
|
-
const
|
|
517
|
+
const headers = typeof defaultHeaders === "function" ? await defaultHeaders() : defaultHeaders;
|
|
518
|
+
const response = await fetchFn(url.toString(), {
|
|
519
|
+
...clientOptions,
|
|
453
520
|
...requestInit,
|
|
454
521
|
method,
|
|
455
522
|
headers: {
|
|
456
|
-
...
|
|
523
|
+
...headers,
|
|
457
524
|
...ctx?.headers
|
|
458
525
|
},
|
|
459
526
|
body: ctx?.body ? ctx.body instanceof FormData ? ctx.body : JSON.stringify(ctx.body) : void 0
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export { createClient } from './client.js';
|
|
|
4
4
|
export { isInvalidZodSchemaError, isRouterError } from './assert.js';
|
|
5
5
|
export { RouterError, statusCode, statusText } from './error.js';
|
|
6
6
|
export { HeadersBuilder } from './headers.js';
|
|
7
|
-
export { Client, ClientOptions, ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, ExtractEndpoint, ExtractRoutesByMethod, Find, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferEndpoints, InferMethod, InferZod, MiddlewareFunction, Prettify, RemoveUndefined, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, Router, RouterConfig, RoutesByMethod, ToInferZod } from './types.js';
|
|
7
|
+
export { Client, ClientOptions, ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, ExtractEndpoint, ExtractRoutesByMethod, FetchLike, Find, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferEndpoints, InferMethod, InferZod, MiddlewareFunction, Prettify, RemoveUndefined, RequestContext, RequestHeaders, RouteEndpoint, RouteHandler, RoutePattern, Router, RouterConfig, RoutesByMethod, ToInferZod } from './types.js';
|
|
8
8
|
import 'cookie';
|
|
9
9
|
import 'zod';
|
|
10
|
-
import 'http';
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createRouter
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-3ATEFJBU.js";
|
|
4
|
+
import "./chunk-JAYQXZDB.js";
|
|
5
|
+
import "./chunk-VRGPOTTV.js";
|
|
6
|
+
import "./chunk-FS3EN7NZ.js";
|
|
5
7
|
import {
|
|
6
8
|
createClient
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import "./chunk-
|
|
9
|
+
} from "./chunk-ISIML7WX.js";
|
|
10
|
+
import "./chunk-HHL2LY22.js";
|
|
9
11
|
import {
|
|
10
12
|
createEndpoint,
|
|
11
13
|
createEndpointConfig
|
|
12
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-ZTWC6PXG.js";
|
|
13
15
|
import {
|
|
14
16
|
isInvalidZodSchemaError,
|
|
15
17
|
isRouterError
|
|
16
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-NFASFT4W.js";
|
|
17
19
|
import {
|
|
18
20
|
RouterError,
|
|
19
21
|
statusCode,
|
package/dist/middlewares.cjs
CHANGED
|
@@ -92,9 +92,9 @@ var RouterError = class extends AuraStackRouterError {
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
// src/middlewares.ts
|
|
95
|
-
var executeGlobalMiddlewares = async (context,
|
|
96
|
-
if (!
|
|
97
|
-
for (const middleware of
|
|
95
|
+
var executeGlobalMiddlewares = async (context, use) => {
|
|
96
|
+
if (!use) return context;
|
|
97
|
+
for (const middleware of use) {
|
|
98
98
|
if (typeof middleware !== "function") {
|
|
99
99
|
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
100
100
|
}
|
|
@@ -109,10 +109,10 @@ var executeGlobalMiddlewares = async (context, middlewares) => {
|
|
|
109
109
|
}
|
|
110
110
|
return context;
|
|
111
111
|
};
|
|
112
|
-
var executeMiddlewares = async (context,
|
|
112
|
+
var executeMiddlewares = async (context, use = []) => {
|
|
113
113
|
try {
|
|
114
114
|
let ctx = context;
|
|
115
|
-
for (const middleware of
|
|
115
|
+
for (const middleware of use) {
|
|
116
116
|
if (typeof middleware !== "function") {
|
|
117
117
|
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
118
118
|
}
|
package/dist/middlewares.d.ts
CHANGED
|
@@ -3,16 +3,15 @@ import 'zod';
|
|
|
3
3
|
import './error.js';
|
|
4
4
|
import './headers.js';
|
|
5
5
|
import 'cookie';
|
|
6
|
-
import 'http';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Executes the middlewares in sequence, passing the request to each middleware.
|
|
10
9
|
*
|
|
11
10
|
* @param request - Original request made from the client
|
|
12
11
|
* @param middlewares - Array of global middleware functions to be executed
|
|
13
|
-
* @returns - The modified
|
|
12
|
+
* @returns - The modified context after all middlewares have been executed
|
|
14
13
|
*/
|
|
15
|
-
declare const executeGlobalMiddlewares: (context: GlobalMiddlewareContext,
|
|
14
|
+
declare const executeGlobalMiddlewares: (context: GlobalMiddlewareContext, use: RouterConfig["use"]) => Promise<Response | GlobalMiddlewareContext>;
|
|
16
15
|
/**
|
|
17
16
|
* Executes middlewares in sequence, passing the request and context to each middleware.
|
|
18
17
|
*
|
|
@@ -21,6 +20,6 @@ declare const executeGlobalMiddlewares: (context: GlobalMiddlewareContext, middl
|
|
|
21
20
|
* @param middlewares - Array of middleware functions to be executed
|
|
22
21
|
* @returns The modified context after all middlewares have been executed
|
|
23
22
|
*/
|
|
24
|
-
declare const executeMiddlewares: <const RouteParams extends Record<string, string>, const Config extends EndpointConfig>(context: RequestContext<RouteParams, Config>,
|
|
23
|
+
declare const executeMiddlewares: <const RouteParams extends Record<string, string>, const Config extends EndpointConfig>(context: RequestContext<RouteParams, Config>, use?: MiddlewareFunction<RouteParams, Config>[]) => Promise<RequestContext<RouteParams, Config>>;
|
|
25
24
|
|
|
26
25
|
export { executeGlobalMiddlewares, executeMiddlewares };
|
package/dist/middlewares.js
CHANGED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/on-error.ts
|
|
21
|
+
var on_error_exports = {};
|
|
22
|
+
__export(on_error_exports, {
|
|
23
|
+
onError: () => onError
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(on_error_exports);
|
|
26
|
+
|
|
27
|
+
// src/error.ts
|
|
28
|
+
var statusCode = {
|
|
29
|
+
OK: 200,
|
|
30
|
+
CREATED: 201,
|
|
31
|
+
ACCEPTED: 202,
|
|
32
|
+
NO_CONTENT: 204,
|
|
33
|
+
MULTIPLE_CHOICES: 300,
|
|
34
|
+
MOVED_PERMANENTLY: 301,
|
|
35
|
+
FOUND: 302,
|
|
36
|
+
SEE_OTHER: 303,
|
|
37
|
+
NOT_MODIFIED: 304,
|
|
38
|
+
TEMPORARY_REDIRECT: 307,
|
|
39
|
+
BAD_REQUEST: 400,
|
|
40
|
+
UNAUTHORIZED: 401,
|
|
41
|
+
PAYMENT_REQUIRED: 402,
|
|
42
|
+
FORBIDDEN: 403,
|
|
43
|
+
NOT_FOUND: 404,
|
|
44
|
+
METHOD_NOT_ALLOWED: 405,
|
|
45
|
+
NOT_ACCEPTABLE: 406,
|
|
46
|
+
PROXY_AUTHENTICATION_REQUIRED: 407,
|
|
47
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
48
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
49
|
+
NOT_IMPLEMENTED: 501,
|
|
50
|
+
BAD_GATEWAY: 502,
|
|
51
|
+
SERVICE_UNAVAILABLE: 503,
|
|
52
|
+
HTTP_VERSION_NOT_SUPPORTED: 505
|
|
53
|
+
};
|
|
54
|
+
var statusText = Object.keys(statusCode).reduce(
|
|
55
|
+
(previous, status) => {
|
|
56
|
+
return { ...previous, [status]: status };
|
|
57
|
+
},
|
|
58
|
+
{}
|
|
59
|
+
);
|
|
60
|
+
var AuraStackRouterError = class extends Error {
|
|
61
|
+
/**
|
|
62
|
+
* The HTTP status code associated with the error.
|
|
63
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
|
|
64
|
+
* @example
|
|
65
|
+
* NOT_FOUND: 404
|
|
66
|
+
* METHOD_NOT_ALLOWED: 405
|
|
67
|
+
* INTERNAL_SERVER_ERROR: 500
|
|
68
|
+
*/
|
|
69
|
+
status;
|
|
70
|
+
/**
|
|
71
|
+
* The HTTP status text associated with the status code of the error.
|
|
72
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
|
|
73
|
+
* @example
|
|
74
|
+
* NOT_FOUND: NOT_FOUND
|
|
75
|
+
* METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
|
|
76
|
+
* INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
|
|
77
|
+
*/
|
|
78
|
+
statusText;
|
|
79
|
+
constructor(type, message, name) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = name ?? "RouterError";
|
|
82
|
+
this.status = statusCode[type];
|
|
83
|
+
this.statusText = statusText[type];
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var RouterError = class extends AuraStackRouterError {
|
|
87
|
+
constructor(type, message, name) {
|
|
88
|
+
super(type, message, name);
|
|
89
|
+
this.name = name ?? "RouterError";
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var InvalidZodSchemaError = class {
|
|
93
|
+
status;
|
|
94
|
+
statusText;
|
|
95
|
+
errors;
|
|
96
|
+
constructor(type, errors) {
|
|
97
|
+
this.status = statusCode[type];
|
|
98
|
+
this.statusText = statusText[type];
|
|
99
|
+
this.errors = errors;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/assert.ts
|
|
104
|
+
var isRouterError = (error) => {
|
|
105
|
+
return error instanceof RouterError;
|
|
106
|
+
};
|
|
107
|
+
var isInvalidZodSchemaError = (error) => {
|
|
108
|
+
return error instanceof InvalidZodSchemaError;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// src/on-error.ts
|
|
112
|
+
var onError = async (error, request, config) => {
|
|
113
|
+
if (config.onError) {
|
|
114
|
+
try {
|
|
115
|
+
const response = await config.onError(error, request);
|
|
116
|
+
return response;
|
|
117
|
+
} catch {
|
|
118
|
+
return Response.json(
|
|
119
|
+
{ message: "A critical failure occurred during error handling" },
|
|
120
|
+
{ status: 500, statusText: statusText.INTERNAL_SERVER_ERROR }
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (isInvalidZodSchemaError(error)) {
|
|
125
|
+
const { errors, status, statusText: statusText2 } = error;
|
|
126
|
+
return Response.json(
|
|
127
|
+
{
|
|
128
|
+
message: "Invalid request data",
|
|
129
|
+
error: "validation_error",
|
|
130
|
+
details: errors
|
|
131
|
+
},
|
|
132
|
+
{ status, statusText: statusText2 }
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (isRouterError(error)) {
|
|
136
|
+
const { message, status, statusText: statusText2 } = error;
|
|
137
|
+
return Response.json({ message }, { status, statusText: statusText2 });
|
|
138
|
+
}
|
|
139
|
+
return Response.json({ message: "Internal Server Error" }, { status: 500, statusText: statusText.INTERNAL_SERVER_ERROR });
|
|
140
|
+
};
|
|
141
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
142
|
+
0 && (module.exports = {
|
|
143
|
+
onError
|
|
144
|
+
});
|