Skip to content

Commit 63d5777

Browse files
committed
fix: fix IoC support for callables and API reousorces.
1 parent d340e46 commit 63d5777

8 files changed

Lines changed: 53 additions & 29 deletions

File tree

examples/basic-app/src/app/Http/Controllers/UserController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Controller } from '@h3ravel/core'
2-
import { HttpContext } from '@h3ravel/http'
1+
import { Controller, Injectable } from '@h3ravel/core'
2+
import { HttpContext, Request } from '@h3ravel/http'
33

44
export class UserController extends Controller {
5+
@Injectable()
56
index () {
67
return [{ id: 1, name: 'John Doe' }]
78
}

examples/basic-app/src/routes/web.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { HttpContext, Request } from '@h3ravel/http'
2+
13
import { HomeController } from 'App/Http/Controllers/HomeController'
4+
import { Injectable } from '@h3ravel/core'
25
import { MailController } from 'src/app/Http/Controllers/MailController'
36
import { Router } from '@h3ravel/router'
47

58
export default (Route: Router) => {
69
Route.get('/', [HomeController, 'index'])
710
Route.get('/mail', [MailController, 'send'])
8-
Route.get('/app', async function ({ request, response }) {
11+
12+
13+
Route.get('/app', async function ({ request, response }: HttpContext, req: Request) {
14+
console.log(req)
915
const view = request.app.make('view')
1016

1117
return response.html(await view('index', {

packages/router/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @h3ravel/router
22

3+
## 1.8.1
4+
5+
### Patch Changes
6+
7+
- fix: fix IoC support for callables and API reousorces.
8+
- Updated dependencies
9+
- @h3ravel/shared@0.17.1
10+
311
## 1.8.0
412

513
### Minor Changes

packages/router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@h3ravel/router",
3-
"version": "1.8.0",
3+
"version": "1.8.1",
44
"description": "Route facade, decorators and controller system for H3ravel.",
55
"type": "module",
66
"main": "./dist/index.js",

packages/router/src/Route.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { H3Event, Middleware, MiddlewareOptions, type H3 } from 'h3'
33
import { Application, Container, Kernel } from '@h3ravel/core'
44
import { Request, Response } from '@h3ravel/http'
55
import { singularize } from '@h3ravel/support'
6-
import { HttpContext, type EventHandler, type IController, type IMiddleware, type IRouter, type RouterEnd } from '@h3ravel/shared'
6+
import { HttpContext, RouteEventHandler, type EventHandler, type IController, type IMiddleware, type IRouter, type RouterEnd } from '@h3ravel/shared'
77

88
interface RouteDefinition {
99
method: string
@@ -96,7 +96,7 @@ export class Router implements IRouter {
9696
/**
9797
* Checks if the handler is a function (either a plain function or a class constructor)
9898
*/
99-
if (typeof handler === 'function') {
99+
if (typeof handler === 'function' && typeof (handler as any).prototype !== 'undefined') {
100100
return (_ctx) => {
101101
let controller: IController
102102

@@ -126,18 +126,14 @@ export class Router implements IRouter {
126126
}
127127

128128
/**
129-
* Get param types for the method
129+
* Get param types for the controller method
130130
*/
131-
const paramTypes = Reflect.getMetadata(
132-
'design:paramtypes',
133-
controller,
134-
action
135-
) || [];
131+
const paramTypes: [] = Reflect.getMetadata('design:paramtypes', controller, action) || [];
136132

137133
/**
138134
* Resolve the bound dependencies
139135
*/
140-
const args: [] = paramTypes.map((paramType: any) => {
136+
let args: any[] = paramTypes.map((paramType: any) => {
141137
switch (paramType?.name) {
142138
case 'Application':
143139
return this.app
@@ -152,6 +148,13 @@ export class Router implements IRouter {
152148
}
153149
});
154150

151+
/**
152+
* Ensure that the HttpContext is always available
153+
*/
154+
if (args.length < 1) {
155+
args = [_ctx]
156+
}
157+
155158
/**
156159
* Call the controller method, passing all resolved dependencies
157160
*/
@@ -174,7 +177,7 @@ export class Router implements IRouter {
174177
*/
175178
get (
176179
path: string,
177-
definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
180+
definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
178181
name?: string,
179182
middleware: IMiddleware[] = []
180183
): Omit<this, RouterEnd> {
@@ -197,7 +200,7 @@ export class Router implements IRouter {
197200
*/
198201
post (
199202
path: string,
200-
definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
203+
definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
201204
name?: string,
202205
middleware: IMiddleware[] = []
203206
): Omit<this, RouterEnd> {
@@ -219,7 +222,7 @@ export class Router implements IRouter {
219222
*/
220223
put (
221224
path: string,
222-
definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
225+
definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
223226
name?: string,
224227
middleware: IMiddleware[] = []
225228
): Omit<this, RouterEnd> {
@@ -241,7 +244,7 @@ export class Router implements IRouter {
241244
*/
242245
patch (
243246
path: string,
244-
definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
247+
definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
245248
name?: string,
246249
middleware: IMiddleware[] = []
247250
): Omit<this, RouterEnd> {
@@ -263,7 +266,7 @@ export class Router implements IRouter {
263266
*/
264267
delete (
265268
path: string,
266-
definition: EventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
269+
definition: RouteEventHandler | [(new (...args: any[]) => Record<string, any>), methodName: string],
267270
name?: string,
268271
middleware: IMiddleware[] = []
269272
): Omit<this, RouterEnd> {
@@ -290,14 +293,13 @@ export class Router implements IRouter {
290293
const name = basePath.substring(basePath.lastIndexOf('/') + 1).replaceAll(/\/|:/g, '') || '';
291294
const param = singularize(name)
292295

293-
const controller = new Controller(this.app)
296+
this.get(basePath, [Controller, 'index'], `${name}.index`, middleware)
297+
this.post(basePath, [Controller, 'store'], `${name}.store`, middleware)
298+
this.get(`${basePath}/:${param}`, [Controller, 'show'], `${name}.show`, middleware)
299+
this.put(`${basePath}/:${param}`, [Controller, 'update'], `${name}.update`, middleware)
300+
this.patch(`${basePath}/:${param}`, [Controller, 'update'], `${name}.update`, middleware)
301+
this.delete(`${basePath}/:${param}`, [Controller, 'destroy'], `${name}.destroy`, middleware)
294302

295-
this.addRoute('get', basePath, controller.index.bind(controller), `${name}.index`, middleware)
296-
this.addRoute('post', basePath, controller.store.bind(controller), `${name}.store`, middleware)
297-
this.addRoute('get', `${basePath}/:${param}`, controller.show.bind(controller), `${name}.show`, middleware)
298-
this.addRoute('put', `${basePath}/:${param}`, controller.update.bind(controller), `${name}.update`, middleware)
299-
this.addRoute('patch', `${basePath}/:${param}`, controller.update.bind(controller), `${name}.update`, middleware)
300-
this.addRoute('delete', `${basePath}/:${param}`, controller.destroy.bind(controller), `${name}.destroy`, middleware)
301303
return this
302304
}
303305

@@ -325,14 +327,14 @@ export class Router implements IRouter {
325327
* @param options
326328
* @param callback
327329
*/
328-
group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void) {
330+
group (options: { prefix?: string; middleware?: EventHandler[] }, callback: (_e: this) => void) {
329331
const prevPrefix = this.groupPrefix
330332
const prevMiddleware = [...this.groupMiddleware]
331333

332334
this.groupPrefix += options.prefix || ''
333335
this.groupMiddleware.push(...(options.middleware || []))
334336

335-
callback()
337+
callback(this)
336338

337339
/**
338340
* Restore state after group

packages/shared/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @h3ravel/shared
22

3+
## 0.17.1
4+
5+
### Patch Changes
6+
7+
- fix: fix IoC support for callables and API reousorces.
8+
39
## 0.17.0
410

511
### Minor Changes

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@h3ravel/shared",
3-
"version": "0.17.0",
3+
"version": "0.17.1",
44
"description": "Shared Utilities.",
55
"type": "module",
66
"main": "./dist/index.js",

packages/shared/src/Contracts/IHttp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export interface IRouter {
9999
* @param options - Configuration for prefix or middleware.
100100
* @param callback - Callback function defining grouped routes.
101101
*/
102-
group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void): this;
102+
group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => this): this;
103103

104104
/**
105105
* Registers middleware for a specific path.
@@ -139,6 +139,7 @@ export class HttpContext {
139139
* Type for EventHandler, representing a function that handles an H3 event.
140140
*/
141141
export type EventHandler = (ctx: HttpContext) => any
142+
export type RouteEventHandler = (...args: any[]) => any
142143

143144
/**
144145
* Defines the contract for all controllers.

0 commit comments

Comments
 (0)