UNPKG

@sentzunhat/zacatl

Version:

A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.

134 lines 6.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceType = exports.Service = void 0; const dependency_injection_1 = require("../dependency-injection/index.js"); const error_1 = require("../error/index.js"); const localization_1 = require("../localization/index.js"); const layers_1 = require("./layers"); const platforms_1 = require("./platforms/platforms"); const port_1 = require("./platforms/server/database/port"); const types_1 = require("./types"); Object.defineProperty(exports, "ServiceType", { enumerable: true, get: function () { return types_1.ServiceType; } }); class Service { config; platforms; constructor(config) { (0, localization_1.configureI18nNode)(config.localization); this.validateConfig(config); this.config = config; const { layers, platforms, run } = config; if (platforms?.server?.databases != null) { for (const dbConfig of platforms.server.databases) { if (dbConfig.vendor === port_1.DatabaseVendor.MONGOOSE && dbConfig.instance != null) { (0, dependency_injection_1.getContainer)().register(dbConfig.instance.constructor, { useValue: dbConfig.instance, }); } else if (dbConfig.vendor === port_1.DatabaseVendor.SEQUELIZE && dbConfig.instance != null) { (0, dependency_injection_1.getContainer)().register(dbConfig.instance.constructor, { useValue: dbConfig.instance, }); } } } if (layers != null) { new layers_1.Layers(layers); } if (platforms != null) { this.platforms = new platforms_1.Platforms(platforms); } if (run?.auto === true) { this.start().catch((err) => { throw new error_1.InternalServerError({ message: 'Failed to start service automatically', reason: 'Service auto-start encountered an error during initialization', component: 'Service', operation: 'constructor', error: err instanceof Error ? err : undefined, }); }); } } validateConfig(config) { const { type, layers, platforms } = config; const configType = type ?? undefined; if (configType == null) { throw new error_1.InternalServerError({ message: "Service configuration must specify a 'type' field", reason: 'Missing service type', component: 'Service', operation: 'constructor', }); } switch (configType) { case types_1.ServiceType.SERVER: if (platforms?.server == null) { throw new error_1.InternalServerError({ message: "ServiceType.SERVER requires 'platforms.server' configuration", reason: 'Server platform configuration is missing', component: 'Service', operation: 'validateConfig', metadata: { serviceType: configType }, }); } if (layers?.application == null || layers.application.entryPoints?.rest == null) { throw new error_1.InternalServerError({ message: "ServiceType.SERVER requires 'layers.application.entryPoints.rest' configuration", reason: 'REST entry points configuration is missing', component: 'Service', operation: 'validateConfig', metadata: { serviceType: configType }, }); } break; case types_1.ServiceType.CLI: if (platforms?.cli == null) { throw new error_1.InternalServerError({ message: "ServiceType.CLI requires 'platforms.cli' configuration", reason: 'CLI platform configuration is missing', component: 'Service', operation: 'validateConfig', metadata: { serviceType: configType }, }); } if (layers?.application == null || layers.application.entryPoints?.cli == null) { throw new error_1.InternalServerError({ message: "ServiceType.CLI requires 'layers.application.entryPoints.cli' configuration", reason: 'CLI entry points configuration is missing', component: 'Service', operation: 'validateConfig', metadata: { serviceType: configType }, }); } break; case types_1.ServiceType.DESKTOP: if (platforms?.desktop == null) { throw new error_1.InternalServerError({ message: "ServiceType.DESKTOP requires 'platforms.desktop' configuration", reason: 'Desktop platform configuration is missing', component: 'Service', operation: 'validateConfig', metadata: { serviceType: configType }, }); } if (layers?.application == null || layers.application.entryPoints?.ipc == null) { throw new error_1.InternalServerError({ message: "ServiceType.DESKTOP requires 'layers.application.entryPoints.ipc' configuration", reason: 'IPC entry points configuration is missing', component: 'Service', operation: 'validateConfig', metadata: { serviceType: configType }, }); } break; } } async start(options) { if (this.config.layers?.application?.entryPoints) { await this.platforms?.registerEntrypoints(this.config.layers?.application?.entryPoints); await this.platforms?.start(options); } } } exports.Service = Service; //# sourceMappingURL=service.js.map