@digikare/nestjs-azure-appconfig
Version:
An azure app configuration module for nestjs
109 lines (108 loc) • 5.11 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppConfigService = void 0;
const common_1 = require("@nestjs/common");
const constants_1 = require("./constants");
const app_configuration_1 = require("@azure/app-configuration");
const debug_1 = __importDefault(require("debug"));
const debug = debug_1.default('AppConfig:Service');
function getHashKey(key, label) {
return `${key}__#__${label}`;
}
let AppConfigService = class AppConfigService {
constructor(_appConfigClient, _options = {}) {
var _a, _b, _c;
this._appConfigClient = _appConfigClient;
this._options = _options;
this._data = new Map();
this.cache = true;
this.cache = (_c = (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.cache) === null || _b === void 0 ? void 0 : _b.enabled) !== null && _c !== void 0 ? _c : false;
}
get label() {
return this._options.label;
}
get(key, label) {
return __awaiter(this, void 0, void 0, function* () {
try {
const result = yield this.getAsync(key, label);
return JSON.parse(JSON.stringify(result === null || result === void 0 ? void 0 : result.value));
}
catch (err) {
debug(`error`, err);
if (err.statusCode !== 404) {
console.error(err);
}
return undefined;
}
});
}
getLabel(label) {
if (label === undefined) {
return this.label;
}
if (label === '') {
return undefined;
}
return label !== null && label !== void 0 ? label : this.label;
}
getAsync(key, label) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const normalizeLabel = this.getLabel(label);
const hashKey = getHashKey(key, normalizeLabel);
if (this.cache && this._data.has(hashKey)) {
const cacheEntry = this._data.get(hashKey);
if (cacheEntry) {
if (((_a = this._options.cache) === null || _a === void 0 ? void 0 : _a.ttl) === undefined) {
debug(`[key=${key}] return cached value`);
return cacheEntry;
}
const ttl = this._options.cache.ttl;
if (ttl > 0 && (Date.now() - cacheEntry.time) / 1000 < ttl) {
debug(`[key=${key}] return cached value`);
return cacheEntry;
}
}
}
const result = yield this._appConfigClient.getConfigurationSetting({
key,
label: normalizeLabel,
});
if (this.cache) {
this._data.set(hashKey, Object.assign(Object.assign({}, result), { time: Date.now() }));
}
return result;
});
}
};
AppConfigService = __decorate([
common_1.Injectable(),
__param(0, common_1.Inject(constants_1.APP_CONFIG_INSTANCE)),
__param(1, common_1.Optional()), __param(1, common_1.Inject(constants_1.APP_CONFIG_OPTIONS)),
__metadata("design:paramtypes", [app_configuration_1.AppConfigurationClient, Object])
], AppConfigService);
exports.AppConfigService = AppConfigService;