@aura-stack/router 0.3.0 → 0.5.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/README.md +2 -2
- package/dist/assert.cjs +17 -0
- package/dist/assert.d.ts +12 -2
- package/dist/assert.js +6 -2
- package/dist/{chunk-6PZEXNTS.js → chunk-4UYDR5IO.js} +2 -2
- package/dist/{chunk-GJC3ODME.js → chunk-B6PMGVSL.js} +9 -1
- package/dist/{chunk-FWDOXDWG.js → chunk-BWIKAYZV.js} +9 -9
- package/dist/{chunk-JNMXLKDG.js → chunk-FU35BPU7.js} +11 -2
- package/dist/{chunk-JIA6NLL6.js → chunk-TEDN6QMU.js} +39 -19
- package/dist/{chunk-PT4GU6PH.js → chunk-XB5P5CQZ.js} +22 -9
- package/dist/chunk-Z6JJAIWN.js +37 -0
- package/dist/context.cjs +27 -8
- package/dist/context.d.ts +12 -20
- package/dist/context.js +5 -5
- package/dist/cookie.cjs +24 -0
- package/dist/cookie.d.ts +1 -0
- package/dist/cookie.js +2 -0
- package/dist/endpoint.d.ts +2 -0
- package/dist/endpoint.js +3 -3
- package/dist/error.cjs +9 -0
- package/dist/error.d.ts +7 -1
- package/dist/error.js +3 -1
- package/dist/headers.cjs +61 -0
- package/dist/headers.d.ts +20 -0
- package/dist/headers.js +6 -0
- package/dist/index.cjs +105 -28
- package/dist/index.d.ts +4 -2
- package/dist/index.js +14 -8
- package/dist/middlewares.cjs +8 -8
- package/dist/middlewares.d.ts +5 -3
- package/dist/middlewares.js +2 -2
- package/dist/router.cjs +101 -28
- package/dist/router.d.ts +2 -0
- package/dist/router.js +6 -5
- package/dist/types.d.ts +29 -6
- package/package.json +39 -13
- package/dist/assert.d.cts +0 -52
- package/dist/context.d.cts +0 -85
- package/dist/endpoint.d.cts +0 -60
- package/dist/error.d.cts +0 -65
- package/dist/index.d.cts +0 -6
- package/dist/middlewares.d.cts +0 -23
- package/dist/router.d.cts +0 -37
- package/dist/types.d.cts +0 -191
package/dist/error.d.ts
CHANGED
|
@@ -61,5 +61,11 @@ declare class AuraStackRouterError extends Error {
|
|
|
61
61
|
declare class RouterError extends AuraStackRouterError {
|
|
62
62
|
constructor(type: StatusCode, message: string, name?: string);
|
|
63
63
|
}
|
|
64
|
+
declare class InvalidZodSchemaError {
|
|
65
|
+
readonly status: number;
|
|
66
|
+
readonly statusText: StatusCode;
|
|
67
|
+
readonly errors: Record<string, string>;
|
|
68
|
+
constructor(type: StatusCode, errors: Record<string, string>);
|
|
69
|
+
}
|
|
64
70
|
|
|
65
|
-
export { AuraStackRouterError, RouterError, statusCode, statusText };
|
|
71
|
+
export { AuraStackRouterError, InvalidZodSchemaError, RouterError, statusCode, statusText };
|
package/dist/error.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AuraStackRouterError,
|
|
3
|
+
InvalidZodSchemaError,
|
|
3
4
|
RouterError,
|
|
4
5
|
statusCode,
|
|
5
6
|
statusText
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-B6PMGVSL.js";
|
|
7
8
|
export {
|
|
8
9
|
AuraStackRouterError,
|
|
10
|
+
InvalidZodSchemaError,
|
|
9
11
|
RouterError,
|
|
10
12
|
statusCode,
|
|
11
13
|
statusText
|
package/dist/headers.cjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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/headers.ts
|
|
21
|
+
var headers_exports = {};
|
|
22
|
+
__export(headers_exports, {
|
|
23
|
+
HeadersBuilder: () => HeadersBuilder
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(headers_exports);
|
|
26
|
+
var import_cookie = require("cookie");
|
|
27
|
+
var HeadersBuilder = class {
|
|
28
|
+
constructor(initialHeaders) {
|
|
29
|
+
this.headers = new Headers(initialHeaders);
|
|
30
|
+
}
|
|
31
|
+
setHeader(name, value) {
|
|
32
|
+
this.headers.set(name, value);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
setCookie(name, value, options) {
|
|
36
|
+
this.headers.append("Set-Cookie", (0, import_cookie.serialize)(name, value, options));
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
getHeader(name) {
|
|
40
|
+
return this.headers.get(name);
|
|
41
|
+
}
|
|
42
|
+
getCookie(name) {
|
|
43
|
+
const cookies = (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
44
|
+
return cookies[name];
|
|
45
|
+
}
|
|
46
|
+
getSetCookie(name) {
|
|
47
|
+
const cookies = this.headers.getSetCookie();
|
|
48
|
+
const cookie = cookies.find((cookie2) => cookie2.startsWith(name + "="));
|
|
49
|
+
return cookie ? (0, import_cookie.parseSetCookie)(cookie).value : void 0;
|
|
50
|
+
}
|
|
51
|
+
toHeaders() {
|
|
52
|
+
return new Headers(this.headers);
|
|
53
|
+
}
|
|
54
|
+
toCookies() {
|
|
55
|
+
return (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
+
0 && (module.exports = {
|
|
60
|
+
HeadersBuilder
|
|
61
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SerializeOptions, Cookies } from 'cookie';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A builder class for constructing and manipulating HTTP headers.
|
|
5
|
+
* It includes methods to set, delete, and retrieve headers, as well as
|
|
6
|
+
* manage cookies within the headers.
|
|
7
|
+
*/
|
|
8
|
+
declare class HeadersBuilder {
|
|
9
|
+
private headers;
|
|
10
|
+
constructor(initialHeaders?: HeadersInit);
|
|
11
|
+
setHeader(name: string, value: string): HeadersBuilder;
|
|
12
|
+
setCookie(name: string, value: string, options?: SerializeOptions): HeadersBuilder;
|
|
13
|
+
getHeader(name: string): string | null;
|
|
14
|
+
getCookie(name: string): string | undefined;
|
|
15
|
+
getSetCookie(name: string): string | undefined;
|
|
16
|
+
toHeaders(): Headers;
|
|
17
|
+
toCookies(): Cookies;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { HeadersBuilder };
|
package/dist/headers.js
ADDED
package/dist/index.cjs
CHANGED
|
@@ -20,10 +20,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
HeadersBuilder: () => HeadersBuilder,
|
|
23
24
|
RouterError: () => RouterError,
|
|
24
25
|
createEndpoint: () => createEndpoint,
|
|
25
26
|
createEndpointConfig: () => createEndpointConfig,
|
|
26
27
|
createRouter: () => createRouter,
|
|
28
|
+
isInvalidZodSchemaError: () => isInvalidZodSchemaError,
|
|
27
29
|
isRouterError: () => isRouterError,
|
|
28
30
|
statusCode: () => statusCode,
|
|
29
31
|
statusText: () => statusText
|
|
@@ -77,6 +79,13 @@ var RouterError = class extends AuraStackRouterError {
|
|
|
77
79
|
this.name = name ?? "RouterError";
|
|
78
80
|
}
|
|
79
81
|
};
|
|
82
|
+
var InvalidZodSchemaError = class {
|
|
83
|
+
constructor(type, errors) {
|
|
84
|
+
this.status = statusCode[type];
|
|
85
|
+
this.statusText = statusText[type];
|
|
86
|
+
this.errors = errors;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
80
89
|
|
|
81
90
|
// src/assert.ts
|
|
82
91
|
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
|
|
@@ -97,6 +106,9 @@ var isValidHandler = (handler) => {
|
|
|
97
106
|
var isRouterError = (error) => {
|
|
98
107
|
return error instanceof RouterError;
|
|
99
108
|
};
|
|
109
|
+
var isInvalidZodSchemaError = (error) => {
|
|
110
|
+
return error instanceof InvalidZodSchemaError;
|
|
111
|
+
};
|
|
100
112
|
|
|
101
113
|
// src/endpoint.ts
|
|
102
114
|
var createEndpoint = (method, route, handler, config = {}) => {
|
|
@@ -116,12 +128,61 @@ function createEndpointConfig(...args) {
|
|
|
116
128
|
return args[0];
|
|
117
129
|
}
|
|
118
130
|
|
|
131
|
+
// src/headers.ts
|
|
132
|
+
var import_cookie = require("cookie");
|
|
133
|
+
var HeadersBuilder = class {
|
|
134
|
+
constructor(initialHeaders) {
|
|
135
|
+
this.headers = new Headers(initialHeaders);
|
|
136
|
+
}
|
|
137
|
+
setHeader(name, value) {
|
|
138
|
+
this.headers.set(name, value);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
setCookie(name, value, options) {
|
|
142
|
+
this.headers.append("Set-Cookie", (0, import_cookie.serialize)(name, value, options));
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
getHeader(name) {
|
|
146
|
+
return this.headers.get(name);
|
|
147
|
+
}
|
|
148
|
+
getCookie(name) {
|
|
149
|
+
const cookies = (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
150
|
+
return cookies[name];
|
|
151
|
+
}
|
|
152
|
+
getSetCookie(name) {
|
|
153
|
+
const cookies = this.headers.getSetCookie();
|
|
154
|
+
const cookie = cookies.find((cookie2) => cookie2.startsWith(name + "="));
|
|
155
|
+
return cookie ? (0, import_cookie.parseSetCookie)(cookie).value : void 0;
|
|
156
|
+
}
|
|
157
|
+
toHeaders() {
|
|
158
|
+
return new Headers(this.headers);
|
|
159
|
+
}
|
|
160
|
+
toCookies() {
|
|
161
|
+
return (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
119
165
|
// src/context.ts
|
|
166
|
+
var formatZodError = (error) => {
|
|
167
|
+
if (!error.issues || error.issues.length === 0) {
|
|
168
|
+
return {};
|
|
169
|
+
}
|
|
170
|
+
return error.issues.reduce((previous, issue) => {
|
|
171
|
+
const key = issue.path.join(".");
|
|
172
|
+
return {
|
|
173
|
+
...previous,
|
|
174
|
+
[key]: {
|
|
175
|
+
code: issue.code,
|
|
176
|
+
message: issue.message
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}, {});
|
|
180
|
+
};
|
|
120
181
|
var getRouteParams = (params, config) => {
|
|
121
182
|
if (config.schemas?.params) {
|
|
122
183
|
const parsed = config.schemas.params.safeParse(params);
|
|
123
184
|
if (!parsed.success) {
|
|
124
|
-
throw new
|
|
185
|
+
throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
|
|
125
186
|
}
|
|
126
187
|
return parsed.data;
|
|
127
188
|
}
|
|
@@ -132,15 +193,12 @@ var getSearchParams = (url, config) => {
|
|
|
132
193
|
if (config.schemas?.searchParams) {
|
|
133
194
|
const parsed = config.schemas.searchParams.safeParse(Object.fromEntries(route.searchParams.entries()));
|
|
134
195
|
if (!parsed.success) {
|
|
135
|
-
throw new
|
|
196
|
+
throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
|
|
136
197
|
}
|
|
137
198
|
return parsed.data;
|
|
138
199
|
}
|
|
139
200
|
return new URLSearchParams(route.searchParams.toString());
|
|
140
201
|
};
|
|
141
|
-
var getHeaders = (request) => {
|
|
142
|
-
return new Headers(request.headers);
|
|
143
|
-
};
|
|
144
202
|
var getBody = async (request, config) => {
|
|
145
203
|
if (!isSupportedBodyMethod(request.method)) {
|
|
146
204
|
return null;
|
|
@@ -152,7 +210,7 @@ var getBody = async (request, config) => {
|
|
|
152
210
|
if (config.schemas?.body) {
|
|
153
211
|
const parsed = config.schemas.body.safeParse(json);
|
|
154
212
|
if (!parsed.success) {
|
|
155
|
-
throw new
|
|
213
|
+
throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
|
|
156
214
|
}
|
|
157
215
|
return parsed.data;
|
|
158
216
|
}
|
|
@@ -182,31 +240,31 @@ var createContentTypeRegex = (contentTypes, contenType) => {
|
|
|
182
240
|
};
|
|
183
241
|
|
|
184
242
|
// src/middlewares.ts
|
|
185
|
-
var executeGlobalMiddlewares = async (
|
|
186
|
-
if (!middlewares) return
|
|
243
|
+
var executeGlobalMiddlewares = async (context, middlewares) => {
|
|
244
|
+
if (!middlewares) return context;
|
|
187
245
|
for (const middleware of middlewares) {
|
|
188
246
|
if (typeof middleware !== "function") {
|
|
189
247
|
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
190
248
|
}
|
|
191
|
-
const executed = await middleware(
|
|
249
|
+
const executed = await middleware(context);
|
|
192
250
|
if (executed instanceof Response) {
|
|
193
251
|
return executed;
|
|
194
252
|
}
|
|
195
|
-
|
|
253
|
+
context = executed;
|
|
196
254
|
}
|
|
197
|
-
if (!
|
|
255
|
+
if (!context || !(context.request instanceof Request)) {
|
|
198
256
|
throw new RouterError("BAD_REQUEST", "Global middleware must return a Request or Response object");
|
|
199
257
|
}
|
|
200
|
-
return
|
|
258
|
+
return context;
|
|
201
259
|
};
|
|
202
|
-
var executeMiddlewares = async (
|
|
260
|
+
var executeMiddlewares = async (context, middlewares = []) => {
|
|
203
261
|
try {
|
|
204
262
|
let ctx = context;
|
|
205
263
|
for (const middleware of middlewares) {
|
|
206
264
|
if (typeof middleware !== "function") {
|
|
207
265
|
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
208
266
|
}
|
|
209
|
-
ctx = await middleware(
|
|
267
|
+
ctx = await middleware(ctx);
|
|
210
268
|
}
|
|
211
269
|
return ctx;
|
|
212
270
|
} catch {
|
|
@@ -278,6 +336,17 @@ var handleError = async (error, request, config) => {
|
|
|
278
336
|
);
|
|
279
337
|
}
|
|
280
338
|
}
|
|
339
|
+
if (isInvalidZodSchemaError(error)) {
|
|
340
|
+
const { errors, status, statusText: statusText2 } = error;
|
|
341
|
+
return Response.json(
|
|
342
|
+
{
|
|
343
|
+
message: "Invalid request data",
|
|
344
|
+
error: "validation_error",
|
|
345
|
+
details: errors
|
|
346
|
+
},
|
|
347
|
+
{ status, statusText: statusText2 }
|
|
348
|
+
);
|
|
349
|
+
}
|
|
281
350
|
if (isRouterError(error)) {
|
|
282
351
|
const { message, status, statusText: statusText2 } = error;
|
|
283
352
|
return Response.json({ message }, { status, statusText: statusText2 });
|
|
@@ -289,29 +358,35 @@ var handleRequest = async (method, request, config, root) => {
|
|
|
289
358
|
if (!isSupportedMethod(request.method)) {
|
|
290
359
|
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${request.method}' is not supported`);
|
|
291
360
|
}
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
361
|
+
const globalContext = { request, context: config.context ?? {} };
|
|
362
|
+
const globalRequestContext = await executeGlobalMiddlewares(globalContext, config.middlewares);
|
|
363
|
+
if (globalRequestContext instanceof Response) return globalRequestContext;
|
|
364
|
+
const url = new URL(globalRequestContext.request.url);
|
|
295
365
|
const pathnameWithBase = url.pathname;
|
|
296
|
-
if (
|
|
297
|
-
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${
|
|
366
|
+
if (globalRequestContext.request.method !== method) {
|
|
367
|
+
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${globalRequestContext.request.method}' is not allowed`);
|
|
298
368
|
}
|
|
299
369
|
const { endpoint, params } = search(method, root, pathnameWithBase);
|
|
300
|
-
if (endpoint.method !==
|
|
301
|
-
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${
|
|
370
|
+
if (endpoint.method !== globalRequestContext.request.method) {
|
|
371
|
+
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${globalRequestContext.request.method}' is not allowed`);
|
|
302
372
|
}
|
|
303
373
|
const dynamicParams = getRouteParams(params, endpoint.config);
|
|
304
|
-
const body = await getBody(
|
|
305
|
-
const searchParams = getSearchParams(
|
|
306
|
-
const headers =
|
|
307
|
-
|
|
374
|
+
const body = await getBody(globalRequestContext.request, endpoint.config);
|
|
375
|
+
const searchParams = getSearchParams(globalRequestContext.request.url, endpoint.config);
|
|
376
|
+
const headers = new HeadersBuilder(globalRequestContext.request.headers);
|
|
377
|
+
let context = {
|
|
308
378
|
params: dynamicParams,
|
|
309
379
|
searchParams,
|
|
310
380
|
headers,
|
|
311
|
-
body
|
|
381
|
+
body,
|
|
382
|
+
request: globalRequestContext.request,
|
|
383
|
+
url,
|
|
384
|
+
method: globalRequestContext.request.method,
|
|
385
|
+
route: endpoint.route,
|
|
386
|
+
context: config.context ?? {}
|
|
312
387
|
};
|
|
313
|
-
await executeMiddlewares(
|
|
314
|
-
const response = await endpoint.handler(
|
|
388
|
+
context = await executeMiddlewares(context, endpoint.config.middlewares);
|
|
389
|
+
const response = await endpoint.handler(context);
|
|
315
390
|
return response;
|
|
316
391
|
} catch (error) {
|
|
317
392
|
return handleError(error, request, config);
|
|
@@ -333,10 +408,12 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
333
408
|
};
|
|
334
409
|
// Annotate the CommonJS export names for ESM import in node:
|
|
335
410
|
0 && (module.exports = {
|
|
411
|
+
HeadersBuilder,
|
|
336
412
|
RouterError,
|
|
337
413
|
createEndpoint,
|
|
338
414
|
createEndpointConfig,
|
|
339
415
|
createRouter,
|
|
416
|
+
isInvalidZodSchemaError,
|
|
340
417
|
isRouterError,
|
|
341
418
|
statusCode,
|
|
342
419
|
statusText
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { createEndpoint, createEndpointConfig } from './endpoint.js';
|
|
2
2
|
export { createRouter } from './router.js';
|
|
3
|
-
export { isRouterError } from './assert.js';
|
|
3
|
+
export { isInvalidZodSchemaError, isRouterError } from './assert.js';
|
|
4
4
|
export { RouterError, statusCode, statusText } from './error.js';
|
|
5
|
-
export {
|
|
5
|
+
export { HeadersBuilder } from './headers.js';
|
|
6
|
+
export { ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferMethod, MiddlewareFunction, Prettify, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, RouterConfig } from './types.js';
|
|
7
|
+
import 'cookie';
|
|
6
8
|
import 'zod';
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRouter
|
|
3
|
+
} from "./chunk-TEDN6QMU.js";
|
|
4
|
+
import "./chunk-XB5P5CQZ.js";
|
|
1
5
|
import {
|
|
2
6
|
createEndpoint,
|
|
3
7
|
createEndpointConfig
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import {
|
|
6
|
-
createRouter
|
|
7
|
-
} from "./chunk-JIA6NLL6.js";
|
|
8
|
-
import "./chunk-PT4GU6PH.js";
|
|
8
|
+
} from "./chunk-4UYDR5IO.js";
|
|
9
9
|
import {
|
|
10
|
+
isInvalidZodSchemaError,
|
|
10
11
|
isRouterError
|
|
11
|
-
} from "./chunk-
|
|
12
|
-
import
|
|
12
|
+
} from "./chunk-FU35BPU7.js";
|
|
13
|
+
import {
|
|
14
|
+
HeadersBuilder
|
|
15
|
+
} from "./chunk-Z6JJAIWN.js";
|
|
16
|
+
import "./chunk-BWIKAYZV.js";
|
|
13
17
|
import {
|
|
14
18
|
RouterError,
|
|
15
19
|
statusCode,
|
|
16
20
|
statusText
|
|
17
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-B6PMGVSL.js";
|
|
18
22
|
export {
|
|
23
|
+
HeadersBuilder,
|
|
19
24
|
RouterError,
|
|
20
25
|
createEndpoint,
|
|
21
26
|
createEndpointConfig,
|
|
22
27
|
createRouter,
|
|
28
|
+
isInvalidZodSchemaError,
|
|
23
29
|
isRouterError,
|
|
24
30
|
statusCode,
|
|
25
31
|
statusText
|
package/dist/middlewares.cjs
CHANGED
|
@@ -74,31 +74,31 @@ var RouterError = class extends AuraStackRouterError {
|
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
// src/middlewares.ts
|
|
77
|
-
var executeGlobalMiddlewares = async (
|
|
78
|
-
if (!middlewares) return
|
|
77
|
+
var executeGlobalMiddlewares = async (context, middlewares) => {
|
|
78
|
+
if (!middlewares) return context;
|
|
79
79
|
for (const middleware of middlewares) {
|
|
80
80
|
if (typeof middleware !== "function") {
|
|
81
81
|
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
82
82
|
}
|
|
83
|
-
const executed = await middleware(
|
|
83
|
+
const executed = await middleware(context);
|
|
84
84
|
if (executed instanceof Response) {
|
|
85
85
|
return executed;
|
|
86
86
|
}
|
|
87
|
-
|
|
87
|
+
context = executed;
|
|
88
88
|
}
|
|
89
|
-
if (!
|
|
89
|
+
if (!context || !(context.request instanceof Request)) {
|
|
90
90
|
throw new RouterError("BAD_REQUEST", "Global middleware must return a Request or Response object");
|
|
91
91
|
}
|
|
92
|
-
return
|
|
92
|
+
return context;
|
|
93
93
|
};
|
|
94
|
-
var executeMiddlewares = async (
|
|
94
|
+
var executeMiddlewares = async (context, middlewares = []) => {
|
|
95
95
|
try {
|
|
96
96
|
let ctx = context;
|
|
97
97
|
for (const middleware of middlewares) {
|
|
98
98
|
if (typeof middleware !== "function") {
|
|
99
99
|
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
100
100
|
}
|
|
101
|
-
ctx = await middleware(
|
|
101
|
+
ctx = await middleware(ctx);
|
|
102
102
|
}
|
|
103
103
|
return ctx;
|
|
104
104
|
} catch {
|
package/dist/middlewares.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { RouterConfig, EndpointConfig, RequestContext, MiddlewareFunction } from './types.js';
|
|
1
|
+
import { GlobalMiddlewareContext, RouterConfig, EndpointConfig, RequestContext, MiddlewareFunction } from './types.js';
|
|
2
2
|
import 'zod';
|
|
3
3
|
import './error.js';
|
|
4
|
+
import './headers.js';
|
|
5
|
+
import 'cookie';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Executes the middlewares in sequence, passing the request to each middleware.
|
|
@@ -9,7 +11,7 @@ import './error.js';
|
|
|
9
11
|
* @param middlewares - Array of global middleware functions to be executed
|
|
10
12
|
* @returns - The modified request after all middlewares have been executed
|
|
11
13
|
*/
|
|
12
|
-
declare const executeGlobalMiddlewares: (
|
|
14
|
+
declare const executeGlobalMiddlewares: (context: GlobalMiddlewareContext, middlewares: RouterConfig["middlewares"]) => Promise<Response | GlobalMiddlewareContext>;
|
|
13
15
|
/**
|
|
14
16
|
* Executes middlewares in sequence, passing the request and context to each middleware.
|
|
15
17
|
*
|
|
@@ -18,6 +20,6 @@ declare const executeGlobalMiddlewares: (request: Request, middlewares: RouterCo
|
|
|
18
20
|
* @param middlewares - Array of middleware functions to be executed
|
|
19
21
|
* @returns The modified context after all middlewares have been executed
|
|
20
22
|
*/
|
|
21
|
-
declare const executeMiddlewares: <const RouteParams extends Record<string, string>, const Config extends EndpointConfig>(
|
|
23
|
+
declare const executeMiddlewares: <const RouteParams extends Record<string, string>, const Config extends EndpointConfig>(context: RequestContext<RouteParams, Config>, middlewares?: MiddlewareFunction<RouteParams, Config>[]) => Promise<RequestContext<RouteParams, Config>>;
|
|
22
24
|
|
|
23
25
|
export { executeGlobalMiddlewares, executeMiddlewares };
|
package/dist/middlewares.js
CHANGED