react-native-xenon
Version:
A powerful in-app debugging tool for React Native.
107 lines (103 loc) • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.formatRequestStatusCode = exports.formatRequestMethod = exports.formatRequestDuration = exports.formatLogMessage = exports.convertToCurl = exports.clamp = exports.beautify = void 0;
exports.frozen = frozen;
exports.limitChar = exports.keyValueToString = exports.getVerticalSafeMargin = exports.getNetworkUtils = exports.getHttpInterceptorId = void 0;
exports.singleton = singleton;
var _reactNativeUrlPolyfill = require("react-native-url-polyfill");
var _types = require("../types");
const getNetworkUtils = data => {
const isHttp = data?.type !== _types.NetworkType.WS;
const requestUrl = new _reactNativeUrlPolyfill.URL(data.url);
const overviewShown = !!data.url;
const headersShown = isHttp && (!!data.requestHeaders || !!data.responseHeaders);
const requestShown = isHttp && (!!requestUrl.search || !!data.body);
const responseShown = isHttp && !!data.response;
const messagesShown = !isHttp && !!data.messages;
return {
isHttp,
requestUrl,
overviewShown,
headersShown,
requestShown,
responseShown,
messagesShown
};
};
//#region metrics
exports.getNetworkUtils = getNetworkUtils;
const getVerticalSafeMargin = screenHeight => screenHeight / 8;
exports.getVerticalSafeMargin = getVerticalSafeMargin;
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
exports.clamp = clamp;
const getHttpInterceptorId = () => {
const timestamp = Date.now().toString(36);
const randomNum = Math.random().toString(36).substring(2, 10);
return timestamp + randomNum;
};
//#endregion
//#region formatters
exports.getHttpInterceptorId = getHttpInterceptorId;
const limitChar = (value, limit = 5000) => {
const stringValue = typeof value === 'string' ? value : JSON.stringify(value ?? '');
return stringValue.length > limit ? `${stringValue.slice(0, limit)}\n---LIMITED TO ${limit} CHARACTERS---` : stringValue;
};
exports.limitChar = limitChar;
const keyValueToString = (key, value, newLine = 'trailing') => `${newLine === 'leading' ? '\n' : ''}${key}: ${limitChar(value)}${newLine === 'trailing' ? '\n' : ''}`;
exports.keyValueToString = keyValueToString;
const formatRequestMethod = method => method ?? 'GET';
exports.formatRequestMethod = formatRequestMethod;
const formatRequestDuration = duration => duration ? `${duration}ms` : 'pending';
exports.formatRequestDuration = formatRequestDuration;
const formatRequestStatusCode = statusCode => `${statusCode ?? 'pending'}`;
exports.formatRequestStatusCode = formatRequestStatusCode;
const formatLogMessage = (type, values) => {
const message = values.reduce((pre, cur, index) => pre + (!index ? '' : ', ') + limitChar(cur), '');
return `${type.toUpperCase()}: ${message}`;
};
exports.formatLogMessage = formatLogMessage;
const beautify = (data, beautified) => {
try {
const res = typeof data === 'string' ? JSON.parse(data) : data;
return beautified ? JSON.stringify(res, null, 2) : limitChar(res);
} catch (error) {
return limitChar(data);
}
};
exports.beautify = beautify;
const convertToCurl = (method, url, headers, body) => {
let curlCommand = `curl -X ${method.toUpperCase()} "${url}"`;
if (headers) {
for (const [key, value] of Object.entries(headers)) {
curlCommand += ` -H "${key}: ${value}"`;
}
}
if (body) {
const bodyString = typeof body === 'string' ? body : JSON.stringify(body);
curlCommand += ` -d '${bodyString}'`;
}
return curlCommand;
};
//#endregion
//#region decorators
exports.convertToCurl = convertToCurl;
function frozen(_target) {
const descriptor = arguments[2];
descriptor.configurable = false;
descriptor.writable = false;
}
function singleton(constructor) {
class Singleton extends constructor {
static #instance;
constructor(...args) {
if (Singleton.#instance) return Singleton.#instance;
super(...args);
Singleton.#instance = this;
}
}
return Singleton;
}
//#endregion
//# sourceMappingURL=utils.js.map