1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | 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;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | var HttpFetchService_1;
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const common_1 = require("@nestjs/common");
|
17 | const http_exception_1 = require("./http.exception");
|
18 | const http_helper_1 = require("./http.helper");
|
19 | const http_constants_1 = require("./http.constants");
|
20 | const http_interface_1 = require("./http.interface");
|
21 | let HttpFetchService = HttpFetchService_1 = class HttpFetchService {
|
22 | constructor(httpService, httpOption) {
|
23 | this.httpService = httpService;
|
24 | this.httpOption = httpOption;
|
25 | const { logger, requestInterceptors = [], responseInterceptors = [] } = this.httpOption;
|
26 | this.logger = logger || new common_1.Logger(HttpFetchService_1.name);
|
27 | requestInterceptors.forEach(({ onFulfilled, onRejected }) => {
|
28 | this.httpService.axiosRef.interceptors.request.use(onFulfilled, onRejected);
|
29 | });
|
30 | this.httpService.axiosRef.interceptors.response.use(response => response, error => {
|
31 | const { response = {}, config = {} } = error;
|
32 | this.logger.error(`Api request failed -> status: ${response.status}; url: ${config.url}; params: ${JSON.stringify(config.params || {})}; body: ${JSON.stringify(config.data || {})}; response: ${JSON.stringify(response.data)}`);
|
33 | throw error;
|
34 | });
|
35 | responseInterceptors.forEach(({ onFulfilled, onRejected }) => {
|
36 | this.httpService.axiosRef.interceptors.response.use(onFulfilled, onRejected);
|
37 | });
|
38 | }
|
39 | async request(url, config = {}) {
|
40 | config = config || {};
|
41 | const protocol = this.httpOption.protocol || 'http';
|
42 | if (url.indexOf(protocol) !== 0) {
|
43 | url = `${protocol}://${url}`;
|
44 | }
|
45 | switch (this.httpOption.parameterStyle) {
|
46 | case http_interface_1.ParameterStyle.CAMEL_TO_SNAKE:
|
47 | if (config.data) {
|
48 | config.data = http_helper_1.HttpHepler.humpToSnake(config.data);
|
49 | }
|
50 | if (config.params) {
|
51 | config.params = http_helper_1.HttpHepler.humpToSnake(config.params);
|
52 | }
|
53 | break;
|
54 | case http_interface_1.ParameterStyle.SNAKE_TO_CAMEL:
|
55 | if (config.data) {
|
56 | config.data = http_helper_1.HttpHepler.snakeToHump(config.data);
|
57 | }
|
58 | if (config.params) {
|
59 | config.params = http_helper_1.HttpHepler.snakeToHump(config.params);
|
60 | }
|
61 | break;
|
62 | default:
|
63 | break;
|
64 | }
|
65 | let response, resp;
|
66 | const stringParams = JSON.stringify(config.params || {});
|
67 | const stringBody = JSON.stringify(config.data || {});
|
68 | const start = new Date().getTime();
|
69 | try {
|
70 | resp = await this.httpService.axiosRef.request(Object.assign({}, this.httpOption, config, { url }));
|
71 | url = resp.config.url;
|
72 | this.logger.log(`Api request -> (${new Date().getTime() -
|
73 | start}ms) url: ${url}; params: ${stringParams}; body: ${stringBody}`);
|
74 | const data = resp.data;
|
75 | let content = data.data;
|
76 | if (this.httpOption.parameterStyle === http_interface_1.ParameterStyle.CAMEL_TO_SNAKE) {
|
77 | content = http_helper_1.HttpHepler.snakeToHump(data.data || {});
|
78 | }
|
79 | else if (this.httpOption.parameterStyle === http_interface_1.ParameterStyle.SNAKE_TO_CAMEL) {
|
80 | content = http_helper_1.HttpHepler.humpToSnake(data.data || {});
|
81 | }
|
82 | response = {
|
83 | code: data.code || 0,
|
84 | msg: data.msg || '',
|
85 | data: content,
|
86 | success: data.code === 600,
|
87 | };
|
88 | }
|
89 | catch (err) {
|
90 | this.logger.error(err.message);
|
91 | if (!err.response.data) {
|
92 | err.response.data = {};
|
93 | }
|
94 | if (!err.response.data.code && !err.response.data.data) {
|
95 | throw new http_exception_1.HttpRequestApiException({
|
96 | msg: err.message,
|
97 | });
|
98 | }
|
99 | response = {
|
100 | code: err.response.data.code || 0,
|
101 | msg: err.response.data.msg || err.message,
|
102 | data: err.response.data.data,
|
103 | success: false,
|
104 | };
|
105 | }
|
106 | return response;
|
107 | }
|
108 | async post(url, config) {
|
109 | return this.request(url, Object.assign({}, config, { method: 'post' }));
|
110 | }
|
111 | async get(url, config) {
|
112 | return this.request(url, Object.assign({}, config, { method: 'get' }));
|
113 | }
|
114 | async put(url, config) {
|
115 | return this.request(url, Object.assign({}, config, { method: 'put' }));
|
116 | }
|
117 | async delete(url, config) {
|
118 | return this.request(url, Object.assign({}, config, { method: 'delete' }));
|
119 | }
|
120 | async patch(url, config) {
|
121 | return this.request(url, Object.assign({}, config, { method: 'patch' }));
|
122 | }
|
123 | async head(url, config) {
|
124 | return this.request(url, Object.assign({}, config, { method: 'head' }));
|
125 | }
|
126 | };
|
127 | HttpFetchService = HttpFetchService_1 = __decorate([
|
128 | common_1.Injectable(),
|
129 | __param(0, common_1.Inject(common_1.HttpService)),
|
130 | __param(1, common_1.Inject(http_constants_1.HTTP_OPTION)),
|
131 | __metadata("design:paramtypes", [common_1.HttpService, Object])
|
132 | ], HttpFetchService);
|
133 | exports.HttpFetchService = HttpFetchService;
|
134 |
|
\ | No newline at end of file |