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 Mar 9, 2018
1 parent de08aa5 commit 6916fcf
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 46 deletions.
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,
factory?: ControllerFactory,
) {
this._routes.registerController(controllerCtor, spec);
}

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
32 changes: 28 additions & 4 deletions packages/rest/src/rest-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import {AssertionError} from 'assert';
import {safeDump} from 'js-yaml';
import {Binding, Context, Constructor, inject} from '@loopback/context';
import {Route, ControllerRoute, RouteEntry} from './router/routing-table';
import {
Route,
ControllerRoute,
RouteEntry,
createControllerFactory,
ControllerFactory,
} from './router/routing-table';
import {ParsedRequest} from './internal-types';
import {OpenApiSpec, OperationObject} from '@loopback/openapi-v3-types';
import {ServerRequest, ServerResponse, createServer} from 'http';
Expand Down Expand Up @@ -228,7 +234,8 @@ export class RestServer extends Context implements Server {
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 @@ -277,7 +284,15 @@ export class RestServer extends Context implements Server {
);
}

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 @@ -366,6 +381,7 @@ export class RestServer extends Context implements Server {
spec: OperationObject,
controller: ControllerClass,
methodName: string,
factory?: ControllerFactory,
): Binding;

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

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 6916fcf

Please sign in to comment.