UNPKG

2.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.HttpService = void 0;
4const tslib_1 = require("tslib");
5const axios_1 = require("axios");
6const rxjs_1 = require("rxjs");
7const decorators_1 = require("../decorators");
8const services_1 = require("../services");
9const http_constants_1 = require("./http.constants");
10/**
11 * @deprecated "HttpModule" (from the "@nestjs/common" package) is deprecated and will be removed in the next major release. Please, use the "@nestjs/axios" package instead.
12 */
13let HttpService = class HttpService {
14 constructor(instance = axios_1.default) {
15 this.instance = instance;
16 this.logger = new services_1.Logger(HttpService.name);
17 this.logger.warn('DEPRECATED! "HttpModule" (from the "@nestjs/common" package) is deprecated and will be removed in the next major release. Please, use the "@nestjs/axios" package instead.');
18 }
19 request(config) {
20 return this.makeObservable(this.instance.request, config);
21 }
22 get(url, config) {
23 return this.makeObservable(this.instance.get, url, config);
24 }
25 delete(url, config) {
26 return this.makeObservable(this.instance.delete, url, config);
27 }
28 head(url, config) {
29 return this.makeObservable(this.instance.head, url, config);
30 }
31 post(url, data, config) {
32 return this.makeObservable(this.instance.post, url, data, config);
33 }
34 put(url, data, config) {
35 return this.makeObservable(this.instance.put, url, data, config);
36 }
37 patch(url, data, config) {
38 return this.makeObservable(this.instance.patch, url, data, config);
39 }
40 get axiosRef() {
41 return this.instance;
42 }
43 makeObservable(axios, ...args) {
44 return new rxjs_1.Observable(subscriber => {
45 const config = Object.assign({}, (args[args.length - 1] || {}));
46 let cancelSource;
47 if (!config.cancelToken) {
48 cancelSource = axios_1.default.CancelToken.source();
49 config.cancelToken = cancelSource.token;
50 }
51 axios(...args)
52 .then(res => {
53 subscriber.next(res);
54 subscriber.complete();
55 })
56 .catch(err => {
57 subscriber.error(err);
58 });
59 return () => {
60 if (config.responseType === 'stream') {
61 return;
62 }
63 if (cancelSource) {
64 cancelSource.cancel();
65 }
66 };
67 });
68 }
69};
70HttpService = tslib_1.__decorate([
71 tslib_1.__param(0, decorators_1.Inject(http_constants_1.AXIOS_INSTANCE_TOKEN)),
72 tslib_1.__metadata("design:paramtypes", [Function])
73], HttpService);
74exports.HttpService = HttpService;