Skip to content

Commit

Permalink
feat(rest): allow factory for controller routes
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Apr 11, 2018
1 parent 0331df8 commit 184371b
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 47 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {BindingKey} from '@loopback/context';
import {Application, ControllerClass} from './application';
import {ControllerInstance} from '../../rest/src';

/**
* Namespace for core binding keys
Expand Down Expand Up @@ -53,4 +54,12 @@ export namespace CoreBindings {
* request context
*/
export const CONTROLLER_METHOD_META = 'controller.method.meta';

/**
* Binding key for the controller instance resolved in the current request
* context
*/
export const CONTROLLER_CURRENT = BindingKey.create<ControllerInstance>(
'controller.current',
);
}
9 changes: 7 additions & 2 deletions packages/rest/src/http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ResolvedRoute,
RouteEntry,
ControllerClass,
ControllerFactory,
} from './router/routing-table';
import {ParsedRequest} from './internal-types';

Expand All @@ -33,8 +34,12 @@ export class HttpHandler {
this.handleRequest = (req, res) => this._handleRequest(req, res);
}

registerController(name: ControllerClass, spec: ControllerSpec) {
this._routes.registerController(name, spec);
registerController(
controllerCtor: ControllerClass,
spec: ControllerSpec,
controllerFactory?: ControllerFactory,
) {
this._routes.registerController(controllerCtor, spec, controllerFactory);
}

registerRoute(route: RouteEntry) {
Expand Down
4 changes: 4 additions & 0 deletions packages/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export {
ResolvedRoute,
createResolvedRoute,
parseRequestUrl,
ControllerClass,
ControllerInstance,
ControllerFactory,
createControllerFactory,
} from './router/routing-table';

export * from './providers';
Expand Down
18 changes: 16 additions & 2 deletions packages/rest/src/rest.application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {Binding, Constructor} from '@loopback/context';
import {format} from 'util';
import {RestBindings} from './keys';
import {RestServer, HttpRequestListener, HttpServerLike} from './rest.server';
import {ControllerClass, RouteEntry} from './router/routing-table';
import {
RouteEntry,
ControllerClass,
ControllerFactory,
} from './router/routing-table';
import {OperationObject, OpenApiSpec} from '@loopback/openapi-v3-types';

export const ERR_NO_MULTI_SERVER = format(
Expand Down Expand Up @@ -97,13 +101,15 @@ export class RestApplication extends Application implements HttpServerLike {
* @param spec The OpenAPI spec describing the endpoint (operation)
* @param controller Controller constructor
* @param methodName The name of the controller method
* @param factory A factory function to create controller instance
*/
route(
verb: string,
path: string,
spec: OperationObject,
controller: ControllerClass,
methodName: string,
factory?: ControllerFactory,
): Binding;

/**
Expand All @@ -127,12 +133,20 @@ export class RestApplication extends Application implements HttpServerLike {
spec?: OperationObject,
controller?: ControllerClass,
methodName?: string,
factory?: ControllerFactory,
): Binding {
const server = this.restServer;
if (typeof routeOrVerb === 'object') {
return server.route(routeOrVerb);
} else {
return server.route(routeOrVerb, path!, spec!, controller!, methodName!);
return server.route(
routeOrVerb,
path!,
spec!,
controller!,
methodName!,
factory,
);
}
}

Expand Down
27 changes: 24 additions & 3 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Route,
ControllerRoute,
RouteEntry,
createControllerFactory,
ControllerFactory,
ControllerClass,
} from './router/routing-table';
import {ParsedRequest} from './internal-types';
Expand Down Expand Up @@ -243,7 +245,8 @@ export class RestServer extends Context implements Server, HttpServerLike {
if (apiSpec.components && apiSpec.components.schemas) {
this._httpHandler.registerApiDefinitions(apiSpec.components.schemas);
}
this._httpHandler.registerController(ctor, apiSpec);
const controllerFactory = createControllerFactory(b.key);
this._httpHandler.registerController(ctor, apiSpec, controllerFactory);
}

for (const b of this.find('routes.*')) {
Expand Down Expand Up @@ -292,7 +295,15 @@ export class RestServer extends Context implements Server, HttpServerLike {
);
}

const route = new ControllerRoute(verb, path, spec, ctor);
const controllerFactory = createControllerFactory(b.key);
const route = new ControllerRoute(
verb,
path,
spec,
ctor,
undefined,
controllerFactory,
);
this._httpHandler.registerRoute(route);
return;
}
Expand Down Expand Up @@ -374,13 +385,15 @@ export class RestServer extends Context implements Server, HttpServerLike {
* @param spec The OpenAPI spec describing the endpoint (operation)
* @param controller Controller constructor
* @param methodName The name of the controller method
* @param factory A factory function to create controller instance
*/
route(
verb: string,
path: string,
spec: OperationObject,
controller: ControllerClass,
methodName: string,
factory?: ControllerFactory,
): Binding;

/**
Expand All @@ -404,6 +417,7 @@ export class RestServer extends Context implements Server, HttpServerLike {
spec?: OperationObject,
controller?: ControllerClass,
methodName?: string,
controllerFactory?: ControllerFactory,
): Binding {
if (typeof routeOrVerb === 'object') {
const r = routeOrVerb;
Expand Down Expand Up @@ -439,7 +453,14 @@ export class RestServer extends Context implements Server, HttpServerLike {
}

return this.route(
new ControllerRoute(routeOrVerb, path, spec, controller, methodName),
new ControllerRoute(
routeOrVerb,
path,
spec,
controller,
methodName,
controllerFactory,
),
);
}

Expand Down
Loading

0 comments on commit 184371b

Please sign in to comment.