# @bloomingbox-ms/common

> BloomingBox common package

Latest version **1.0.24** (published 2026-06-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install @bloomingbox-ms/common
pnpm add @bloomingbox-ms/common
yarn add @bloomingbox-ms/common
bun add @bloomingbox-ms/common
```

## Health

**Score 45/100 (D)** — status: active.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.24 |
| Published | 2026-06-30 |
| First published | 2022-01-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 37 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | isurindu prabashwara |
| Maintainers | bb.isurindu, hamza.bloomingbox, yohandal-bb |

## Links

- npm: https://www.npmjs.com/package/@bloomingbox-ms/common
- npm.io page: https://npm.io/package/@bloomingbox-ms/common

## Dependencies (7)

- [axios](https://npm.io/package/axios.md) ^1.6.4
- [dotenv](https://npm.io/package/dotenv.md) ^14.2.0
- [moment](https://npm.io/package/moment.md) ^2.30.1
- [amqplib](https://npm.io/package/amqplib.md) ^0.10.3
- [express](https://npm.io/package/express.md) ^4.18.2
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.2
- [moment-timezone](https://npm.io/package/moment-timezone.md) ^0.5.45

## Recent versions

- 1.0.24 (latest) — 2026-06-30
- 1.0.23 — 2026-06-10
- 1.0.22 — 2024-10-25
- 1.0.21 — 2024-10-25
- 1.0.20 — 2024-10-13
- 1.0.19 — 2024-08-27
- 1.0.18 — 2024-07-16
- 1.0.16 — 2024-06-26
- 1.0.15 — 2024-06-25
- 1.0.14 — 2024-06-18
- 1.0.13 — 2024-05-27
- 1.0.12 — 2024-05-27
- 1.0.11 — 2024-03-24
- 1.0.10 — 2024-01-25
- 1.0.9 — 2024-01-25
- … 9 more at https://npm.io/package/@bloomingbox-ms/common/versions

## README

# Description 

This package include all the common things needed for each service 

## Installation 

```console
# 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 

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

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

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

```
### console.log
```js
  user: {
    _id: "6206a6c25e36716c181391cf",
    firstName: "isurindu",
    lastName: "***********",
    email: "*****@******.com",
    mobileNumber: "+971554573967",
    warehouse: [
      "6390d63a1eb4bc41e722d3a5",
      "6394363fb0486fdce0526ec5",
      "6390d6401eb4bc41e722d3a7"
    ],
    roles: [ "65597a978ee2584d4863194a" ]
  }
```
## How to secure API 


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

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

Use `authGuard.canAny` when the user needs **at least one** of the listed permissions (instead of all of them):

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

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

### Insufficient Permissions Response - Status Code 403 Forbidden
```js 
module.exports = async (req, res, next) => {
  const user = req.user; //null or userObject
  console.log({user})
};

```

## Guest Middleware provide user if exist otherwise null 


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

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

### Insufficient Permissions Response - Status Code 403 Forbidden
```json 
{
    "success": false,
    "code": 403,
    "msg": "You do not have permission to perform this action!"
}

```
## Health Check 
```js
const {healthRoutes} = require('@bloomingbox-ms/common');

app.use('/', healthRoutes);

```

## Cluster events 

### how to dispatch a event 
```js
const {clusterEvent} = require('@bloomingbox-ms/common');

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

```
### how to listen to the event 
```js
const {clusterEvent} = require('@bloomingbox-ms/common');

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

## Timezone 

```js
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  

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

app.use(countryCodeMiddleware);


```

## Upgrade 

```
npm install @bloomingbox-ms/common@latest
```

configure  using .env 

```.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.

```javascript
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: "مرحبًا بك في خدمة العربة"

---
_Source: https://npm.io/package/@bloomingbox-ms/common · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
