0.2.9 • Published 2 years ago

@nebulae/backend-node-tools v0.2.9

Weekly downloads
70
License
Apache-2.0
Repository
github
Last release
2 years ago

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.envdescvaluesdefaults
LOG_LEVELLog Level ThresholdDEBUG INFO WARN ERROR FATALWARN

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.envdescvaluesdefaults
BROKER_TYPEDefault broker to useMQTT PUBSUBN/A
GOOGLE_APPLICATION_CREDENTIALSgcloud-service-key json file to configure PubSubgcloud-service-key json fileN/A
MICROBACKEND_KEYThe MicroBackend unique Key is used as PubSub Subscription suffixms-lineadirecta-generator_mbe_lineadirecta-generatordefault-suffix
MQTT_SERVER_URLmqtt server URLmqtt://host:portN/A
REPLY_TIMEOUTsend & recieve response timeout millismilliseconds (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
)
0.2.9

2 years ago

0.2.7

2 years ago

0.2.8

2 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.1

3 years ago

0.2.2

3 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago