npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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);
}