|
1 |
| -"use strict"; |
2 |
| -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
3 |
| - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
4 |
| - return new (P || (P = Promise))(function (resolve, reject) { |
5 |
| - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
6 |
| - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
7 |
| - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
8 |
| - step((generator = generator.apply(thisArg, _arguments || [])).next()); |
9 |
| - }); |
10 |
| -}; |
11 |
| -var __importDefault = (this && this.__importDefault) || function (mod) { |
12 |
| - return (mod && mod.__esModule) ? mod : { "default": mod }; |
13 |
| -}; |
14 |
| -Object.defineProperty(exports, "__esModule", { value: true }); |
15 |
| -exports.Custom = void 0; |
16 |
| -/* global result, CustomConfig */ |
17 |
| -const axios_1 = __importDefault(require("axios")); |
18 |
| -const tool_1 = require("./tool"); |
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var _defineProperty = require("@babel/runtime/helpers/defineProperty"); |
| 4 | + |
| 5 | +Object.defineProperty(exports, '__esModule', { |
| 6 | + value: true |
| 7 | +}); |
| 8 | + |
| 9 | +var axios = require('axios'); |
| 10 | + |
| 11 | +var tool = require('./tool'); |
| 12 | + |
| 13 | +function _interopDefaultLegacy(e) { |
| 14 | + return e && typeof e === 'object' && 'default' in e ? e : { |
| 15 | + 'default': e |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios); |
| 20 | + |
19 | 21 | class Custom {
|
20 |
| - constructor({ url, method, contentType, headers, key, success, proxy }) { |
21 |
| - this.method = 'POST'; |
22 |
| - this.contentType = 'application/json'; |
23 |
| - if (!url && !(key === null || key === void 0 ? void 0 : key.url)) { |
24 |
| - throw new Error('Missing Parameter: url'); |
25 |
| - } |
26 |
| - if (!success && !(key === null || key === void 0 ? void 0 : key.success)) { |
27 |
| - throw new Error('Missing Parameter: success'); |
28 |
| - } |
29 |
| - // @ts-ignore |
30 |
| - this._URL = url || key.url; |
31 |
| - // @ts-ignore |
32 |
| - this.success = success || key.success; |
33 |
| - if (method || (key === null || key === void 0 ? void 0 : key.method)) { |
34 |
| - // @ts-ignore |
35 |
| - this.method = method || key.method; |
36 |
| - } |
37 |
| - if (contentType || (key === null || key === void 0 ? void 0 : key.contentType)) { |
38 |
| - // @ts-ignore |
39 |
| - this.contentType = contentType || key.contentType; |
40 |
| - } |
41 |
| - if (headers || (key === null || key === void 0 ? void 0 : key.headers)) { |
42 |
| - // @ts-ignore |
43 |
| - this._HEADERS = headers || key.headers; |
44 |
| - } |
45 |
| - if (proxy) { |
46 |
| - this.httpsAgent = (0, tool_1.proxy2httpsAgent)(proxy); |
47 |
| - } |
| 22 | + constructor({ |
| 23 | + url, |
| 24 | + method, |
| 25 | + contentType, |
| 26 | + headers, |
| 27 | + key, |
| 28 | + success, |
| 29 | + proxy |
| 30 | + }) { |
| 31 | + _defineProperty(this, "_URL", void 0); |
| 32 | + |
| 33 | + _defineProperty(this, "httpsAgent", void 0); |
| 34 | + |
| 35 | + _defineProperty(this, "method", 'POST'); |
| 36 | + |
| 37 | + _defineProperty(this, "contentType", 'application/json'); |
| 38 | + |
| 39 | + _defineProperty(this, "_HEADERS", void 0); |
| 40 | + |
| 41 | + _defineProperty(this, "success", void 0); |
| 42 | + |
| 43 | + if (!url && !(key !== null && key !== void 0 && key.url)) { |
| 44 | + throw new Error('Missing Parameter: url'); |
48 | 45 | }
|
49 |
| - send(sendOptions) { |
50 |
| - var _a, _b; |
51 |
| - return __awaiter(this, void 0, void 0, function* () { |
52 |
| - const axiosOptions = { |
53 |
| - url: this._URL, |
54 |
| - method: this.method, |
55 |
| - headers: { |
56 |
| - 'Content-type': 'application/json' |
57 |
| - }, |
58 |
| - data: sendOptions.extraMessage || sendOptions |
59 |
| - }; |
60 |
| - if (this._HEADERS) { |
61 |
| - axiosOptions.headers = this._HEADERS; |
62 |
| - } |
63 |
| - if (this.contentType) { |
64 |
| - axiosOptions.headers['Content-type'] = this.contentType; |
65 |
| - } |
66 |
| - if (this.httpsAgent) { |
67 |
| - axiosOptions.httpsAgent = this.httpsAgent; |
68 |
| - } |
69 |
| - if (((_a = axiosOptions.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === 'POST' && |
70 |
| - axiosOptions.headers['Content-type'] === 'application/x-www-form-urlencoded') { |
71 |
| - axiosOptions.data = (0, tool_1.queryStringify)(sendOptions); |
72 |
| - } |
73 |
| - if (((_b = axiosOptions.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()) === 'GET') { |
74 |
| - axiosOptions.data = null; |
75 |
| - axiosOptions.url += (0, tool_1.queryStringify)(sendOptions); |
76 |
| - } |
77 |
| - return (0, axios_1.default)(axiosOptions).then((response) => { |
78 |
| - if (response.data) { |
79 |
| - let variate = response.data; |
80 |
| - this.success.key.split('.').forEach((key, index) => { |
81 |
| - if (index === 0) { |
82 |
| - return; |
83 |
| - } |
84 |
| - variate = variate === null || variate === void 0 ? void 0 : variate[key]; |
85 |
| - }); |
86 |
| - if (variate === this.success.value) { |
87 |
| - return { |
88 |
| - status: 200, |
89 |
| - statusText: 'Success', |
90 |
| - extraMessage: response |
91 |
| - }; |
92 |
| - } |
93 |
| - return { |
94 |
| - status: 100, |
95 |
| - statusText: 'Error', |
96 |
| - extraMessage: response |
97 |
| - }; |
98 |
| - } |
99 |
| - return { |
100 |
| - status: 101, |
101 |
| - statusText: 'No Response Data', |
102 |
| - extraMessage: response |
103 |
| - }; |
104 |
| - }).catch((error) => ({ |
105 |
| - status: 102, |
106 |
| - statusText: 'Request Error', |
107 |
| - extraMessage: error |
108 |
| - })); |
109 |
| - }); |
| 46 | + |
| 47 | + if (!success && !(key !== null && key !== void 0 && key.success)) { |
| 48 | + throw new Error('Missing Parameter: success'); |
| 49 | + } // @ts-ignore |
| 50 | + |
| 51 | + |
| 52 | + this._URL = url || key.url; // @ts-ignore |
| 53 | + |
| 54 | + this.success = success || key.success; |
| 55 | + |
| 56 | + if (method || key !== null && key !== void 0 && key.method) { |
| 57 | + // @ts-ignore |
| 58 | + this.method = method || key.method; |
| 59 | + } |
| 60 | + |
| 61 | + if (contentType || key !== null && key !== void 0 && key.contentType) { |
| 62 | + // @ts-ignore |
| 63 | + this.contentType = contentType || key.contentType; |
110 | 64 | }
|
| 65 | + |
| 66 | + if (headers || key !== null && key !== void 0 && key.headers) { |
| 67 | + // @ts-ignore |
| 68 | + this._HEADERS = headers || key.headers; |
| 69 | + } |
| 70 | + |
| 71 | + if (proxy) { |
| 72 | + this.httpsAgent = tool.proxy2httpsAgent(proxy); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + async send(sendOptions) { |
| 77 | + var _axiosOptions$method, _axiosOptions$method2; |
| 78 | + |
| 79 | + const axiosOptions = { |
| 80 | + url: this._URL, |
| 81 | + method: this.method, |
| 82 | + headers: { |
| 83 | + 'Content-type': 'application/json' |
| 84 | + }, |
| 85 | + data: sendOptions.extraMessage || sendOptions |
| 86 | + }; |
| 87 | + |
| 88 | + if (this._HEADERS) { |
| 89 | + axiosOptions.headers = this._HEADERS; |
| 90 | + } |
| 91 | + |
| 92 | + if (this.contentType) { |
| 93 | + axiosOptions.headers['Content-type'] = this.contentType; |
| 94 | + } |
| 95 | + |
| 96 | + if (this.httpsAgent) { |
| 97 | + axiosOptions.httpsAgent = this.httpsAgent; |
| 98 | + } |
| 99 | + |
| 100 | + if (((_axiosOptions$method = axiosOptions.method) === null || _axiosOptions$method === void 0 ? void 0 : _axiosOptions$method.toUpperCase()) === 'POST' && axiosOptions.headers['Content-type'] === 'application/x-www-form-urlencoded') { |
| 101 | + axiosOptions.data = tool.queryStringify(sendOptions); |
| 102 | + } |
| 103 | + |
| 104 | + if (((_axiosOptions$method2 = axiosOptions.method) === null || _axiosOptions$method2 === void 0 ? void 0 : _axiosOptions$method2.toUpperCase()) === 'GET') { |
| 105 | + axiosOptions.data = null; |
| 106 | + axiosOptions.url += tool.queryStringify(sendOptions); |
| 107 | + } |
| 108 | + |
| 109 | + return axios__default["default"](axiosOptions).then(response => { |
| 110 | + if (response.data) { |
| 111 | + let variate = response.data; |
| 112 | + this.success.key.split('.').forEach((key, index) => { |
| 113 | + var _variate; |
| 114 | + |
| 115 | + if (index === 0) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + variate = (_variate = variate) === null || _variate === void 0 ? void 0 : _variate[key]; |
| 120 | + }); |
| 121 | + |
| 122 | + if (variate === this.success.value) { |
| 123 | + return { |
| 124 | + status: 200, |
| 125 | + statusText: 'Success', |
| 126 | + extraMessage: response |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + return { |
| 131 | + status: 100, |
| 132 | + statusText: 'Error', |
| 133 | + extraMessage: response |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + return { |
| 138 | + status: 101, |
| 139 | + statusText: 'No Response Data', |
| 140 | + extraMessage: response |
| 141 | + }; |
| 142 | + }).catch(error => ({ |
| 143 | + status: 102, |
| 144 | + statusText: 'Request Error', |
| 145 | + extraMessage: error |
| 146 | + })); |
| 147 | + } |
| 148 | + |
111 | 149 | }
|
| 150 | + |
112 | 151 | exports.Custom = Custom;
|
0 commit comments