nestjs-temporal-core
Version:
Complete NestJS integration for Temporal.io with auto-discovery, declarative scheduling, enhanced monitoring, and enterprise-ready features
231 lines • 10.7 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 TemporalModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemporalModule = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const temporal_service_1 = require("./services/temporal.service");
const constants_1 = require("./constants");
const temporal_discovery_service_1 = require("./services/temporal-discovery.service");
const temporal_client_service_1 = require("./services/temporal-client.service");
const temporal_schedule_service_1 = require("./services/temporal-schedule.service");
const temporal_worker_service_1 = require("./services/temporal-worker.service");
const temporal_metadata_service_1 = require("./services/temporal-metadata.service");
const logger_1 = require("./utils/logger");
const temporal_connection_factory_1 = require("./providers/temporal-connection.factory");
let TemporalModule = TemporalModule_1 = class TemporalModule {
static register(options = {}) {
this.validateOptions(options);
const providers = this.createCoreProviders(options);
return {
module: TemporalModule_1,
imports: [core_1.DiscoveryModule],
providers,
exports: [temporal_service_1.TemporalService, logger_1.TemporalLoggerManager, constants_1.TEMPORAL_MODULE_OPTIONS],
global: options.isGlobal,
};
}
static registerAsync(options) {
this.validateAsyncOptions(options);
const imports = [
core_1.DiscoveryModule,
];
const providers = [];
if (options.imports) {
for (const importModule of options.imports) {
if (typeof importModule === 'function') {
imports.push(importModule);
}
else if (typeof importModule === 'object' && importModule !== null) {
imports.push(importModule);
}
}
}
if (options.useClass) {
providers.push(options.useClass);
}
providers.push(this.createAsyncOptionsProvider(options));
providers.push(...this.createCoreAsyncProviders());
return {
module: TemporalModule_1,
imports,
providers,
exports: [temporal_service_1.TemporalService, logger_1.TemporalLoggerManager, constants_1.TEMPORAL_MODULE_OPTIONS],
global: options.isGlobal,
};
}
static createCoreProviders(options) {
return [
{
provide: constants_1.TEMPORAL_MODULE_OPTIONS,
useValue: options,
},
temporal_connection_factory_1.TemporalConnectionFactory,
{
provide: constants_1.TEMPORAL_CLIENT,
useFactory: async (connectionFactory, temporalOptions) => {
return connectionFactory.createClient(temporalOptions);
},
inject: [temporal_connection_factory_1.TemporalConnectionFactory, constants_1.TEMPORAL_MODULE_OPTIONS],
},
{
provide: constants_1.TEMPORAL_CONNECTION,
useFactory: async (connectionFactory, temporalOptions) => {
return connectionFactory.createWorkerConnection(temporalOptions);
},
inject: [temporal_connection_factory_1.TemporalConnectionFactory, constants_1.TEMPORAL_MODULE_OPTIONS],
},
{
provide: constants_1.ACTIVITY_MODULE_OPTIONS,
useValue: {
activityClasses: options.worker?.activityClasses || [],
},
},
{
provide: logger_1.TemporalLoggerManager,
useFactory: (temporalOptions) => {
const manager = logger_1.TemporalLoggerManager.getInstance();
manager.configure({
enableLogger: temporalOptions.enableLogger,
logLevel: temporalOptions.logLevel,
appName: 'NestJS-Temporal-Core',
});
return manager;
},
inject: [constants_1.TEMPORAL_MODULE_OPTIONS],
},
temporal_client_service_1.TemporalClientService,
temporal_schedule_service_1.TemporalScheduleService,
temporal_discovery_service_1.TemporalDiscoveryService,
temporal_worker_service_1.TemporalWorkerManagerService,
temporal_metadata_service_1.TemporalMetadataAccessor,
temporal_service_1.TemporalService,
];
}
static createCoreAsyncProviders() {
return [
temporal_connection_factory_1.TemporalConnectionFactory,
{
provide: constants_1.TEMPORAL_CLIENT,
useFactory: async (connectionFactory, temporalOptions) => {
return connectionFactory.createClient(temporalOptions);
},
inject: [temporal_connection_factory_1.TemporalConnectionFactory, constants_1.TEMPORAL_MODULE_OPTIONS],
},
{
provide: constants_1.TEMPORAL_CONNECTION,
useFactory: async (connectionFactory, temporalOptions) => {
return connectionFactory.createWorkerConnection(temporalOptions);
},
inject: [temporal_connection_factory_1.TemporalConnectionFactory, constants_1.TEMPORAL_MODULE_OPTIONS],
},
{
provide: constants_1.ACTIVITY_MODULE_OPTIONS,
useFactory: (temporalOptions) => {
return {
activityClasses: temporalOptions.worker?.activityClasses || [],
};
},
inject: [constants_1.TEMPORAL_MODULE_OPTIONS],
},
{
provide: logger_1.TemporalLoggerManager,
useFactory: (temporalOptions) => {
const manager = logger_1.TemporalLoggerManager.getInstance();
manager.configure({
enableLogger: temporalOptions.enableLogger,
logLevel: temporalOptions.logLevel,
appName: 'NestJS-Temporal-Core',
});
return manager;
},
inject: [constants_1.TEMPORAL_MODULE_OPTIONS],
},
temporal_client_service_1.TemporalClientService,
temporal_schedule_service_1.TemporalScheduleService,
temporal_discovery_service_1.TemporalDiscoveryService,
temporal_worker_service_1.TemporalWorkerManagerService,
temporal_metadata_service_1.TemporalMetadataAccessor,
temporal_service_1.TemporalService,
];
}
static createAsyncOptionsProvider(options) {
if (options.useFactory) {
return {
provide: constants_1.TEMPORAL_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
else if (options.useClass) {
return {
provide: constants_1.TEMPORAL_MODULE_OPTIONS,
useFactory: async (optionsFactory) => optionsFactory.createTemporalOptions(),
inject: [options.useClass],
};
}
else if (options.useExisting) {
return {
provide: constants_1.TEMPORAL_MODULE_OPTIONS,
useFactory: async (optionsFactory) => optionsFactory.createTemporalOptions(),
inject: [options.useExisting],
};
}
else {
throw new Error('Invalid async options configuration');
}
}
static validateOptions(options) {
if (!options) {
return;
}
if (options.connection) {
if (!options.connection.address || !options.connection.address.trim()) {
throw new Error('Connection address is required when connection is configured');
}
if (!options.connection.address.includes(':')) {
throw new Error('Connection address must include port (e.g., localhost:7233)');
}
}
if (options.worker) {
const hasWorkflowsPath = Boolean(options.worker.workflowsPath);
const hasWorkflowBundle = Boolean(options.worker.workflowBundle);
if (hasWorkflowsPath && hasWorkflowBundle) {
throw new Error('Worker cannot have both workflowsPath and workflowBundle');
}
if (options.taskQueue && options.taskQueue.trim().length === 0) {
throw new Error('Task queue cannot be empty string');
}
}
if (options.logLevel &&
!['error', 'warn', 'info', 'debug', 'verbose'].includes(options.logLevel)) {
throw new Error(`Invalid log level: ${options.logLevel}`);
}
}
static validateAsyncOptions(options) {
const hasFactory = Boolean(options.useFactory);
const hasClass = Boolean(options.useClass);
const hasExisting = Boolean(options.useExisting);
const configMethods = [hasFactory, hasClass, hasExisting].filter(Boolean).length;
if (configMethods === 0) {
throw new Error('Invalid Temporal module options: Must provide useFactory, useClass, or useExisting');
}
if (configMethods > 1) {
throw new Error('Invalid Temporal module options: Cannot provide multiple configuration methods');
}
if (options.useFactory && options.inject && !Array.isArray(options.inject)) {
throw new Error('inject option must be an array when using useFactory');
}
}
};
exports.TemporalModule = TemporalModule;
exports.TemporalModule = TemporalModule = TemporalModule_1 = __decorate([
(0, common_1.Module)({})
], TemporalModule);
//# sourceMappingURL=temporal.module.js.map