@bloomingbox-ms/common
v1.0.19
Published
BloomingBox common package
Downloads
265
Readme
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=""