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