@nodeflip/nest-axios-http
Version:
A NestJS module for simplified HTTP requests using Axios with dynamic configuration, logging, and interceptor support.
82 lines • 3.56 kB
JavaScript
"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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var HttpService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpService = void 0;
const axios_1 = __importDefault(require("axios"));
const common_1 = require("@nestjs/common");
let HttpService = HttpService_1 = class HttpService {
axiosInstance;
logger;
constructor(options) {
const { logger, serviceName, config } = options;
const { enableLogging, ...axiosOptions } = config;
this.axiosInstance = axios_1.default.create(axiosOptions);
if (enableLogging) {
this.axiosInstance.interceptors.request.use(axiosOptions.onRequest || this.handleRequest.bind(this));
this.axiosInstance.interceptors.response.use(axiosOptions.onResponse || this.handleResponse.bind(this), axiosOptions.onError || this.handleErrorResponse.bind(this));
}
if (logger) {
this.logger = logger;
}
else {
this.logger = new common_1.Logger(serviceName || HttpService_1.name);
}
}
handleRequest(config) {
this.logger.log(`HTTP Request: ${config.method?.toUpperCase()} ${config.baseURL}${config.url}`);
return config;
}
handleResponse(response) {
this.logger.log(`HTTP Response: ${response.status} ${response.config.method?.toUpperCase()} ${response.config.baseURL}${response.config.url}`);
return response;
}
handleErrorResponse(error) {
this.logger.error(`HTTP Error: ${error.message}`, error.stack);
return Promise.reject(error);
}
async get(url, config) {
return this.axiosInstance.get(url, config);
}
async post(url, data, config) {
return this.axiosInstance.post(url, data, config);
}
async put(url, data, config) {
return this.axiosInstance.put(url, data, config);
}
async patch(url, data, config) {
return this.axiosInstance.patch(url, data, config);
}
async delete(url, config) {
return this.axiosInstance.delete(url, config);
}
async options(url, config) {
return this.axiosInstance.options(url, config);
}
async postForm(url, data, config) {
return this.axiosInstance.postForm(url, data, config);
}
async putForm(url, data, config) {
return this.axiosInstance.putForm(url, data, config);
}
async patchForm(url, data, config) {
return this.axiosInstance.patchForm(url, data, config);
}
};
HttpService = HttpService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [Object])
], HttpService);
exports.HttpService = HttpService;
//# sourceMappingURL=http.service.js.map