UNPKG

6.72 kBTypeScriptView Raw
1import * as express from "express";
2import App from "./app";
3import { FileOptions } from "./file.response";
4import RenderResponse from "./render.response";
5import RedirectResponse from "./redirect.response";
6import SkipResponse from "./skip.response";
7import UnauthorizedResponse from "./unauthorized.response";
8import FileResponse from "./file.response";
9import Request from "./request";
10import { LynxControllerMetadata } from "./decorators";
11import Media from "./entities/media.entity";
12import StatusError from "./status-error";
13import Logger from "./logger";
14export declare enum FlashType {
15 primary = 0,
16 secondary = 1,
17 success = 2,
18 danger = 3,
19 warning = 4,
20 info = 5,
21 light = 6,
22 dark = 7
23}
24export interface FlashMessage {
25 type: FlashType;
26 message: string;
27}
28/**
29 * This class defines the basic class for any controllers. It implements a lot
30 * of utility methods in order to correctly generate any response.
31 */
32export declare class BaseController {
33 app: App;
34 private _metadata;
35 logger: Logger;
36 get metadata(): LynxControllerMetadata;
37 constructor(app: App);
38 /**
39 * This method is called only when the constructed has been completed.
40 * Since this method is async, it can be used to perform some initialization
41 * that needed the use of the await keyword. */
42 postConstructor(): Promise<void>;
43 /**
44 * Add a value to the current request context.
45 * Any variable added with this method will available in the template context
46 * thought the @method render method.
47 * @param req the current Request
48 * @param key the key of the value to add
49 * @param value the value to add
50 */
51 addToContext(req: Request, key: string, value: any): void;
52 /**
53 * Utility method to generate an error with a status code.
54 * This method should be used instead of the usual throw new Error(msg).
55 * In this way, a proper HTTP status code can be used (for example, 404 or 500),
56 * instead of the default 400.
57 * @param status the http status code to return
58 * @param message the error message
59 * @return a new @type StatusError object
60 */
61 error(status: number, message: string): StatusError;
62 /**
63 * This method generate an url to a route starting from the route name and
64 * optionally its parameters.
65 * If a parameter not is used to generate the route url, it will be appended
66 * as a query parameter.
67 * @param name the name of the route
68 * @param parameters a plain object containing the paramters for the route.
69 */
70 route(name: string, parameters?: any): string;
71 /**
72 * Generate a web page starting from a template and using a generated context.
73 * @param view the name of the view
74 * @param req the request object
75 * @param context a plain object containing any necessary data needed by the view
76 */
77 render(view: string, req: Request, context?: any): RenderResponse;
78 /**
79 * Redirect the current route to another
80 * @param routeName the new of the target route
81 * @param routeParams a plain object containing the paramters for the route.
82 */
83 redirect(routeName: string, routeParams?: any): RedirectResponse;
84 /**
85 * Add a flash message in the current request.
86 * @param msg the FlashMessage to be included
87 * @param req the request
88 */
89 addFlashMessage(msg: FlashMessage, req: Request): void;
90 /**
91 * Add a success flash message in the current request.
92 * @param msg the string (can be localized) of the message
93 * @param req the request
94 */
95 addSuccessMessage(msg: string, req: Request): void;
96 /**
97 * Add an error flash message in the current request.
98 * @param msg the string (can be localized) of the message
99 * @param req the request
100 */
101 addErrorMessage(msg: string, req: Request): void;
102 /**
103 * Generate a response suitable to file download. This method can also be
104 * used to generate images of specific dimensions.
105 * @param path the string path of the file, or a Media object to be downloaded
106 * @param options options to correctly generate the output file
107 */
108 download(path: string | Media, options?: FileOptions): FileResponse;
109 /**
110 * Generate an unauthorized response.
111 */
112 unauthorized(): UnauthorizedResponse;
113 /**
114 * Generate a skip resopnse. In this particuar case, the original Express `next()`
115 * will be executed, causing the controller chain to continue its execution.
116 */
117 next(): SkipResponse;
118 /**
119 * Utility method to send emails from a controller.
120 * This method is similar to the `sendMail` method, but define a lower level API.
121 * Indeed, it directly accepts the text and the html of the email, and not the templates urls.
122 * @param dest the email destination (can also be an array of addresses)
123 * @param subject the subject of the email
124 * @param text the text version of the email
125 * @param html the html version of the email
126 */
127 sendRawMail(dest: string | string[], subject: string, text: string, html: string): Promise<boolean>;
128 /**
129 * Utility method to send an email from a controller. This method is async,
130 * so use the await keyword (or eventually a promise) to correctly read the
131 * return value.
132 * This method uses the template engine to compile the email.
133 * NOTE: internally, this method uses the `sendRawMail` method.
134 * @param req the current request
135 * @param dest the email destination (can also be an array of addresses)
136 * @param subjectTemplateString the subject of the email, that can also be a string template
137 * @param textTemplate the text version of the email, referencing a path in the view folders
138 * @param htmlTemplate the html version of the email, referencing a path in the view folders
139 * @param context a plain object containing any necessary data needed by the view
140 */
141 sendMail(req: express.Request, dest: string | string[], subjectTemplateString: string, textTemplate: string, htmlTemplate: string, context: any): Promise<boolean>;
142 /**
143 * Utility method to obtain a translated string.
144 * @param str the string key to be translated
145 * @param req the original request
146 */
147 tr(str: string, req: Request): string;
148 /**
149 * Utility method to obtain a translated string, formatted with parameters.
150 * Each parameter should be encoded as {0}, {1}, etc...
151 * @param str the string key to be translated
152 * @param req the original request
153 * @param args the arguments to format the string
154 */
155 trFormat(str: string, req: Request, ...args: any): string;
156 private format;
157}