@aura-stack/router 0.2.0 → 0.3.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 +61 -3
- package/dist/assert.d.cts +21 -1
- package/dist/assert.d.ts +21 -1
- package/dist/assert.js +8 -3
- package/dist/chunk-6PZEXNTS.js +31 -0
- package/dist/{chunk-O6SY753N.js → chunk-FWDOXDWG.js} +6 -6
- package/dist/{chunk-RFYOPPMW.js → chunk-GJC3ODME.js} +15 -6
- package/dist/chunk-JIA6NLL6.js +143 -0
- package/dist/{chunk-JRJKKBSH.js → chunk-JNMXLKDG.js} +12 -2
- package/dist/chunk-PT4GU6PH.js +78 -0
- package/dist/context.cjs +50 -47
- package/dist/context.d.cts +3 -2
- package/dist/context.d.ts +3 -2
- package/dist/context.js +3 -4
- package/dist/endpoint.cjs +29 -29
- package/dist/endpoint.d.cts +17 -16
- package/dist/endpoint.d.ts +17 -16
- package/dist/endpoint.js +5 -7
- package/dist/error.cjs +19 -7
- package/dist/error.d.cts +28 -3
- package/dist/error.d.ts +28 -3
- package/dist/error.js +9 -3
- package/dist/index.cjs +192 -110
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +19 -9
- package/dist/middlewares.cjs +15 -9
- package/dist/middlewares.d.cts +1 -0
- package/dist/middlewares.d.ts +1 -0
- package/dist/middlewares.js +2 -2
- package/dist/router.cjs +180 -98
- package/dist/router.d.cts +24 -2
- package/dist/router.d.ts +24 -2
- package/dist/router.js +13 -8
- package/dist/types.d.cts +67 -10
- package/dist/types.d.ts +67 -10
- package/package.json +7 -1
- package/dist/chunk-DR4C6QTF.js +0 -72
- package/dist/chunk-OXDCFAMF.js +0 -78
- package/dist/chunk-YUX3YHXF.js +0 -36
package/dist/index.cjs
CHANGED
|
@@ -20,29 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
RouterError: () => RouterError,
|
|
23
24
|
createEndpoint: () => createEndpoint,
|
|
24
25
|
createEndpointConfig: () => createEndpointConfig,
|
|
25
|
-
createRouter: () => createRouter
|
|
26
|
+
createRouter: () => createRouter,
|
|
27
|
+
isRouterError: () => isRouterError,
|
|
28
|
+
statusCode: () => statusCode,
|
|
29
|
+
statusText: () => statusText
|
|
26
30
|
});
|
|
27
31
|
module.exports = __toCommonJS(index_exports);
|
|
28
32
|
|
|
29
|
-
// src/assert.ts
|
|
30
|
-
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH"]);
|
|
31
|
-
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
32
|
-
var isSupportedMethod = (method) => {
|
|
33
|
-
return supportedMethods.has(method);
|
|
34
|
-
};
|
|
35
|
-
var isSupportedBodyMethod = (method) => {
|
|
36
|
-
return supportedBodyMethods.has(method);
|
|
37
|
-
};
|
|
38
|
-
var isValidRoute = (route) => {
|
|
39
|
-
const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
|
|
40
|
-
return routePattern.test(route);
|
|
41
|
-
};
|
|
42
|
-
var isValidHandler = (handler) => {
|
|
43
|
-
return typeof handler === "function";
|
|
44
|
-
};
|
|
45
|
-
|
|
46
33
|
// src/error.ts
|
|
47
34
|
var statusCode = {
|
|
48
35
|
OK: 200,
|
|
@@ -70,35 +57,57 @@ var statusCode = {
|
|
|
70
57
|
SERVICE_UNAVAILABLE: 503,
|
|
71
58
|
HTTP_VERSION_NOT_SUPPORTED: 505
|
|
72
59
|
};
|
|
73
|
-
var statusText = Object.
|
|
74
|
-
(previous,
|
|
75
|
-
return { ...previous, [
|
|
60
|
+
var statusText = Object.keys(statusCode).reduce(
|
|
61
|
+
(previous, status) => {
|
|
62
|
+
return { ...previous, [status]: status };
|
|
76
63
|
},
|
|
77
64
|
{}
|
|
78
65
|
);
|
|
79
66
|
var AuraStackRouterError = class extends Error {
|
|
80
67
|
constructor(type, message, name) {
|
|
81
68
|
super(message);
|
|
82
|
-
this.name = name ?? "
|
|
69
|
+
this.name = name ?? "RouterError";
|
|
83
70
|
this.status = statusCode[type];
|
|
84
|
-
this.statusText = statusText[
|
|
71
|
+
this.statusText = statusText[type];
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var RouterError = class extends AuraStackRouterError {
|
|
75
|
+
constructor(type, message, name) {
|
|
76
|
+
super(type, message, name);
|
|
77
|
+
this.name = name ?? "RouterError";
|
|
85
78
|
}
|
|
86
79
|
};
|
|
87
80
|
|
|
88
|
-
// src/
|
|
89
|
-
var
|
|
90
|
-
|
|
91
|
-
|
|
81
|
+
// src/assert.ts
|
|
82
|
+
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
|
|
83
|
+
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
84
|
+
var isSupportedMethod = (method) => {
|
|
85
|
+
return supportedMethods.has(method);
|
|
92
86
|
};
|
|
87
|
+
var isSupportedBodyMethod = (method) => {
|
|
88
|
+
return supportedBodyMethods.has(method);
|
|
89
|
+
};
|
|
90
|
+
var isValidRoute = (route) => {
|
|
91
|
+
const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
|
|
92
|
+
return routePattern.test(route);
|
|
93
|
+
};
|
|
94
|
+
var isValidHandler = (handler) => {
|
|
95
|
+
return typeof handler === "function";
|
|
96
|
+
};
|
|
97
|
+
var isRouterError = (error) => {
|
|
98
|
+
return error instanceof RouterError;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// src/endpoint.ts
|
|
93
102
|
var createEndpoint = (method, route, handler, config = {}) => {
|
|
94
103
|
if (!isSupportedMethod(method)) {
|
|
95
|
-
throw new
|
|
104
|
+
throw new RouterError("METHOD_NOT_ALLOWED", `Unsupported HTTP method: ${method}`);
|
|
96
105
|
}
|
|
97
106
|
if (!isValidRoute(route)) {
|
|
98
|
-
throw new
|
|
107
|
+
throw new RouterError("BAD_REQUEST", `Invalid route format: ${route}`);
|
|
99
108
|
}
|
|
100
109
|
if (!isValidHandler(handler)) {
|
|
101
|
-
throw new
|
|
110
|
+
throw new RouterError("BAD_REQUEST", "Handler must be a function");
|
|
102
111
|
}
|
|
103
112
|
return { method, route, handler, config };
|
|
104
113
|
};
|
|
@@ -108,28 +117,22 @@ function createEndpointConfig(...args) {
|
|
|
108
117
|
}
|
|
109
118
|
|
|
110
119
|
// src/context.ts
|
|
111
|
-
var getRouteParams = (
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
120
|
+
var getRouteParams = (params, config) => {
|
|
121
|
+
if (config.schemas?.params) {
|
|
122
|
+
const parsed = config.schemas.params.safeParse(params);
|
|
123
|
+
if (!parsed.success) {
|
|
124
|
+
throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid route parameters");
|
|
125
|
+
}
|
|
126
|
+
return parsed.data;
|
|
115
127
|
}
|
|
116
|
-
|
|
117
|
-
if (!params) return {};
|
|
118
|
-
const values = routeRegex.exec(path)?.slice(1);
|
|
119
|
-
return params.reduce(
|
|
120
|
-
(previous, now, idx) => ({
|
|
121
|
-
...previous,
|
|
122
|
-
[now]: decodeURIComponent(values?.[idx] ?? "")
|
|
123
|
-
}),
|
|
124
|
-
{}
|
|
125
|
-
);
|
|
128
|
+
return params;
|
|
126
129
|
};
|
|
127
130
|
var getSearchParams = (url, config) => {
|
|
128
131
|
const route = new URL(url);
|
|
129
132
|
if (config.schemas?.searchParams) {
|
|
130
133
|
const parsed = config.schemas.searchParams.safeParse(Object.fromEntries(route.searchParams.entries()));
|
|
131
134
|
if (!parsed.success) {
|
|
132
|
-
throw new
|
|
135
|
+
throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid search parameters");
|
|
133
136
|
}
|
|
134
137
|
return parsed.data;
|
|
135
138
|
}
|
|
@@ -140,33 +143,42 @@ var getHeaders = (request) => {
|
|
|
140
143
|
};
|
|
141
144
|
var getBody = async (request, config) => {
|
|
142
145
|
if (!isSupportedBodyMethod(request.method)) {
|
|
143
|
-
return
|
|
146
|
+
return null;
|
|
144
147
|
}
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
+
const clone = request.clone();
|
|
149
|
+
const contentType = clone.headers.get("Content-Type") ?? "";
|
|
150
|
+
if (contentType.includes("application/json") || config.schemas?.body) {
|
|
151
|
+
const json = await clone.json();
|
|
148
152
|
if (config.schemas?.body) {
|
|
149
153
|
const parsed = config.schemas.body.safeParse(json);
|
|
150
154
|
if (!parsed.success) {
|
|
151
|
-
throw new
|
|
155
|
+
throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid request body");
|
|
152
156
|
}
|
|
153
157
|
return parsed.data;
|
|
154
158
|
}
|
|
155
159
|
return json;
|
|
156
160
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
161
|
+
try {
|
|
162
|
+
if (createContentTypeRegex(["application/x-www-form-urlencoded", "multipart/form-data"], contentType)) {
|
|
163
|
+
return await clone.formData();
|
|
164
|
+
}
|
|
165
|
+
if (createContentTypeRegex(["text/", "application/xml"], contentType)) {
|
|
166
|
+
return await clone.text();
|
|
167
|
+
}
|
|
168
|
+
if (createContentTypeRegex(["application/octet-stream"], contentType)) {
|
|
169
|
+
return await clone.arrayBuffer();
|
|
170
|
+
}
|
|
171
|
+
if (createContentTypeRegex(["image/", "video/", "audio/", "application/pdf"], contentType)) {
|
|
172
|
+
return await clone.blob();
|
|
173
|
+
}
|
|
174
|
+
return null;
|
|
175
|
+
} catch {
|
|
176
|
+
throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid request body, the content-type does not match the body format");
|
|
168
177
|
}
|
|
169
|
-
|
|
178
|
+
};
|
|
179
|
+
var createContentTypeRegex = (contentTypes, contenType) => {
|
|
180
|
+
const regex = new RegExp(`${contentTypes.join("|")}`);
|
|
181
|
+
return regex.test(contenType);
|
|
170
182
|
};
|
|
171
183
|
|
|
172
184
|
// src/middlewares.ts
|
|
@@ -174,7 +186,7 @@ var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
|
174
186
|
if (!middlewares) return request;
|
|
175
187
|
for (const middleware of middlewares) {
|
|
176
188
|
if (typeof middleware !== "function") {
|
|
177
|
-
throw new
|
|
189
|
+
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
178
190
|
}
|
|
179
191
|
const executed = await middleware(request);
|
|
180
192
|
if (executed instanceof Response) {
|
|
@@ -183,7 +195,7 @@ var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
|
183
195
|
request = executed;
|
|
184
196
|
}
|
|
185
197
|
if (!request || !(request instanceof Request)) {
|
|
186
|
-
throw new
|
|
198
|
+
throw new RouterError("BAD_REQUEST", "Global middleware must return a Request or Response object");
|
|
187
199
|
}
|
|
188
200
|
return request;
|
|
189
201
|
};
|
|
@@ -192,70 +204,140 @@ var executeMiddlewares = async (request, context, middlewares = []) => {
|
|
|
192
204
|
let ctx = context;
|
|
193
205
|
for (const middleware of middlewares) {
|
|
194
206
|
if (typeof middleware !== "function") {
|
|
195
|
-
throw new
|
|
207
|
+
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
196
208
|
}
|
|
197
209
|
ctx = await middleware(request, ctx);
|
|
198
210
|
}
|
|
199
211
|
return ctx;
|
|
200
212
|
} catch {
|
|
201
|
-
throw new
|
|
213
|
+
throw new RouterError("BAD_REQUEST", "Handler threw an error");
|
|
202
214
|
}
|
|
203
215
|
};
|
|
204
216
|
|
|
205
217
|
// src/router.ts
|
|
218
|
+
var createNode = () => ({
|
|
219
|
+
statics: /* @__PURE__ */ new Map(),
|
|
220
|
+
endpoints: /* @__PURE__ */ new Map()
|
|
221
|
+
});
|
|
222
|
+
var insert = (root, endpoint) => {
|
|
223
|
+
if (!root || !endpoint) return;
|
|
224
|
+
let node = root;
|
|
225
|
+
const segments = endpoint.route === "/" ? [] : endpoint.route.split("/").filter(Boolean);
|
|
226
|
+
for (const segment of segments) {
|
|
227
|
+
if (segment.startsWith(":")) {
|
|
228
|
+
const name = segment.slice(1);
|
|
229
|
+
if (!node.param) {
|
|
230
|
+
node.param = { name, node: createNode() };
|
|
231
|
+
} else if (node.param.name !== name) {
|
|
232
|
+
throw new RouterError(
|
|
233
|
+
"BAD_REQUEST",
|
|
234
|
+
`Conflicting in the route by the dynamic segment "${node.param.name}" and "${name}"`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
node = node.param.node;
|
|
238
|
+
} else {
|
|
239
|
+
if (!node.statics.has(segment)) {
|
|
240
|
+
node.statics.set(segment, createNode());
|
|
241
|
+
}
|
|
242
|
+
node = node.statics.get(segment);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (node.endpoints.has(endpoint.method)) {
|
|
246
|
+
throw new RouterError("BAD_REQUEST", `Duplicate endpoint for ${endpoint?.method} ${endpoint?.route}`);
|
|
247
|
+
}
|
|
248
|
+
node.endpoints.set(endpoint.method, endpoint);
|
|
249
|
+
};
|
|
250
|
+
var search = (method, root, pathname) => {
|
|
251
|
+
let node = root;
|
|
252
|
+
const params = {};
|
|
253
|
+
const segments = pathname === "/" ? [] : pathname.split("/").filter(Boolean);
|
|
254
|
+
for (const segment of segments) {
|
|
255
|
+
if (node?.statics.has(segment)) {
|
|
256
|
+
node = node.statics.get(segment);
|
|
257
|
+
} else if (node?.param) {
|
|
258
|
+
params[node.param.name] = decodeURIComponent(segment);
|
|
259
|
+
node = node.param.node;
|
|
260
|
+
} else {
|
|
261
|
+
throw new RouterError("NOT_FOUND", `No route found for path: ${pathname}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (!node.endpoints.has(method)) {
|
|
265
|
+
throw new RouterError("NOT_FOUND", `No route found for path: ${pathname}`);
|
|
266
|
+
}
|
|
267
|
+
return { endpoint: node.endpoints.get(method), params };
|
|
268
|
+
};
|
|
269
|
+
var handleError = async (error, request, config) => {
|
|
270
|
+
if (config.onError) {
|
|
271
|
+
try {
|
|
272
|
+
const response = await config.onError(error, request);
|
|
273
|
+
return response;
|
|
274
|
+
} catch {
|
|
275
|
+
return Response.json(
|
|
276
|
+
{ message: "A critical failure occurred during error handling" },
|
|
277
|
+
{ status: 500, statusText: statusText.INTERNAL_SERVER_ERROR }
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (isRouterError(error)) {
|
|
282
|
+
const { message, status, statusText: statusText2 } = error;
|
|
283
|
+
return Response.json({ message }, { status, statusText: statusText2 });
|
|
284
|
+
}
|
|
285
|
+
return Response.json({ message: "Internal Server Error" }, { status: 500, statusText: statusText.INTERNAL_SERVER_ERROR });
|
|
286
|
+
};
|
|
287
|
+
var handleRequest = async (method, request, config, root) => {
|
|
288
|
+
try {
|
|
289
|
+
if (!isSupportedMethod(request.method)) {
|
|
290
|
+
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${request.method}' is not supported`);
|
|
291
|
+
}
|
|
292
|
+
const globalRequest = await executeGlobalMiddlewares(request, config.middlewares);
|
|
293
|
+
if (globalRequest instanceof Response) return globalRequest;
|
|
294
|
+
const url = new URL(globalRequest.url);
|
|
295
|
+
const pathnameWithBase = url.pathname;
|
|
296
|
+
if (globalRequest.method !== method) {
|
|
297
|
+
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${globalRequest.method}' is not allowed`);
|
|
298
|
+
}
|
|
299
|
+
const { endpoint, params } = search(method, root, pathnameWithBase);
|
|
300
|
+
if (endpoint.method !== globalRequest.method) {
|
|
301
|
+
throw new RouterError("METHOD_NOT_ALLOWED", `The HTTP method '${globalRequest.method}' is not allowed`);
|
|
302
|
+
}
|
|
303
|
+
const dynamicParams = getRouteParams(params, endpoint.config);
|
|
304
|
+
const body = await getBody(globalRequest, endpoint.config);
|
|
305
|
+
const searchParams = getSearchParams(globalRequest.url, endpoint.config);
|
|
306
|
+
const headers = getHeaders(globalRequest);
|
|
307
|
+
const context = {
|
|
308
|
+
params: dynamicParams,
|
|
309
|
+
searchParams,
|
|
310
|
+
headers,
|
|
311
|
+
body
|
|
312
|
+
};
|
|
313
|
+
await executeMiddlewares(globalRequest, context, endpoint.config.middlewares);
|
|
314
|
+
const response = await endpoint.handler(globalRequest, context);
|
|
315
|
+
return response;
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return handleError(error, request, config);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
206
320
|
var createRouter = (endpoints, config = {}) => {
|
|
321
|
+
const root = createNode();
|
|
207
322
|
const server = {};
|
|
208
|
-
const
|
|
323
|
+
const methods = /* @__PURE__ */ new Set();
|
|
209
324
|
for (const endpoint of endpoints) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
groups.get(endpoint.method)?.push(endpoint);
|
|
325
|
+
const withBasePath = config.basePath ? `${config.basePath}${endpoint.route}` : endpoint.route;
|
|
326
|
+
insert(root, { ...endpoint, route: withBasePath });
|
|
327
|
+
methods.add(endpoint.method);
|
|
214
328
|
}
|
|
215
|
-
for (const method of
|
|
216
|
-
server[method] =
|
|
217
|
-
try {
|
|
218
|
-
const globalRequest = await executeGlobalMiddlewares(request, config.middlewares);
|
|
219
|
-
if (globalRequest instanceof Response) {
|
|
220
|
-
return globalRequest;
|
|
221
|
-
}
|
|
222
|
-
const url = new URL(globalRequest.url);
|
|
223
|
-
const pathname = url.pathname;
|
|
224
|
-
const endpoint = groups.get(method)?.find((endpoint2) => {
|
|
225
|
-
const withBasePath = config.basePath ? `${config.basePath}${endpoint2.route}` : endpoint2.route;
|
|
226
|
-
const regex = createRoutePattern(withBasePath);
|
|
227
|
-
return regex.test(pathname);
|
|
228
|
-
});
|
|
229
|
-
if (endpoint) {
|
|
230
|
-
const withBasePath = config.basePath ? `${config.basePath}${endpoint.route}` : endpoint.route;
|
|
231
|
-
const body = await getBody(globalRequest, endpoint.config);
|
|
232
|
-
const params = getRouteParams(withBasePath, pathname);
|
|
233
|
-
const searchParams = getSearchParams(globalRequest.url, endpoint.config);
|
|
234
|
-
const headers = getHeaders(globalRequest);
|
|
235
|
-
const context = {
|
|
236
|
-
params,
|
|
237
|
-
searchParams,
|
|
238
|
-
headers,
|
|
239
|
-
body
|
|
240
|
-
};
|
|
241
|
-
await executeMiddlewares(globalRequest, context, endpoint.config.middlewares);
|
|
242
|
-
return endpoint.handler(globalRequest, context);
|
|
243
|
-
}
|
|
244
|
-
return Response.json({ message: "Not Found" }, { status: 404 });
|
|
245
|
-
} catch (error) {
|
|
246
|
-
if (error instanceof AuraStackRouterError) {
|
|
247
|
-
const { message, status, statusText: statusText2 } = error;
|
|
248
|
-
return Response.json({ message }, { status, statusText: statusText2 });
|
|
249
|
-
}
|
|
250
|
-
return Response.json({ message: "Internal Server Error" }, { status: 500 });
|
|
251
|
-
}
|
|
252
|
-
};
|
|
329
|
+
for (const method of methods) {
|
|
330
|
+
server[method] = (request) => handleRequest(method, request, config, root);
|
|
253
331
|
}
|
|
254
332
|
return server;
|
|
255
333
|
};
|
|
256
334
|
// Annotate the CommonJS export names for ESM import in node:
|
|
257
335
|
0 && (module.exports = {
|
|
336
|
+
RouterError,
|
|
258
337
|
createEndpoint,
|
|
259
338
|
createEndpointConfig,
|
|
260
|
-
createRouter
|
|
339
|
+
createRouter,
|
|
340
|
+
isRouterError,
|
|
341
|
+
statusCode,
|
|
342
|
+
statusText
|
|
261
343
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { createEndpoint, createEndpointConfig } from './endpoint.cjs';
|
|
2
2
|
export { createRouter } from './router.cjs';
|
|
3
|
-
export {
|
|
3
|
+
export { isRouterError } from './assert.cjs';
|
|
4
|
+
export { RouterError, statusCode, statusText } from './error.cjs';
|
|
5
|
+
export { ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, GetHttpHandlers, GetRouteParams, GlobalMiddleware, HTTPMethod, InferMethod, MiddlewareFunction, Prettify, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, RouterConfig } from './types.cjs';
|
|
4
6
|
import 'zod';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { createEndpoint, createEndpointConfig } from './endpoint.js';
|
|
2
2
|
export { createRouter } from './router.js';
|
|
3
|
-
export {
|
|
3
|
+
export { isRouterError } from './assert.js';
|
|
4
|
+
export { RouterError, statusCode, statusText } from './error.js';
|
|
5
|
+
export { ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, GetHttpHandlers, GetRouteParams, GlobalMiddleware, HTTPMethod, InferMethod, MiddlewareFunction, Prettify, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, RouterConfig } from './types.js';
|
|
4
6
|
import 'zod';
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createRouter
|
|
3
|
-
} from "./chunk-DR4C6QTF.js";
|
|
4
|
-
import "./chunk-OXDCFAMF.js";
|
|
5
1
|
import {
|
|
6
2
|
createEndpoint,
|
|
7
3
|
createEndpointConfig
|
|
8
|
-
} from "./chunk-
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
} from "./chunk-6PZEXNTS.js";
|
|
5
|
+
import {
|
|
6
|
+
createRouter
|
|
7
|
+
} from "./chunk-JIA6NLL6.js";
|
|
8
|
+
import "./chunk-PT4GU6PH.js";
|
|
9
|
+
import {
|
|
10
|
+
isRouterError
|
|
11
|
+
} from "./chunk-JNMXLKDG.js";
|
|
12
|
+
import "./chunk-FWDOXDWG.js";
|
|
13
|
+
import {
|
|
14
|
+
RouterError,
|
|
15
|
+
statusCode,
|
|
16
|
+
statusText
|
|
17
|
+
} from "./chunk-GJC3ODME.js";
|
|
12
18
|
export {
|
|
19
|
+
RouterError,
|
|
13
20
|
createEndpoint,
|
|
14
21
|
createEndpointConfig,
|
|
15
|
-
createRouter
|
|
22
|
+
createRouter,
|
|
23
|
+
isRouterError,
|
|
24
|
+
statusCode,
|
|
25
|
+
statusText
|
|
16
26
|
};
|
package/dist/middlewares.cjs
CHANGED
|
@@ -52,18 +52,24 @@ var statusCode = {
|
|
|
52
52
|
SERVICE_UNAVAILABLE: 503,
|
|
53
53
|
HTTP_VERSION_NOT_SUPPORTED: 505
|
|
54
54
|
};
|
|
55
|
-
var statusText = Object.
|
|
56
|
-
(previous,
|
|
57
|
-
return { ...previous, [
|
|
55
|
+
var statusText = Object.keys(statusCode).reduce(
|
|
56
|
+
(previous, status) => {
|
|
57
|
+
return { ...previous, [status]: status };
|
|
58
58
|
},
|
|
59
59
|
{}
|
|
60
60
|
);
|
|
61
61
|
var AuraStackRouterError = class extends Error {
|
|
62
62
|
constructor(type, message, name) {
|
|
63
63
|
super(message);
|
|
64
|
-
this.name = name ?? "
|
|
64
|
+
this.name = name ?? "RouterError";
|
|
65
65
|
this.status = statusCode[type];
|
|
66
|
-
this.statusText = statusText[
|
|
66
|
+
this.statusText = statusText[type];
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var RouterError = class extends AuraStackRouterError {
|
|
70
|
+
constructor(type, message, name) {
|
|
71
|
+
super(type, message, name);
|
|
72
|
+
this.name = name ?? "RouterError";
|
|
67
73
|
}
|
|
68
74
|
};
|
|
69
75
|
|
|
@@ -72,7 +78,7 @@ var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
|
72
78
|
if (!middlewares) return request;
|
|
73
79
|
for (const middleware of middlewares) {
|
|
74
80
|
if (typeof middleware !== "function") {
|
|
75
|
-
throw new
|
|
81
|
+
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
76
82
|
}
|
|
77
83
|
const executed = await middleware(request);
|
|
78
84
|
if (executed instanceof Response) {
|
|
@@ -81,7 +87,7 @@ var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
|
81
87
|
request = executed;
|
|
82
88
|
}
|
|
83
89
|
if (!request || !(request instanceof Request)) {
|
|
84
|
-
throw new
|
|
90
|
+
throw new RouterError("BAD_REQUEST", "Global middleware must return a Request or Response object");
|
|
85
91
|
}
|
|
86
92
|
return request;
|
|
87
93
|
};
|
|
@@ -90,13 +96,13 @@ var executeMiddlewares = async (request, context, middlewares = []) => {
|
|
|
90
96
|
let ctx = context;
|
|
91
97
|
for (const middleware of middlewares) {
|
|
92
98
|
if (typeof middleware !== "function") {
|
|
93
|
-
throw new
|
|
99
|
+
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
94
100
|
}
|
|
95
101
|
ctx = await middleware(request, ctx);
|
|
96
102
|
}
|
|
97
103
|
return ctx;
|
|
98
104
|
} catch {
|
|
99
|
-
throw new
|
|
105
|
+
throw new RouterError("BAD_REQUEST", "Handler threw an error");
|
|
100
106
|
}
|
|
101
107
|
};
|
|
102
108
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/middlewares.d.cts
CHANGED
package/dist/middlewares.d.ts
CHANGED
package/dist/middlewares.js
CHANGED