@sinuos/nestjs-notification
Version:
NestJS notification based on channel
95 lines (94 loc) • 4.32 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 __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 NestjsNotificationModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NestjsNotificationModule = void 0;
const common_1 = require("@nestjs/common");
const nestjs_notification_service_1 = require("./nestjs-notification.service");
const constants_1 = require("./constants");
let NestjsNotificationModule = NestjsNotificationModule_1 = class NestjsNotificationModule {
static register(options) {
const notifyProvider = {
provide: constants_1.NESTJS_NOTIFICATION_OPTIONS,
useValue: options,
};
return {
module: NestjsNotificationModule_1,
providers: [nestjs_notification_service_1.NestjsNotificationService, notifyProvider],
exports: [nestjs_notification_service_1.NestjsNotificationService, notifyProvider],
};
}
static registerAsync(options) {
const notifyProvider = {
provide: constants_1.NESTJS_NOTIFICATION_OPTIONS,
useValue: options,
};
return {
module: NestjsNotificationModule_1,
imports: options.imports || [],
providers: [nestjs_notification_service_1.NestjsNotificationService, notifyProvider, ...this.createAsyncProviders(options)],
exports: [nestjs_notification_service_1.NestjsNotificationService, notifyProvider],
};
}
static createAsyncProviders(options) {
if (options.useExisting || options.useFactory) {
return [this.createAsyncConfigProvider(options)];
}
else if (!options.useClass) {
return [
{
provide: constants_1.NESTJS_NOTIFICATION_OPTIONS,
useValue: {},
inject: options.inject || [],
},
];
}
return [
this.createAsyncConfigProvider(options),
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
static createAsyncConfigProvider(options) {
if (options.useFactory) {
return {
provide: constants_1.NESTJS_NOTIFICATION_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
const inject = options.useClass || options.useExisting;
if (!inject) {
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
}
return {
provide: constants_1.NESTJS_NOTIFICATION_OPTIONS,
useFactory(optionsFactory) {
return __awaiter(this, void 0, void 0, function* () {
return optionsFactory.createNestjsNotificationOptions();
});
},
inject: [inject],
};
}
};
NestjsNotificationModule = NestjsNotificationModule_1 = __decorate([
(0, common_1.Module)({})
], NestjsNotificationModule);
exports.NestjsNotificationModule = NestjsNotificationModule;