Giter Club home page Giter Club logo

lib-core's Introduction

1. Introduction

A lib to help us the ease of functional programming (based on ramda (https://ramdajs.com/) & folktale(https://www.npmjs.com/package/folktale))

└── lib
    ├── logger.js
    ├── token.js
    ├── utilities
    │   ├── api-error.js
    │   ├── args.js
    │   ├── compose-result.js
    │   ├── doNothing.js
    │   ├── http-constant.js
    │   ├── ifElse.js
    │   ├── logger.js
    │   ├── respond.js
    │   ├── transform-to-result.js
    │   ├── uuid.js
    │   ├── whenResult.js
    │   └── with-args.js
    └── validations
        ├── check-given-values.js
        ├── has-length-of.js
        ├── is-boolean.js
        ├── is-email.js
        ├── is-mobile-number.js
        ├── is-string-numeric.js
        ├── is-timestamp.js
        ├── is-undefined.js
        ├── max-value.js
        ├── min-value.js
        ├── not-empty.js
        ├── numeric.js
        ├── should-be-uuid.js
        ├── validate-given-pattern.js
        └── validate.js

3. Installation

    npm install @mvp-rockets/namma-lib

4. How to imports.

  • All the function from @mvp-rockets/namma-lib
    const {
        utilities:{
                logInfo,
                logError,
                ....
                ....
                all the utilities function here
         },
         validations:{
            isBoolean,
	        hasLengthOf,
            ....
            ....
            all the validations function here
         },
         HTTP_CONSTANT,
         token
        } = require('@mvp-rockets/namma-lib')
  • Only the utilities
const {
        logInfo,
        logError,
        ....
        ....
        all the utilities function here
        } = require('@mvp-rockets/namma-lib/utilities')
  • Only the validations
const {
        logInfo,
        logError,
        ....
        ....
        all the utilities function here
        } = require('@mvp-rockets/namma-lib/validations')
  • Only the token
const {
         token
        } = require('@mvp-rockets/namma-lib')
  • Only the HTTP_CONSTANT

    const {
    HTTP_CONSTANT
    } = require('@mvp-rockets/namma-lib')
    

5. How to use token

<!-- initialize token in your index.js -->
const {
         token
        } = require('@mvp-rockets/namma-lib');

token.initialize("Your Jwt secret key");

<!-- Generate Token  -->
const tokenResult =  await token.generate("Your object")

console.log(tokenResult); // Result.Ok("Your generated token")

<!-- decode token -->
const decodedTokenResult =  await token.decode("Your token")

console.log(decodedTokenResult); // Result.Ok("Your decoded object")

if case of invalid or expired token
console.log(decodedTokenResult); // Result.Error("Invalid token")

6. How to use logger.

Now logger comes with two libraries internally ie, winston and pino. By default it uses winston.

<!-- initialize logger in your index.js -->
const { Logger } = require('@mvp-rockets/namma-lib');

Logger.initialize({
	isEnable: true, // for dev,qa use false
	type: 'aws',
	environment: "<env name>",
	clsNameSpace: <"cls name for trace Id">,
	configurations: {
		region: <"aws region">,
		accessKeyId: <"aws access Key Id">,
		secretKey: <"aws secret Key">,
		logGroupName: <"log group name">,
		logStreamName: <"log stream name">
	}
});

<!-- add below code for unique traceId for each request -->
const { logInfo } = require('@mvp-rockets/namma-lib/utilitiesut');
app.use((req, res, next) => {
	const namespace = cls.getNamespace("<cls name for trace Id>");
	const platform = req.headers['x-platform'] || 'unknown-platform';
	namespace.run(() => {
		namespace.set('traceId', uuid.v4());
		logInfo(`${req.method} ${req.originalUrl}`, { ...req.body, platform });
		next();
	});
});
To use pino, you need to pass following properties
        loggerType: "pino"
        loggerOptions: "cloudwatch"

Using pino logger you can send logs to more than one destination. for now Options are file, cloudwatch, terminal and loki.
To use more than one destination you can specify in loggerOptions by comma separated values.
for example:
        loggerOptions: "cloudwatch, file, loki"

Usage example for pino logger:

Logger.initialize({
	environment: "<env name>",
	clsNameSpace: <"cls name for trace Id">,
	configurations: {
		region: <"aws region">,
		accessKeyId: <"aws access Key Id">,
		secretKey: <"aws secret Key">,
		logGroupName: <"log group name">,
		logStreamName: <"log stream name">,
                interval: <"interval integer value">
	},
        loggerType: "pino"
        loggerOptions: "cloudwatch"
});


NOTE: Before switching to pino logger, make sure if you have alerts based on level: error. you need to make some change because for now using this library. level will be integer(info as 30, error as 50) and there will be another property label where value will be error, info, debug.


3. Video walkthrough Tutorials

Youtube Tutorials

lib-core's People

Contributors

yashjeet avatar mangal-napses avatar bhattisatish avatar yashnapses avatar aakashgaur01 avatar dansteve avatar dependabot[bot] avatar saranapses avatar harishbunicorn avatar descholar-ceo avatar

Stargazers

Vasanth Kumar avatar VK avatar

Watchers

 avatar  avatar

lib-core's Issues

compose result not working in 1.2.0-beta.0 version

it gives internal server error when using composeResult

ApiError { code: 500, error: TypeError: Cannot read properties of null (reading 'update') at /home/ashutosh/aris_infra/functions/resources/requirements/queries/update-requirement-query.js:12:36 at processTicksAndRejections (node:internal/process/task_queues:96:5), errorMessage: 'Failed to create requirement!', errorDescription: 'Internal Server Error' }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.