UNPKG

react-native-xenon

Version:

A powerful in-app debugging tool for React Native.

105 lines (97 loc) 4.64 kB
"use strict"; var _class, _class2; function _applyDecoratedDescriptor(i, e, r, n, l) { var a = {}; return Object.keys(n).forEach(function (i) { a[i] = n[i]; }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function (r, n) { return n(i, e, r) || r; }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a; } import { NETWORK_REQUEST_HEADER } from "../core/constants.js"; import { formatRequestMethod, frozen, getHttpInterceptorId, singleton } from "../core/utils.js"; import { NetworkType } from "../types/index.js"; import HttpInterceptor from "./HttpInterceptor.js"; const originalFetch = global.fetch; let FetchInterceptor = singleton(_class = (_class2 = class FetchInterceptor extends HttpInterceptor { enableInterception() { if (this.isInterceptorEnabled) return; const { openCallback, requestHeaderCallback, sendCallback, headerReceivedCallback, responseCallback } = this.getCallbacks(); global.fetch = async function (input, init) { const interceptionId = getHttpInterceptorId(); const requestHeaders = new Headers(init?.headers); requestHeaders.append(NETWORK_REQUEST_HEADER, NetworkType.Fetch); const requestInit = { ...init, headers: requestHeaders }; //#region open const method = formatRequestMethod(init?.method); let url; switch (true) { case input instanceof Request: url = input.url; break; case input instanceof URL: url = input.href; break; default: url = input; } openCallback?.(interceptionId, NetworkType.Fetch, method, url); //#endregion //#region requestHeader const headers = requestInit?.headers; if (headers) { switch (true) { case headers instanceof Headers: for (const [headerKey, headerValue] of headers.entries()) { requestHeaderCallback?.(interceptionId, headerKey, headerValue); } break; case Array.isArray(headers): for (const [headerKey, headerValue] of headers) { if (headerKey && headerValue) requestHeaderCallback?.(interceptionId, headerKey, headerValue); } break; default: for (const key in headers) { if (headers[key]) requestHeaderCallback?.(interceptionId, key, headers[key]); } break; } } //#endregion //#region send sendCallback?.(interceptionId, Date.now(), init?.body ?? null); //#endregion const response = await originalFetch.call(this, input, requestInit); const clonedResponse = response.clone(); const clonedResponseHeaders = clonedResponse.headers; //#region headerReceived const contentTypeString = clonedResponseHeaders.get('Content-Type'); const contentLengthString = clonedResponseHeaders.get('Content-Length'); const responseContentType = contentTypeString ? contentTypeString.split(';')[0] : undefined; const responseSize = contentLengthString ? parseInt(contentLengthString, 10) : undefined; const responseHeaders = new Map(); clonedResponseHeaders.forEach((headerValue, headerKey) => { responseHeaders.set(headerKey, headerValue); }); headerReceivedCallback?.(interceptionId, responseContentType, responseSize, responseHeaders); //#endregion //#region response const responseBody = await clonedResponse.text().catch(() => null); responseCallback?.(interceptionId, clonedResponse.status, 0, Date.now(), responseBody, clonedResponse.url, clonedResponse.type); //#endregion return response; }; this.isInterceptorEnabled = true; } disableInterception() { if (!this.isInterceptorEnabled) return; this.isInterceptorEnabled = false; global.fetch = originalFetch; this.clearCallbacks(); } }, _applyDecoratedDescriptor(_class2.prototype, "enableInterception", [frozen], Object.getOwnPropertyDescriptor(_class2.prototype, "enableInterception"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "disableInterception", [frozen], Object.getOwnPropertyDescriptor(_class2.prototype, "disableInterception"), _class2.prototype), _class2)) || _class; export { FetchInterceptor as default }; //# sourceMappingURL=FetchInterceptor.js.map