@h3ravel/router 1.8.0 → 1.8.2

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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { H3, Middleware, MiddlewareOptions } from 'h3';
2
2
  import { Application, ServiceProvider } from '@h3ravel/core';
3
- import { IRouter, EventHandler, IMiddleware, RouterEnd, IController } from '@h3ravel/shared';
3
+ import { IRouter, RouteEventHandler, IMiddleware, RouterEnd, IController, EventHandler } from '@h3ravel/shared';
4
4
 
5
5
  declare class Router implements IRouter {
6
6
  protected h3App: H3;
@@ -54,7 +54,7 @@ declare class Router implements IRouter {
54
54
  * @param name Optional route name (for URL generation or referencing).
55
55
  * @param middleware Optional array of middleware functions to execute before the handler.
56
56
  */
57
- get(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
57
+ get(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
58
58
  /**
59
59
  * Registers a route that responds to HTTP POST requests.
60
60
  *
@@ -65,7 +65,7 @@ declare class Router implements IRouter {
65
65
  * @param name Optional route name (for URL generation or referencing).
66
66
  * @param middleware Optional array of middleware functions to execute before the handler.
67
67
  */
68
- post(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
68
+ post(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
69
69
  /**
70
70
  * Registers a route that responds to HTTP PUT requests.
71
71
  *
@@ -76,7 +76,7 @@ declare class Router implements IRouter {
76
76
  * @param name Optional route name (for URL generation or referencing).
77
77
  * @param middleware Optional array of middleware functions to execute before the handler.
78
78
  */
79
- put(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
79
+ put(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
80
80
  /**
81
81
  * Registers a route that responds to HTTP PATCH requests.
82
82
  *
@@ -87,7 +87,7 @@ declare class Router implements IRouter {
87
87
  * @param name Optional route name (for URL generation or referencing).
88
88
  * @param middleware Optional array of middleware functions to execute before the handler.
89
89
  */
90
- patch(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
90
+ patch(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
91
91
  /**
92
92
  * Registers a route that responds to HTTP DELETE requests.
93
93
  *
@@ -98,7 +98,7 @@ declare class Router implements IRouter {
98
98
  * @param name Optional route name (for URL generation or referencing).
99
99
  * @param middleware Optional array of middleware functions to execute before the handler.
100
100
  */
101
- delete(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
101
+ delete(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
102
102
  /**
103
103
  * API Resource support
104
104
  *
@@ -123,7 +123,7 @@ declare class Router implements IRouter {
123
123
  group(options: {
124
124
  prefix?: string;
125
125
  middleware?: EventHandler[];
126
- }, callback: () => void): this;
126
+ }, callback: (_e: this) => void): this;
127
127
  /**
128
128
  * Set the name of the current route
129
129
  *
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { H3, Middleware, MiddlewareOptions } from 'h3';
2
2
  import { Application, ServiceProvider } from '@h3ravel/core';
3
- import { IRouter, EventHandler, IMiddleware, RouterEnd, IController } from '@h3ravel/shared';
3
+ import { IRouter, RouteEventHandler, IMiddleware, RouterEnd, IController, EventHandler } from '@h3ravel/shared';
4
4
 
5
5
  declare class Router implements IRouter {
6
6
  protected h3App: H3;
@@ -54,7 +54,7 @@ declare class Router implements IRouter {
54
54
  * @param name Optional route name (for URL generation or referencing).
55
55
  * @param middleware Optional array of middleware functions to execute before the handler.
56
56
  */
57
- get(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
57
+ get(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
58
58
  /**
59
59
  * Registers a route that responds to HTTP POST requests.
60
60
  *
@@ -65,7 +65,7 @@ declare class Router implements IRouter {
65
65
  * @param name Optional route name (for URL generation or referencing).
66
66
  * @param middleware Optional array of middleware functions to execute before the handler.
67
67
  */
68
- post(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
68
+ post(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
69
69
  /**
70
70
  * Registers a route that responds to HTTP PUT requests.
71
71
  *
@@ -76,7 +76,7 @@ declare class Router implements IRouter {
76
76
  * @param name Optional route name (for URL generation or referencing).
77
77
  * @param middleware Optional array of middleware functions to execute before the handler.
78
78
  */
79
- put(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
79
+ put(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
80
80
  /**
81
81
  * Registers a route that responds to HTTP PATCH requests.
82
82
  *
@@ -87,7 +87,7 @@ declare class Router implements IRouter {
87
87
  * @param name Optional route name (for URL generation or referencing).
88
88
  * @param middleware Optional array of middleware functions to execute before the handler.
89
89
  */
90
- patch(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
90
+ patch(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
91
91
  /**
92
92
  * Registers a route that responds to HTTP DELETE requests.
93
93
  *
@@ -98,7 +98,7 @@ declare class Router implements IRouter {
98
98
  * @param name Optional route name (for URL generation or referencing).
99
99
  * @param middleware Optional array of middleware functions to execute before the handler.
100
100
  */
101
- delete(path: string, definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
101
+ delete(path: string, definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string], name?: string, middleware?: IMiddleware[]): Omit<this, RouterEnd>;
102
102
  /**
103
103
  * API Resource support
104
104
  *
@@ -123,7 +123,7 @@ declare class Router implements IRouter {
123
123
  group(options: {
124
124
  prefix?: string;
125
125
  middleware?: EventHandler[];
126
- }, callback: () => void): this;
126
+ }, callback: (_e: this) => void): this;
127
127
  /**
128
128
  * Set the name of the current route
129
129
  *
package/dist/index.js CHANGED
@@ -1288,7 +1288,7 @@ var Router = class {
1288
1288
  * @param methodName Method to invoke on the controller (defaults to 'index')
1289
1289
  */
1290
1290
  resolveControllerOrHandler(handler, methodName) {
1291
- if (typeof handler === "function") {
1291
+ if (typeof handler === "function" && typeof handler.prototype !== "undefined") {
1292
1292
  return (_ctx) => {
1293
1293
  let controller;
1294
1294
  if (Container.hasAnyDecorator(handler)) {
@@ -1301,7 +1301,7 @@ var Router = class {
1301
1301
  throw new Error(`Method "${String(action)}" not found on controller ${handler.name}`);
1302
1302
  }
1303
1303
  const paramTypes = Reflect.getMetadata("design:paramtypes", controller, action) || [];
1304
- const args = paramTypes.map((paramType) => {
1304
+ let args = paramTypes.map((paramType) => {
1305
1305
  switch (paramType?.name) {
1306
1306
  case "Application":
1307
1307
  return this.app;
@@ -1315,6 +1315,11 @@ var Router = class {
1315
1315
  return this.app.make(paramType);
1316
1316
  }
1317
1317
  });
1318
+ if (args.length < 1) {
1319
+ args = [
1320
+ _ctx
1321
+ ];
1322
+ }
1318
1323
  return controller[action](...args);
1319
1324
  };
1320
1325
  }
@@ -1411,13 +1416,30 @@ var Router = class {
1411
1416
  const basePath = `/${path2}`.replace(/\/+$/, "").replace(/(\/)+/g, "$1");
1412
1417
  const name = basePath.substring(basePath.lastIndexOf("/") + 1).replaceAll(/\/|:/g, "") || "";
1413
1418
  const param = singularize(name);
1414
- const controller = new Controller(this.app);
1415
- this.addRoute("get", basePath, controller.index.bind(controller), `${name}.index`, middleware);
1416
- this.addRoute("post", basePath, controller.store.bind(controller), `${name}.store`, middleware);
1417
- this.addRoute("get", `${basePath}/:${param}`, controller.show.bind(controller), `${name}.show`, middleware);
1418
- this.addRoute("put", `${basePath}/:${param}`, controller.update.bind(controller), `${name}.update`, middleware);
1419
- this.addRoute("patch", `${basePath}/:${param}`, controller.update.bind(controller), `${name}.update`, middleware);
1420
- this.addRoute("delete", `${basePath}/:${param}`, controller.destroy.bind(controller), `${name}.destroy`, middleware);
1419
+ this.get(basePath, [
1420
+ Controller,
1421
+ "index"
1422
+ ], `${name}.index`, middleware);
1423
+ this.post(basePath, [
1424
+ Controller,
1425
+ "store"
1426
+ ], `${name}.store`, middleware);
1427
+ this.get(`${basePath}/:${param}`, [
1428
+ Controller,
1429
+ "show"
1430
+ ], `${name}.show`, middleware);
1431
+ this.put(`${basePath}/:${param}`, [
1432
+ Controller,
1433
+ "update"
1434
+ ], `${name}.update`, middleware);
1435
+ this.patch(`${basePath}/:${param}`, [
1436
+ Controller,
1437
+ "update"
1438
+ ], `${name}.update`, middleware);
1439
+ this.delete(`${basePath}/:${param}`, [
1440
+ Controller,
1441
+ "destroy"
1442
+ ], `${name}.destroy`, middleware);
1421
1443
  return this;
1422
1444
  }
1423
1445
  /**
@@ -1450,7 +1472,7 @@ var Router = class {
1450
1472
  ];
1451
1473
  this.groupPrefix += options.prefix || "";
1452
1474
  this.groupMiddleware.push(...options.middleware || []);
1453
- callback();
1475
+ callback(this);
1454
1476
  this.groupPrefix = prevPrefix;
1455
1477
  this.groupMiddleware = prevMiddleware;
1456
1478
  return this;