@pdg/api 1.0.23 → 1.0.25
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/README.md +1 -1
- package/dist/index.esm.js +66 -158
- package/dist/index.js +66 -158
- package/package.json +45 -27
package/README.md
CHANGED
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,33 @@ 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
|
-
.
|
|
105
|
+
.then((res) => {
|
|
106
|
+
const { data: resData } = res;
|
|
107
|
+
if (this.option.onResponse) {
|
|
108
|
+
this.option
|
|
109
|
+
.onResponse(res, requestConfig, this.option.baseUrl, path, data, option)
|
|
110
|
+
.then((finalResData) => {
|
|
198
111
|
resolve(finalResData);
|
|
199
112
|
})
|
|
200
|
-
.catch(
|
|
113
|
+
.catch((err) => {
|
|
201
114
|
setErrorInfo(err, res.status, res);
|
|
202
115
|
fireError(err);
|
|
203
116
|
});
|
|
@@ -207,7 +120,7 @@ var Api = /** @class */ (function () {
|
|
|
207
120
|
}
|
|
208
121
|
})
|
|
209
122
|
.catch(fireError)
|
|
210
|
-
.finally(
|
|
123
|
+
.finally(() => {
|
|
211
124
|
if (requestInterceptor) {
|
|
212
125
|
instance.interceptors.request.eject(requestInterceptor);
|
|
213
126
|
}
|
|
@@ -216,25 +129,24 @@ var Api = /** @class */ (function () {
|
|
|
216
129
|
};
|
|
217
130
|
this.option = option;
|
|
218
131
|
}
|
|
219
|
-
|
|
132
|
+
get(path, data, option) {
|
|
220
133
|
return this.run('get', path, data, option);
|
|
221
|
-
}
|
|
222
|
-
|
|
134
|
+
}
|
|
135
|
+
post(path, data, option) {
|
|
223
136
|
return this.run('post', path, data, option);
|
|
224
|
-
}
|
|
225
|
-
|
|
137
|
+
}
|
|
138
|
+
patch(path, data, option) {
|
|
226
139
|
return this.run('patch', path, data, option);
|
|
227
|
-
}
|
|
228
|
-
|
|
140
|
+
}
|
|
141
|
+
delete(path, data, option) {
|
|
229
142
|
return this.run('delete', path, data, option);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
}());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
233
145
|
/********************************************************************************************************************
|
|
234
146
|
* empty
|
|
235
147
|
* ******************************************************************************************************************/
|
|
236
148
|
function empty(v) {
|
|
237
|
-
|
|
149
|
+
let result = false;
|
|
238
150
|
if (v == null) {
|
|
239
151
|
result = true;
|
|
240
152
|
}
|
|
@@ -260,23 +172,19 @@ function notEmpty(v) {
|
|
|
260
172
|
/********************************************************************************************************************
|
|
261
173
|
* urlJoin
|
|
262
174
|
* ******************************************************************************************************************/
|
|
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) {
|
|
175
|
+
function urlJoin(...parts) {
|
|
176
|
+
return parts.reduce((acc, part) => {
|
|
269
177
|
if (acc === '') {
|
|
270
178
|
return part;
|
|
271
179
|
}
|
|
272
180
|
else if (part.startsWith('?')) {
|
|
273
|
-
return
|
|
181
|
+
return `${acc}${part}`;
|
|
274
182
|
}
|
|
275
183
|
else if (acc.endsWith('/')) {
|
|
276
|
-
return
|
|
184
|
+
return `${acc}${part.startsWith('/') ? part.substring(1) : part}`;
|
|
277
185
|
}
|
|
278
186
|
else {
|
|
279
|
-
return
|
|
187
|
+
return `${acc}${part.startsWith('/') ? part : `/${part}`}`;
|
|
280
188
|
}
|
|
281
189
|
});
|
|
282
190
|
}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,33 @@ 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
|
-
.
|
|
105
|
+
.then((res) => {
|
|
106
|
+
const { data: resData } = res;
|
|
107
|
+
if (this.option.onResponse) {
|
|
108
|
+
this.option
|
|
109
|
+
.onResponse(res, requestConfig, this.option.baseUrl, path, data, option)
|
|
110
|
+
.then((finalResData) => {
|
|
198
111
|
resolve(finalResData);
|
|
199
112
|
})
|
|
200
|
-
.catch(
|
|
113
|
+
.catch((err) => {
|
|
201
114
|
setErrorInfo(err, res.status, res);
|
|
202
115
|
fireError(err);
|
|
203
116
|
});
|
|
@@ -207,7 +120,7 @@ var Api = /** @class */ (function () {
|
|
|
207
120
|
}
|
|
208
121
|
})
|
|
209
122
|
.catch(fireError)
|
|
210
|
-
.finally(
|
|
123
|
+
.finally(() => {
|
|
211
124
|
if (requestInterceptor) {
|
|
212
125
|
instance.interceptors.request.eject(requestInterceptor);
|
|
213
126
|
}
|
|
@@ -216,25 +129,24 @@ var Api = /** @class */ (function () {
|
|
|
216
129
|
};
|
|
217
130
|
this.option = option;
|
|
218
131
|
}
|
|
219
|
-
|
|
132
|
+
get(path, data, option) {
|
|
220
133
|
return this.run('get', path, data, option);
|
|
221
|
-
}
|
|
222
|
-
|
|
134
|
+
}
|
|
135
|
+
post(path, data, option) {
|
|
223
136
|
return this.run('post', path, data, option);
|
|
224
|
-
}
|
|
225
|
-
|
|
137
|
+
}
|
|
138
|
+
patch(path, data, option) {
|
|
226
139
|
return this.run('patch', path, data, option);
|
|
227
|
-
}
|
|
228
|
-
|
|
140
|
+
}
|
|
141
|
+
delete(path, data, option) {
|
|
229
142
|
return this.run('delete', path, data, option);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
}());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
233
145
|
/********************************************************************************************************************
|
|
234
146
|
* empty
|
|
235
147
|
* ******************************************************************************************************************/
|
|
236
148
|
function empty(v) {
|
|
237
|
-
|
|
149
|
+
let result = false;
|
|
238
150
|
if (v == null) {
|
|
239
151
|
result = true;
|
|
240
152
|
}
|
|
@@ -260,23 +172,19 @@ function notEmpty(v) {
|
|
|
260
172
|
/********************************************************************************************************************
|
|
261
173
|
* urlJoin
|
|
262
174
|
* ******************************************************************************************************************/
|
|
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) {
|
|
175
|
+
function urlJoin(...parts) {
|
|
176
|
+
return parts.reduce((acc, part) => {
|
|
269
177
|
if (acc === '') {
|
|
270
178
|
return part;
|
|
271
179
|
}
|
|
272
180
|
else if (part.startsWith('?')) {
|
|
273
|
-
return
|
|
181
|
+
return `${acc}${part}`;
|
|
274
182
|
}
|
|
275
183
|
else if (acc.endsWith('/')) {
|
|
276
|
-
return
|
|
184
|
+
return `${acc}${part.startsWith('/') ? part.substring(1) : part}`;
|
|
277
185
|
}
|
|
278
186
|
else {
|
|
279
|
-
return
|
|
187
|
+
return `${acc}${part.startsWith('/') ? part : `/${part}`}`;
|
|
280
188
|
}
|
|
281
189
|
});
|
|
282
190
|
}exports.Api=Api;exports.ApiError=ApiError;
|
package/package.json
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pdg/api",
|
|
3
|
-
"title": "API Module",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"title": "Typescript API Module",
|
|
4
|
+
"description": "Typescript API Module",
|
|
5
|
+
"version": "1.0.25",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.esm.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.esm.js",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
10
17
|
"repository": {
|
|
11
18
|
"type": "git",
|
|
12
19
|
"url": "git+https://github.com/parkdigy/api.git",
|
|
@@ -21,39 +28,50 @@
|
|
|
21
28
|
"README.md"
|
|
22
29
|
],
|
|
23
30
|
"scripts": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
31
|
+
"dev": "cd examples && npm run dev",
|
|
32
|
+
"watchman:del": "watchman watch-del \"${PWD}\" ; watchman watch-project \"${PWD}\"",
|
|
33
|
+
"build": "npm run watchman:del && npm run test && npm run lint && rollup -c --bundleConfigAsCjs",
|
|
34
|
+
"git:commit": "node .git-commit.cjs",
|
|
35
|
+
"git:push": "git push",
|
|
36
|
+
"git:commit:push": "npm run git:commit && npm run git:push",
|
|
37
|
+
"git:merge:mirror": "node .git-merge.cjs mirror main",
|
|
38
|
+
"reset:gitignore": "git rm -r --cached . && git add .",
|
|
26
39
|
"pub": "npm i && npm run build && npm publish --access=public && rm ./.git/hooks/pre-commit",
|
|
27
40
|
"lint": "eslint './src/**/*.ts'",
|
|
28
|
-
"reinstall
|
|
41
|
+
"reinstall": "npm run reinstall:module",
|
|
42
|
+
"reinstall:module": "rm -rf node_modules && rm -f package-lock.json && npm i",
|
|
43
|
+
"test": "jest"
|
|
29
44
|
},
|
|
30
45
|
"author": "YOUNG CHUL PARK",
|
|
31
46
|
"license": "MIT",
|
|
32
47
|
"readmeFilename": "README.md",
|
|
33
48
|
"keywords": [
|
|
34
|
-
"
|
|
49
|
+
"util",
|
|
35
50
|
"typescript",
|
|
36
51
|
"javascript"
|
|
37
52
|
],
|
|
38
53
|
"dependencies": {
|
|
39
|
-
"axios": "^1.
|
|
54
|
+
"axios": "^1.13.2"
|
|
40
55
|
},
|
|
41
56
|
"devDependencies": {
|
|
42
|
-
"@eslint/js": "
|
|
43
|
-
"@rollup/plugin-commonjs": "
|
|
44
|
-
"@rollup/plugin-node-resolve": "
|
|
45
|
-
"@types/
|
|
46
|
-
"@
|
|
47
|
-
"@typescript-eslint/parser": "
|
|
48
|
-
"eslint": "9.
|
|
49
|
-
"eslint-config-prettier": "
|
|
50
|
-
"eslint-plugin-
|
|
51
|
-
"
|
|
52
|
-
"prettier": "
|
|
53
|
-
"rollup": "
|
|
54
|
-
"rollup-plugin-delete": "
|
|
55
|
-
"rollup-plugin-peer-deps-external": "
|
|
56
|
-
"rollup-plugin-typescript2": "
|
|
57
|
-
"
|
|
57
|
+
"@eslint/js": "9.39.1",
|
|
58
|
+
"@rollup/plugin-commonjs": "29.0.0",
|
|
59
|
+
"@rollup/plugin-node-resolve": "16.0.3",
|
|
60
|
+
"@types/jest": "29.5.14",
|
|
61
|
+
"@types/node": "22.19.2",
|
|
62
|
+
"@typescript-eslint/parser": "8.49.0",
|
|
63
|
+
"eslint": "9.39.1",
|
|
64
|
+
"eslint-config-prettier": "10.1.8",
|
|
65
|
+
"eslint-plugin-prettier": "5.5.4",
|
|
66
|
+
"jest": "29.7.0",
|
|
67
|
+
"prettier": "3.7.4",
|
|
68
|
+
"rollup": "4.53.3",
|
|
69
|
+
"rollup-plugin-delete": "2.2.0",
|
|
70
|
+
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
71
|
+
"rollup-plugin-typescript2": "0.36.0",
|
|
72
|
+
"ts-jest": "29.4.6",
|
|
73
|
+
"ts-node": "10.9.2",
|
|
74
|
+
"typescript": "5.9.3",
|
|
75
|
+
"typescript-eslint": "8.49.0"
|
|
58
76
|
}
|
|
59
77
|
}
|