@bloomingbox-ms/common

1.0.22 • Public • Published

Description

This package include all the common things needed for each service

Installation

# Using NPM
npm install @bloomingbox-ms/common

# Using yarn (recommended)
yarn add @bloomingbox-ms/common

Features

  • permissions
  • auth middleware
  • cluster events handler
  • health check routes
  • timezone

How to get JWT token data

const {authMiddleware} = require('@bloomingbox-ms/common');

app.get(
  "/auth/me",
  authMiddleware,
  meController
);

meController.js

module.exports = async (req, res, next) => {
  const user = req.user;
  console.log({user})
};

console.log

  user: {
    _id: "6206a6c25e36716c181391cf",
    firstName: "isurindu",
    lastName: "***********",
    email: "*****@******.com",
    mobileNumber: "+971554573967",
    warehouse: [
      "6390d63a1eb4bc41e722d3a5",
      "6394363fb0486fdce0526ec5",
      "6390d6401eb4bc41e722d3a7"
    ],
    roles: [ "65597a978ee2584d4863194a" ]
  }

How to secure API

const {authMiddleware,authGuard} = require('@bloomingbox-ms/common');

app.get(
  "/auth/users",
  authMiddleware,
  authGuard.can(["user.view"]),
);

Insufficient Permissions Response - Status Code 403 Forbidden

module.exports = async (req, res, next) => {
  const user = req.user; //null or userObject
  console.log({user})
};

Guest Middleware provide user if exist otherwise null

const {guestMiddleware,authGuard} = require('@bloomingbox-ms/common');

app.get(
  "/auth/guest",
  guestMiddleware,
);

Insufficient Permissions Response - Status Code 403 Forbidden

{
    "success": false,
    "code": 403,
    "msg": "You do not have permission to perform this action!"
}

Health Check

const {healthRoutes} = require('@bloomingbox-ms/common');

app.use('/', healthRoutes);

Cluster events

how to dispatch a event

const {clusterEvent} = require('@bloomingbox-ms/common');

clusterEvent.dispatch("invoice.paid", {
    invoiceId: "123",
    amount: 100,
    currency: "USD",
});

how to listen to the event

const {clusterEvent} = require('@bloomingbox-ms/common');

clusterEvent.listen("payment-service", "invoice.paid", (payload) => {
    console.log("payment service pod 1", "invoice.paid");
});

Timezone

const { timezone } = require('@bloomingbox-ms/common');

const countryWithTimezone = timezone.availableCountries()
//{ ae: 'Asia/Dubai', sa: 'Asia/Riyadh' }

// Get current time in UTC format
const utcTime = timezone.getCurrentTimeUtc();

//Convert local time to UTC 
const convertLocalToUtc = timezone.convertLocalToUtc("2024-05-27","ae");

Get Country Code

const { countryCodeMiddleware } = require('@bloomingbox-ms/common');

app.use(countryCodeMiddleware);

Upgrade

npm install @bloomingbox-ms/common@latest

configure using .env

# JWT encryption key
KEY="" 

How to Use Translations

This package supports translations for multiple services, providing both English (en.json) and Arabic (ar.json) translation files for each service.

Available Services

  • cart-service
  • order-service
  • inventory-service
  • search-service
  • payment-service
  • slot-service
  • report-service
  • promo-service
  • auth-service
  • blog-service
  • cms-service
  • product-service
  • notification-service
  • azdan-service

Accessing Translations

To access translations, you can import the translations object from the package. Each service has its own translation files in English and Arabic.

const { translations } = require('@bloomingbox-ms/common');

// Access translations for cart-service
const cartServiceEn = translations['cart-service'].en;
const cartServiceAr = translations['cart-service'].ar;

console.log(cartServiceEn.welcome_message);  // Outputs: "Welcome to Cart Service"
console.log(cartServiceAr.welcome_message);  // Outputs: "مرحبًا بك في خدمة العربة"

Readme

Keywords

none

Package Sidebar

Install

npm i @bloomingbox-ms/common

Weekly Downloads

32

Version

1.0.22

License

ISC

Unpacked Size

36.3 kB

Total Files

52

Last publish

Collaborators

  • hamza.bloomingbox
  • bb.isurindu