UNPKG

naga-audit-service

Version:

A comprehensive audit service library for NestJS applications with MongoDB support

139 lines 5.95 kB
"use strict"; 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 __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClientService = void 0; const common_1 = require("@nestjs/common"); const axios_1 = require("@nestjs/axios"); const config_1 = require("@nestjs/config"); const rxjs_1 = require("rxjs"); const http_headers_constants_1 = require("../constants/http-headers.constants"); const custom_error_exception_1 = require("../errors/custom-error.exception"); let HttpClientService = class HttpClientService { constructor(httpService, req, configService) { this.httpService = httpService; this.req = req; this.configService = configService; } getHeaders() { const headers = this.req['headers']; const intereServiceHeaders = { [http_headers_constants_1.HTTP_HEADERS.ACCESS_TOKEN]: headers[http_headers_constants_1.HTTP_HEADERS.ACCESS_TOKEN], [http_headers_constants_1.HTTP_HEADERS.ID_TOKEN]: headers[http_headers_constants_1.HTTP_HEADERS.ID_TOKEN], [http_headers_constants_1.HTTP_HEADERS.USER_CURRENT_VIEW]: headers[http_headers_constants_1.HTTP_HEADERS.USER_CURRENT_VIEW], }; return intereServiceHeaders; } handleError(error) { if (error.response) { const { status = 500, data = {}, message: errorMessage } = error.response; const message = errorMessage || data.message || 'An unexpected error occurred'; const errorCode = data.errorCode || 'ERR404'; throw new custom_error_exception_1.ErrorException(errorCode, message, status); } else { throw new custom_error_exception_1.ErrorException(null, 'No response received from the server', 500); } } resolveUrl(serviceKey) { const serviceUrlEnvVar = `${serviceKey}_URL`; const url = this.configService.get(serviceUrlEnvVar); if (!url) { throw new custom_error_exception_1.ErrorException(null, `Service URL for "${serviceKey}" is not defined in configuration. Please set ${serviceUrlEnvVar}`, 500); } return url; } buildUrl(serviceKey, endpoint) { const baseUrl = this.resolveUrl(serviceKey); return endpoint ? `${baseUrl}${endpoint}` : baseUrl; } async get(serviceKey, endpoint, query, skipError = false) { try { const url = this.buildUrl(serviceKey, endpoint); const response$ = this.httpService.get(url, { params: query, headers: this.getHeaders(), }); const response = await (0, rxjs_1.lastValueFrom)(response$); return response.data.data; } catch (error) { console.log(error); if (!skipError) { this.handleError(error); } return null; } } async post(serviceKey, endpoint, body, query) { try { const url = this.buildUrl(serviceKey, endpoint); const response$ = this.httpService.post(url, body, { params: query, headers: this.getHeaders(), }); const response = await (0, rxjs_1.lastValueFrom)(response$); return response.data.data; } catch (error) { this.handleError(error); } } async put(serviceKey, endpoint, body) { try { const url = this.buildUrl(serviceKey, endpoint); const response$ = this.httpService.put(url, body, { headers: this.getHeaders(), }); const response = await (0, rxjs_1.lastValueFrom)(response$); return response.data.data; } catch (error) { this.handleError(error); } } async patch(serviceKey, endpoint, body) { try { const url = this.buildUrl(serviceKey, endpoint); const response$ = this.httpService.patch(url, body, { headers: this.getHeaders(), }); const response = await (0, rxjs_1.lastValueFrom)(response$); return response.data.data; } catch (error) { this.handleError(error); } } async delete(serviceKey, endpoint, query) { try { const url = this.buildUrl(serviceKey, endpoint); const response$ = this.httpService.delete(url, { params: query, headers: this.getHeaders(), }); const response = await (0, rxjs_1.lastValueFrom)(response$); return response.data.data; } catch (error) { this.handleError(error); } } }; exports.HttpClientService = HttpClientService; exports.HttpClientService = HttpClientService = __decorate([ (0, common_1.Injectable)(), __param(1, (0, common_1.Inject)('REQUEST')), __metadata("design:paramtypes", [axios_1.HttpService, Object, config_1.ConfigService]) ], HttpClientService); //# sourceMappingURL=http-client.service.js.map