@h3ravel/router 1.8.3 → 1.9.1

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/index.js CHANGED
@@ -1,388 +1,428 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/Providers/AssetsServiceProvider.ts
5
- import { readFile, stat } from "fs/promises";
6
- import { ServiceProvider } from "@h3ravel/core";
7
- import { before } from "@h3ravel/support";
8
- import { join } from "path";
1
+ import { readFile, readdir, stat } from "node:fs/promises";
2
+ import { Container, Kernel, ServiceProvider } from "@h3ravel/core";
3
+ import { before, singularize } from "@h3ravel/support";
4
+ import path, { join } from "node:path";
9
5
  import { serveStatic } from "h3";
10
- import { statSync } from "fs";
11
- var AssetsServiceProvider = class extends ServiceProvider {
12
- static {
13
- __name(this, "AssetsServiceProvider");
14
- }
15
- static priority = 996;
16
- register() {
17
- const app = this.app.make("router");
18
- const config = this.app.make("config");
19
- const fsconfig = config.get("filesystem");
20
- const publicPath = this.app.getPath("public");
21
- app.middleware(`/${fsconfig.public_mask}/**`, (event) => {
22
- return serveStatic(event, {
23
- indexNames: [
24
- "/index.html"
25
- ],
26
- getContents: /* @__PURE__ */ __name((id) => {
27
- const newId = id.replace(`/${fsconfig.public_mask}/`, "");
28
- return readFile(join(before(publicPath, newId), newId));
29
- }, "getContents"),
30
- getMeta: /* @__PURE__ */ __name(async (id) => {
31
- const newId = id.replace(`/${fsconfig.public_mask}/`, "");
32
- const stats = await stat(join(before(publicPath, newId), newId)).catch(() => {
33
- });
34
- if (stats?.isFile()) {
35
- return {
36
- size: stats.size,
37
- mtime: stats.mtimeMs
38
- };
39
- }
40
- }, "getMeta")
41
- });
42
- });
43
- this.app.singleton("asset", () => {
44
- return (key, def = "") => {
45
- try {
46
- statSync(join(before(publicPath, key), key));
47
- } catch {
48
- key = def;
49
- }
50
- return join(fsconfig.public_mask, key);
51
- };
52
- });
53
- }
54
- };
55
-
56
- // src/Route.ts
6
+ import { statSync } from "node:fs";
57
7
  import "reflect-metadata";
58
- import { Container, Kernel } from "@h3ravel/core";
59
8
  import { Request, Response } from "@h3ravel/http";
60
- import { singularize } from "@h3ravel/support";
61
9
  import { HttpContext } from "@h3ravel/shared";
62
- var Router = class {
63
- static {
64
- __name(this, "Router");
65
- }
66
- h3App;
67
- app;
68
- routes = [];
69
- nameMap = [];
70
- groupPrefix = "";
71
- middlewareMap = [];
72
- groupMiddleware = [];
73
- constructor(h3App, app) {
74
- this.h3App = h3App;
75
- this.app = app;
76
- }
77
- /**
78
- * Route Resolver
79
- *
80
- * @param handler
81
- * @param middleware
82
- * @returns
83
- */
84
- resolveHandler(handler, middleware = []) {
85
- return async (event) => {
86
- const kernel = new Kernel(() => HttpContext.init({
87
- app: this.app,
88
- request: new Request(event, this.app),
89
- response: new Response(event, this.app)
90
- }), middleware);
91
- return kernel.handle(event, (ctx) => Promise.resolve(handler(ctx)));
92
- };
93
- }
94
- /**
95
- * Add a route to the stack
96
- *
97
- * @param method
98
- * @param path
99
- * @param handler
100
- * @param name
101
- * @param middleware
102
- */
103
- addRoute(method, path2, handler, name, middleware = []) {
104
- if (this.nameMap.length > 0) {
105
- name = this.nameMap.join(".");
106
- }
107
- if (this.middlewareMap.length > 0) {
108
- middleware = this.middlewareMap;
109
- }
110
- const fullPath = `${this.groupPrefix}${path2}`.replace(/\/+/g, "/");
111
- this.routes.push({
112
- method,
113
- path: fullPath,
114
- name,
115
- handler
116
- });
117
- this.h3App[method](fullPath, this.resolveHandler(handler, middleware));
118
- }
119
- /**
120
- * Resolves a route handler definition into an executable EventHandler.
121
- *
122
- * A handler can be:
123
- * - A function matching the EventHandler signature
124
- * - A controller class (optionally decorated for IoC resolution)
125
- *
126
- * If it’s a controller class, this method will:
127
- * - Instantiate it (via IoC or manually)
128
- * - Call the specified method (defaults to `index`)
129
- *
130
- * @param handler Event handler function OR controller class
131
- * @param methodName Method to invoke on the controller (defaults to 'index')
132
- */
133
- resolveControllerOrHandler(handler, methodName) {
134
- if (typeof handler === "function" && typeof handler.prototype !== "undefined") {
135
- return (_ctx) => {
136
- let controller;
137
- if (Container.hasAnyDecorator(handler)) {
138
- controller = this.app.make(handler);
139
- } else {
140
- controller = new handler(this.app);
141
- }
142
- const action = methodName || "index";
143
- if (typeof controller[action] !== "function") {
144
- throw new Error(`Method "${String(action)}" not found on controller ${handler.name}`);
145
- }
146
- const paramTypes = Reflect.getMetadata("design:paramtypes", controller, action) || [];
147
- let args = paramTypes.map((paramType) => {
148
- switch (paramType?.name) {
149
- case "Application":
150
- return this.app;
151
- case "Request":
152
- return _ctx.request;
153
- case "Response":
154
- return _ctx.response;
155
- case "HttpContext":
156
- return _ctx;
157
- default:
158
- return this.app.make(paramType);
159
- }
160
- });
161
- if (args.length < 1) {
162
- args = [
163
- _ctx
164
- ];
165
- }
166
- return controller[action](...args);
167
- };
168
- }
169
- return handler;
170
- }
171
- /**
172
- * Registers a route that responds to HTTP GET requests.
173
- *
174
- * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
175
- * @param definition Either:
176
- * - An EventHandler function
177
- * - A tuple: [ControllerClass, methodName]
178
- * @param name Optional route name (for URL generation or referencing).
179
- * @param middleware Optional array of middleware functions to execute before the handler.
180
- */
181
- get(path2, definition, name, middleware = []) {
182
- const handler = Array.isArray(definition) ? definition[0] : definition;
183
- const methodName = Array.isArray(definition) ? definition[1] : void 0;
184
- this.addRoute("get", path2, this.resolveControllerOrHandler(handler, methodName), name, middleware);
185
- return this;
186
- }
187
- /**
188
- * Registers a route that responds to HTTP POST requests.
189
- *
190
- * @param path The URL pattern to match (can include parameters, e.g., '/users').
191
- * @param definition Either:
192
- * - An EventHandler function
193
- * - A tuple: [ControllerClass, methodName]
194
- * @param name Optional route name (for URL generation or referencing).
195
- * @param middleware Optional array of middleware functions to execute before the handler.
196
- */
197
- post(path2, definition, name, middleware = []) {
198
- const handler = Array.isArray(definition) ? definition[0] : definition;
199
- const methodName = Array.isArray(definition) ? definition[1] : void 0;
200
- this.addRoute("post", path2, this.resolveControllerOrHandler(handler, methodName), name, middleware);
201
- return this;
202
- }
203
- /**
204
- * Registers a route that responds to HTTP PUT requests.
205
- *
206
- * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
207
- * @param definition Either:
208
- * - An EventHandler function
209
- * - A tuple: [ControllerClass, methodName]
210
- * @param name Optional route name (for URL generation or referencing).
211
- * @param middleware Optional array of middleware functions to execute before the handler.
212
- */
213
- put(path2, definition, name, middleware = []) {
214
- const handler = Array.isArray(definition) ? definition[0] : definition;
215
- const methodName = Array.isArray(definition) ? definition[1] : void 0;
216
- this.addRoute("put", path2, this.resolveControllerOrHandler(handler, methodName), name, middleware);
217
- return this;
218
- }
219
- /**
220
- * Registers a route that responds to HTTP PATCH requests.
221
- *
222
- * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
223
- * @param definition Either:
224
- * - An EventHandler function
225
- * - A tuple: [ControllerClass, methodName]
226
- * @param name Optional route name (for URL generation or referencing).
227
- * @param middleware Optional array of middleware functions to execute before the handler.
228
- */
229
- patch(path2, definition, name, middleware = []) {
230
- const handler = Array.isArray(definition) ? definition[0] : definition;
231
- const methodName = Array.isArray(definition) ? definition[1] : void 0;
232
- this.addRoute("patch", path2, this.resolveControllerOrHandler(handler, methodName), name, middleware);
233
- return this;
234
- }
235
- /**
236
- * Registers a route that responds to HTTP DELETE requests.
237
- *
238
- * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
239
- * @param definition Either:
240
- * - An EventHandler function
241
- * - A tuple: [ControllerClass, methodName]
242
- * @param name Optional route name (for URL generation or referencing).
243
- * @param middleware Optional array of middleware functions to execute before the handler.
244
- */
245
- delete(path2, definition, name, middleware = []) {
246
- const handler = Array.isArray(definition) ? definition[0] : definition;
247
- const methodName = Array.isArray(definition) ? definition[1] : void 0;
248
- this.addRoute("delete", path2, this.resolveControllerOrHandler(handler, methodName), name, middleware);
249
- return this;
250
- }
251
- /**
252
- * API Resource support
253
- *
254
- * @param path
255
- * @param controller
256
- */
257
- apiResource(path2, Controller, middleware = []) {
258
- path2 = path2.replace(/\//g, "/");
259
- const basePath = `/${path2}`.replace(/\/+$/, "").replace(/(\/)+/g, "$1");
260
- const name = basePath.substring(basePath.lastIndexOf("/") + 1).replaceAll(/\/|:/g, "") || "";
261
- const param = singularize(name);
262
- this.get(basePath, [
263
- Controller,
264
- "index"
265
- ], `${name}.index`, middleware);
266
- this.post(basePath, [
267
- Controller,
268
- "store"
269
- ], `${name}.store`, middleware);
270
- this.get(`${basePath}/:${param}`, [
271
- Controller,
272
- "show"
273
- ], `${name}.show`, middleware);
274
- this.put(`${basePath}/:${param}`, [
275
- Controller,
276
- "update"
277
- ], `${name}.update`, middleware);
278
- this.patch(`${basePath}/:${param}`, [
279
- Controller,
280
- "update"
281
- ], `${name}.update`, middleware);
282
- this.delete(`${basePath}/:${param}`, [
283
- Controller,
284
- "destroy"
285
- ], `${name}.destroy`, middleware);
286
- return this;
287
- }
288
- /**
289
- * Named route URL generator
290
- *
291
- * @param name
292
- * @param params
293
- * @returns
294
- */
295
- route(name, params = {}) {
296
- const found = this.routes.find((r) => r.name === name);
297
- if (!found) return void 0;
298
- let url = found.path;
299
- for (const [key, value] of Object.entries(params)) {
300
- url = url.replace(`:${key}`, value);
301
- }
302
- return url;
303
- }
304
- /**
305
- * Grouping
306
- *
307
- * @param options
308
- * @param callback
309
- */
310
- group(options, callback) {
311
- const prevPrefix = this.groupPrefix;
312
- const prevMiddleware = [
313
- ...this.groupMiddleware
314
- ];
315
- this.groupPrefix += options.prefix || "";
316
- this.groupMiddleware.push(...options.middleware || []);
317
- callback(this);
318
- this.groupPrefix = prevPrefix;
319
- this.groupMiddleware = prevMiddleware;
320
- return this;
321
- }
322
- /**
323
- * Set the name of the current route
324
- *
325
- * @param name
326
- */
327
- name(name) {
328
- this.nameMap.push(name);
329
- return this;
330
- }
331
- /**
332
- * Registers middleware for a specific path.
333
- * @param path - The path to apply the middleware.
334
- * @param handler - The middleware handler.
335
- * @param opts - Optional middleware options.
336
- */
337
- middleware(path2, handler, opts) {
338
- if (typeof path2 === "string") {
339
- this.h3App.use(path2, handler, opts);
340
- } else {
341
- this.middlewareMap.concat(path2);
342
- }
343
- return this;
344
- }
10
+ import { Model } from "@h3ravel/database";
11
+
12
+ //#region src/Helpers.ts
13
+ var Helpers = class Helpers {
14
+ /**
15
+ * Extracts parameter names from a route path string.
16
+ *
17
+ * - Looks for segments prefixed with ":" (e.g. "/users/:id")
18
+ * - Captures only the param name (without the ":")
19
+ * - Returns all matches in order of appearance
20
+ *
21
+ * @param path - The route path string (e.g. "/groups/:group/users/:user")
22
+ * @returns An array of parameter names (e.g. ["group", "user"])
23
+ */
24
+ static extractParams(path$1) {
25
+ const regex = /:([^/]+)/g;
26
+ const params = [];
27
+ let match;
28
+ while ((match = regex.exec(path$1)) !== null) params.push(match[1]);
29
+ return params;
30
+ }
31
+ /**
32
+ * Resolves route model binding for a given path, HTTP context, and model.
33
+ *
34
+ * - Extracts all route parameters from the given path
35
+ * - If a parameter matches the model name, it attempts to resolve the model binding
36
+ * using the provided value and binding field (defaults to "id" unless specified).
37
+ * - For non-matching parameters, it simply returns the key-value pair as is.
38
+ * - If no parameters are found, returns an empty object.
39
+ *
40
+ * @param path - The route path (e.g. "/groups/:group/users/:user")
41
+ * @param ctx - The HTTP context containing the request
42
+ * @param model - The model instance to resolve bindings against
43
+ * @returns A resolved model instance or an object containing param values
44
+ */
45
+ static async resolveRouteModelBinding(path$1, ctx, model) {
46
+ const name = model.constructor.name.toLowerCase();
47
+ /**
48
+ * Extract field (defaults to 'id' if not specified after '|')
49
+ */
50
+ const field = name.split("|").at(1) ?? "id";
51
+ /**
52
+ * Iterate through extracted parameters from the path
53
+ */
54
+ for await (const e of Helpers.extractParams(path$1)) {
55
+ const value = ctx.request.params[e] ?? null;
56
+ if (e === name) return await model.resolveRouteBinding(value, field);
57
+ else return { [e]: ctx.request.params[e] ?? {} };
58
+ }
59
+ return {};
60
+ }
345
61
  };
346
62
 
347
- // src/Providers/RouteServiceProvider.ts
348
- import { ServiceProvider as ServiceProvider2 } from "@h3ravel/core";
349
- import path from "path";
350
- import { readdir } from "fs/promises";
351
- var RouteServiceProvider = class extends ServiceProvider2 {
352
- static {
353
- __name(this, "RouteServiceProvider");
354
- }
355
- static priority = 997;
356
- register() {
357
- this.app.singleton("router", () => {
358
- const h3App = this.app.make("http.app");
359
- return new Router(h3App, this.app);
360
- });
361
- }
362
- /**
363
- * Load routes from src/routes
364
- */
365
- async boot() {
366
- try {
367
- const routePath = this.app.getPath("routes");
368
- const files = (await readdir(routePath)).filter((e) => {
369
- return !e.includes(".d.ts") && !e.includes(".map");
370
- });
371
- for (let i = 0; i < files.length; i++) {
372
- const routesModule = await import(path.join(routePath, files[i]));
373
- if (typeof routesModule.default === "function") {
374
- const router = this.app.make("router");
375
- routesModule.default(router);
376
- }
377
- }
378
- } catch (e) {
379
- console.warn("No web routes found or failed to load:", e);
380
- }
381
- }
63
+ //#endregion
64
+ //#region src/Providers/AssetsServiceProvider.ts
65
+ /**
66
+ * Handles public assets loading
67
+ *
68
+ * Auto-Registered
69
+ */
70
+ var AssetsServiceProvider = class extends ServiceProvider {
71
+ static priority = 996;
72
+ register() {
73
+ const app = this.app.make("router");
74
+ const fsconfig = this.app.make("config").get("filesystem");
75
+ const publicPath = this.app.getPath("public");
76
+ app.middleware(`/${fsconfig.public_mask}/**`, (event) => {
77
+ return serveStatic(event, {
78
+ indexNames: ["/index.html"],
79
+ getContents: (id) => {
80
+ const newId = id.replace(`/${fsconfig.public_mask}/`, "");
81
+ return readFile(join(before(publicPath, newId), newId));
82
+ },
83
+ getMeta: async (id) => {
84
+ const newId = id.replace(`/${fsconfig.public_mask}/`, "");
85
+ const stats = await stat(join(before(publicPath, newId), newId)).catch(() => {});
86
+ if (stats?.isFile()) return {
87
+ size: stats.size,
88
+ mtime: stats.mtimeMs
89
+ };
90
+ }
91
+ });
92
+ });
93
+ this.app.singleton("asset", () => {
94
+ return (key, def = "") => {
95
+ try {
96
+ statSync(join(before(publicPath, key), key));
97
+ } catch {
98
+ key = def;
99
+ }
100
+ return join(fsconfig.public_mask, key);
101
+ };
102
+ });
103
+ }
104
+ };
105
+
106
+ //#endregion
107
+ //#region src/Route.ts
108
+ var Router = class {
109
+ routes = [];
110
+ nameMap = [];
111
+ groupPrefix = "";
112
+ middlewareMap = [];
113
+ groupMiddleware = [];
114
+ constructor(h3App, app) {
115
+ this.h3App = h3App;
116
+ this.app = app;
117
+ }
118
+ /**
119
+ * Route Resolver
120
+ *
121
+ * @param handler
122
+ * @param middleware
123
+ * @returns
124
+ */
125
+ resolveHandler(handler, middleware = []) {
126
+ return async (event) => {
127
+ return new Kernel(() => HttpContext.init({
128
+ app: this.app,
129
+ request: new Request(event, this.app),
130
+ response: new Response(event, this.app)
131
+ }), middleware).handle(event, (ctx) => Promise.resolve(handler(ctx)));
132
+ };
133
+ }
134
+ /**
135
+ * Add a route to the stack
136
+ *
137
+ * @param method
138
+ * @param path
139
+ * @param handler
140
+ * @param name
141
+ * @param middleware
142
+ */
143
+ addRoute(method, path$1, handler, name, middleware = []) {
144
+ /**
145
+ * Join all defined route names to make a single route name
146
+ */
147
+ if (this.nameMap.length > 0) name = this.nameMap.join(".");
148
+ /**
149
+ * Join all defined middlewares
150
+ */
151
+ if (this.middlewareMap.length > 0) middleware = this.middlewareMap;
152
+ const fullPath = `${this.groupPrefix}${path$1}`.replace(/\/+/g, "/");
153
+ this.routes.push({
154
+ method,
155
+ path: fullPath,
156
+ name,
157
+ handler
158
+ });
159
+ this.h3App[method](fullPath, this.resolveHandler(handler, middleware));
160
+ }
161
+ /**
162
+ * Resolves a route handler definition into an executable EventHandler.
163
+ *
164
+ * A handler can be:
165
+ * - A function matching the EventHandler signature
166
+ * - A controller class (optionally decorated for IoC resolution)
167
+ *
168
+ * If it’s a controller class, this method will:
169
+ * - Instantiate it (via IoC or manually)
170
+ * - Call the specified method (defaults to `index`)
171
+ *
172
+ * @param handler Event handler function OR controller class
173
+ * @param methodName Method to invoke on the controller (defaults to 'index')
174
+ */
175
+ resolveControllerOrHandler(handler, methodName, path$1) {
176
+ /**
177
+ * Checks if the handler is a function (either a plain function or a class constructor)
178
+ */
179
+ if (typeof handler === "function" && typeof handler.prototype !== "undefined") return async (ctx) => {
180
+ let controller;
181
+ if (Container.hasAnyDecorator(handler))
182
+ /**
183
+ * If the controller is decorated use the IoC container
184
+ */
185
+ controller = this.app.make(handler);
186
+ else
187
+ /**
188
+ * Otherwise instantiate manually so that we can at least
189
+ * pass the app instance
190
+ */
191
+ controller = new handler(this.app);
192
+ /**
193
+ * The method to execute (defaults to 'index')
194
+ */
195
+ const action = methodName || "index";
196
+ /**
197
+ * Ensure the method exists on the controller
198
+ */
199
+ if (typeof controller[action] !== "function") throw new Error(`Method "${String(action)}" not found on controller ${handler.name}`);
200
+ /**
201
+ * Get param types for the controller method
202
+ */
203
+ const paramTypes = Reflect.getMetadata("design:paramtypes", controller, action) || [];
204
+ /**
205
+ * Resolve the bound dependencies
206
+ */
207
+ let args = await Promise.all(paramTypes.map(async (paramType) => {
208
+ switch (paramType?.name) {
209
+ case "Application": return this.app;
210
+ case "Request": return ctx.request;
211
+ case "Response": return ctx.response;
212
+ case "HttpContext": return ctx;
213
+ default: {
214
+ const inst = this.app.make(paramType);
215
+ if (inst instanceof Model) return await Helpers.resolveRouteModelBinding(path$1 ?? "", ctx, inst);
216
+ return inst;
217
+ }
218
+ }
219
+ }));
220
+ /**
221
+ * Ensure that the HttpContext is always available
222
+ */
223
+ if (args.length < 1) args = [ctx];
224
+ /**
225
+ * Call the controller method, passing all resolved dependencies
226
+ */
227
+ return await controller[action](...args);
228
+ };
229
+ return handler;
230
+ }
231
+ /**
232
+ * Registers a route that responds to HTTP GET requests.
233
+ *
234
+ * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
235
+ * @param definition Either:
236
+ * - An EventHandler function
237
+ * - A tuple: [ControllerClass, methodName]
238
+ * @param name Optional route name (for URL generation or referencing).
239
+ * @param middleware Optional array of middleware functions to execute before the handler.
240
+ */
241
+ get(path$1, definition, name, middleware = []) {
242
+ const handler = Array.isArray(definition) ? definition[0] : definition;
243
+ const methodName = Array.isArray(definition) ? definition[1] : void 0;
244
+ this.addRoute("get", path$1, this.resolveControllerOrHandler(handler, methodName, path$1), name, middleware);
245
+ return this;
246
+ }
247
+ /**
248
+ * Registers a route that responds to HTTP POST requests.
249
+ *
250
+ * @param path The URL pattern to match (can include parameters, e.g., '/users').
251
+ * @param definition Either:
252
+ * - An EventHandler function
253
+ * - A tuple: [ControllerClass, methodName]
254
+ * @param name Optional route name (for URL generation or referencing).
255
+ * @param middleware Optional array of middleware functions to execute before the handler.
256
+ */
257
+ post(path$1, definition, name, middleware = []) {
258
+ const handler = Array.isArray(definition) ? definition[0] : definition;
259
+ const methodName = Array.isArray(definition) ? definition[1] : void 0;
260
+ this.addRoute("post", path$1, this.resolveControllerOrHandler(handler, methodName, path$1), name, middleware);
261
+ return this;
262
+ }
263
+ /**
264
+ * Registers a route that responds to HTTP PUT requests.
265
+ *
266
+ * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
267
+ * @param definition Either:
268
+ * - An EventHandler function
269
+ * - A tuple: [ControllerClass, methodName]
270
+ * @param name Optional route name (for URL generation or referencing).
271
+ * @param middleware Optional array of middleware functions to execute before the handler.
272
+ */
273
+ put(path$1, definition, name, middleware = []) {
274
+ const handler = Array.isArray(definition) ? definition[0] : definition;
275
+ const methodName = Array.isArray(definition) ? definition[1] : void 0;
276
+ this.addRoute("put", path$1, this.resolveControllerOrHandler(handler, methodName, path$1), name, middleware);
277
+ return this;
278
+ }
279
+ /**
280
+ * Registers a route that responds to HTTP PATCH requests.
281
+ *
282
+ * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
283
+ * @param definition Either:
284
+ * - An EventHandler function
285
+ * - A tuple: [ControllerClass, methodName]
286
+ * @param name Optional route name (for URL generation or referencing).
287
+ * @param middleware Optional array of middleware functions to execute before the handler.
288
+ */
289
+ patch(path$1, definition, name, middleware = []) {
290
+ const handler = Array.isArray(definition) ? definition[0] : definition;
291
+ const methodName = Array.isArray(definition) ? definition[1] : void 0;
292
+ this.addRoute("patch", path$1, this.resolveControllerOrHandler(handler, methodName, path$1), name, middleware);
293
+ return this;
294
+ }
295
+ /**
296
+ * Registers a route that responds to HTTP DELETE requests.
297
+ *
298
+ * @param path The URL pattern to match (can include parameters, e.g., '/users/:id').
299
+ * @param definition Either:
300
+ * - An EventHandler function
301
+ * - A tuple: [ControllerClass, methodName]
302
+ * @param name Optional route name (for URL generation or referencing).
303
+ * @param middleware Optional array of middleware functions to execute before the handler.
304
+ */
305
+ delete(path$1, definition, name, middleware = []) {
306
+ const handler = Array.isArray(definition) ? definition[0] : definition;
307
+ const methodName = Array.isArray(definition) ? definition[1] : void 0;
308
+ this.addRoute("delete", path$1, this.resolveControllerOrHandler(handler, methodName, path$1), name, middleware);
309
+ return this;
310
+ }
311
+ /**
312
+ * API Resource support
313
+ *
314
+ * @param path
315
+ * @param controller
316
+ */
317
+ apiResource(path$1, Controller, middleware = []) {
318
+ path$1 = path$1.replace(/\//g, "/");
319
+ const basePath = `/${path$1}`.replace(/\/+$/, "").replace(/(\/)+/g, "$1");
320
+ const name = basePath.substring(basePath.lastIndexOf("/") + 1).replaceAll(/\/|:/g, "") || "";
321
+ const param = singularize(name);
322
+ this.get(basePath, [Controller, "index"], `${name}.index`, middleware);
323
+ this.post(basePath, [Controller, "store"], `${name}.store`, middleware);
324
+ this.get(`${basePath}/:${param}`, [Controller, "show"], `${name}.show`, middleware);
325
+ this.put(`${basePath}/:${param}`, [Controller, "update"], `${name}.update`, middleware);
326
+ this.patch(`${basePath}/:${param}`, [Controller, "update"], `${name}.update`, middleware);
327
+ this.delete(`${basePath}/:${param}`, [Controller, "destroy"], `${name}.destroy`, middleware);
328
+ return this;
329
+ }
330
+ /**
331
+ * Named route URL generator
332
+ *
333
+ * @param name
334
+ * @param params
335
+ * @returns
336
+ */
337
+ route(name, params = {}) {
338
+ const found = this.routes.find((r) => r.name === name);
339
+ if (!found) return void 0;
340
+ let url = found.path;
341
+ for (const [key, value] of Object.entries(params)) url = url.replace(`:${key}`, value);
342
+ return url;
343
+ }
344
+ /**
345
+ * Grouping
346
+ *
347
+ * @param options
348
+ * @param callback
349
+ */
350
+ group(options, callback) {
351
+ const prevPrefix = this.groupPrefix;
352
+ const prevMiddleware = [...this.groupMiddleware];
353
+ this.groupPrefix += options.prefix || "";
354
+ this.groupMiddleware.push(...options.middleware || []);
355
+ callback(this);
356
+ /**
357
+ * Restore state after group
358
+ */
359
+ this.groupPrefix = prevPrefix;
360
+ this.groupMiddleware = prevMiddleware;
361
+ return this;
362
+ }
363
+ /**
364
+ * Set the name of the current route
365
+ *
366
+ * @param name
367
+ */
368
+ name(name) {
369
+ this.nameMap.push(name);
370
+ return this;
371
+ }
372
+ /**
373
+ * Registers middleware for a specific path.
374
+ * @param path - The path to apply the middleware.
375
+ * @param handler - The middleware handler.
376
+ * @param opts - Optional middleware options.
377
+ */
378
+ middleware(path$1, handler, opts) {
379
+ if (typeof path$1 === "string") this.h3App.use(path$1, handler, opts);
380
+ else this.middlewareMap.concat(path$1);
381
+ return this;
382
+ }
382
383
  };
383
- export {
384
- AssetsServiceProvider,
385
- RouteServiceProvider,
386
- Router
384
+
385
+ //#endregion
386
+ //#region src/Providers/RouteServiceProvider.ts
387
+ /**
388
+ * Handles routing registration
389
+ *
390
+ * Load route files (web.ts, api.ts).
391
+ * Map controllers to routes.
392
+ * Register route-related middleware.
393
+ *
394
+ * Auto-Registered
395
+ */
396
+ var RouteServiceProvider = class extends ServiceProvider {
397
+ static priority = 997;
398
+ register() {
399
+ this.app.singleton("router", () => {
400
+ const h3App = this.app.make("http.app");
401
+ return new Router(h3App, this.app);
402
+ });
403
+ }
404
+ /**
405
+ * Load routes from src/routes
406
+ */
407
+ async boot() {
408
+ try {
409
+ const routePath = this.app.getPath("routes");
410
+ const files = (await readdir(routePath)).filter((e) => {
411
+ return !e.includes(".d.ts") && !e.includes(".d.cts") && !e.includes(".map");
412
+ });
413
+ for (let i = 0; i < files.length; i++) {
414
+ const routesModule = await import(path.join(routePath, files[i]));
415
+ if (typeof routesModule.default === "function") {
416
+ const router = this.app.make("router");
417
+ routesModule.default(router);
418
+ }
419
+ }
420
+ } catch (e) {
421
+ console.warn("No web routes found or failed to load:", e);
422
+ }
423
+ }
387
424
  };
425
+
426
+ //#endregion
427
+ export { AssetsServiceProvider, Helpers, RouteServiceProvider, Router };
388
428
  //# sourceMappingURL=index.js.map