authorization-z
Version:
`Authorization-Z` is a comprehensive Express middleware package for validating JWT Authorization-Z tokens, attaching permissions to requests, verifying permissions, and granting access accordingly. This package provides a robust solution for implementing
35 lines (34 loc) • 1.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSupportedLanguages = exports.getMessage = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const localesPath = path_1.default.join(__dirname, '../locales');
const messages = {};
const supportedLanguages = [];
// Dynamically load all JSON files in /locales
if (fs_1.default.existsSync(localesPath)) {
fs_1.default.readdirSync(localesPath).forEach((file) => {
if (file.endsWith('.json')) {
const langCode = path_1.default.basename(file, '.json');
supportedLanguages.push(langCode);
messages[langCode] = require(path_1.default.join(localesPath, file));
}
});
}
const getMessage = (lang = 'en', category, key) => {
var _a, _b, _c, _d;
const fallbackLang = 'en';
const selectedLang = supportedLanguages.includes(lang) ? lang : fallbackLang;
const msg = (_b = (_a = messages[selectedLang]) === null || _a === void 0 ? void 0 : _a[category]) === null || _b === void 0 ? void 0 : _b[key];
if (!msg) {
console.warn(`[i18n] Missing key '${key}' in ${selectedLang}/${category}`);
}
return msg || ((_d = (_c = messages[fallbackLang]) === null || _c === void 0 ? void 0 : _c[category]) === null || _d === void 0 ? void 0 : _d[key]) || key;
};
exports.getMessage = getMessage;
const getSupportedLanguages = () => supportedLanguages;
exports.getSupportedLanguages = getSupportedLanguages;