@narando/notifications-parser
v0.36.0
Published
A custom error message parser on narando architecture.
Downloads
39
Readme
@narando/notifications-parser
A custom error message parser on narando architecture.
Getting Started
You need to have nodejs
and npm
installed.
$ npm install @narando/notifications-parser
Usage
Parse errors witch were send via the url queries or created as a custom object. The object structure looks like:
// Permission
{
// Name of error or could be an Array of names
"errors": "An error occured",
// Success message can be set while some errors exists.
"success": true
}
With custom messages
To create an instance of the notifications-parser module you have to import notifications-parser
import notificationsParser from "@narando/notifications-parser";
// You can preconfigure custom error messages.
// If you don't specify custom messages the default messages will be used.
// The custom messages can expand or replace the existing messages.
//
// Example url:
// http://localhost/path?success=true&error=ERRORTYPE1&error=ERRORTYPE2
function getArticles(req, res) {
const customMessage = {
ERRORTYPE1: "custom message 1",
ERRORTYPE1: "custom message 2"
};
// Now you can use the parser to set your locals which will be send to your
// template engine.
res.locals.notifications = notificationsParser(req.query, customMessage);
// Returned Object:
// {
// success: "Success!",
// errors: "["custom Message 1", "custom message 2"]"
// }
res.render(`page/article`, res.locals);
}
Without custom messages
Use the notifications-parser
without the custom messages
// Example url:
// http://localhost/path?success=true&error=ResourceMissingError
res.locals.notifications = notificationsParser(req.query);
// Returned Object:
// {
// success: "Success!",
// errors: "["custom Message 1", "Resource is missing!"]"
// }
}
Show the result with mustache
The messages will be displayed by the following mustache logic. In this case it will display all error messages and a potential success message.
{{#notifications}}
<div class="container">
{{#error}}
<div class="row">
<div class="alert alert-danger alert-dismissable col-lg-12">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
{{{.}}}
</div>
</div>
{{/error}}
{{#success}}
<div class="row">
<div class="alert alert-success alert-dismissable col-lg-12">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
{{{.}}}
</div>
</div>
{{/success}}
</div>
{{/notifications}}
Flashing some notifications
In case you need to add a new error that is not set in the query you can use the following method.
The function will add a new error to the object with the errors which will be parsed by the notificationsParser
import notificationsParser, {
flashNotification
} from "@narando/notifications-parser";
// To add a new error to the query.error or your custom object
// You have to use flashNotification.error();
// This function adds the new error to the object.
const newError = "NewError";
flashNotification.error(req.query, newError);
res.locals.notifications = notificationsParser(req.query);
L10n notifications
The default messages are available in German and English (US). The l10n parameter is optional and set to English (US) by default.
Please use the BCP 47 language codes
Get the localized default messages
import notificationsParser from "@narando/notifications-parser";
function getArticles(req, res) {
// Example url:
// http://localhost/path?success=true&error=ResourceMissingError
res.locals.notifications = notificationsParser(req.query, {}, "de-DE");
// Returned Object:
// {
// success: "Aktion erfolgreich ausgeführt!",
// errors: ["Das angefragte Objekt fehlt."]
// }
res.render(`page/article`, res.locals);
}
L10n with custom messages
import notificationsParser from "@narando/notifications-parser";
function getArticles(req, res) {
// Get the custom messages from your l10n function.
const customMessage = {
ERRORTYPE1: "Spezifische Nachricht 1",
ERRORTYPE1: "Spezifische Nachricht 2"
};
// Example url:
// http://localhost/path?success=true&error=ERRORTYPE1&error=ERRORTYPE2
res.locals.notifications = notificationsParser(
req.query,
customMessage,
"de-DE"
);
// Returned Object:
// {
// success: "Aktion erfolgreich ausgeführt!",
// errors: "["Spezifische Nachricht 1", "Spezifische Nachricht 2"]"
// }
res.render(`page/article`, res.locals);
}