@pdg/api 1.0.10 → 1.0.12
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/@util/compare.d.ts +3 -3
- package/dist/@util/index.d.ts +2 -2
- package/dist/@util/url.d.ts +1 -1
- package/dist/Api/Api.d.ts +12 -12
- package/dist/Api/Api.types.d.ts +34 -34
- package/dist/Api/index.d.ts +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +183 -183
- package/dist/index.js +183 -183
- package/package.json +2 -2
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/@util/compare.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
declare const empty: (v: any) => boolean;
|
|
2
|
-
declare const notEmpty: (v: any) => boolean;
|
|
3
|
-
export { empty, notEmpty };
|
|
1
|
+
declare const empty: (v: any) => boolean;
|
|
2
|
+
declare const notEmpty: (v: any) => boolean;
|
|
3
|
+
export { empty, notEmpty };
|
package/dist/@util/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './compare';
|
|
2
|
-
export * from './url';
|
|
1
|
+
export * from './compare';
|
|
2
|
+
export * from './url';
|
package/dist/@util/url.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function joinUrl(...parts: string[]): string;
|
|
1
|
+
export declare function joinUrl(...parts: string[]): string;
|
package/dist/Api/Api.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Method } from 'axios';
|
|
2
|
-
import { ApiRequestData, ApiOption, ApiRequestOption } from './Api.types';
|
|
3
|
-
declare class Api<T = any> {
|
|
4
|
-
option: ApiOption;
|
|
5
|
-
constructor(option: ApiOption<T>);
|
|
6
|
-
get(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
7
|
-
post(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
8
|
-
patch(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
9
|
-
delete(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
10
|
-
run: (method: Method, path: string, data?: ApiRequestData, option?: ApiRequestOption) => Promise<T>;
|
|
11
|
-
}
|
|
12
|
-
export default Api;
|
|
1
|
+
import { Method } from 'axios';
|
|
2
|
+
import { ApiRequestData, ApiOption, ApiRequestOption } from './Api.types';
|
|
3
|
+
declare class Api<T = any> {
|
|
4
|
+
option: ApiOption;
|
|
5
|
+
constructor(option: ApiOption<T>);
|
|
6
|
+
get(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
7
|
+
post(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
8
|
+
patch(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
9
|
+
delete(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T>;
|
|
10
|
+
run: (method: Method, path: string, data?: ApiRequestData, option?: ApiRequestOption) => Promise<T>;
|
|
11
|
+
}
|
|
12
|
+
export default Api;
|
package/dist/Api/Api.types.d.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, ResponseType } from 'axios';
|
|
2
|
-
export interface ApiRequestData {
|
|
3
|
-
[key: string]: any;
|
|
4
|
-
}
|
|
5
|
-
export interface ApiRequestOption {
|
|
6
|
-
raw?: boolean;
|
|
7
|
-
rawResponseType?: ResponseType;
|
|
8
|
-
silent?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface ApiRequestConfig extends AxiosRequestConfig {
|
|
11
|
-
silent?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare class ApiError<T = any> extends Error {
|
|
14
|
-
code?: string;
|
|
15
|
-
config?: ApiRequestConfig;
|
|
16
|
-
baseUrl?: string;
|
|
17
|
-
path?: string;
|
|
18
|
-
requestData?: ApiRequestData;
|
|
19
|
-
requestOption?: ApiRequestOption;
|
|
20
|
-
response?: AxiosResponse<T>;
|
|
21
|
-
status?: number;
|
|
22
|
-
isAxiosError: boolean;
|
|
23
|
-
constructor(message?: string, code?: string);
|
|
24
|
-
}
|
|
25
|
-
export interface ApiOption<T = any> {
|
|
26
|
-
baseUrl: string;
|
|
27
|
-
timeParamName: string;
|
|
28
|
-
withCredentials?: boolean;
|
|
29
|
-
headers?: AxiosRequestConfig['headers'];
|
|
30
|
-
onRequest?(config: InternalAxiosRequestConfig, baseUrl: string, path: string, requestData?: ApiRequestData, requestOption?: ApiRequestOption): Promise<InternalAxiosRequestConfig>;
|
|
31
|
-
onResponse?(response: AxiosResponse<T>, config: AxiosRequestConfig, baseUrl: string, path: string, requestData?: ApiRequestData, requestOption?: ApiRequestOption): Promise<T>;
|
|
32
|
-
onError?(err: ApiError): void;
|
|
33
|
-
dataKeysToLowerCase?: boolean;
|
|
34
|
-
}
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, ResponseType } from 'axios';
|
|
2
|
+
export interface ApiRequestData {
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
}
|
|
5
|
+
export interface ApiRequestOption {
|
|
6
|
+
raw?: boolean;
|
|
7
|
+
rawResponseType?: ResponseType;
|
|
8
|
+
silent?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface ApiRequestConfig extends AxiosRequestConfig {
|
|
11
|
+
silent?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class ApiError<T = any> extends Error {
|
|
14
|
+
code?: string;
|
|
15
|
+
config?: ApiRequestConfig;
|
|
16
|
+
baseUrl?: string;
|
|
17
|
+
path?: string;
|
|
18
|
+
requestData?: ApiRequestData;
|
|
19
|
+
requestOption?: ApiRequestOption;
|
|
20
|
+
response?: AxiosResponse<T>;
|
|
21
|
+
status?: number;
|
|
22
|
+
isAxiosError: boolean;
|
|
23
|
+
constructor(message?: string, code?: string);
|
|
24
|
+
}
|
|
25
|
+
export interface ApiOption<T = any> {
|
|
26
|
+
baseUrl: string;
|
|
27
|
+
timeParamName: string;
|
|
28
|
+
withCredentials?: boolean;
|
|
29
|
+
headers?: AxiosRequestConfig['headers'];
|
|
30
|
+
onRequest?(config: InternalAxiosRequestConfig, baseUrl: string, path: string, requestData?: ApiRequestData, requestOption?: ApiRequestOption): Promise<InternalAxiosRequestConfig>;
|
|
31
|
+
onResponse?(response: AxiosResponse<T>, config: AxiosRequestConfig, baseUrl: string, path: string, requestData?: ApiRequestData, requestOption?: ApiRequestOption): Promise<T>;
|
|
32
|
+
onError?(err: ApiError): void;
|
|
33
|
+
dataKeysToLowerCase?: boolean;
|
|
34
|
+
}
|
package/dist/Api/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Api from './Api';
|
|
2
|
-
export default Api;
|
|
3
|
-
export { Api };
|
|
4
|
-
export * from './Api.types';
|
|
1
|
+
import Api from './Api';
|
|
2
|
+
export default Api;
|
|
3
|
+
export { Api };
|
|
4
|
+
export * from './Api.types';
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './Api';
|
|
1
|
+
export * from './Api';
|
package/dist/index.esm.js
CHANGED
|
@@ -81,186 +81,186 @@ function __generator(thisArg, body) {
|
|
|
81
81
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
82
82
|
var e = new Error(message);
|
|
83
83
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
84
|
-
};//--------------------------------------------------------------------------------------------------------------------
|
|
85
|
-
var ApiError = /** @class */ (function (_super) {
|
|
86
|
-
__extends(ApiError, _super);
|
|
87
|
-
function ApiError(message, code) {
|
|
88
|
-
var _this = _super.call(this, message) || this;
|
|
89
|
-
_this.isAxiosError = false;
|
|
90
|
-
_this.code = code;
|
|
91
|
-
return _this;
|
|
92
|
-
}
|
|
93
|
-
return ApiError;
|
|
94
|
-
}(Error));var empty = function (v) {
|
|
95
|
-
var result = false;
|
|
96
|
-
if (v == null) {
|
|
97
|
-
result = true;
|
|
98
|
-
}
|
|
99
|
-
else if (typeof v === 'string') {
|
|
100
|
-
result = v === '';
|
|
101
|
-
}
|
|
102
|
-
else if (typeof v === 'object') {
|
|
103
|
-
if (Array.isArray(v)) {
|
|
104
|
-
result = v.length === 0;
|
|
105
|
-
}
|
|
106
|
-
else if (!(v instanceof Date)) {
|
|
107
|
-
result = Object.entries(v).length === 0;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
};
|
|
112
|
-
var notEmpty = function (v) {
|
|
113
|
-
return !empty(v);
|
|
114
|
-
};function joinUrl() {
|
|
115
|
-
var parts = [];
|
|
116
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
117
|
-
parts[_i] = arguments[_i];
|
|
118
|
-
}
|
|
119
|
-
return parts.reduce(function (acc, part) {
|
|
120
|
-
if (acc === '') {
|
|
121
|
-
return part;
|
|
122
|
-
}
|
|
123
|
-
else if (part.startsWith('?')) {
|
|
124
|
-
return "".concat(acc).concat(part);
|
|
125
|
-
}
|
|
126
|
-
else if (acc.endsWith('/')) {
|
|
127
|
-
return "".concat(acc).concat(part.startsWith('/') ? part.substring(1) : part);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
return "".concat(acc).concat(part.startsWith('/') ? part : "/".concat(part));
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}var _a;
|
|
134
|
-
var AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
135
|
-
var Api = /** @class */ (function () {
|
|
136
|
-
// constructor -------------------------------------------------------------------------------------------------------
|
|
137
|
-
function Api(option) {
|
|
138
|
-
var _this = this;
|
|
139
|
-
// Run ---------------------------------------------------------------------------------------------------------------
|
|
140
|
-
this.run = function (method, path, data, option) {
|
|
141
|
-
return new Promise(function (resolve, reject) {
|
|
142
|
-
var _a;
|
|
143
|
-
var headers = __assign({}, _this.option.headers);
|
|
144
|
-
if (typeof window !== 'undefined') {
|
|
145
|
-
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
146
|
-
headers['X-Referer'] = window.location.href;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
var requestConfig = {
|
|
150
|
-
method: method,
|
|
151
|
-
withCredentials: _this.option.withCredentials,
|
|
152
|
-
headers: headers,
|
|
153
|
-
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
154
|
-
};
|
|
155
|
-
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
156
|
-
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
157
|
-
}
|
|
158
|
-
requestConfig.url = joinUrl(_this.option.baseUrl, path.replace(/\./g, '/'));
|
|
159
|
-
if (data) {
|
|
160
|
-
if (method === 'get') {
|
|
161
|
-
if (notEmpty(data)) {
|
|
162
|
-
var finalData = {};
|
|
163
|
-
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
164
|
-
for (var key in data) {
|
|
165
|
-
if (data[key] != null) {
|
|
166
|
-
finalData[key] = data[key];
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
requestConfig.url += "?".concat(new URLSearchParams(finalData).toString());
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
if (data instanceof FormData) {
|
|
174
|
-
data.append(_this.option.timeParamName, "".concat(new Date().getTime()));
|
|
175
|
-
requestConfig.data = data;
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
var finalData = __assign({}, data);
|
|
179
|
-
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
180
|
-
requestConfig.data = finalData;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
var setErrorInfo = function (err, status, response) {
|
|
185
|
-
err.config = requestConfig;
|
|
186
|
-
err.baseUrl = _this.option.baseUrl;
|
|
187
|
-
err.path = path;
|
|
188
|
-
err.requestData = data;
|
|
189
|
-
err.requestOption = option;
|
|
190
|
-
err.response = response;
|
|
191
|
-
err.status = status;
|
|
192
|
-
};
|
|
193
|
-
var fireError = function (err) {
|
|
194
|
-
var apiError = new ApiError();
|
|
195
|
-
if (typeof err === 'object') {
|
|
196
|
-
apiError.message = err.message;
|
|
197
|
-
apiError.code = err.code;
|
|
198
|
-
setErrorInfo(apiError, err.status, err.response);
|
|
199
|
-
}
|
|
200
|
-
else if (typeof err === 'string') {
|
|
201
|
-
apiError.message = err;
|
|
202
|
-
}
|
|
203
|
-
else if (err) {
|
|
204
|
-
apiError.message = err.toString();
|
|
205
|
-
}
|
|
206
|
-
if (_this.option.onError)
|
|
207
|
-
_this.option.onError(apiError);
|
|
208
|
-
reject(apiError);
|
|
209
|
-
};
|
|
210
|
-
var instance = AxiosCreate();
|
|
211
|
-
var requestInterceptor;
|
|
212
|
-
if (_this.option.onRequest) {
|
|
213
|
-
requestInterceptor = instance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
214
|
-
return __generator(this, function (_a) {
|
|
215
|
-
if (this.option.onRequest) {
|
|
216
|
-
return [2 /*return*/, this.option.onRequest(config, this.option.baseUrl, path, data, option)];
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
return [2 /*return*/, config];
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}); });
|
|
223
|
-
}
|
|
224
|
-
instance
|
|
225
|
-
.request(requestConfig)
|
|
226
|
-
.then(function (res) {
|
|
227
|
-
var resData = res.data;
|
|
228
|
-
if (_this.option.onResponse) {
|
|
229
|
-
_this.option
|
|
230
|
-
.onResponse(res, requestConfig, _this.option.baseUrl, path, data, option)
|
|
231
|
-
.then(function (finalResData) {
|
|
232
|
-
resolve(finalResData);
|
|
233
|
-
})
|
|
234
|
-
.catch(function (err) {
|
|
235
|
-
setErrorInfo(err, res.status, res);
|
|
236
|
-
fireError(err);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
resolve(resData);
|
|
241
|
-
}
|
|
242
|
-
})
|
|
243
|
-
.catch(fireError)
|
|
244
|
-
.finally(function () {
|
|
245
|
-
if (requestInterceptor) {
|
|
246
|
-
instance.interceptors.request.eject(requestInterceptor);
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
};
|
|
251
|
-
this.option = option;
|
|
252
|
-
}
|
|
253
|
-
Api.prototype.get = function (path, data, option) {
|
|
254
|
-
return this.run('get', path, data, option);
|
|
255
|
-
};
|
|
256
|
-
Api.prototype.post = function (path, data, option) {
|
|
257
|
-
return this.run('post', path, data, option);
|
|
258
|
-
};
|
|
259
|
-
Api.prototype.patch = function (path, data, option) {
|
|
260
|
-
return this.run('patch', path, data, option);
|
|
261
|
-
};
|
|
262
|
-
Api.prototype.delete = function (path, data, option) {
|
|
263
|
-
return this.run('delete', path, data, option);
|
|
264
|
-
};
|
|
265
|
-
return Api;
|
|
266
|
-
}());export{Api,ApiError}
|
|
84
|
+
};//--------------------------------------------------------------------------------------------------------------------
|
|
85
|
+
var ApiError = /** @class */ (function (_super) {
|
|
86
|
+
__extends(ApiError, _super);
|
|
87
|
+
function ApiError(message, code) {
|
|
88
|
+
var _this = _super.call(this, message) || this;
|
|
89
|
+
_this.isAxiosError = false;
|
|
90
|
+
_this.code = code;
|
|
91
|
+
return _this;
|
|
92
|
+
}
|
|
93
|
+
return ApiError;
|
|
94
|
+
}(Error));var empty = function (v) {
|
|
95
|
+
var result = false;
|
|
96
|
+
if (v == null) {
|
|
97
|
+
result = true;
|
|
98
|
+
}
|
|
99
|
+
else if (typeof v === 'string') {
|
|
100
|
+
result = v === '';
|
|
101
|
+
}
|
|
102
|
+
else if (typeof v === 'object') {
|
|
103
|
+
if (Array.isArray(v)) {
|
|
104
|
+
result = v.length === 0;
|
|
105
|
+
}
|
|
106
|
+
else if (!(v instanceof Date)) {
|
|
107
|
+
result = Object.entries(v).length === 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
var notEmpty = function (v) {
|
|
113
|
+
return !empty(v);
|
|
114
|
+
};function joinUrl() {
|
|
115
|
+
var parts = [];
|
|
116
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
117
|
+
parts[_i] = arguments[_i];
|
|
118
|
+
}
|
|
119
|
+
return parts.reduce(function (acc, part) {
|
|
120
|
+
if (acc === '') {
|
|
121
|
+
return part;
|
|
122
|
+
}
|
|
123
|
+
else if (part.startsWith('?')) {
|
|
124
|
+
return "".concat(acc).concat(part);
|
|
125
|
+
}
|
|
126
|
+
else if (acc.endsWith('/')) {
|
|
127
|
+
return "".concat(acc).concat(part.startsWith('/') ? part.substring(1) : part);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return "".concat(acc).concat(part.startsWith('/') ? part : "/".concat(part));
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}var _a;
|
|
134
|
+
var AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
135
|
+
var Api = /** @class */ (function () {
|
|
136
|
+
// constructor -------------------------------------------------------------------------------------------------------
|
|
137
|
+
function Api(option) {
|
|
138
|
+
var _this = this;
|
|
139
|
+
// Run ---------------------------------------------------------------------------------------------------------------
|
|
140
|
+
this.run = function (method, path, data, option) {
|
|
141
|
+
return new Promise(function (resolve, reject) {
|
|
142
|
+
var _a;
|
|
143
|
+
var headers = __assign({}, _this.option.headers);
|
|
144
|
+
if (typeof window !== 'undefined') {
|
|
145
|
+
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
146
|
+
headers['X-Referer'] = window.location.href;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
var requestConfig = {
|
|
150
|
+
method: method,
|
|
151
|
+
withCredentials: _this.option.withCredentials,
|
|
152
|
+
headers: headers,
|
|
153
|
+
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
154
|
+
};
|
|
155
|
+
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
156
|
+
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
157
|
+
}
|
|
158
|
+
requestConfig.url = joinUrl(_this.option.baseUrl, path.replace(/\./g, '/'));
|
|
159
|
+
if (data) {
|
|
160
|
+
if (method === 'get') {
|
|
161
|
+
if (notEmpty(data)) {
|
|
162
|
+
var finalData = {};
|
|
163
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
164
|
+
for (var key in data) {
|
|
165
|
+
if (data[key] != null) {
|
|
166
|
+
finalData[key] = data[key];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
requestConfig.url += "?".concat(new URLSearchParams(finalData).toString());
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
if (data instanceof FormData) {
|
|
174
|
+
data.append(_this.option.timeParamName, "".concat(new Date().getTime()));
|
|
175
|
+
requestConfig.data = data;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
var finalData = __assign({}, data);
|
|
179
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
180
|
+
requestConfig.data = finalData;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
var setErrorInfo = function (err, status, response) {
|
|
185
|
+
err.config = requestConfig;
|
|
186
|
+
err.baseUrl = _this.option.baseUrl;
|
|
187
|
+
err.path = path;
|
|
188
|
+
err.requestData = data;
|
|
189
|
+
err.requestOption = option;
|
|
190
|
+
err.response = response;
|
|
191
|
+
err.status = status;
|
|
192
|
+
};
|
|
193
|
+
var fireError = function (err) {
|
|
194
|
+
var apiError = new ApiError();
|
|
195
|
+
if (typeof err === 'object') {
|
|
196
|
+
apiError.message = err.message;
|
|
197
|
+
apiError.code = err.code;
|
|
198
|
+
setErrorInfo(apiError, err.status, err.response);
|
|
199
|
+
}
|
|
200
|
+
else if (typeof err === 'string') {
|
|
201
|
+
apiError.message = err;
|
|
202
|
+
}
|
|
203
|
+
else if (err) {
|
|
204
|
+
apiError.message = err.toString();
|
|
205
|
+
}
|
|
206
|
+
if (_this.option.onError)
|
|
207
|
+
_this.option.onError(apiError);
|
|
208
|
+
reject(apiError);
|
|
209
|
+
};
|
|
210
|
+
var instance = AxiosCreate();
|
|
211
|
+
var requestInterceptor;
|
|
212
|
+
if (_this.option.onRequest) {
|
|
213
|
+
requestInterceptor = instance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
214
|
+
return __generator(this, function (_a) {
|
|
215
|
+
if (this.option.onRequest) {
|
|
216
|
+
return [2 /*return*/, this.option.onRequest(config, this.option.baseUrl, path, data, option)];
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
return [2 /*return*/, config];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}); });
|
|
223
|
+
}
|
|
224
|
+
instance
|
|
225
|
+
.request(requestConfig)
|
|
226
|
+
.then(function (res) {
|
|
227
|
+
var resData = res.data;
|
|
228
|
+
if (_this.option.onResponse) {
|
|
229
|
+
_this.option
|
|
230
|
+
.onResponse(res, requestConfig, _this.option.baseUrl, path, data, option)
|
|
231
|
+
.then(function (finalResData) {
|
|
232
|
+
resolve(finalResData);
|
|
233
|
+
})
|
|
234
|
+
.catch(function (err) {
|
|
235
|
+
setErrorInfo(err, res.status, res);
|
|
236
|
+
fireError(err);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
resolve(resData);
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
.catch(fireError)
|
|
244
|
+
.finally(function () {
|
|
245
|
+
if (requestInterceptor) {
|
|
246
|
+
instance.interceptors.request.eject(requestInterceptor);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
this.option = option;
|
|
252
|
+
}
|
|
253
|
+
Api.prototype.get = function (path, data, option) {
|
|
254
|
+
return this.run('get', path, data, option);
|
|
255
|
+
};
|
|
256
|
+
Api.prototype.post = function (path, data, option) {
|
|
257
|
+
return this.run('post', path, data, option);
|
|
258
|
+
};
|
|
259
|
+
Api.prototype.patch = function (path, data, option) {
|
|
260
|
+
return this.run('patch', path, data, option);
|
|
261
|
+
};
|
|
262
|
+
Api.prototype.delete = function (path, data, option) {
|
|
263
|
+
return this.run('delete', path, data, option);
|
|
264
|
+
};
|
|
265
|
+
return Api;
|
|
266
|
+
}());export{Api,ApiError};
|
package/dist/index.js
CHANGED
|
@@ -81,186 +81,186 @@ function __generator(thisArg, body) {
|
|
|
81
81
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
82
82
|
var e = new Error(message);
|
|
83
83
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
84
|
-
};//--------------------------------------------------------------------------------------------------------------------
|
|
85
|
-
var ApiError = /** @class */ (function (_super) {
|
|
86
|
-
__extends(ApiError, _super);
|
|
87
|
-
function ApiError(message, code) {
|
|
88
|
-
var _this = _super.call(this, message) || this;
|
|
89
|
-
_this.isAxiosError = false;
|
|
90
|
-
_this.code = code;
|
|
91
|
-
return _this;
|
|
92
|
-
}
|
|
93
|
-
return ApiError;
|
|
94
|
-
}(Error));var empty = function (v) {
|
|
95
|
-
var result = false;
|
|
96
|
-
if (v == null) {
|
|
97
|
-
result = true;
|
|
98
|
-
}
|
|
99
|
-
else if (typeof v === 'string') {
|
|
100
|
-
result = v === '';
|
|
101
|
-
}
|
|
102
|
-
else if (typeof v === 'object') {
|
|
103
|
-
if (Array.isArray(v)) {
|
|
104
|
-
result = v.length === 0;
|
|
105
|
-
}
|
|
106
|
-
else if (!(v instanceof Date)) {
|
|
107
|
-
result = Object.entries(v).length === 0;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
};
|
|
112
|
-
var notEmpty = function (v) {
|
|
113
|
-
return !empty(v);
|
|
114
|
-
};function joinUrl() {
|
|
115
|
-
var parts = [];
|
|
116
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
117
|
-
parts[_i] = arguments[_i];
|
|
118
|
-
}
|
|
119
|
-
return parts.reduce(function (acc, part) {
|
|
120
|
-
if (acc === '') {
|
|
121
|
-
return part;
|
|
122
|
-
}
|
|
123
|
-
else if (part.startsWith('?')) {
|
|
124
|
-
return "".concat(acc).concat(part);
|
|
125
|
-
}
|
|
126
|
-
else if (acc.endsWith('/')) {
|
|
127
|
-
return "".concat(acc).concat(part.startsWith('/') ? part.substring(1) : part);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
return "".concat(acc).concat(part.startsWith('/') ? part : "/".concat(part));
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}var _a;
|
|
134
|
-
var AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
135
|
-
var Api = /** @class */ (function () {
|
|
136
|
-
// constructor -------------------------------------------------------------------------------------------------------
|
|
137
|
-
function Api(option) {
|
|
138
|
-
var _this = this;
|
|
139
|
-
// Run ---------------------------------------------------------------------------------------------------------------
|
|
140
|
-
this.run = function (method, path, data, option) {
|
|
141
|
-
return new Promise(function (resolve, reject) {
|
|
142
|
-
var _a;
|
|
143
|
-
var headers = __assign({}, _this.option.headers);
|
|
144
|
-
if (typeof window !== 'undefined') {
|
|
145
|
-
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
146
|
-
headers['X-Referer'] = window.location.href;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
var requestConfig = {
|
|
150
|
-
method: method,
|
|
151
|
-
withCredentials: _this.option.withCredentials,
|
|
152
|
-
headers: headers,
|
|
153
|
-
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
154
|
-
};
|
|
155
|
-
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
156
|
-
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
157
|
-
}
|
|
158
|
-
requestConfig.url = joinUrl(_this.option.baseUrl, path.replace(/\./g, '/'));
|
|
159
|
-
if (data) {
|
|
160
|
-
if (method === 'get') {
|
|
161
|
-
if (notEmpty(data)) {
|
|
162
|
-
var finalData = {};
|
|
163
|
-
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
164
|
-
for (var key in data) {
|
|
165
|
-
if (data[key] != null) {
|
|
166
|
-
finalData[key] = data[key];
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
requestConfig.url += "?".concat(new URLSearchParams(finalData).toString());
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
if (data instanceof FormData) {
|
|
174
|
-
data.append(_this.option.timeParamName, "".concat(new Date().getTime()));
|
|
175
|
-
requestConfig.data = data;
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
var finalData = __assign({}, data);
|
|
179
|
-
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
180
|
-
requestConfig.data = finalData;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
var setErrorInfo = function (err, status, response) {
|
|
185
|
-
err.config = requestConfig;
|
|
186
|
-
err.baseUrl = _this.option.baseUrl;
|
|
187
|
-
err.path = path;
|
|
188
|
-
err.requestData = data;
|
|
189
|
-
err.requestOption = option;
|
|
190
|
-
err.response = response;
|
|
191
|
-
err.status = status;
|
|
192
|
-
};
|
|
193
|
-
var fireError = function (err) {
|
|
194
|
-
var apiError = new ApiError();
|
|
195
|
-
if (typeof err === 'object') {
|
|
196
|
-
apiError.message = err.message;
|
|
197
|
-
apiError.code = err.code;
|
|
198
|
-
setErrorInfo(apiError, err.status, err.response);
|
|
199
|
-
}
|
|
200
|
-
else if (typeof err === 'string') {
|
|
201
|
-
apiError.message = err;
|
|
202
|
-
}
|
|
203
|
-
else if (err) {
|
|
204
|
-
apiError.message = err.toString();
|
|
205
|
-
}
|
|
206
|
-
if (_this.option.onError)
|
|
207
|
-
_this.option.onError(apiError);
|
|
208
|
-
reject(apiError);
|
|
209
|
-
};
|
|
210
|
-
var instance = AxiosCreate();
|
|
211
|
-
var requestInterceptor;
|
|
212
|
-
if (_this.option.onRequest) {
|
|
213
|
-
requestInterceptor = instance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
214
|
-
return __generator(this, function (_a) {
|
|
215
|
-
if (this.option.onRequest) {
|
|
216
|
-
return [2 /*return*/, this.option.onRequest(config, this.option.baseUrl, path, data, option)];
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
return [2 /*return*/, config];
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}); });
|
|
223
|
-
}
|
|
224
|
-
instance
|
|
225
|
-
.request(requestConfig)
|
|
226
|
-
.then(function (res) {
|
|
227
|
-
var resData = res.data;
|
|
228
|
-
if (_this.option.onResponse) {
|
|
229
|
-
_this.option
|
|
230
|
-
.onResponse(res, requestConfig, _this.option.baseUrl, path, data, option)
|
|
231
|
-
.then(function (finalResData) {
|
|
232
|
-
resolve(finalResData);
|
|
233
|
-
})
|
|
234
|
-
.catch(function (err) {
|
|
235
|
-
setErrorInfo(err, res.status, res);
|
|
236
|
-
fireError(err);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
resolve(resData);
|
|
241
|
-
}
|
|
242
|
-
})
|
|
243
|
-
.catch(fireError)
|
|
244
|
-
.finally(function () {
|
|
245
|
-
if (requestInterceptor) {
|
|
246
|
-
instance.interceptors.request.eject(requestInterceptor);
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
};
|
|
251
|
-
this.option = option;
|
|
252
|
-
}
|
|
253
|
-
Api.prototype.get = function (path, data, option) {
|
|
254
|
-
return this.run('get', path, data, option);
|
|
255
|
-
};
|
|
256
|
-
Api.prototype.post = function (path, data, option) {
|
|
257
|
-
return this.run('post', path, data, option);
|
|
258
|
-
};
|
|
259
|
-
Api.prototype.patch = function (path, data, option) {
|
|
260
|
-
return this.run('patch', path, data, option);
|
|
261
|
-
};
|
|
262
|
-
Api.prototype.delete = function (path, data, option) {
|
|
263
|
-
return this.run('delete', path, data, option);
|
|
264
|
-
};
|
|
265
|
-
return Api;
|
|
266
|
-
}());exports.Api=Api;exports.ApiError=ApiError
|
|
84
|
+
};//--------------------------------------------------------------------------------------------------------------------
|
|
85
|
+
var ApiError = /** @class */ (function (_super) {
|
|
86
|
+
__extends(ApiError, _super);
|
|
87
|
+
function ApiError(message, code) {
|
|
88
|
+
var _this = _super.call(this, message) || this;
|
|
89
|
+
_this.isAxiosError = false;
|
|
90
|
+
_this.code = code;
|
|
91
|
+
return _this;
|
|
92
|
+
}
|
|
93
|
+
return ApiError;
|
|
94
|
+
}(Error));var empty = function (v) {
|
|
95
|
+
var result = false;
|
|
96
|
+
if (v == null) {
|
|
97
|
+
result = true;
|
|
98
|
+
}
|
|
99
|
+
else if (typeof v === 'string') {
|
|
100
|
+
result = v === '';
|
|
101
|
+
}
|
|
102
|
+
else if (typeof v === 'object') {
|
|
103
|
+
if (Array.isArray(v)) {
|
|
104
|
+
result = v.length === 0;
|
|
105
|
+
}
|
|
106
|
+
else if (!(v instanceof Date)) {
|
|
107
|
+
result = Object.entries(v).length === 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
var notEmpty = function (v) {
|
|
113
|
+
return !empty(v);
|
|
114
|
+
};function joinUrl() {
|
|
115
|
+
var parts = [];
|
|
116
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
117
|
+
parts[_i] = arguments[_i];
|
|
118
|
+
}
|
|
119
|
+
return parts.reduce(function (acc, part) {
|
|
120
|
+
if (acc === '') {
|
|
121
|
+
return part;
|
|
122
|
+
}
|
|
123
|
+
else if (part.startsWith('?')) {
|
|
124
|
+
return "".concat(acc).concat(part);
|
|
125
|
+
}
|
|
126
|
+
else if (acc.endsWith('/')) {
|
|
127
|
+
return "".concat(acc).concat(part.startsWith('/') ? part.substring(1) : part);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return "".concat(acc).concat(part.startsWith('/') ? part : "/".concat(part));
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}var _a;
|
|
134
|
+
var AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
135
|
+
var Api = /** @class */ (function () {
|
|
136
|
+
// constructor -------------------------------------------------------------------------------------------------------
|
|
137
|
+
function Api(option) {
|
|
138
|
+
var _this = this;
|
|
139
|
+
// Run ---------------------------------------------------------------------------------------------------------------
|
|
140
|
+
this.run = function (method, path, data, option) {
|
|
141
|
+
return new Promise(function (resolve, reject) {
|
|
142
|
+
var _a;
|
|
143
|
+
var headers = __assign({}, _this.option.headers);
|
|
144
|
+
if (typeof window !== 'undefined') {
|
|
145
|
+
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
146
|
+
headers['X-Referer'] = window.location.href;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
var requestConfig = {
|
|
150
|
+
method: method,
|
|
151
|
+
withCredentials: _this.option.withCredentials,
|
|
152
|
+
headers: headers,
|
|
153
|
+
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
154
|
+
};
|
|
155
|
+
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
156
|
+
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
157
|
+
}
|
|
158
|
+
requestConfig.url = joinUrl(_this.option.baseUrl, path.replace(/\./g, '/'));
|
|
159
|
+
if (data) {
|
|
160
|
+
if (method === 'get') {
|
|
161
|
+
if (notEmpty(data)) {
|
|
162
|
+
var finalData = {};
|
|
163
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
164
|
+
for (var key in data) {
|
|
165
|
+
if (data[key] != null) {
|
|
166
|
+
finalData[key] = data[key];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
requestConfig.url += "?".concat(new URLSearchParams(finalData).toString());
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
if (data instanceof FormData) {
|
|
174
|
+
data.append(_this.option.timeParamName, "".concat(new Date().getTime()));
|
|
175
|
+
requestConfig.data = data;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
var finalData = __assign({}, data);
|
|
179
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
180
|
+
requestConfig.data = finalData;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
var setErrorInfo = function (err, status, response) {
|
|
185
|
+
err.config = requestConfig;
|
|
186
|
+
err.baseUrl = _this.option.baseUrl;
|
|
187
|
+
err.path = path;
|
|
188
|
+
err.requestData = data;
|
|
189
|
+
err.requestOption = option;
|
|
190
|
+
err.response = response;
|
|
191
|
+
err.status = status;
|
|
192
|
+
};
|
|
193
|
+
var fireError = function (err) {
|
|
194
|
+
var apiError = new ApiError();
|
|
195
|
+
if (typeof err === 'object') {
|
|
196
|
+
apiError.message = err.message;
|
|
197
|
+
apiError.code = err.code;
|
|
198
|
+
setErrorInfo(apiError, err.status, err.response);
|
|
199
|
+
}
|
|
200
|
+
else if (typeof err === 'string') {
|
|
201
|
+
apiError.message = err;
|
|
202
|
+
}
|
|
203
|
+
else if (err) {
|
|
204
|
+
apiError.message = err.toString();
|
|
205
|
+
}
|
|
206
|
+
if (_this.option.onError)
|
|
207
|
+
_this.option.onError(apiError);
|
|
208
|
+
reject(apiError);
|
|
209
|
+
};
|
|
210
|
+
var instance = AxiosCreate();
|
|
211
|
+
var requestInterceptor;
|
|
212
|
+
if (_this.option.onRequest) {
|
|
213
|
+
requestInterceptor = instance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
214
|
+
return __generator(this, function (_a) {
|
|
215
|
+
if (this.option.onRequest) {
|
|
216
|
+
return [2 /*return*/, this.option.onRequest(config, this.option.baseUrl, path, data, option)];
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
return [2 /*return*/, config];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}); });
|
|
223
|
+
}
|
|
224
|
+
instance
|
|
225
|
+
.request(requestConfig)
|
|
226
|
+
.then(function (res) {
|
|
227
|
+
var resData = res.data;
|
|
228
|
+
if (_this.option.onResponse) {
|
|
229
|
+
_this.option
|
|
230
|
+
.onResponse(res, requestConfig, _this.option.baseUrl, path, data, option)
|
|
231
|
+
.then(function (finalResData) {
|
|
232
|
+
resolve(finalResData);
|
|
233
|
+
})
|
|
234
|
+
.catch(function (err) {
|
|
235
|
+
setErrorInfo(err, res.status, res);
|
|
236
|
+
fireError(err);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
resolve(resData);
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
.catch(fireError)
|
|
244
|
+
.finally(function () {
|
|
245
|
+
if (requestInterceptor) {
|
|
246
|
+
instance.interceptors.request.eject(requestInterceptor);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
this.option = option;
|
|
252
|
+
}
|
|
253
|
+
Api.prototype.get = function (path, data, option) {
|
|
254
|
+
return this.run('get', path, data, option);
|
|
255
|
+
};
|
|
256
|
+
Api.prototype.post = function (path, data, option) {
|
|
257
|
+
return this.run('post', path, data, option);
|
|
258
|
+
};
|
|
259
|
+
Api.prototype.patch = function (path, data, option) {
|
|
260
|
+
return this.run('patch', path, data, option);
|
|
261
|
+
};
|
|
262
|
+
Api.prototype.delete = function (path, data, option) {
|
|
263
|
+
return this.run('delete', path, data, option);
|
|
264
|
+
};
|
|
265
|
+
return Api;
|
|
266
|
+
}());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.12",
|
|
5
5
|
"description": "API Module",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -54,6 +54,6 @@
|
|
|
54
54
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
55
55
|
"rollup-plugin-postcss": "^4.0.2",
|
|
56
56
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
57
|
-
"typescript": "^
|
|
57
|
+
"typescript": "^4.9.5"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/dist/index.esm.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/Api/Api.types.ts","../src/@util/compare.ts","../src/@util/url.ts","../src/Api/Api.ts"],"sourcesContent":["import { AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, ResponseType } from 'axios';\n\nexport interface ApiRequestData {\n [key: string]: any;\n}\n\nexport interface ApiRequestOption {\n raw?: boolean;\n rawResponseType?: ResponseType;\n silent?: boolean;\n}\n\nexport interface ApiRequestConfig extends AxiosRequestConfig {\n silent?: boolean;\n}\n\n//--------------------------------------------------------------------------------------------------------------------\n\nexport class ApiError<T = any> extends Error {\n code?: string;\n config?: ApiRequestConfig;\n baseUrl?: string;\n path?: string;\n requestData?: ApiRequestData;\n requestOption?: ApiRequestOption;\n response?: AxiosResponse<T>;\n status?: number;\n isAxiosError = false;\n\n constructor(message?: string, code?: string) {\n super(message);\n\n this.code = code;\n }\n}\n\n//--------------------------------------------------------------------------------------------------------------------\n\nexport interface ApiOption<T = any> {\n baseUrl: string;\n timeParamName: string;\n withCredentials?: boolean;\n headers?: AxiosRequestConfig['headers'];\n onRequest?(\n config: InternalAxiosRequestConfig,\n baseUrl: string,\n path: string,\n requestData?: ApiRequestData,\n requestOption?: ApiRequestOption\n ): Promise<InternalAxiosRequestConfig>;\n onResponse?(\n response: AxiosResponse<T>,\n config: AxiosRequestConfig,\n baseUrl: string,\n path: string,\n requestData?: ApiRequestData,\n requestOption?: ApiRequestOption\n ): Promise<T>;\n onError?(err: ApiError): void;\n dataKeysToLowerCase?: boolean;\n}\n","const empty = function (v: any) {\n let result = false;\n if (v == null) {\n result = true;\n } else if (typeof v === 'string') {\n result = v === '';\n } else if (typeof v === 'object') {\n if (Array.isArray(v)) {\n result = v.length === 0;\n } else if (!(v instanceof Date)) {\n result = Object.entries(v).length === 0;\n }\n }\n return result;\n};\n\nconst notEmpty = function (v: any) {\n return !empty(v);\n};\n\nexport { empty, notEmpty };\n","export function joinUrl(...parts: string[]): string {\n return parts.reduce((acc, part) => {\n if (acc === '') {\n return part;\n } else if (part.startsWith('?')) {\n return `${acc}${part}`;\n } else if (acc.endsWith('/')) {\n return `${acc}${part.startsWith('/') ? part.substring(1) : part}`;\n } else {\n return `${acc}${part.startsWith('/') ? part : `/${part}`}`;\n }\n });\n}\n","import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios';\nimport { ApiError, ApiRequestData, ApiOption, ApiRequestOption, ApiRequestConfig } from './Api.types';\nimport { notEmpty, joinUrl } from '../@util';\n\nconst AxiosCreate = axios.create ? axios.create : require('axios').default?.create;\n\nclass Api<T = any> {\n option: ApiOption;\n\n // constructor -------------------------------------------------------------------------------------------------------\n\n constructor(option: ApiOption<T>) {\n this.option = option;\n }\n\n get(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('get', path, data, option);\n }\n\n post(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('post', path, data, option);\n }\n\n patch(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('patch', path, data, option);\n }\n\n delete(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('delete', path, data, option);\n }\n\n // Run ---------------------------------------------------------------------------------------------------------------\n\n run = (method: Method, path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> => {\n return new Promise<T>((resolve, reject) => {\n const headers: ApiRequestConfig['headers'] = { ...this.option.headers };\n\n if (typeof window !== 'undefined') {\n if (window?.location?.href) {\n headers['X-Referer'] = window.location.href;\n }\n }\n\n const requestConfig: ApiRequestConfig = {\n method,\n withCredentials: this.option.withCredentials,\n headers,\n silent: !!option?.silent,\n };\n if (option?.raw) {\n requestConfig.responseType = option?.rawResponseType || 'arraybuffer';\n }\n\n requestConfig.url = joinUrl(this.option.baseUrl, path.replace(/\\./g, '/'));\n\n if (data) {\n if (method === 'get') {\n if (notEmpty(data)) {\n const finalData: ApiRequestData = {};\n finalData[this.option.timeParamName] = new Date().getTime();\n for (const key in data) {\n if (data[key] != null) {\n finalData[key] = data[key];\n }\n }\n requestConfig.url += `?${new URLSearchParams(finalData).toString()}`;\n }\n } else {\n if (data instanceof FormData) {\n data.append(this.option.timeParamName, `${new Date().getTime()}`);\n requestConfig.data = data;\n } else {\n const finalData: ApiRequestData = { ...data };\n finalData[this.option.timeParamName] = new Date().getTime();\n requestConfig.data = finalData;\n }\n }\n }\n\n const setErrorInfo = (err: ApiError, status?: number, response?: AxiosResponse) => {\n err.config = requestConfig;\n err.baseUrl = this.option.baseUrl;\n err.path = path;\n err.requestData = data;\n err.requestOption = option;\n err.response = response;\n err.status = status;\n };\n\n const fireError = (err: any): void => {\n const apiError: ApiError = new ApiError();\n\n if (typeof err === 'object') {\n apiError.message = err.message;\n apiError.code = err.code;\n setErrorInfo(apiError, err.status, err.response);\n } else if (typeof err === 'string') {\n apiError.message = err;\n } else if (err) {\n apiError.message = err.toString();\n }\n\n if (this.option.onError) this.option.onError(apiError);\n\n reject(apiError);\n };\n\n const instance: AxiosInstance = AxiosCreate();\n let requestInterceptor: number;\n if (this.option.onRequest) {\n requestInterceptor = instance.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {\n if (this.option.onRequest) {\n return this.option.onRequest(config, this.option.baseUrl, path, data, option);\n } else {\n return config;\n }\n });\n }\n\n instance\n .request<T>(requestConfig)\n .then((res) => {\n const { data: resData } = res;\n if (this.option.onResponse) {\n this.option\n .onResponse(res, requestConfig, this.option.baseUrl, path, data, option)\n .then((finalResData) => {\n resolve(finalResData);\n })\n .catch((err: ApiError) => {\n setErrorInfo(err, res.status, res);\n fireError(err);\n });\n } else {\n resolve(resData);\n }\n })\n .catch(fireError)\n .finally(() => {\n if (requestInterceptor) {\n instance.interceptors.request.eject(requestInterceptor);\n }\n });\n });\n };\n}\n\nexport default Api;\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgBA;AAEA,IAAA,QAAA,kBAAA,UAAA,MAAA,EAAA;IAAuC,SAAK,CAAA,QAAA,EAAA,MAAA,CAAA,CAAA;IAW1C,SAAY,QAAA,CAAA,OAAgB,EAAE,IAAa,EAAA;AACzC,QAAA,IAAA,KAAA,GAAA,MAAK,CAAA,IAAA,CAAA,IAAA,EAAC,OAAO,CAAC,IAAC,IAAA,CAAA;QAHjB,KAAY,CAAA,YAAA,GAAG,KAAK,CAAC;AAKnB,QAAA,KAAI,CAAC,IAAI,GAAG,IAAI,CAAC;;KAClB;IACH,OAAC,QAAA,CAAA;AAAD,CAhBA,CAAuC,KAAK,CAgB3C,EClCD,IAAM,KAAK,GAAG,UAAU,CAAM,EAAA;IAC5B,IAAI,MAAM,GAAG,KAAK,CAAC;AACnB,IAAA,IAAI,CAAC,IAAI,IAAI,EAAE;QACb,MAAM,GAAG,IAAI,CAAC;KACf;AAAM,SAAA,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE;AAChC,QAAA,MAAM,GAAG,CAAC,KAAK,EAAE,CAAC;KACnB;AAAM,SAAA,IAAI,O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ED,IAAA,GAAA,CAAA,SAAA,CAAA,KAAK,GAAL,UAAM,IAAY,EAAE,IAAqB,EAAE,MAAyB,EAAA;AAClE,QAAA,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;KAC9C,CAAA;AAED,IAAA,GAAA,CAAA,SAAA,CAAA,MAAM,GAAN,UAAO,IAAY,EAAE,IAAqB,EAAE,MAAyB,EAAA;AACnE,QAAA,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;KAC/C,CAAA;IAoHH,OAAC,GAAA,CAAA;AAAD,CAAC,EAAA"}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/Api/Api.types.ts","../src/@util/compare.ts","../src/@util/url.ts","../src/Api/Api.ts"],"sourcesContent":["import { AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, ResponseType } from 'axios';\n\nexport interface ApiRequestData {\n [key: string]: any;\n}\n\nexport interface ApiRequestOption {\n raw?: boolean;\n rawResponseType?: ResponseType;\n silent?: boolean;\n}\n\nexport interface ApiRequestConfig extends AxiosRequestConfig {\n silent?: boolean;\n}\n\n//--------------------------------------------------------------------------------------------------------------------\n\nexport class ApiError<T = any> extends Error {\n code?: string;\n config?: ApiRequestConfig;\n baseUrl?: string;\n path?: string;\n requestData?: ApiRequestData;\n requestOption?: ApiRequestOption;\n response?: AxiosResponse<T>;\n status?: number;\n isAxiosError = false;\n\n constructor(message?: string, code?: string) {\n super(message);\n\n this.code = code;\n }\n}\n\n//--------------------------------------------------------------------------------------------------------------------\n\nexport interface ApiOption<T = any> {\n baseUrl: string;\n timeParamName: string;\n withCredentials?: boolean;\n headers?: AxiosRequestConfig['headers'];\n onRequest?(\n config: InternalAxiosRequestConfig,\n baseUrl: string,\n path: string,\n requestData?: ApiRequestData,\n requestOption?: ApiRequestOption\n ): Promise<InternalAxiosRequestConfig>;\n onResponse?(\n response: AxiosResponse<T>,\n config: AxiosRequestConfig,\n baseUrl: string,\n path: string,\n requestData?: ApiRequestData,\n requestOption?: ApiRequestOption\n ): Promise<T>;\n onError?(err: ApiError): void;\n dataKeysToLowerCase?: boolean;\n}\n","const empty = function (v: any) {\n let result = false;\n if (v == null) {\n result = true;\n } else if (typeof v === 'string') {\n result = v === '';\n } else if (typeof v === 'object') {\n if (Array.isArray(v)) {\n result = v.length === 0;\n } else if (!(v instanceof Date)) {\n result = Object.entries(v).length === 0;\n }\n }\n return result;\n};\n\nconst notEmpty = function (v: any) {\n return !empty(v);\n};\n\nexport { empty, notEmpty };\n","export function joinUrl(...parts: string[]): string {\n return parts.reduce((acc, part) => {\n if (acc === '') {\n return part;\n } else if (part.startsWith('?')) {\n return `${acc}${part}`;\n } else if (acc.endsWith('/')) {\n return `${acc}${part.startsWith('/') ? part.substring(1) : part}`;\n } else {\n return `${acc}${part.startsWith('/') ? part : `/${part}`}`;\n }\n });\n}\n","import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios';\nimport { ApiError, ApiRequestData, ApiOption, ApiRequestOption, ApiRequestConfig } from './Api.types';\nimport { notEmpty, joinUrl } from '../@util';\n\nconst AxiosCreate = axios.create ? axios.create : require('axios').default?.create;\n\nclass Api<T = any> {\n option: ApiOption;\n\n // constructor -------------------------------------------------------------------------------------------------------\n\n constructor(option: ApiOption<T>) {\n this.option = option;\n }\n\n get(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('get', path, data, option);\n }\n\n post(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('post', path, data, option);\n }\n\n patch(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('patch', path, data, option);\n }\n\n delete(path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> {\n return this.run('delete', path, data, option);\n }\n\n // Run ---------------------------------------------------------------------------------------------------------------\n\n run = (method: Method, path: string, data?: ApiRequestData, option?: ApiRequestOption): Promise<T> => {\n return new Promise<T>((resolve, reject) => {\n const headers: ApiRequestConfig['headers'] = { ...this.option.headers };\n\n if (typeof window !== 'undefined') {\n if (window?.location?.href) {\n headers['X-Referer'] = window.location.href;\n }\n }\n\n const requestConfig: ApiRequestConfig = {\n method,\n withCredentials: this.option.withCredentials,\n headers,\n silent: !!option?.silent,\n };\n if (option?.raw) {\n requestConfig.responseType = option?.rawResponseType || 'arraybuffer';\n }\n\n requestConfig.url = joinUrl(this.option.baseUrl, path.replace(/\\./g, '/'));\n\n if (data) {\n if (method === 'get') {\n if (notEmpty(data)) {\n const finalData: ApiRequestData = {};\n finalData[this.option.timeParamName] = new Date().getTime();\n for (const key in data) {\n if (data[key] != null) {\n finalData[key] = data[key];\n }\n }\n requestConfig.url += `?${new URLSearchParams(finalData).toString()}`;\n }\n } else {\n if (data instanceof FormData) {\n data.append(this.option.timeParamName, `${new Date().getTime()}`);\n requestConfig.data = data;\n } else {\n const finalData: ApiRequestData = { ...data };\n finalData[this.option.timeParamName] = new Date().getTime();\n requestConfig.data = finalData;\n }\n }\n }\n\n const setErrorInfo = (err: ApiError, status?: number, response?: AxiosResponse) => {\n err.config = requestConfig;\n err.baseUrl = this.option.baseUrl;\n err.path = path;\n err.requestData = data;\n err.requestOption = option;\n err.response = response;\n err.status = status;\n };\n\n const fireError = (err: any): void => {\n const apiError: ApiError = new ApiError();\n\n if (typeof err === 'object') {\n apiError.message = err.message;\n apiError.code = err.code;\n setErrorInfo(apiError, err.status, err.response);\n } else if (typeof err === 'string') {\n apiError.message = err;\n } else if (err) {\n apiError.message = err.toString();\n }\n\n if (this.option.onError) this.option.onError(apiError);\n\n reject(apiError);\n };\n\n const instance: AxiosInstance = AxiosCreate();\n let requestInterceptor: number;\n if (this.option.onRequest) {\n requestInterceptor = instance.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {\n if (this.option.onRequest) {\n return this.option.onRequest(config, this.option.baseUrl, path, data, option);\n } else {\n return config;\n }\n });\n }\n\n instance\n .request<T>(requestConfig)\n .then((res) => {\n const { data: resData } = res;\n if (this.option.onResponse) {\n this.option\n .onResponse(res, requestConfig, this.option.baseUrl, path, data, option)\n .then((finalResData) => {\n resolve(finalResData);\n })\n .catch((err: ApiError) => {\n setErrorInfo(err, res.status, res);\n fireError(err);\n });\n } else {\n resolve(resData);\n }\n })\n .catch(fireError)\n .finally(() => {\n if (requestInterceptor) {\n instance.interceptors.request.eject(requestInterceptor);\n }\n });\n });\n };\n}\n\nexport default Api;\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgBA;AAEA,IAAA,QAAA,kBAAA,UAAA,MAAA,EAAA;IAAuC,SAAK,CAAA,QAAA,EAAA,MAAA,CAAA,CAAA;IAW1C,SAAY,QAAA,CAAA,OAAgB,EAAE,IAAa,EAAA;AACzC,QAAA,IAAA,KAAA,GAAA,MAAK,CAAA,IAAA,CAAA,IAAA,EAAC,OAAO,CAAC,IAAC,IAAA,CAAA;QAHjB,KAAY,CAAA,YAAA,GAAG,KAAK,CAAC;AAKnB,QAAA,KAAI,CAAC,IAAI,GAAG,IAAI,CAAC;;KAClB;IACH,OAAC,QAAA,CAAA;AAAD,CAhBA,CAAuC,KAAK,CAgB3C,EClCD,IAAM,KAAK,GAAG,UAAU,CAAM,EAAA;IAC5B,IAAI,MAAM,GAAG,KAAK,CAAC;AACnB,IAAA,IAAI,CAAC,IAAI,IAAI,EAAE;QACb,MAAM,GAAG,IAAI,CAAC;KACf;AAAM,SAAA,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE;AAChC,QAAA,MAAM,GAAG,CAAC,KAAK,EAAE,CAAC;KACnB;AAAM,SAAA,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE;AAChC,QAAA,IAAI,KAAK,CAAC,OA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