Skip to main content
Glama
request-utils.js4.02 kB
import { each } from './'; import { isArray, isFile, isUndefined } from './type-utils'; import { logger } from './logger'; import { document } from './globals'; var localDomains = ['localhost', '127.0.0.1']; /** * IE11 doesn't support `new URL` * so we can create an anchor element and use that to parse the URL * there's a lot of overlap between HTMLHyperlinkElementUtils and URL * meaning useful properties like `pathname` are available on both */ export var convertToURL = function (url) { var location = document === null || document === void 0 ? void 0 : document.createElement('a'); if (isUndefined(location)) { return null; } location.href = url; return location; }; export var formDataToQuery = function (formdata, arg_separator) { if (arg_separator === void 0) { arg_separator = '&'; } var use_val; var use_key; var tph_arr = []; each(formdata, function (val, key) { // the key might be literally the string undefined for e.g. if {undefined: 'something'} if (isUndefined(val) || isUndefined(key) || key === 'undefined') { return; } use_val = encodeURIComponent(isFile(val) ? val.name : val.toString()); use_key = encodeURIComponent(key); tph_arr[tph_arr.length] = use_key + '=' + use_val; }); return tph_arr.join(arg_separator); }; // NOTE: Once we get rid of IE11/op_mini we can start using URLSearchParams export var getQueryParam = function (url, param) { var withoutHash = url.split('#')[0] || ''; // Split only on the first ? to sort problem out for those with multiple ?s // and then remove them var queryParams = withoutHash.split(/\?(.*)/)[1] || ''; var cleanedQueryParams = queryParams.replace(/^\?+/g, ''); var queryParts = cleanedQueryParams.split('&'); var keyValuePair; for (var i = 0; i < queryParts.length; i++) { var parts = queryParts[i].split('='); if (parts[0] === param) { keyValuePair = parts; break; } } if (!isArray(keyValuePair) || keyValuePair.length < 2) { return ''; } else { var result = keyValuePair[1]; try { result = decodeURIComponent(result); } catch (_a) { logger.error('Skipping decoding for malformed query param: ' + result); } return result.replace(/\+/g, ' '); } }; // replace any query params in the url with the provided mask value. Tries to keep the URL as instant as possible, // including preserving malformed text in most cases export var maskQueryParams = function (url, maskedParams, mask) { if (!url || !maskedParams || !maskedParams.length) { return url; } var splitHash = url.split('#'); var withoutHash = splitHash[0] || ''; var hash = splitHash[1]; var splitQuery = withoutHash.split('?'); var queryString = splitQuery[1]; var urlWithoutQueryAndHash = splitQuery[0]; var queryParts = (queryString || '').split('&'); // use an array of strings rather than an object to preserve ordering and duplicates var paramStrings = []; for (var i = 0; i < queryParts.length; i++) { var keyValuePair = queryParts[i].split('='); if (!isArray(keyValuePair)) { continue; } else if (maskedParams.includes(keyValuePair[0])) { paramStrings.push(keyValuePair[0] + '=' + mask); } else { paramStrings.push(queryParts[i]); } } var result = urlWithoutQueryAndHash; if (queryString != null) { result += '?' + paramStrings.join('&'); } if (hash != null) { result += '#' + hash; } return result; }; export var _getHashParam = function (hash, param) { var matches = hash.match(new RegExp(param + '=([^&]*)')); return matches ? matches[1] : null; }; export var isLocalhost = function () { return localDomains.includes(location.hostname); }; //# sourceMappingURL=request-utils.js.map

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sadiuysal/mem0-mcp-server-ts'

If you have feedback or need assistance with the MCP directory API, please join our Discord server