UNPKG

@agnostack/verifyd

Version:

Please contact agnoStack via info@agnostack.com for any questions

67 lines 3.03 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeURIParts = exports.prepareRequestOptions = exports.getRequestMethod = exports.CONTENT_TYPES = exports.VERIFYD_HEADERS = void 0; const display_1 = require("./display"); const TEMP_HOSTNAME = 'xyz.com'; const REMOVABLE_KEYS = ['shop', 'host']; exports.VERIFYD_HEADERS = { PUBLIC_KEY: 'X-Public-Key', EPHEMERAL_KEY: 'X-Ephemeral-Key', AUTHORIZATION: 'X-Authorization', AUTHORIZATION_TIMESTAMP: 'X-Authorization-Timestamp', }; const CONTENT_TYPE_HEADER = 'Content-Type'; exports.CONTENT_TYPES = { APPLICATION_JSON: 'application/json', }; const getRequestMethod = (body, _method) => { const method = _method !== null && _method !== void 0 ? _method : (body ? 'POST' : 'GET'); return (0, display_1.uppercase)(method); }; exports.getRequestMethod = getRequestMethod; const prepareRequestOptions = (_a = {}) => { var { method: _method, body: _body, headers: _headers } = _a, requestOptions = __rest(_a, ["method", "body", "headers"]); const method = (0, exports.getRequestMethod)(_body, _method); const _contentType = _headers === null || _headers === void 0 ? void 0 : _headers[CONTENT_TYPE_HEADER.toLowerCase()]; const headers = Object.assign({ [CONTENT_TYPE_HEADER]: _contentType || exports.CONTENT_TYPES.APPLICATION_JSON }, _headers); let body = _body; if (body && headers[CONTENT_TYPE_HEADER].startsWith(exports.CONTENT_TYPES.APPLICATION_JSON)) { body = JSON.stringify((0, display_1.safeParse)(body)); } return Object.assign(Object.assign({ method, headers }, (body && (method !== 'GET')) && { body }), requestOptions); }; exports.prepareRequestOptions = prepareRequestOptions; const convertToURL = (uri) => { if ((0, display_1.stringEmpty)(uri)) { return undefined; } const protocolRegex = /^(https?:\/\/).+/i; if (!protocolRegex.test(uri)) { return new URL(`https://${TEMP_HOSTNAME}${(0, display_1.ensureLeadingSlash)(uri)}`); } return new URL(uri); }; const normalizeURIParts = (uri) => { const urlObject = convertToURL(uri); if (!urlObject) { return undefined; } REMOVABLE_KEYS.forEach((key) => urlObject.searchParams.delete(key)); return Object.assign(Object.assign({}, (urlObject.hostname !== TEMP_HOSTNAME) && { origin: urlObject.origin, }), { pathname: urlObject.pathname, search: urlObject.search }); }; exports.normalizeURIParts = normalizeURIParts; //# sourceMappingURL=request.js.map