Skip to content

Commit 99bbe1e

Browse files
committed
refactor: use utils module for invoking controllers and middleware
1 parent 33e99b8 commit 99bbe1e

File tree

10 files changed

+25
-43
lines changed

10 files changed

+25
-43
lines changed

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
export { Router } from './src/Router'
1515
export { Server } from './src/Server'
16-
export { MiddlewareStore } from './src/Server/MiddlewareStore'
1716
export { HttpContext } from './src/HttpContext'
17+
export { MiddlewareStore } from './src/Server/MiddlewareStore'
1818
export { routePreProcessor } from './src/Server/routePreProcessor'
1919

2020
export {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"ts-node": "^8.3.0",
4545
"tslint": "^5.18.0",
4646
"tslint-eslint-rules": "^5.4.0",
47-
"typedoc": "^0.14.2",
47+
"typedoc": "^0.15.0",
4848
"typedoc-plugin-external-module-name": "^2.1.0",
4949
"typedoc-plugin-markdown": "^2.0.11",
5050
"typescript": "^3.5.3"
@@ -78,7 +78,7 @@
7878
"dependencies": {
7979
"@poppinss/request": "^1.0.9",
8080
"@poppinss/response": "^1.0.9",
81-
"@poppinss/utils": "^1.0.3",
81+
"@poppinss/utils": "^1.0.4",
8282
"co-compose": "^5.1.1",
8383
"haye": "^2.0.2",
8484
"lodash": "^4.17.15",

src/Server/MiddlewareStore.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import * as haye from 'haye'
15-
import { Exception } from '@poppinss/utils'
15+
import { Exception, parseIocReference } from '@poppinss/utils'
1616

1717
import {
1818
RouteNode,
@@ -21,7 +21,7 @@ import {
2121
ResolvedMiddlewareNode,
2222
} from '../contracts'
2323

24-
import { exceptionCodes, iocMethods } from '../helpers'
24+
import { exceptionCodes } from '../helpers'
2525

2626
/**
2727
* Middleware store register and keep all the application middleware at one
@@ -61,15 +61,13 @@ export class MiddlewareStore<Context extends any> implements MiddlewareStoreCont
6161
* it, otherwise an exception will be raised.
6262
*/
6363
private _resolveMiddlewareItem (middleware: MiddlewareNode<Context>): ResolvedMiddlewareNode<Context> {
64-
return typeof(middleware) === 'string' ? {
65-
type: 'class',
66-
value: global[iocMethods.use](middleware),
67-
args: [],
68-
} : {
64+
return typeof(middleware) === 'function' ? {
6965
type: 'function',
7066
value: middleware,
7167
args: [],
72-
}
68+
} : Object.assign(parseIocReference(`${middleware}.handle`, undefined, undefined, true), {
69+
args: [],
70+
})
7371
}
7472

7573
/**

src/Server/finalErrorHandler.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { iocMethods } from '../helpers'
1110
import { useReturnValue } from './useReturnValue'
1211
import { ErrorHandlerNode, HttpContextContract } from '../contracts'
1312

@@ -37,8 +36,8 @@ export async function finalErrorHandler<Context extends HttpContextContract> (
3736
* Otherwise resolve the IoC container binding and call `handle` method
3837
* on it. The `handle` must always exist.
3938
*/
40-
const errorHandlerInstance = global[iocMethods.make](errorHandler)
41-
const returnValue = await global[iocMethods.call](errorHandlerInstance, 'handle', [error, ctx])
39+
const errorHandlerInstance = global[Symbol.for('ioc.make')](errorHandler)
40+
const returnValue = await global[Symbol.for('ioc.call')](errorHandlerInstance, 'handle', [error, ctx])
4241
if (useReturnValue(returnValue, ctx)) {
4342
ctx.response.send(returnValue)
4443
}
@@ -48,6 +47,6 @@ export async function finalErrorHandler<Context extends HttpContextContract> (
4847
* attempt to make an HTTP response
4948
*/
5049
if (typeof (errorHandlerInstance.report) === 'function') {
51-
await global[iocMethods.call](errorHandlerInstance, 'report', [error, ctx])
50+
await global[Symbol.for('ioc.call')](errorHandlerInstance, 'report', [error, ctx])
5251
}
5352
}

src/Server/finalMiddlewareHandler.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14+
import { callIocReference } from '@poppinss/utils'
1415
import { ResolvedMiddlewareNode } from '../contracts'
15-
import { iocMethods } from '../helpers'
1616

1717
/**
1818
* Final middleware handler executes a middleware
@@ -28,10 +28,6 @@ export function finalMiddlewareHandler<Context> (
2828
return middleware.value(params[0], params[1], middleware.args)
2929
}
3030

31-
const middlewareInstance = global[iocMethods.make](middleware.value)
32-
return global[iocMethods.call](middlewareInstance, 'handle', [
33-
params[0],
34-
params[1],
35-
middleware.args,
36-
])
31+
const args = ([params[0], params[1]] as any).concat([middleware.args])
32+
return callIocReference(middleware, args)
3733
}

src/Server/finalRouteHandler.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
import { ResolvedControllerNode, HttpContextContract } from '../contracts'
14+
import { callIocReference } from '@poppinss/utils'
1515
import { useReturnValue } from './useReturnValue'
16-
import { iocMethods } from '../helpers'
16+
import { ResolvedControllerNode, HttpContextContract } from '../contracts'
1717

1818
/**
1919
* Final handler executes the route handler based on it's resolved
@@ -39,9 +39,7 @@ export async function finalRouteHandler<Context extends HttpContextContract> (ct
3939
* Otherwise lookup the controller inside the IoC container
4040
* and make the response
4141
*/
42-
const controllerInstance = global[iocMethods.make](handler.namespace)
43-
const returnValue = await global[iocMethods.call](controllerInstance, handler.method, [ctx])
44-
42+
const returnValue = await callIocReference(handler, [ctx])
4543
if (useReturnValue(returnValue, ctx)) {
4644
ctx.response.send(returnValue)
4745
}

src/Server/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import { LoggerContract } from '@poppinss/logger'
2929
import { Request, RequestContract } from '@poppinss/request'
3030
import { Response, ResponseContract } from '@poppinss/response'
3131

32-
import { finalMiddlewareHandler } from './finalMiddlewareHandler'
33-
import { finalErrorHandler } from './finalErrorHandler'
3432
import { exceptionCodes } from '../helpers'
33+
import { finalErrorHandler } from './finalErrorHandler'
34+
import { finalMiddlewareHandler } from './finalMiddlewareHandler'
3535

3636
class RouteNotFound extends Exception {}
3737

src/contracts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,11 @@ export type ResolvedMiddlewareNode<Context> = {
266266
value: Exclude<MiddlewareNode<Context>, string>,
267267
args: string[],
268268
} | {
269-
type: 'class',
269+
type: 'iocObject',
270270
value: {
271271
handle: Exclude<MiddlewareNode<Context>, string>,
272272
},
273+
method: string,
273274
args: string[],
274275
}
275276

src/helpers.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import { Exception } from '@poppinss/utils'
1616

1717
import { Route } from './Router/Route'
1818
import { RouteGroup } from './Router/Group'
19+
import { RouteDefinition } from './contracts'
1920
import { BriskRoute } from './Router/BriskRoute'
2021
import { RouteResource } from './Router/Resource'
21-
import { RouteDefinition } from './contracts'
2222

2323
/**
2424
* Makes input string consistent by having only the starting
@@ -124,12 +124,3 @@ export const exceptionCodes = {
124124
E_ROUTE_NOT_FOUND: 'E_ROUTE_NOT_FOUND',
125125
E_MISSING_NAMED_MIDDLEWARE: 'E_MISSING_NAMED_MIDDLEWARE',
126126
}
127-
128-
/**
129-
* Symbols to use IoC container global methods.
130-
*/
131-
export const iocMethods = {
132-
use: Symbol.for('ioc.use'),
133-
make: Symbol.for('ioc.make'),
134-
call: Symbol.for('ioc.call'),
135-
}

test/route-pre-processor.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ test.group('Route pre processor', (group) => {
7171
routePreProcessor(route, middlewareStore)
7272

7373
assert.deepEqual(route.meta.resolvedMiddleware, [{
74-
type: 'class',
74+
type: 'iocObject',
7575
value: Auth,
76+
method: 'handle',
7677
args: ['jwt'],
7778
}])
7879
})
@@ -167,7 +168,5 @@ test.group('Route pre processor', (group) => {
167168

168169
const route = router.get('/', '/UserController.store').toJSON()
169170
routePreProcessor(route, middlewareStore)
170-
171-
console.log(route.meta.finalHandler)
172171
})
173172
})

0 commit comments

Comments
 (0)