@pdg/api 1.0.23 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +65 -158
- package/dist/index.js +65 -158
- package/package.json +15 -7
package/dist/index.esm.js
CHANGED
|
@@ -1,164 +1,78 @@
|
|
|
1
|
-
import axios from'axios'
|
|
2
|
-
Copyright (c) Microsoft Corporation.
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
-
|
|
17
|
-
var extendStatics = function(d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function __extends(d, b) {
|
|
25
|
-
if (typeof b !== "function" && b !== null)
|
|
26
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
-
extendStatics(d, b);
|
|
28
|
-
function __() { this.constructor = d; }
|
|
29
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var __assign = function() {
|
|
33
|
-
__assign = Object.assign || function __assign(t) {
|
|
34
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
35
|
-
s = arguments[i];
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
37
|
-
}
|
|
38
|
-
return t;
|
|
39
|
-
};
|
|
40
|
-
return __assign.apply(this, arguments);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
44
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
45
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
46
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
47
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
48
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
49
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function __generator(thisArg, body) {
|
|
54
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
55
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
56
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
57
|
-
function step(op) {
|
|
58
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
59
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
60
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
61
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
62
|
-
switch (op[0]) {
|
|
63
|
-
case 0: case 1: t = op; break;
|
|
64
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
65
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
66
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
67
|
-
default:
|
|
68
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
69
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
70
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
71
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
72
|
-
if (t[2]) _.ops.pop();
|
|
73
|
-
_.trys.pop(); continue;
|
|
74
|
-
}
|
|
75
|
-
op = body.call(thisArg, _);
|
|
76
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
77
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
82
|
-
var e = new Error(message);
|
|
83
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
84
|
-
};/********************************************************************************************************************
|
|
1
|
+
import axios from'axios';/********************************************************************************************************************
|
|
85
2
|
* ApiError
|
|
86
3
|
* ******************************************************************************************************************/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
_this.code = code;
|
|
93
|
-
return _this;
|
|
4
|
+
class ApiError extends Error {
|
|
5
|
+
constructor(message, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.isAxiosError = false;
|
|
8
|
+
this.code = code;
|
|
94
9
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
10
|
+
}var _a;
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
12
|
+
const AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
13
|
+
class Api {
|
|
99
14
|
/********************************************************************************************************************
|
|
100
15
|
* constructor
|
|
101
16
|
* ******************************************************************************************************************/
|
|
102
|
-
|
|
103
|
-
var _this = this;
|
|
17
|
+
constructor(option) {
|
|
104
18
|
/********************************************************************************************************************
|
|
105
19
|
* run
|
|
106
20
|
* ******************************************************************************************************************/
|
|
107
|
-
this.run =
|
|
108
|
-
return new Promise(
|
|
21
|
+
this.run = (method, path, data, option) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
109
23
|
var _a;
|
|
110
|
-
|
|
24
|
+
const headers = Object.assign({}, this.option.headers);
|
|
111
25
|
if (typeof window !== 'undefined') {
|
|
112
26
|
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
113
27
|
headers['X-Referer'] = window.location.href;
|
|
114
28
|
}
|
|
115
29
|
}
|
|
116
|
-
|
|
117
|
-
method
|
|
118
|
-
withCredentials:
|
|
119
|
-
headers
|
|
30
|
+
const requestConfig = {
|
|
31
|
+
method,
|
|
32
|
+
withCredentials: this.option.withCredentials,
|
|
33
|
+
headers,
|
|
120
34
|
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
121
35
|
};
|
|
122
36
|
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
123
37
|
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
124
38
|
}
|
|
125
|
-
requestConfig.url = urlJoin(
|
|
39
|
+
requestConfig.url = urlJoin(this.option.baseUrl, path.replace(/\./g, '/'));
|
|
126
40
|
if (data) {
|
|
127
41
|
if (method === 'get') {
|
|
128
42
|
if (notEmpty(data)) {
|
|
129
|
-
|
|
130
|
-
finalData[
|
|
131
|
-
for (
|
|
43
|
+
const finalData = {};
|
|
44
|
+
finalData[this.option.timeParamName] = new Date().getTime();
|
|
45
|
+
for (const key in data) {
|
|
132
46
|
if (data[key] != null) {
|
|
133
47
|
finalData[key] = data[key];
|
|
134
48
|
}
|
|
135
49
|
}
|
|
136
|
-
requestConfig.url +=
|
|
50
|
+
requestConfig.url += `?${new URLSearchParams(finalData).toString()}`;
|
|
137
51
|
}
|
|
138
52
|
}
|
|
139
53
|
else {
|
|
140
54
|
if (data instanceof FormData) {
|
|
141
|
-
data.append(
|
|
55
|
+
data.append(this.option.timeParamName, `${new Date().getTime()}`);
|
|
142
56
|
requestConfig.data = data;
|
|
143
57
|
}
|
|
144
58
|
else {
|
|
145
|
-
|
|
146
|
-
finalData[
|
|
59
|
+
const finalData = Object.assign({}, data);
|
|
60
|
+
finalData[this.option.timeParamName] = new Date().getTime();
|
|
147
61
|
requestConfig.data = finalData;
|
|
148
62
|
}
|
|
149
63
|
}
|
|
150
64
|
}
|
|
151
|
-
|
|
65
|
+
const setErrorInfo = (err, status, response) => {
|
|
152
66
|
err.config = requestConfig;
|
|
153
|
-
err.baseUrl =
|
|
67
|
+
err.baseUrl = this.option.baseUrl;
|
|
154
68
|
err.path = path;
|
|
155
69
|
err.requestData = data;
|
|
156
70
|
err.requestOption = option;
|
|
157
71
|
err.response = response;
|
|
158
72
|
err.status = status;
|
|
159
73
|
};
|
|
160
|
-
|
|
161
|
-
|
|
74
|
+
const fireError = (err) => {
|
|
75
|
+
const apiError = new ApiError();
|
|
162
76
|
if (typeof err === 'object') {
|
|
163
77
|
apiError.message = err.message;
|
|
164
78
|
apiError.code = err.code;
|
|
@@ -170,34 +84,32 @@ var Api = /** @class */ (function () {
|
|
|
170
84
|
else if (err) {
|
|
171
85
|
apiError.message = err.toString();
|
|
172
86
|
}
|
|
173
|
-
if (
|
|
174
|
-
|
|
87
|
+
if (this.option.onError)
|
|
88
|
+
this.option.onError(apiError);
|
|
175
89
|
reject(apiError);
|
|
176
90
|
};
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
requestInterceptor = instance.interceptors.request.use(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
189
|
-
}); });
|
|
91
|
+
const instance = AxiosCreate();
|
|
92
|
+
let requestInterceptor;
|
|
93
|
+
if (this.option.onRequest) {
|
|
94
|
+
requestInterceptor = instance.interceptors.request.use(async (config) => {
|
|
95
|
+
if (this.option.onRequest) {
|
|
96
|
+
return this.option.onRequest(config, this.option.baseUrl, path, data, option);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return config;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
190
102
|
}
|
|
191
103
|
instance
|
|
192
104
|
.request(requestConfig)
|
|
193
|
-
.then(
|
|
194
|
-
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
.then(
|
|
105
|
+
.then((res) => {
|
|
106
|
+
const { data: resData } = res;
|
|
107
|
+
if (this.option.onResponse) {
|
|
108
|
+
this.option.onResponse(res, requestConfig, this.option.baseUrl, path, data, option)
|
|
109
|
+
.then((finalResData) => {
|
|
198
110
|
resolve(finalResData);
|
|
199
111
|
})
|
|
200
|
-
.catch(
|
|
112
|
+
.catch((err) => {
|
|
201
113
|
setErrorInfo(err, res.status, res);
|
|
202
114
|
fireError(err);
|
|
203
115
|
});
|
|
@@ -207,7 +119,7 @@ var Api = /** @class */ (function () {
|
|
|
207
119
|
}
|
|
208
120
|
})
|
|
209
121
|
.catch(fireError)
|
|
210
|
-
.finally(
|
|
122
|
+
.finally(() => {
|
|
211
123
|
if (requestInterceptor) {
|
|
212
124
|
instance.interceptors.request.eject(requestInterceptor);
|
|
213
125
|
}
|
|
@@ -216,25 +128,24 @@ var Api = /** @class */ (function () {
|
|
|
216
128
|
};
|
|
217
129
|
this.option = option;
|
|
218
130
|
}
|
|
219
|
-
|
|
131
|
+
get(path, data, option) {
|
|
220
132
|
return this.run('get', path, data, option);
|
|
221
|
-
}
|
|
222
|
-
|
|
133
|
+
}
|
|
134
|
+
post(path, data, option) {
|
|
223
135
|
return this.run('post', path, data, option);
|
|
224
|
-
}
|
|
225
|
-
|
|
136
|
+
}
|
|
137
|
+
patch(path, data, option) {
|
|
226
138
|
return this.run('patch', path, data, option);
|
|
227
|
-
}
|
|
228
|
-
|
|
139
|
+
}
|
|
140
|
+
delete(path, data, option) {
|
|
229
141
|
return this.run('delete', path, data, option);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
}());
|
|
142
|
+
}
|
|
143
|
+
}
|
|
233
144
|
/********************************************************************************************************************
|
|
234
145
|
* empty
|
|
235
146
|
* ******************************************************************************************************************/
|
|
236
147
|
function empty(v) {
|
|
237
|
-
|
|
148
|
+
let result = false;
|
|
238
149
|
if (v == null) {
|
|
239
150
|
result = true;
|
|
240
151
|
}
|
|
@@ -260,23 +171,19 @@ function notEmpty(v) {
|
|
|
260
171
|
/********************************************************************************************************************
|
|
261
172
|
* urlJoin
|
|
262
173
|
* ******************************************************************************************************************/
|
|
263
|
-
function urlJoin() {
|
|
264
|
-
|
|
265
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
266
|
-
parts[_i] = arguments[_i];
|
|
267
|
-
}
|
|
268
|
-
return parts.reduce(function (acc, part) {
|
|
174
|
+
function urlJoin(...parts) {
|
|
175
|
+
return parts.reduce((acc, part) => {
|
|
269
176
|
if (acc === '') {
|
|
270
177
|
return part;
|
|
271
178
|
}
|
|
272
179
|
else if (part.startsWith('?')) {
|
|
273
|
-
return
|
|
180
|
+
return `${acc}${part}`;
|
|
274
181
|
}
|
|
275
182
|
else if (acc.endsWith('/')) {
|
|
276
|
-
return
|
|
183
|
+
return `${acc}${part.startsWith('/') ? part.substring(1) : part}`;
|
|
277
184
|
}
|
|
278
185
|
else {
|
|
279
|
-
return
|
|
186
|
+
return `${acc}${part.startsWith('/') ? part : `/${part}`}`;
|
|
280
187
|
}
|
|
281
188
|
});
|
|
282
189
|
}export{Api,ApiError};
|
package/dist/index.js
CHANGED
|
@@ -1,164 +1,78 @@
|
|
|
1
|
-
'use strict';var axios=require('axios')
|
|
2
|
-
Copyright (c) Microsoft Corporation.
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
-
|
|
17
|
-
var extendStatics = function(d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function __extends(d, b) {
|
|
25
|
-
if (typeof b !== "function" && b !== null)
|
|
26
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
-
extendStatics(d, b);
|
|
28
|
-
function __() { this.constructor = d; }
|
|
29
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var __assign = function() {
|
|
33
|
-
__assign = Object.assign || function __assign(t) {
|
|
34
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
35
|
-
s = arguments[i];
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
37
|
-
}
|
|
38
|
-
return t;
|
|
39
|
-
};
|
|
40
|
-
return __assign.apply(this, arguments);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
44
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
45
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
46
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
47
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
48
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
49
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function __generator(thisArg, body) {
|
|
54
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
55
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
56
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
57
|
-
function step(op) {
|
|
58
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
59
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
60
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
61
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
62
|
-
switch (op[0]) {
|
|
63
|
-
case 0: case 1: t = op; break;
|
|
64
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
65
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
66
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
67
|
-
default:
|
|
68
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
69
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
70
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
71
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
72
|
-
if (t[2]) _.ops.pop();
|
|
73
|
-
_.trys.pop(); continue;
|
|
74
|
-
}
|
|
75
|
-
op = body.call(thisArg, _);
|
|
76
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
77
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
82
|
-
var e = new Error(message);
|
|
83
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
84
|
-
};/********************************************************************************************************************
|
|
1
|
+
'use strict';var axios=require('axios');/********************************************************************************************************************
|
|
85
2
|
* ApiError
|
|
86
3
|
* ******************************************************************************************************************/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
_this.code = code;
|
|
93
|
-
return _this;
|
|
4
|
+
class ApiError extends Error {
|
|
5
|
+
constructor(message, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.isAxiosError = false;
|
|
8
|
+
this.code = code;
|
|
94
9
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
10
|
+
}var _a;
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
12
|
+
const AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
13
|
+
class Api {
|
|
99
14
|
/********************************************************************************************************************
|
|
100
15
|
* constructor
|
|
101
16
|
* ******************************************************************************************************************/
|
|
102
|
-
|
|
103
|
-
var _this = this;
|
|
17
|
+
constructor(option) {
|
|
104
18
|
/********************************************************************************************************************
|
|
105
19
|
* run
|
|
106
20
|
* ******************************************************************************************************************/
|
|
107
|
-
this.run =
|
|
108
|
-
return new Promise(
|
|
21
|
+
this.run = (method, path, data, option) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
109
23
|
var _a;
|
|
110
|
-
|
|
24
|
+
const headers = Object.assign({}, this.option.headers);
|
|
111
25
|
if (typeof window !== 'undefined') {
|
|
112
26
|
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
113
27
|
headers['X-Referer'] = window.location.href;
|
|
114
28
|
}
|
|
115
29
|
}
|
|
116
|
-
|
|
117
|
-
method
|
|
118
|
-
withCredentials:
|
|
119
|
-
headers
|
|
30
|
+
const requestConfig = {
|
|
31
|
+
method,
|
|
32
|
+
withCredentials: this.option.withCredentials,
|
|
33
|
+
headers,
|
|
120
34
|
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
121
35
|
};
|
|
122
36
|
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
123
37
|
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
124
38
|
}
|
|
125
|
-
requestConfig.url = urlJoin(
|
|
39
|
+
requestConfig.url = urlJoin(this.option.baseUrl, path.replace(/\./g, '/'));
|
|
126
40
|
if (data) {
|
|
127
41
|
if (method === 'get') {
|
|
128
42
|
if (notEmpty(data)) {
|
|
129
|
-
|
|
130
|
-
finalData[
|
|
131
|
-
for (
|
|
43
|
+
const finalData = {};
|
|
44
|
+
finalData[this.option.timeParamName] = new Date().getTime();
|
|
45
|
+
for (const key in data) {
|
|
132
46
|
if (data[key] != null) {
|
|
133
47
|
finalData[key] = data[key];
|
|
134
48
|
}
|
|
135
49
|
}
|
|
136
|
-
requestConfig.url +=
|
|
50
|
+
requestConfig.url += `?${new URLSearchParams(finalData).toString()}`;
|
|
137
51
|
}
|
|
138
52
|
}
|
|
139
53
|
else {
|
|
140
54
|
if (data instanceof FormData) {
|
|
141
|
-
data.append(
|
|
55
|
+
data.append(this.option.timeParamName, `${new Date().getTime()}`);
|
|
142
56
|
requestConfig.data = data;
|
|
143
57
|
}
|
|
144
58
|
else {
|
|
145
|
-
|
|
146
|
-
finalData[
|
|
59
|
+
const finalData = Object.assign({}, data);
|
|
60
|
+
finalData[this.option.timeParamName] = new Date().getTime();
|
|
147
61
|
requestConfig.data = finalData;
|
|
148
62
|
}
|
|
149
63
|
}
|
|
150
64
|
}
|
|
151
|
-
|
|
65
|
+
const setErrorInfo = (err, status, response) => {
|
|
152
66
|
err.config = requestConfig;
|
|
153
|
-
err.baseUrl =
|
|
67
|
+
err.baseUrl = this.option.baseUrl;
|
|
154
68
|
err.path = path;
|
|
155
69
|
err.requestData = data;
|
|
156
70
|
err.requestOption = option;
|
|
157
71
|
err.response = response;
|
|
158
72
|
err.status = status;
|
|
159
73
|
};
|
|
160
|
-
|
|
161
|
-
|
|
74
|
+
const fireError = (err) => {
|
|
75
|
+
const apiError = new ApiError();
|
|
162
76
|
if (typeof err === 'object') {
|
|
163
77
|
apiError.message = err.message;
|
|
164
78
|
apiError.code = err.code;
|
|
@@ -170,34 +84,32 @@ var Api = /** @class */ (function () {
|
|
|
170
84
|
else if (err) {
|
|
171
85
|
apiError.message = err.toString();
|
|
172
86
|
}
|
|
173
|
-
if (
|
|
174
|
-
|
|
87
|
+
if (this.option.onError)
|
|
88
|
+
this.option.onError(apiError);
|
|
175
89
|
reject(apiError);
|
|
176
90
|
};
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
requestInterceptor = instance.interceptors.request.use(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
189
|
-
}); });
|
|
91
|
+
const instance = AxiosCreate();
|
|
92
|
+
let requestInterceptor;
|
|
93
|
+
if (this.option.onRequest) {
|
|
94
|
+
requestInterceptor = instance.interceptors.request.use(async (config) => {
|
|
95
|
+
if (this.option.onRequest) {
|
|
96
|
+
return this.option.onRequest(config, this.option.baseUrl, path, data, option);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return config;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
190
102
|
}
|
|
191
103
|
instance
|
|
192
104
|
.request(requestConfig)
|
|
193
|
-
.then(
|
|
194
|
-
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
.then(
|
|
105
|
+
.then((res) => {
|
|
106
|
+
const { data: resData } = res;
|
|
107
|
+
if (this.option.onResponse) {
|
|
108
|
+
this.option.onResponse(res, requestConfig, this.option.baseUrl, path, data, option)
|
|
109
|
+
.then((finalResData) => {
|
|
198
110
|
resolve(finalResData);
|
|
199
111
|
})
|
|
200
|
-
.catch(
|
|
112
|
+
.catch((err) => {
|
|
201
113
|
setErrorInfo(err, res.status, res);
|
|
202
114
|
fireError(err);
|
|
203
115
|
});
|
|
@@ -207,7 +119,7 @@ var Api = /** @class */ (function () {
|
|
|
207
119
|
}
|
|
208
120
|
})
|
|
209
121
|
.catch(fireError)
|
|
210
|
-
.finally(
|
|
122
|
+
.finally(() => {
|
|
211
123
|
if (requestInterceptor) {
|
|
212
124
|
instance.interceptors.request.eject(requestInterceptor);
|
|
213
125
|
}
|
|
@@ -216,25 +128,24 @@ var Api = /** @class */ (function () {
|
|
|
216
128
|
};
|
|
217
129
|
this.option = option;
|
|
218
130
|
}
|
|
219
|
-
|
|
131
|
+
get(path, data, option) {
|
|
220
132
|
return this.run('get', path, data, option);
|
|
221
|
-
}
|
|
222
|
-
|
|
133
|
+
}
|
|
134
|
+
post(path, data, option) {
|
|
223
135
|
return this.run('post', path, data, option);
|
|
224
|
-
}
|
|
225
|
-
|
|
136
|
+
}
|
|
137
|
+
patch(path, data, option) {
|
|
226
138
|
return this.run('patch', path, data, option);
|
|
227
|
-
}
|
|
228
|
-
|
|
139
|
+
}
|
|
140
|
+
delete(path, data, option) {
|
|
229
141
|
return this.run('delete', path, data, option);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
}());
|
|
142
|
+
}
|
|
143
|
+
}
|
|
233
144
|
/********************************************************************************************************************
|
|
234
145
|
* empty
|
|
235
146
|
* ******************************************************************************************************************/
|
|
236
147
|
function empty(v) {
|
|
237
|
-
|
|
148
|
+
let result = false;
|
|
238
149
|
if (v == null) {
|
|
239
150
|
result = true;
|
|
240
151
|
}
|
|
@@ -260,23 +171,19 @@ function notEmpty(v) {
|
|
|
260
171
|
/********************************************************************************************************************
|
|
261
172
|
* urlJoin
|
|
262
173
|
* ******************************************************************************************************************/
|
|
263
|
-
function urlJoin() {
|
|
264
|
-
|
|
265
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
266
|
-
parts[_i] = arguments[_i];
|
|
267
|
-
}
|
|
268
|
-
return parts.reduce(function (acc, part) {
|
|
174
|
+
function urlJoin(...parts) {
|
|
175
|
+
return parts.reduce((acc, part) => {
|
|
269
176
|
if (acc === '') {
|
|
270
177
|
return part;
|
|
271
178
|
}
|
|
272
179
|
else if (part.startsWith('?')) {
|
|
273
|
-
return
|
|
180
|
+
return `${acc}${part}`;
|
|
274
181
|
}
|
|
275
182
|
else if (acc.endsWith('/')) {
|
|
276
|
-
return
|
|
183
|
+
return `${acc}${part.startsWith('/') ? part.substring(1) : part}`;
|
|
277
184
|
}
|
|
278
185
|
else {
|
|
279
|
-
return
|
|
186
|
+
return `${acc}${part.startsWith('/') ? part : `/${part}`}`;
|
|
280
187
|
}
|
|
281
188
|
});
|
|
282
189
|
}exports.Api=Api;exports.ApiError=ApiError;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pdg/api",
|
|
3
3
|
"title": "API Module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.24",
|
|
5
5
|
"description": "API Module",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -24,8 +24,13 @@
|
|
|
24
24
|
"test:get": "cd examples && npm run test:get",
|
|
25
25
|
"build": "rollup -c --bundleConfigAsCjs",
|
|
26
26
|
"pub": "npm i && npm run build && npm publish --access=public && rm ./.git/hooks/pre-commit",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"git:commit": "node .git-commit.cjs",
|
|
28
|
+
"git:push": "git push",
|
|
29
|
+
"git:commit:push": "npm run git:commit && npm run git:push",
|
|
30
|
+
"reset:gitignore": "git rm -r --cached . && git add .",
|
|
31
|
+
"reinstall": "npm run reinstall:module",
|
|
32
|
+
"reinstall:module": "rm -rf node_modules && rm -f package-lock.json && npm i",
|
|
33
|
+
"lint": "eslint './src/**/*.ts'"
|
|
29
34
|
},
|
|
30
35
|
"author": "YOUNG CHUL PARK",
|
|
31
36
|
"license": "MIT",
|
|
@@ -36,24 +41,27 @@
|
|
|
36
41
|
"javascript"
|
|
37
42
|
],
|
|
38
43
|
"dependencies": {
|
|
39
|
-
"axios": "^1.
|
|
44
|
+
"axios": "^1.13.2"
|
|
40
45
|
},
|
|
41
46
|
"devDependencies": {
|
|
42
47
|
"@eslint/js": "^9.28.0",
|
|
43
48
|
"@rollup/plugin-commonjs": "^28.0.3",
|
|
44
49
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
50
|
+
"@types/jest": "^29.5.14",
|
|
45
51
|
"@types/node": "^22.15.29",
|
|
46
|
-
"@typescript-eslint/eslint-plugin": "^8.33.0",
|
|
47
52
|
"@typescript-eslint/parser": "^8.33.0",
|
|
48
53
|
"eslint": "9.28.0",
|
|
49
54
|
"eslint-config-prettier": "^10.1.5",
|
|
50
|
-
"eslint-plugin-jsx-a11y": "^6.10.2",
|
|
51
55
|
"eslint-plugin-prettier": "^5.4.1",
|
|
56
|
+
"jest": "^29.7.0",
|
|
52
57
|
"prettier": "^3.5.3",
|
|
53
58
|
"rollup": "^4.41.1",
|
|
54
59
|
"rollup-plugin-delete": "^2.2.0",
|
|
55
60
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
56
61
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
57
|
-
"
|
|
62
|
+
"ts-jest": "^29.3.4",
|
|
63
|
+
"ts-node": "^10.9.2",
|
|
64
|
+
"typescript": "^5.8.3",
|
|
65
|
+
"typescript-eslint": "^8.33.0"
|
|
58
66
|
}
|
|
59
67
|
}
|