@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
81 lines (80 loc) • 3.82 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenApiConfigService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const utilities_1 = require("@rxap/utilities");
const tokens_1 = require("./tokens");
let OpenApiConfigService = class OpenApiConfigService {
constructor(inject_serverConfig, inject_upstreamInterceptor, logger) {
this.inject_serverConfig = inject_serverConfig;
this.inject_upstreamInterceptor = inject_upstreamInterceptor;
this.logger = logger;
this.serverConfig = (0, utilities_1.coerceArray)(this.inject_serverConfig);
if (this.serverConfig.length) {
this.logger.debug(`Server config: ${JSON.stringify(this.serverConfig)}`, 'OpenApiConfigService');
}
else {
this.logger.debug('No server config provided', 'OpenApiConfigService');
}
this.upstreamInterceptor = this.createUpstreamInterceptorMap((0, utilities_1.coerceArray)(this.inject_upstreamInterceptor));
}
getBaseUrl(serverId) {
const config = this.serverConfig.find(c => c.id === serverId);
if (!config) {
this.logger.error(`Could not find server config for: '${serverId}'`);
this.logger.verbose(`Available server configs: ${JSON.stringify(this.serverConfig.map(sc => sc.id))}`);
throw new Error(`Could not find server config for: '${serverId}'`);
}
return config.url;
}
buildUrl(path, serverId) {
if (serverId) {
return this.getBaseUrl(serverId) + path;
}
return path;
}
getInterceptors(serverId) {
const specificInterceptor = this.upstreamInterceptor[serverId] ?? [];
const defaultInterceptor = this.upstreamInterceptor['__default__'] ?? [];
return [...specificInterceptor, ...defaultInterceptor];
}
addUpstreamInterceptor(interceptor) {
const serverId = Reflect.getMetadata(tokens_1.OPEN_API_SERVER_ID_META_DATA_KEY, interceptor.constructor);
if (!serverId || typeof serverId !== 'string') {
throw new common_1.InternalServerErrorException('Ensure the OpenApiUpstreamInterceptor has the @OpenApiServerId decorator');
}
if (!this.upstreamInterceptor[serverId]) {
this.upstreamInterceptor[serverId] = [];
}
this.upstreamInterceptor[serverId].push(interceptor);
}
addServerConfig(config) {
if (this.serverConfig.some(sc => sc.id === config.id)) {
throw new Error(`Server config with id '${config.id}' already exists`);
}
this.serverConfig.push(config);
}
createUpstreamInterceptorMap(upstreamInterceptor) {
const map = {};
for (const interceptor of upstreamInterceptor) {
const serverId = Reflect.getMetadata(tokens_1.OPEN_API_SERVER_ID_META_DATA_KEY, interceptor.constructor);
if (!serverId || typeof serverId !== 'string') {
throw new common_1.InternalServerErrorException('Ensure the OpenApiUpstreamInterceptor has the @OpenApiServerId decorator');
}
if (!map[serverId]) {
map[serverId] = [];
}
map[serverId].push(interceptor);
}
return map;
}
};
exports.OpenApiConfigService = OpenApiConfigService;
exports.OpenApiConfigService = OpenApiConfigService = tslib_1.__decorate([
(0, common_1.Injectable)(),
tslib_1.__param(0, (0, common_1.Inject)(tokens_1.OPEN_API_SERVER_CONFIG)),
tslib_1.__param(1, (0, common_1.Inject)(tokens_1.OPEN_API_UPSTREAM_INTERCEPTOR)),
tslib_1.__param(2, (0, common_1.Inject)(common_1.Logger)),
tslib_1.__metadata("design:paramtypes", [Object, Object, common_1.Logger])
], OpenApiConfigService);
;