UNPKG

2.81 kBTypeScriptView Raw
1import { Constructor, Context, ValueOrPromise } from '@loopback/core';
2import { ControllerSpec, OperationObject } from '@loopback/openapi-v3';
3import { OperationArgs, OperationRetval } from '../types';
4import { BaseRoute } from './base-route';
5export type ControllerInstance = {
6 [name: string]: any;
7} & object;
8/**
9 * A factory function to create controller instances synchronously or
10 * asynchronously
11 */
12export type ControllerFactory<T extends ControllerInstance> = (ctx: Context) => ValueOrPromise<T>;
13/**
14 * Controller class
15 */
16export type ControllerClass<T extends ControllerInstance> = Constructor<T>;
17/**
18 * A route backed by a controller
19 */
20export declare class ControllerRoute<T extends object> extends BaseRoute {
21 protected readonly _controllerCtor: ControllerClass<T>;
22 protected readonly _controllerName: string;
23 protected readonly _methodName: string;
24 protected readonly _controllerFactory: ControllerFactory<T>;
25 /**
26 * Construct a controller based route
27 * @param verb - http verb
28 * @param path - http request path
29 * @param spec - OpenAPI operation spec
30 * @param controllerCtor - Controller class
31 * @param controllerFactory - A factory function to create a controller instance
32 * @param methodName - Controller method name, default to `x-operation-name`
33 */
34 constructor(verb: string, path: string, spec: OperationObject, controllerCtor: ControllerClass<T>, controllerFactory?: ControllerFactory<T>, methodName?: string);
35 describe(): string;
36 updateBindings(requestContext: Context): void;
37 invokeHandler(requestContext: Context, args: OperationArgs): Promise<OperationRetval>;
38}
39/**
40 * Create a controller factory function for a given binding key
41 * @param key - Binding key
42 */
43export declare function createControllerFactoryForBinding<T extends object>(key: string): ControllerFactory<T>;
44/**
45 * Create a controller factory function for a given class
46 * @param controllerCtor - Controller class
47 */
48export declare function createControllerFactoryForClass<T extends object>(controllerCtor: ControllerClass<T>): ControllerFactory<T>;
49/**
50 * Create a controller factory function for a given instance
51 * @param controllerCtor - Controller instance
52 */
53export declare function createControllerFactoryForInstance<T extends object>(controllerInst: T): ControllerFactory<T>;
54/**
55 * Create routes for a controller with the given spec
56 * @param spec - Controller spec
57 * @param controllerCtor - Controller class
58 * @param controllerFactory - Controller factory
59 */
60export declare function createRoutesForController<T extends object>(spec: ControllerSpec, controllerCtor: ControllerClass<T>, controllerFactory?: ControllerFactory<T>): ControllerRoute<T>[];
61export declare function joinPath(basePath: string, path: string): string;