UNPKG

@rxap/nest-open-api

Version:

This package provides tools and utilities for integrating OpenAPI specifications into NestJS applications. It includes features for handling upstream API requests, managing server configurations, and generating OpenAPI documentation. It also offers interc

77 lines (76 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenApiModuleOptionsLoader = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const nest_utilities_1 = require("@rxap/nest-utilities"); const node_utilities_1 = require("@rxap/node-utilities"); const utilities_1 = require("@rxap/utilities"); const fs_1 = require("fs"); let OpenApiModuleOptionsLoader = class OpenApiModuleOptionsLoader { constructor() { this.environment = nest_utilities_1.RXAP_GLOBAL_STATE.environment; } create() { const openApiServerConfigFilePath = this.config.getOrThrow('OPEN_API_SERVER_CONFIG_FILE_PATH'); const config = {}; if (!this.existsFileWithScope(openApiServerConfigFilePath)) { this.logger.warn(`The open api server config file path "${openApiServerConfigFilePath}" does not exists!`, 'OpenApiModuleOptionsLoader'); } else { let content = this.readFileWithScope(openApiServerConfigFilePath); try { content = this.interpolateEnvVariables(content); } catch (e) { throw new Error(`Could not interpolate env variables in open api server config file "${this.fileNameWithScope(openApiServerConfigFilePath)}": ${e.message}`); } try { config.serverConfig = (0, utilities_1.coerceArray)(JSON.parse(content)); } catch (e) { throw new Error(`Could not parse open api server config file "${this.fileNameWithScope(openApiServerConfigFilePath)}": ${e.message}`); } } return config; } existsFileWithScope(filePath) { return (0, node_utilities_1.ExistsFileWithScope)(filePath, this.environment?.name, this.environment?.production); } readFileWithScope(filePath) { return (0, node_utilities_1.ReadFileWithScope)(filePath, this.environment?.name, this.environment?.production, 'utf-8', this.logger); } fileNameWithScope(filePath) { const filename = (0, node_utilities_1.InjectScopeInFilePath)(filePath, this.environment?.name); if ((0, fs_1.existsSync)(filename)) { return filename; } if (this.environment?.production) { return (0, node_utilities_1.InjectScopeInFilePath)(filePath, 'production'); } else { return filePath; } } interpolateEnvVariables(value) { return value.replace(/\${(.+?)}/g, (_, key) => this.config.getOrThrow(key)); } }; exports.OpenApiModuleOptionsLoader = OpenApiModuleOptionsLoader; tslib_1.__decorate([ (0, common_1.Inject)(config_1.ConfigService), tslib_1.__metadata("design:type", config_1.ConfigService) ], OpenApiModuleOptionsLoader.prototype, "config", void 0); tslib_1.__decorate([ (0, common_1.Inject)(common_1.Logger), tslib_1.__metadata("design:type", common_1.Logger) ], OpenApiModuleOptionsLoader.prototype, "logger", void 0); tslib_1.__decorate([ (0, common_1.Optional)(), (0, common_1.Inject)(nest_utilities_1.ENVIRONMENT), tslib_1.__metadata("design:type", Object) ], OpenApiModuleOptionsLoader.prototype, "environment", void 0); exports.OpenApiModuleOptionsLoader = OpenApiModuleOptionsLoader = tslib_1.__decorate([ (0, common_1.Injectable)() ], OpenApiModuleOptionsLoader);