@openocean.finance/widget-sdk
Version:
OpenOcean Any-to-Any Cross-Chain-Swap SDK
69 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.request = exports.requestSettings = void 0;
const config_js_1 = require("./config.js");
const SDKError_js_1 = require("./errors/SDKError.js");
const errors_js_1 = require("./errors/errors.js");
const httpError_js_1 = require("./errors/httpError.js");
const sleep_js_1 = require("./utils/sleep.js");
const version_js_1 = require("./version.js");
exports.requestSettings = {
retries: 1,
};
const stripExtendRequestInitProperties = ({ retries, ...rest }) => ({
...rest,
});
const request = async (url, options = {
retries: exports.requestSettings.retries,
}) => {
const { userId, integrator, widgetVersion, apiKey } = config_js_1.config.get();
if (!integrator) {
throw new SDKError_js_1.SDKError(new errors_js_1.ValidationError('You need to provide the Integrator property. Please see documentation https://openocean.finance/api'));
}
options.retries = options.retries ?? exports.requestSettings.retries;
try {
if (apiKey) {
options.headers = {
...options.headers,
'x-lifi-api-key': apiKey,
};
}
if (userId) {
options.headers = {
...options.headers,
'x-lifi-userid': userId,
};
}
if (widgetVersion) {
options.headers = {
...options.headers,
'x-lifi-widget': widgetVersion,
};
}
if (version_js_1.version) {
options.headers = {
...options.headers,
'x-lifi-sdk': version_js_1.version,
};
}
options.headers = {
...options.headers,
'x-lifi-integrator': integrator,
};
const response = await fetch(url, stripExtendRequestInitProperties(options));
if (!response.ok) {
throw new httpError_js_1.HTTPError(response, url, options);
}
return await response.json();
}
catch (error) {
if (options.retries > 0 && error.status === 500) {
await (0, sleep_js_1.sleep)(500);
return (0, exports.request)(url, { ...options, retries: options.retries - 1 });
}
await error.buildAdditionalDetails?.();
throw new SDKError_js_1.SDKError(error);
}
};
exports.request = request;
//# sourceMappingURL=request.js.map