@pdg/api 1.0.11 → 1.0.13
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/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 +144 -183
- package/dist/index.js +144 -183
- package/package.json +3 -2
- package/dist/@util/compare.d.ts +0 -3
- package/dist/@util/index.d.ts +0 -2
- package/dist/@util/url.d.ts +0 -1
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import axios from'axios';/******************************************************************************
|
|
1
|
+
import axios from'axios';import {urlJoin,notEmpty}from'@pdg/util';/******************************************************************************
|
|
2
2
|
Copyright (c) Microsoft Corporation.
|
|
3
3
|
|
|
4
4
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
@@ -81,186 +81,147 @@ 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
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
else
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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;
|
|
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 _a;
|
|
95
|
+
var AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
96
|
+
var Api = /** @class */ (function () {
|
|
97
|
+
// constructor -------------------------------------------------------------------------------------------------------
|
|
98
|
+
function Api(option) {
|
|
99
|
+
var _this = this;
|
|
100
|
+
// Run ---------------------------------------------------------------------------------------------------------------
|
|
101
|
+
this.run = function (method, path, data, option) {
|
|
102
|
+
return new Promise(function (resolve, reject) {
|
|
103
|
+
var _a;
|
|
104
|
+
var headers = __assign({}, _this.option.headers);
|
|
105
|
+
if (typeof window !== 'undefined') {
|
|
106
|
+
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
107
|
+
headers['X-Referer'] = window.location.href;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
var requestConfig = {
|
|
111
|
+
method: method,
|
|
112
|
+
withCredentials: _this.option.withCredentials,
|
|
113
|
+
headers: headers,
|
|
114
|
+
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
115
|
+
};
|
|
116
|
+
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
117
|
+
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
118
|
+
}
|
|
119
|
+
requestConfig.url = urlJoin(_this.option.baseUrl, path.replace(/\./g, '/'));
|
|
120
|
+
if (data) {
|
|
121
|
+
if (method === 'get') {
|
|
122
|
+
if (notEmpty(data)) {
|
|
123
|
+
var finalData = {};
|
|
124
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
125
|
+
for (var key in data) {
|
|
126
|
+
if (data[key] != null) {
|
|
127
|
+
finalData[key] = data[key];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
requestConfig.url += "?".concat(new URLSearchParams(finalData).toString());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (data instanceof FormData) {
|
|
135
|
+
data.append(_this.option.timeParamName, "".concat(new Date().getTime()));
|
|
136
|
+
requestConfig.data = data;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
var finalData = __assign({}, data);
|
|
140
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
141
|
+
requestConfig.data = finalData;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var setErrorInfo = function (err, status, response) {
|
|
146
|
+
err.config = requestConfig;
|
|
147
|
+
err.baseUrl = _this.option.baseUrl;
|
|
148
|
+
err.path = path;
|
|
149
|
+
err.requestData = data;
|
|
150
|
+
err.requestOption = option;
|
|
151
|
+
err.response = response;
|
|
152
|
+
err.status = status;
|
|
153
|
+
};
|
|
154
|
+
var fireError = function (err) {
|
|
155
|
+
var apiError = new ApiError();
|
|
156
|
+
if (typeof err === 'object') {
|
|
157
|
+
apiError.message = err.message;
|
|
158
|
+
apiError.code = err.code;
|
|
159
|
+
setErrorInfo(apiError, err.status, err.response);
|
|
160
|
+
}
|
|
161
|
+
else if (typeof err === 'string') {
|
|
162
|
+
apiError.message = err;
|
|
163
|
+
}
|
|
164
|
+
else if (err) {
|
|
165
|
+
apiError.message = err.toString();
|
|
166
|
+
}
|
|
167
|
+
if (_this.option.onError)
|
|
168
|
+
_this.option.onError(apiError);
|
|
169
|
+
reject(apiError);
|
|
170
|
+
};
|
|
171
|
+
var instance = AxiosCreate();
|
|
172
|
+
var requestInterceptor;
|
|
173
|
+
if (_this.option.onRequest) {
|
|
174
|
+
requestInterceptor = instance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
175
|
+
return __generator(this, function (_a) {
|
|
176
|
+
if (this.option.onRequest) {
|
|
177
|
+
return [2 /*return*/, this.option.onRequest(config, this.option.baseUrl, path, data, option)];
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
return [2 /*return*/, config];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}); });
|
|
184
|
+
}
|
|
185
|
+
instance
|
|
186
|
+
.request(requestConfig)
|
|
187
|
+
.then(function (res) {
|
|
188
|
+
var resData = res.data;
|
|
189
|
+
if (_this.option.onResponse) {
|
|
190
|
+
_this.option
|
|
191
|
+
.onResponse(res, requestConfig, _this.option.baseUrl, path, data, option)
|
|
192
|
+
.then(function (finalResData) {
|
|
193
|
+
resolve(finalResData);
|
|
194
|
+
})
|
|
195
|
+
.catch(function (err) {
|
|
196
|
+
setErrorInfo(err, res.status, res);
|
|
197
|
+
fireError(err);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
resolve(resData);
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
.catch(fireError)
|
|
205
|
+
.finally(function () {
|
|
206
|
+
if (requestInterceptor) {
|
|
207
|
+
instance.interceptors.request.eject(requestInterceptor);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
this.option = option;
|
|
213
|
+
}
|
|
214
|
+
Api.prototype.get = function (path, data, option) {
|
|
215
|
+
return this.run('get', path, data, option);
|
|
216
|
+
};
|
|
217
|
+
Api.prototype.post = function (path, data, option) {
|
|
218
|
+
return this.run('post', path, data, option);
|
|
219
|
+
};
|
|
220
|
+
Api.prototype.patch = function (path, data, option) {
|
|
221
|
+
return this.run('patch', path, data, option);
|
|
222
|
+
};
|
|
223
|
+
Api.prototype.delete = function (path, data, option) {
|
|
224
|
+
return this.run('delete', path, data, option);
|
|
225
|
+
};
|
|
226
|
+
return Api;
|
|
266
227
|
}());export{Api,ApiError};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict';var axios=require('axios');/******************************************************************************
|
|
1
|
+
'use strict';var axios=require('axios'),util=require('@pdg/util');/******************************************************************************
|
|
2
2
|
Copyright (c) Microsoft Corporation.
|
|
3
3
|
|
|
4
4
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
@@ -81,186 +81,147 @@ 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
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
else
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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;
|
|
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 _a;
|
|
95
|
+
var AxiosCreate = axios.create ? axios.create : (_a = require('axios').default) === null || _a === void 0 ? void 0 : _a.create;
|
|
96
|
+
var Api = /** @class */ (function () {
|
|
97
|
+
// constructor -------------------------------------------------------------------------------------------------------
|
|
98
|
+
function Api(option) {
|
|
99
|
+
var _this = this;
|
|
100
|
+
// Run ---------------------------------------------------------------------------------------------------------------
|
|
101
|
+
this.run = function (method, path, data, option) {
|
|
102
|
+
return new Promise(function (resolve, reject) {
|
|
103
|
+
var _a;
|
|
104
|
+
var headers = __assign({}, _this.option.headers);
|
|
105
|
+
if (typeof window !== 'undefined') {
|
|
106
|
+
if ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.href) {
|
|
107
|
+
headers['X-Referer'] = window.location.href;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
var requestConfig = {
|
|
111
|
+
method: method,
|
|
112
|
+
withCredentials: _this.option.withCredentials,
|
|
113
|
+
headers: headers,
|
|
114
|
+
silent: !!(option === null || option === void 0 ? void 0 : option.silent),
|
|
115
|
+
};
|
|
116
|
+
if (option === null || option === void 0 ? void 0 : option.raw) {
|
|
117
|
+
requestConfig.responseType = (option === null || option === void 0 ? void 0 : option.rawResponseType) || 'arraybuffer';
|
|
118
|
+
}
|
|
119
|
+
requestConfig.url = util.urlJoin(_this.option.baseUrl, path.replace(/\./g, '/'));
|
|
120
|
+
if (data) {
|
|
121
|
+
if (method === 'get') {
|
|
122
|
+
if (util.notEmpty(data)) {
|
|
123
|
+
var finalData = {};
|
|
124
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
125
|
+
for (var key in data) {
|
|
126
|
+
if (data[key] != null) {
|
|
127
|
+
finalData[key] = data[key];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
requestConfig.url += "?".concat(new URLSearchParams(finalData).toString());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (data instanceof FormData) {
|
|
135
|
+
data.append(_this.option.timeParamName, "".concat(new Date().getTime()));
|
|
136
|
+
requestConfig.data = data;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
var finalData = __assign({}, data);
|
|
140
|
+
finalData[_this.option.timeParamName] = new Date().getTime();
|
|
141
|
+
requestConfig.data = finalData;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var setErrorInfo = function (err, status, response) {
|
|
146
|
+
err.config = requestConfig;
|
|
147
|
+
err.baseUrl = _this.option.baseUrl;
|
|
148
|
+
err.path = path;
|
|
149
|
+
err.requestData = data;
|
|
150
|
+
err.requestOption = option;
|
|
151
|
+
err.response = response;
|
|
152
|
+
err.status = status;
|
|
153
|
+
};
|
|
154
|
+
var fireError = function (err) {
|
|
155
|
+
var apiError = new ApiError();
|
|
156
|
+
if (typeof err === 'object') {
|
|
157
|
+
apiError.message = err.message;
|
|
158
|
+
apiError.code = err.code;
|
|
159
|
+
setErrorInfo(apiError, err.status, err.response);
|
|
160
|
+
}
|
|
161
|
+
else if (typeof err === 'string') {
|
|
162
|
+
apiError.message = err;
|
|
163
|
+
}
|
|
164
|
+
else if (err) {
|
|
165
|
+
apiError.message = err.toString();
|
|
166
|
+
}
|
|
167
|
+
if (_this.option.onError)
|
|
168
|
+
_this.option.onError(apiError);
|
|
169
|
+
reject(apiError);
|
|
170
|
+
};
|
|
171
|
+
var instance = AxiosCreate();
|
|
172
|
+
var requestInterceptor;
|
|
173
|
+
if (_this.option.onRequest) {
|
|
174
|
+
requestInterceptor = instance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
|
175
|
+
return __generator(this, function (_a) {
|
|
176
|
+
if (this.option.onRequest) {
|
|
177
|
+
return [2 /*return*/, this.option.onRequest(config, this.option.baseUrl, path, data, option)];
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
return [2 /*return*/, config];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}); });
|
|
184
|
+
}
|
|
185
|
+
instance
|
|
186
|
+
.request(requestConfig)
|
|
187
|
+
.then(function (res) {
|
|
188
|
+
var resData = res.data;
|
|
189
|
+
if (_this.option.onResponse) {
|
|
190
|
+
_this.option
|
|
191
|
+
.onResponse(res, requestConfig, _this.option.baseUrl, path, data, option)
|
|
192
|
+
.then(function (finalResData) {
|
|
193
|
+
resolve(finalResData);
|
|
194
|
+
})
|
|
195
|
+
.catch(function (err) {
|
|
196
|
+
setErrorInfo(err, res.status, res);
|
|
197
|
+
fireError(err);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
resolve(resData);
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
.catch(fireError)
|
|
205
|
+
.finally(function () {
|
|
206
|
+
if (requestInterceptor) {
|
|
207
|
+
instance.interceptors.request.eject(requestInterceptor);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
this.option = option;
|
|
213
|
+
}
|
|
214
|
+
Api.prototype.get = function (path, data, option) {
|
|
215
|
+
return this.run('get', path, data, option);
|
|
216
|
+
};
|
|
217
|
+
Api.prototype.post = function (path, data, option) {
|
|
218
|
+
return this.run('post', path, data, option);
|
|
219
|
+
};
|
|
220
|
+
Api.prototype.patch = function (path, data, option) {
|
|
221
|
+
return this.run('patch', path, data, option);
|
|
222
|
+
};
|
|
223
|
+
Api.prototype.delete = function (path, data, option) {
|
|
224
|
+
return this.run('delete', path, data, option);
|
|
225
|
+
};
|
|
226
|
+
return Api;
|
|
266
227
|
}());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.13",
|
|
5
5
|
"description": "API Module",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"javascript"
|
|
37
37
|
],
|
|
38
38
|
"peerDependencies": {
|
|
39
|
+
"@pdg/util": "^1.0.0",
|
|
39
40
|
"axios": "^1.0.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
@@ -54,6 +55,6 @@
|
|
|
54
55
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
55
56
|
"rollup-plugin-postcss": "^4.0.2",
|
|
56
57
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
57
|
-
"typescript": "^
|
|
58
|
+
"typescript": "^4.9.5"
|
|
58
59
|
}
|
|
59
60
|
}
|
package/dist/@util/compare.d.ts
DELETED
package/dist/@util/index.d.ts
DELETED
package/dist/@util/url.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function joinUrl(...parts: string[]): string;
|