@internxt/sdk 1.12.1 → 1.12.3
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.
|
@@ -1,8 +1,21 @@
|
|
|
1
|
+
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
1
2
|
import { Headers, Parameters, RequestCanceler, URL, UnauthorizedCallback } from './types';
|
|
2
3
|
export { RequestCanceler } from './types';
|
|
4
|
+
export interface CustomInterceptor {
|
|
5
|
+
request?: {
|
|
6
|
+
onFulfilled?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
|
|
7
|
+
onRejected?: (error: unknown) => unknown;
|
|
8
|
+
};
|
|
9
|
+
response?: {
|
|
10
|
+
onFulfilled?: (response: AxiosResponse) => AxiosResponse;
|
|
11
|
+
onRejected?: (error: unknown) => unknown;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
3
14
|
export declare class HttpClient {
|
|
4
15
|
private readonly axios;
|
|
5
16
|
private readonly unauthorizedCallback;
|
|
17
|
+
static globalInterceptors: CustomInterceptor[];
|
|
18
|
+
static setGlobalInterceptors(interceptors: CustomInterceptor[]): void;
|
|
6
19
|
static create(baseURL: URL, unauthorizedCallback?: UnauthorizedCallback): HttpClient;
|
|
7
20
|
private constructor();
|
|
8
21
|
/**
|
|
@@ -8,12 +8,24 @@ var axios_1 = __importDefault(require("axios"));
|
|
|
8
8
|
var errors_1 = __importDefault(require("../types/errors"));
|
|
9
9
|
var HttpClient = /** @class */ (function () {
|
|
10
10
|
function HttpClient(baseURL, unauthorizedCallback) {
|
|
11
|
+
var _this = this;
|
|
11
12
|
this.axios = axios_1.default.create({
|
|
12
13
|
baseURL: baseURL,
|
|
13
14
|
});
|
|
14
15
|
this.unauthorizedCallback = unauthorizedCallback;
|
|
16
|
+
HttpClient.globalInterceptors.forEach(function (interceptor) {
|
|
17
|
+
if (interceptor.request) {
|
|
18
|
+
_this.axios.interceptors.request.use(interceptor.request.onFulfilled, interceptor.request.onRejected);
|
|
19
|
+
}
|
|
20
|
+
if (interceptor.response) {
|
|
21
|
+
_this.axios.interceptors.response.use(interceptor.response.onFulfilled, interceptor.response.onRejected);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
15
24
|
this.initializeMiddleware();
|
|
16
25
|
}
|
|
26
|
+
HttpClient.setGlobalInterceptors = function (interceptors) {
|
|
27
|
+
HttpClient.globalInterceptors = interceptors;
|
|
28
|
+
};
|
|
17
29
|
HttpClient.create = function (baseURL, unauthorizedCallback) {
|
|
18
30
|
if (unauthorizedCallback === undefined) {
|
|
19
31
|
unauthorizedCallback = function () { return null; };
|
|
@@ -169,7 +181,7 @@ var HttpClient = /** @class */ (function () {
|
|
|
169
181
|
* @private
|
|
170
182
|
*/
|
|
171
183
|
HttpClient.prototype.normalizeError = function (error) {
|
|
172
|
-
var errorMessage, errorStatus, errorCode;
|
|
184
|
+
var errorMessage, errorStatus, errorCode, errorHeaders;
|
|
173
185
|
if (error.response) {
|
|
174
186
|
var response = error.response;
|
|
175
187
|
if (response.status === 401) {
|
|
@@ -178,6 +190,7 @@ var HttpClient = /** @class */ (function () {
|
|
|
178
190
|
errorMessage = response.data.message || response.data.error || JSON.stringify(response.data);
|
|
179
191
|
errorStatus = response.status;
|
|
180
192
|
errorCode = response.data.code;
|
|
193
|
+
errorHeaders = response.headers;
|
|
181
194
|
}
|
|
182
195
|
else if (error.request) {
|
|
183
196
|
errorMessage = 'Server unavailable';
|
|
@@ -187,8 +200,9 @@ var HttpClient = /** @class */ (function () {
|
|
|
187
200
|
errorMessage = error.message;
|
|
188
201
|
errorStatus = 400;
|
|
189
202
|
}
|
|
190
|
-
throw new errors_1.default(errorMessage, errorStatus, errorCode);
|
|
203
|
+
throw new errors_1.default(errorMessage, errorStatus, errorCode, errorHeaders);
|
|
191
204
|
};
|
|
205
|
+
HttpClient.globalInterceptors = [];
|
|
192
206
|
return HttpClient;
|
|
193
207
|
}());
|
|
194
208
|
exports.HttpClient = HttpClient;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export default class AppError extends Error {
|
|
2
2
|
readonly status?: number;
|
|
3
3
|
readonly code?: string;
|
|
4
|
-
|
|
4
|
+
readonly headers?: Record<string, string>;
|
|
5
|
+
constructor(message: string, status?: number, code?: string, headers?: Record<string, string>);
|
|
5
6
|
}
|
|
@@ -17,10 +17,11 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
var AppError = /** @class */ (function (_super) {
|
|
19
19
|
__extends(AppError, _super);
|
|
20
|
-
function AppError(message, status, code) {
|
|
20
|
+
function AppError(message, status, code, headers) {
|
|
21
21
|
var _this = _super.call(this, message) || this;
|
|
22
22
|
_this.status = status;
|
|
23
23
|
_this.code = code;
|
|
24
|
+
_this.headers = headers;
|
|
24
25
|
return _this;
|
|
25
26
|
}
|
|
26
27
|
return AppError;
|