@or-sdk/base
Advanced tools
Comparing version 0.34.1 to 0.35.0-beta.2554.0
@@ -271,3 +271,3 @@ "use strict"; | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -279,2 +279,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -287,3 +288,3 @@ * ``` | ||
return __awaiter(this, void 0, void 0, function () { | ||
var conf, data, e_2; | ||
var timeoutTimer, timeoutController_1, signal, conf, data, e_2; | ||
return __generator(this, function (_a) { | ||
@@ -298,3 +299,8 @@ switch (_a.label) { | ||
case 2: | ||
_a.trys.push([2, 4, , 5]); | ||
_a.trys.push([2, 4, 5, 6]); | ||
signal = params.signal; | ||
if (params.timeout && !params.signal) { | ||
timeoutController_1 = new AbortController(); | ||
signal = timeoutController_1.signal; | ||
} | ||
conf = { | ||
@@ -306,5 +312,9 @@ url: this.getUrl(params), | ||
headers: __assign(__assign({}, this.getHeaders()), params.customHeaders), | ||
signal: params.signal, | ||
signal: signal, | ||
withCredentials: params.withCredentials, | ||
timeout: params.timeout, | ||
}; | ||
if (timeoutController_1) { | ||
timeoutTimer = setTimeout(function () { return timeoutController_1 === null || timeoutController_1 === void 0 ? void 0 : timeoutController_1.abort(constants_1.TIMEOUT_SIGNAL_REASON); }); | ||
} | ||
return [4 /*yield*/, this.axios(conf)]; | ||
@@ -317,3 +327,7 @@ case 3: | ||
throw this.parseError(e_2); | ||
case 5: return [2 /*return*/]; | ||
case 5: | ||
if (timeoutTimer) | ||
clearTimeout(timeoutTimer); | ||
return [7 /*endfinally*/]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
@@ -324,3 +338,3 @@ }); | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -332,2 +346,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -339,4 +354,3 @@ * ``` | ||
return __awaiter(this, void 0, void 0, function () { | ||
var query, data; | ||
var _this = this; | ||
var timeoutTimer, timeoutController_2, signal, query, axiosConfig, data, error_1; | ||
return __generator(this, function (_a) { | ||
@@ -351,2 +365,8 @@ switch (_a.label) { | ||
case 2: | ||
_a.trys.push([2, 4, 5, 6]); | ||
signal = params.signal; | ||
if (params.timeout && !params.signal) { | ||
timeoutController_2 = new AbortController(); | ||
signal = timeoutController_2.signal; | ||
} | ||
query = params.params || {}; | ||
@@ -356,16 +376,27 @@ if (this.isCrossAccount) { | ||
} | ||
return [4 /*yield*/, this.axios({ | ||
url: this.getUrl(params), | ||
method: params.method || 'GET', | ||
data: params.data, | ||
params: query, | ||
headers: __assign(__assign({}, this.getHeaders()), params.customHeaders), | ||
signal: params.signal, | ||
withCredentials: params.withCredentials, | ||
}).catch(function (e) { | ||
throw _this.parseError(e); | ||
})]; | ||
axiosConfig = { | ||
url: this.getUrl(params), | ||
method: params.method || 'GET', | ||
data: params.data, | ||
params: query, | ||
headers: __assign(__assign({}, this.getHeaders()), params.customHeaders), | ||
signal: signal, | ||
withCredentials: params.withCredentials, | ||
timeout: params.timeout, | ||
}; | ||
if (timeoutController_2) { | ||
timeoutTimer = setTimeout(function () { return timeoutController_2 === null || timeoutController_2 === void 0 ? void 0 : timeoutController_2.abort(constants_1.TIMEOUT_SIGNAL_REASON); }); | ||
} | ||
return [4 /*yield*/, this.axios(axiosConfig)]; | ||
case 3: | ||
data = (_a.sent()).data; | ||
return [2 /*return*/, data]; | ||
case 4: | ||
error_1 = _a.sent(); | ||
throw this.parseError(error_1); | ||
case 5: | ||
if (timeoutTimer) | ||
clearTimeout(timeoutTimer); | ||
return [7 /*endfinally*/]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
@@ -372,0 +403,0 @@ }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OrderDirection = exports.DISCOVERY_ROUTE_PROVIDERS_ACCOUNT_ID = exports.DISCOVERY_ROUTE_ACCOUNTID = exports.DISCOVERY_ROUTE = exports.VERSION = exports.SdkStatus = void 0; | ||
exports.TIMEOUT_SIGNAL_REASON = exports.OrderDirection = exports.DISCOVERY_ROUTE_PROVIDERS_ACCOUNT_ID = exports.DISCOVERY_ROUTE_ACCOUNTID = exports.DISCOVERY_ROUTE = exports.VERSION = exports.SdkStatus = void 0; | ||
var SdkStatus; | ||
@@ -19,2 +19,3 @@ (function (SdkStatus) { | ||
})(OrderDirection = exports.OrderDirection || (exports.OrderDirection = {})); | ||
exports.TIMEOUT_SIGNAL_REASON = 'Request timed out'; | ||
//# sourceMappingURL=constants.js.map |
@@ -20,2 +20,3 @@ "use strict"; | ||
Object.defineProperty(exports, "Base", { enumerable: true, get: function () { return Base_1.Base; } }); | ||
__exportStar(require("./errors"), exports); | ||
__exportStar(require("./types"), exports); | ||
@@ -22,0 +23,0 @@ __exportStar(require("./utils"), exports); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var axios_1 = require("axios"); | ||
var errors_1 = require("../errors"); | ||
var constants_1 = require("../constants"); | ||
var parseAxiosError = function (e) { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
// handle timeout from axios `timeout` config property | ||
// or from AbortController injected in callApi method | ||
if (e.code === 'ECONNABORTED' && ((_a = e.message) === null || _a === void 0 ? void 0 : _a.startsWith('timeout')) | ||
|| e instanceof axios_1.CanceledError && ((_c = (_b = e.config) === null || _b === void 0 ? void 0 : _b.signal) === null || _c === void 0 ? void 0 : _c.reason) === constants_1.TIMEOUT_SIGNAL_REASON) { | ||
var errorMessage = 'Request timeout out'; | ||
var timeoutMs = (_d = e.config) === null || _d === void 0 ? void 0 : _d.timeout; | ||
if (timeoutMs) | ||
errorMessage += " after ".concat(timeoutMs, " ms"); | ||
return new errors_1.ApiTimeoutError(errorMessage, { cause: e }); | ||
} | ||
var res; | ||
@@ -9,3 +23,3 @@ if (e.response) { | ||
var responseError = responseData.error; | ||
res = responseMessage || responseError || responseData; | ||
res = (_g = (_f = (_e = responseError === null || responseError === void 0 ? void 0 : responseError.message) !== null && _e !== void 0 ? _e : responseError) !== null && _f !== void 0 ? _f : responseMessage) !== null && _g !== void 0 ? _g : responseData; | ||
} | ||
@@ -18,5 +32,5 @@ else if (e.request) { | ||
} | ||
return new Error(typeof res === 'string' ? res : e.message); | ||
return new Error(typeof res === 'string' ? res : e.message, { cause: e }); | ||
}; | ||
exports.default = parseAxiosError; | ||
//# sourceMappingURL=parseAxiosError.js.map |
@@ -12,3 +12,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { normalizeRoute, normalizeUrl, paramsSerializer, parseAxiosError, getServiceUrlWithRoute } from './utils'; | ||
import { DISCOVERY_ROUTE, DISCOVERY_ROUTE_ACCOUNTID, DISCOVERY_ROUTE_PROVIDERS_ACCOUNT_ID, SdkStatus, } from './constants'; | ||
import { DISCOVERY_ROUTE, DISCOVERY_ROUTE_ACCOUNTID, DISCOVERY_ROUTE_PROVIDERS_ACCOUNT_ID, TIMEOUT_SIGNAL_REASON, SdkStatus, } from './constants'; | ||
export class Base { | ||
@@ -175,3 +175,3 @@ constructor({ token, discoveryUrl, serviceKey, requestAccountId, requestProvidersAccountId, feature, accountId, serviceUrl, useDefaultSerializer, paramSerializer = paramsSerializer, httpAgents = {}, interceptors = {}, }) { | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -183,2 +183,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -191,2 +192,3 @@ * ``` | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let timeoutTimer; | ||
if (this.status === SdkStatus.NEW) { | ||
@@ -196,2 +198,8 @@ yield this.init(); | ||
try { | ||
let timeoutController; | ||
let signal = params.signal; | ||
if (params.timeout && !params.signal) { | ||
timeoutController = new AbortController(); | ||
signal = timeoutController.signal; | ||
} | ||
const conf = { | ||
@@ -203,5 +211,9 @@ url: this.getUrl(params), | ||
headers: Object.assign(Object.assign({}, this.getHeaders()), params.customHeaders), | ||
signal: params.signal, | ||
signal, | ||
withCredentials: params.withCredentials, | ||
timeout: params.timeout, | ||
}; | ||
if (timeoutController) { | ||
timeoutTimer = setTimeout(() => timeoutController === null || timeoutController === void 0 ? void 0 : timeoutController.abort(TIMEOUT_SIGNAL_REASON)); | ||
} | ||
const { data } = yield this.axios(conf); | ||
@@ -213,6 +225,10 @@ return data; | ||
} | ||
finally { | ||
if (timeoutTimer) | ||
clearTimeout(timeoutTimer); | ||
} | ||
}); | ||
} | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -224,2 +240,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -231,22 +248,41 @@ * ``` | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let timeoutTimer; | ||
if (this.status === SdkStatus.NEW) { | ||
yield this.init(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const query = params.params || {}; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
try { | ||
let timeoutController; | ||
let signal = params.signal; | ||
if (params.timeout && !params.signal) { | ||
timeoutController = new AbortController(); | ||
signal = timeoutController.signal; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const query = params.params || {}; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
} | ||
const axiosConfig = { | ||
url: this.getUrl(params), | ||
method: params.method || 'GET', | ||
data: params.data, | ||
params: query, | ||
headers: Object.assign(Object.assign({}, this.getHeaders()), params.customHeaders), | ||
signal, | ||
withCredentials: params.withCredentials, | ||
timeout: params.timeout, | ||
}; | ||
if (timeoutController) { | ||
timeoutTimer = setTimeout(() => timeoutController === null || timeoutController === void 0 ? void 0 : timeoutController.abort(TIMEOUT_SIGNAL_REASON)); | ||
} | ||
const { data } = yield this.axios(axiosConfig); | ||
return data; | ||
} | ||
const { data } = yield this.axios({ | ||
url: this.getUrl(params), | ||
method: params.method || 'GET', | ||
data: params.data, | ||
params: query, | ||
headers: Object.assign(Object.assign({}, this.getHeaders()), params.customHeaders), | ||
signal: params.signal, | ||
withCredentials: params.withCredentials, | ||
}).catch(e => { | ||
throw this.parseError(e); | ||
}); | ||
return data; | ||
catch (error) { | ||
throw this.parseError(error); | ||
} | ||
finally { | ||
if (timeoutTimer) | ||
clearTimeout(timeoutTimer); | ||
} | ||
}); | ||
@@ -253,0 +289,0 @@ } |
@@ -16,2 +16,3 @@ export var SdkStatus; | ||
})(OrderDirection || (OrderDirection = {})); | ||
export const TIMEOUT_SIGNAL_REASON = 'Request timed out'; | ||
//# sourceMappingURL=constants.js.map |
export { Base } from './Base'; | ||
export * from './errors'; | ||
export * from './types'; | ||
@@ -3,0 +4,0 @@ export * from './utils'; |
@@ -0,2 +1,16 @@ | ||
import { CanceledError } from 'axios'; | ||
import { ApiTimeoutError } from '../errors'; | ||
import { TIMEOUT_SIGNAL_REASON } from '../constants'; | ||
const parseAxiosError = (e) => { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
// handle timeout from axios `timeout` config property | ||
// or from AbortController injected in callApi method | ||
if (e.code === 'ECONNABORTED' && ((_a = e.message) === null || _a === void 0 ? void 0 : _a.startsWith('timeout')) | ||
|| e instanceof CanceledError && ((_c = (_b = e.config) === null || _b === void 0 ? void 0 : _b.signal) === null || _c === void 0 ? void 0 : _c.reason) === TIMEOUT_SIGNAL_REASON) { | ||
let errorMessage = 'Request timeout out'; | ||
const timeoutMs = (_d = e.config) === null || _d === void 0 ? void 0 : _d.timeout; | ||
if (timeoutMs) | ||
errorMessage += ` after ${timeoutMs} ms`; | ||
return new ApiTimeoutError(errorMessage, { cause: e }); | ||
} | ||
let res; | ||
@@ -7,3 +21,3 @@ if (e.response) { | ||
const responseError = responseData.error; | ||
res = responseMessage || responseError || responseData; | ||
res = (_g = (_f = (_e = responseError === null || responseError === void 0 ? void 0 : responseError.message) !== null && _e !== void 0 ? _e : responseError) !== null && _f !== void 0 ? _f : responseMessage) !== null && _g !== void 0 ? _g : responseData; | ||
} | ||
@@ -16,5 +30,5 @@ else if (e.request) { | ||
} | ||
return new Error(typeof res === 'string' ? res : e.message); | ||
return new Error(typeof res === 'string' ? res : e.message, { cause: e }); | ||
}; | ||
export default parseAxiosError; | ||
//# sourceMappingURL=parseAxiosError.js.map |
@@ -61,3 +61,3 @@ import { RawAxiosRequestHeaders, AxiosError } from 'axios'; | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -69,2 +69,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -77,3 +78,3 @@ * ``` | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -85,2 +86,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -87,0 +89,0 @@ * ``` |
@@ -14,2 +14,3 @@ export declare enum SdkStatus { | ||
} | ||
export declare const TIMEOUT_SIGNAL_REASON = "Request timed out"; | ||
//# sourceMappingURL=constants.d.ts.map |
export { Base } from './Base'; | ||
export * from './errors'; | ||
export * from './types'; | ||
@@ -3,0 +4,0 @@ export * from './utils'; |
@@ -78,2 +78,3 @@ /// <reference types="node" /> | ||
withCredentials?: boolean; | ||
timeout?: number; | ||
} | ||
@@ -80,0 +81,0 @@ export declare type PaginationOptions = { |
{ | ||
"name": "@or-sdk/base", | ||
"version": "0.34.1", | ||
"version": "0.35.0-beta.2554.0", | ||
"main": "dist/cjs/index.js", | ||
@@ -35,4 +35,3 @@ "module": "dist/esm/index.js", | ||
"access": "public" | ||
}, | ||
"gitHead": "612a537e3697746f2d786500335d78b1e2b9c9f3" | ||
} | ||
} |
@@ -8,2 +8,3 @@ import axios, { AxiosInstance, RawAxiosRequestHeaders, AxiosRequestConfig, AxiosError } from 'axios'; | ||
DISCOVERY_ROUTE_PROVIDERS_ACCOUNT_ID, | ||
TIMEOUT_SIGNAL_REASON, | ||
SdkStatus, | ||
@@ -220,3 +221,3 @@ } from './constants'; | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -228,2 +229,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -235,6 +237,17 @@ * ``` | ||
protected async callApi<T>(params: CalApiParams): Promise<T> { | ||
let timeoutTimer: NodeJS.Timeout | undefined; | ||
if (this.status === SdkStatus.NEW) { | ||
await this.init(); | ||
} | ||
try { | ||
let timeoutController: AbortController | undefined; | ||
let signal = params.signal; | ||
if (params.timeout && !params.signal) { | ||
timeoutController = new AbortController(); | ||
signal = timeoutController.signal; | ||
} | ||
const conf: AxiosRequestConfig = { | ||
@@ -249,9 +262,17 @@ url: this.getUrl(params), | ||
}, | ||
signal: params.signal, | ||
signal, | ||
withCredentials: params.withCredentials, | ||
timeout: params.timeout, | ||
}; | ||
if (timeoutController) { | ||
timeoutTimer = setTimeout(() => timeoutController?.abort(TIMEOUT_SIGNAL_REASON)); | ||
} | ||
const { data } = await this.axios(conf); | ||
return data; | ||
} catch (e) { | ||
throw this.parseError(e as AxiosError); | ||
} finally { | ||
if (timeoutTimer) clearTimeout(timeoutTimer); | ||
} | ||
@@ -261,3 +282,3 @@ } | ||
/** | ||
* Make api request | ||
* Make API request | ||
* ```typescript | ||
@@ -269,2 +290,3 @@ * const result = await instance.callApi({ | ||
* params: queryParams, | ||
* timeout: 2500, | ||
* }); | ||
@@ -275,26 +297,49 @@ * ``` | ||
protected async callApiV2<T>(params: CalApiParams): Promise<T> { | ||
let timeoutTimer: NodeJS.Timeout | undefined; | ||
if (this.status === SdkStatus.NEW) { | ||
await this.init(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const query: any = params.params || {}; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
try { | ||
let timeoutController: AbortController | undefined; | ||
let signal = params.signal; | ||
if (params.timeout && !params.signal) { | ||
timeoutController = new AbortController(); | ||
signal = timeoutController.signal; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const query: any = params.params || {}; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
} | ||
const axiosConfig: AxiosRequestConfig = { | ||
url: this.getUrl(params), | ||
method: params.method || 'GET', | ||
data: params.data, | ||
params: query, | ||
headers: { | ||
...this.getHeaders(), | ||
...params.customHeaders, | ||
}, | ||
signal, | ||
withCredentials: params.withCredentials, | ||
timeout: params.timeout, | ||
}; | ||
if (timeoutController) { | ||
timeoutTimer = setTimeout(() => timeoutController?.abort(TIMEOUT_SIGNAL_REASON)); | ||
} | ||
const { data } = await this.axios(axiosConfig); | ||
return data; | ||
} catch (error) { | ||
throw this.parseError(error as AxiosError); | ||
} finally { | ||
if (timeoutTimer) clearTimeout(timeoutTimer); | ||
} | ||
const { data } = await this.axios({ | ||
url: this.getUrl(params), | ||
method: params.method || 'GET', | ||
data: params.data, | ||
params: query, | ||
headers: { | ||
...this.getHeaders(), | ||
...params.customHeaders, | ||
}, | ||
signal: params.signal, | ||
withCredentials: params.withCredentials, | ||
}).catch(e => { | ||
throw this.parseError(e); | ||
}); | ||
return data; | ||
} | ||
@@ -301,0 +346,0 @@ |
@@ -19,1 +19,3 @@ export enum SdkStatus { | ||
} | ||
export const TIMEOUT_SIGNAL_REASON = 'Request timed out'; |
export { Base } from './Base'; | ||
export * from './errors'; | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './constants'; |
@@ -94,2 +94,3 @@ import { AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios'; | ||
withCredentials?: boolean; | ||
timeout?: number; | ||
} | ||
@@ -96,0 +97,0 @@ |
@@ -1,6 +0,17 @@ | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
import { AxiosError, AxiosResponse, CanceledError } from 'axios'; | ||
import { ApiTimeoutError } from '../errors'; | ||
import { TIMEOUT_SIGNAL_REASON } from '../constants'; | ||
const parseAxiosError = (e: AxiosError): Error => { | ||
// handle timeout from axios `timeout` config property | ||
// or from AbortController injected in callApi method | ||
if (e.code === 'ECONNABORTED' && e.message?.startsWith('timeout') | ||
|| e instanceof CanceledError && (e.config?.signal as AbortSignal)?.reason === TIMEOUT_SIGNAL_REASON) { | ||
let errorMessage = 'Request timeout out'; | ||
const timeoutMs = e.config?.timeout; | ||
if (timeoutMs) errorMessage += ` after ${timeoutMs} ms`; | ||
return new ApiTimeoutError(errorMessage, { cause: e }); | ||
} | ||
let res; | ||
if (e.response as AxiosResponse) { | ||
@@ -10,3 +21,3 @@ const responseData = (e as any).response.data; | ||
const responseError = responseData.error; | ||
res = responseMessage || responseError || responseData; | ||
res = responseError?.message ?? responseError ?? responseMessage ?? responseData; | ||
} else if (e.request) { | ||
@@ -18,5 +29,5 @@ res = e.request; | ||
return new Error(typeof res === 'string' ? res : e.message); | ||
return new Error(typeof res === 'string' ? res : e.message, { cause: e }); | ||
}; | ||
export default parseAxiosError; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
162284
112
2522