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

@nebulae/backend-node-tools

v0.2.9

Published

Tools collection for NebulaE Microservices Node Backends

Downloads

317

Readme

NebulaE

#Backend-Node-Tools

Backend-Node-Tools is a client library with several crosscutting tools for developing micro-backends based NebulaE Microservices Framework

Installation

npm install @nebulae/backend-node-tools --save

Console Logger

Tools for standard console logger

Environment Variables:

process.env | desc | values | defaults --- | --- | --- | --- LOG_LEVEL | Log Level Threshold | DEBUG INFO WARN ERROR FATAL | WARN

Example:

const { ConsoleLogger } = require('@nebulae/backend-node-tools').log;
const { CustomError } = require('@nebulae/backend-node-tools').error;

ConsoleLogger.d('This is a DEBUG Log');
ConsoleLogger.i('This is an INFO Log');
ConsoleLogger.w('This is a WARN Log', new CustomError('CustomError','Class.Method',1234,'CustomError'));
ConsoleLogger.e('This is an ERROR Log', new CustomError('CustomError','Class.Method',1234,'CustomError'));
ConsoleLogger.f('This is a FATAL Log', new Error('Node Error'));

// log format
// 2019-06-01T03:49:20.907Z [WARN]: This is a WARN Log;  ERROR(1234): CustomError

Custom Error

Node Error extension to includes name, code and method. This custom error is compatible with CQRS responses.

Example:


const { CustomError } = require('@nebulae/backend-node-tools').error;

const myCustomError = new CustomError(
    'ERR_NAME', // Error name
    'SomeClass.SomeMethod', // The Class name and Method where this error had generated
    1234, // Error code
    'Some Message' // Error Message
);

Auth Tools

User roles verification

Checks if the user has the role needed, otherwise throws an error according to the passed parameters. returns a RxJS Observable of validated roles if succed or custom error if the verification failed

Example:

const { RoleValidator } = require('@nebulae/backend-node-tools').auth;
const { CustomError, PERMISSION_DENIED } =require('@nebulae/backend-node-tools').error;

const userRoles = ['OPERATOR', 'PLATFORM-ADMIN', 'BUSINESS-OWNER'];
const neededRoles = ['PLATFORM-ADMIN', 'SYSADMIN'];
const permissionDeniedError = new CustomError('PermissionDenied', 'test.mocha', PERMISSION_DENIED, 'the user does not have the needed roles to execute this task');

RoleValidator.verifyRoles$(
    userRoles, // current user roles
    'SomeClass.SomeMethod', //current method
    permissionDeniedError, // The Error to throw if validation fails
    neededRoles // needed roles to verify
    ).subscribe(
        (response) => {
            //prints { 'PLATFORM-ADMIN': true, 'SYSADMIN': false }
            console.log(JSON.stringify(response));
        },
);

User has roles

Returns true if the user has at least one of the required roles or false otherwise

Example:

const { RoleValidator } = require('@nebulae/backend-node-tools').auth;

const userRoles = ['OPERATOR', 'PLATFORM-ADMIN', 'BUSINESS-OWNER'];
const neededRoles = ['PLATFORM-ADMIN', 'SYSADMIN'];

const hasNeededRoles = RoleValidator.hasRoles(userRoles,neededRoles);
//hasNeededRoles is true

Broker Factory

Creates a MQTT or Google Cloud PubSub Broker based on RxJS with pre-build functions for listening and sending messages

Environment Variables:

process.env | desc | values | defaults --- | --- | --- | --- BROKER_TYPE | Default broker to use | MQTT PUBSUB | N/A GOOGLE_APPLICATION_CREDENTIALS | gcloud-service-key json file to configure PubSub | gcloud-service-key json file | N/A MICROBACKEND_KEY | The MicroBackend unique Key is used as PubSub Subscription suffix | ms-lineadirecta-generator_mbe_lineadirecta-generator | default-suffix MQTT_SERVER_URL | mqtt server URL | mqtt://host:port | N/A REPLY_TIMEOUT | send & recieve response timeout millis | milliseconds (number) | 2000

Example:

const { brokerFactory } = require('@nebulae/backend-node-tools').broker;

// generates a multiton instance
const broker = brokerFactory('MQTT'); // Valid options: MQTT | PUBSUB

const subscription = broker.getMessageListener$(['TOPIC'], ['messageType']).pipe(
    mergeMap(message => this.processMessage$(message)),
    mergeMap(response => broker.send$('SomeTopic', 'messageType', response))
).subscribe(
    sentId => console.log(sentId),
);

CQRS tools

build Success Response

Builds an CQRS success response wrapping raw data. Returns a RxJS Observable stream

handle Error

gracefully handles an exception on a CQRS request-response stream Returns a RxJS Observable stream

Example:

const { CqrsResponseHelper } = require('@nebulae/backend-node-tools').cqrs;

const { of } = require('rxjs');
const { mergeMap } = require('rxjs/operators');

of('Some CQRS Requet').pipe(
    mergeMap(request => this.processRequest$(request)),
    mergeMap(rawData => CqrsResponseHelper.buildSuccessResponse$(rawRespponse)), // builds a valid CQRS API response
    catchError(err => CqrsResponseHelper.handleError$(err)) // handles Error and generates a valid CQRS API error response
)