Responses - jellyfishsolutions/lynx GitHub Wiki
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.
Parameters:
- status: the http status code to return
- message: the error message
- return a new @type StatusError object
@Route(APISettings.baseUrl + '/customers')
export default class CustomersController extends BaseController {
@API()
@Verify(notCustomerUser)
@Body('d', loginSchema)
@POST('/login')
async performLogin(
d: ValidateObject<{ email: string; password: string }>,
req: Request
) {
if (!d.isValid) {
throw this.error(403, 'empty email or password');
}
....
}
}Generate a web page starting from a template and using a generated context.
Parameters:
- view: the name of the view
- req: the request object
- context: a plain object containing any necessary data needed by the view
Notes:
The view parameter shall be the name of the view, that is the relative path of the view without the extension, starting from the views folder of the module.
...
return this.render('frontend/forgot-password', req, {
emailError: true,
});Redirect the current route to another.
This method uses the route function to generate the redirect url.
Parameters:
- routeName: the new of the target route
- routeParams: a plain object containing the parameters for the route.
@GET("/")
async index(): Promise<Response> {
return this.redirect("fe_login_view");
}Generate a response suitable to file download. This method can also be used to generate images of specific dimensions.
Parameters:
- path: the string path of the file, or a Media object to be downloaded
- options: options to correctly generate the output file
Notes:
This method correctly works with both file path or the Media entity, that encapsulate, for example, an uploaded resource.
Moreover, the options parameter allows to specify the width and/or height of the final image, that is resized.
If a file path is specified, it is used directly to the usual download express implementation:
...
res.download(path);Warning:
Do not use the options argument if the mime type of the requested media is not an image.
Usage with the Media entity
@Name('preview_gallery_element_2')
@GET('/gallery2/preview/')
async mediaPreview(req: Request) {
let id = req.query.id as any;
let media = await MediaEntity.findBySlugOrId(id);
if (!media) {
throw this.error(404, 'Media not found');
}
if (media.mimetype == 'image/svg+xml') {
return this.download(media);
}
return this.download(media, { width: 300 });
}Generate a skip response. In this particular case, the original Express next() will be executed, causing the controller chain to continue its execution.
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.
Parameters:
- view: the name of the view
- req: the request object
- context: a plain object containing any necessary data needed by the view