lynx-framework
Version:
lynx is a NodeJS framework for Web Development, based on decorators and the async/await support.
171 lines (170 loc) • 7.57 kB
TypeScript
import * as express from 'express';
import App from './app';
import { FileOptions } from './file.response';
import RenderResponse from './render.response';
import RedirectResponse from './redirect.response';
import SkipResponse from './skip.response';
import UnauthorizedResponse from './unauthorized.response';
import FileResponse from './file.response';
import Request from './request';
import { LynxControllerMetadata } from './decorators';
import Media from './entities/media.entity';
import StatusError from './status-error';
import Logger from './logger';
import XmlResponse from './xml.response';
export declare enum FlashType {
primary = 0,
secondary = 1,
success = 2,
danger = 3,
warning = 4,
info = 5,
light = 6,
dark = 7
}
export interface FlashMessage {
type: FlashType;
message: string;
}
/**
* This class defines the basic class for any controllers. It implements a lot
* of utility methods in order to correctly generate any response.
*/
export declare class BaseController {
app: App;
private _metadata;
logger: Logger;
get metadata(): LynxControllerMetadata;
constructor(app: App);
/**
* This method is called only when the constructed has been completed.
* Since this method is async, it can be used to perform some initialization
* that needed the use of the await keyword. */
postConstructor(): Promise<void>;
/**
* Add a value to the current request context.
* Any variable added with this method will available in the template context
* thought the @method render method.
* @param req the current Request
* @param key the key of the value to add
* @param value the value to add
*/
addToContext(req: Request, key: string, value: any): void;
/**
* Utility method to generate an error with a status code.
* This method should be used instead of the usual throw new Error(msg).
* In this way, a proper HTTP status code can be used (for example, 404 or 500),
* instead of the default 400.
* @param status the http status code to return
* @param message the error message
* @return a new @type StatusError object
*/
error(status: number, message: string): StatusError;
/**
* This method generates a url to a route starting from the route name and
* optionally its parameters.
* If a parameter is not used to generate the route url, it will be appended
* as a query parameter.
* @param name the name of the route
* @param parameters a plain object containing the parameters for the route.
*/
route(name: string, parameters?: any): string;
/**
* Generate a web page starting from a template and using a generated context.
* @param view the name of the view
* @param req the request object
* @param context a plain object containing any necessary data needed by the view
*/
render(view: string, req: Request, context?: any): RenderResponse;
/**
* Redirect the current route to another
* @param routeName the new of the target route
* @param routeParams a plain object containing the parameters for the route.
*/
redirect(routeName: string, routeParams?: any): RedirectResponse;
/**
* Add a flash message in the current request.
* @param msg the FlashMessage to be included
* @param req the request
*/
addFlashMessage(msg: FlashMessage, req: Request): void;
/**
* Add a success flash message in the current request.
* @param msg the string (can be localized) of the message
* @param req the request
*/
addSuccessMessage(msg: string, req: Request): void;
/**
* Add an error flash message in the current request.
* @param msg the string (can be localized) of the message
* @param req the request
*/
addErrorMessage(msg: string, req: Request): void;
/**
* Generate a response suitable to file download. This method can also be
* used to generate images of specific dimensions.
* @param path the string path of the file, or a Media object to be downloaded
* @param options options to correctly generate the output file
*/
download(path: string | Media, options?: FileOptions): FileResponse;
/**
* @deprecated use the `throw this.error(401, 'unauthorized') instead.
* Generate an unauthorized response.
*/
unauthorized(): UnauthorizedResponse;
/**
* Generate a skip response. In this particular case, the original Express `next()`
* will be executed, causing the controller chain to continue its execution.
*/
next(): SkipResponse;
/**
* Generate a response as an Xml file, but starting from a standard Nunjucks template.
* This response is very similar to the standard render response. The main difference is
* the `contentType`, set to `application/xml`.
* Moreover, the flash messages are ignored.
* @param view the name of the view
* @param req the request object
* @param context a plain object containing any necessary data needed by the view
*/
xml(view: string, req: Request, context?: any): XmlResponse;
/**
* Utility method to send emails from a controller.
* This method is similar to the `sendMail` method, but define a lower level API.
* Indeed, it directly accepts the text and the html of the email, and not the templates urls.
* @param dest the email destination (can also be an array of addresses)
* @param subject the subject of the email
* @param text the text version of the email
* @param html the html version of the email
*/
sendRawMail(dest: string | string[], subject: string, text: string, html: string): Promise<boolean>;
/**
* Utility method to send an email from a controller. This method is async,
* so use the await keyword (or eventually a promise) to correctly read the
* return value.
* This method uses the template engine to compile the email.
* NOTE: internally, this method uses the `sendRawMail` method.
* @param req the current request
* @param dest the email destination (can also be an array of addresses)
* @param subjectTemplateString the subject of the email, that can also be a string template
* @param textTemplate the text version of the email, referencing a path in the view folders
* @param htmlTemplate the html version of the email, referencing a path in the view folders
* @param context a plain object containing any necessary data needed by the view
*/
sendMail(req: express.Request, dest: string | string[], subjectTemplateString: string, textTemplate: string, htmlTemplate: string, context: any): Promise<boolean>;
/**
* Utility method to obtain a translated string.
* @param str the string key to be translated
* @param req the original request
* @param language optionally, the language can be forced using this variable
*/
tr(str: string, req: Request, language?: string): string;
/**
* Utility method to obtain a translated string, formatted with parameters.
* Each parameter should be encoded as {0}, {1}, etc...
* @param str the string key to be translated
* @param req the original request
* @param language optionally, the language can be forced using this variable
* @param args the arguments to format the string
*/
trFormat(str: string, req: Request, language: string | undefined, ...args: any): string;
}