UNPKG

infobip-rtc

Version:

Infobip RTC JavaScript SDK - Infobip WebRTC API Implementation

91 lines 3.35 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const networkErrorMessages = new Set([ 'network error', 'Failed to fetch', 'NetworkError when attempting to fetch resource.', 'The Internet connection appears to be offline.', 'Load failed', 'Network request failed', 'fetch failed', ]); const DEFAULT_NETWORK_ERROR_MESSAGE = "Network error occurred."; export class NetworkError extends Error { constructor(message) { super(message || DEFAULT_NETWORK_ERROR_MESSAGE); Object.setPrototypeOf(this, NetworkError.prototype); this.name = 'NetworkError'; } } function isNetworkError(error) { const isValid = error.name === 'TypeError' && typeof error.message === 'string'; if (!isValid) { return false; } if (error.message === 'Load failed') { return error.stack === undefined; } return networkErrorMessages.has(error.message); } export class DefaultHttpClient { constructor(baseURL, headers) { this.baseURL = baseURL; this.headers = headers; } delete(url, headers) { return this.request(url, "DELETE", headers); } get(url, headers) { return this.request(url, "GET", headers); } post(url, data, headers) { return this.request(url, "POST", data, headers); } put(url, data, headers) { return this.request(url, "PUT", data, headers); } request(url, method, data, headers) { return __awaiter(this, void 0, void 0, function* () { const fullPath = this.baseURL + url; const options = { method: method }; if (data != null) { const isFormData = data instanceof FormData; options.body = isFormData ? data : JSON.stringify(data); } if (this.headers != null || headers != null) { options.headers = Object.assign(Object.assign({}, this.headers), headers); } try { const response = yield fetch(fullPath, options); const resultJson = yield response.json(); const result = { statusCode: response.status, ok: response.ok }; if (result.ok) { result.data = resultJson; } else { result.error = resultJson; } return result; } catch (e) { if (isNetworkError(e)) { throw new NetworkError(); } throw e; } }); } } //# sourceMappingURL=HttpClient.js.map