Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ 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('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<T>(
spec: ControllerSpec,
controllerCtor: ControllerClass<T>,
controllerFactory?: ControllerFactory<T>,
) {
this._routes.registerController(spec, controllerCtor, controllerFactory);
}

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

export * from './providers';
Expand Down
28 changes: 21 additions & 7 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 @@ -95,14 +99,16 @@ export class RestApplication extends Application implements HttpServerLike {
* @param verb HTTP verb of the endpoint
* @param path URL path of the endpoint
* @param spec The OpenAPI spec describing the endpoint (operation)
* @param controller Controller constructor
* @param controllerCtor Controller constructor
* @param controllerFactory A factory function to create controller instance
* @param methodName The name of the controller method
*/
route(
route<T>(
verb: string,
path: string,
spec: OperationObject,
controller: ControllerClass,
controllerCtor: ControllerClass<T>,
controllerFactory: ControllerFactory<T>,
methodName: string,
): Binding;

Expand All @@ -121,18 +127,26 @@ export class RestApplication extends Application implements HttpServerLike {
*/
route(route: RouteEntry): Binding;

route(
route<T>(
routeOrVerb: RouteEntry | string,
path?: string,
spec?: OperationObject,
controller?: ControllerClass,
controllerCtor?: ControllerClass<T>,
controllerFactory?: ControllerFactory<T>,
methodName?: string,
): 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!,
controllerCtor!,
controllerFactory!,
methodName!,
);
}
}

Expand Down
41 changes: 31 additions & 10 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
Route,
ControllerRoute,
RouteEntry,
ControllerFactory,
ControllerClass,
ControllerInstance,
createControllerFactoryForBinding,
} from './router/routing-table';
import {ParsedRequest} from './internal-types';
import {OpenApiSpec, OperationObject} from '@loopback/openapi-v3-types';
Expand Down Expand Up @@ -243,7 +246,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 = createControllerFactoryForBinding(b.key);
this._httpHandler.registerController(apiSpec, ctor, controllerFactory);
}

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

const route = new ControllerRoute(verb, path, spec, ctor);
const controllerFactory = createControllerFactoryForBinding(b.key);
const route = new ControllerRoute(
verb,
path,
spec,
ctor,
controllerFactory,
);
this._httpHandler.registerRoute(route);
return;
}
Expand Down Expand Up @@ -351,7 +362,7 @@ export class RestServer extends Context implements Server, HttpServerLike {
* ```
*
*/
controller(controllerCtor: ControllerClass): Binding {
controller(controllerCtor: ControllerClass<ControllerInstance>): Binding {
return this.bind('controllers.' + controllerCtor.name).toClass(
controllerCtor,
);
Expand All @@ -372,14 +383,16 @@ export class RestServer extends Context implements Server, HttpServerLike {
* @param verb HTTP verb of the endpoint
* @param path URL path of the endpoint
* @param spec The OpenAPI spec describing the endpoint (operation)
* @param controller Controller constructor
* @param controllerCtor Controller constructor
* @param controllerFactory A factory function to create controller instance
* @param methodName The name of the controller method
*/
route(
route<I>(
verb: string,
path: string,
spec: OperationObject,
controller: ControllerClass,
controllerCtor: ControllerClass<I>,
controllerFactory: ControllerFactory<I>,
methodName: string,
): Binding;

Expand All @@ -398,11 +411,12 @@ export class RestServer extends Context implements Server, HttpServerLike {
*/
route(route: RouteEntry): Binding;

route(
route<I>(
routeOrVerb: RouteEntry | string,
path?: string,
spec?: OperationObject,
controller?: ControllerClass,
controllerCtor?: ControllerClass<I>,
controllerFactory?: ControllerFactory<I>,
methodName?: string,
): Binding {
if (typeof routeOrVerb === 'object') {
Expand All @@ -426,7 +440,7 @@ export class RestServer extends Context implements Server, HttpServerLike {
});
}

if (!controller) {
if (!controllerCtor) {
throw new AssertionError({
message: 'controller is required for a controller-based route',
});
Expand All @@ -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,
controllerCtor,
controllerFactory,
methodName,
),
);
}

Expand Down
Loading