UNPKG

@sanity/migrate

Version:

Tooling for running data migrations on Sanity.io projects

129 lines (128 loc) 4.92 kB
function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function(sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } import { createRequire } from 'node:module'; var require = createRequire(import.meta.url); function getUserAgent() { if (globalThis.window === undefined) { // only set UA if we're in a non-browser environment try { var pkg = require('../../package.json'); return "".concat(pkg.name, "@").concat(pkg.version); // eslint-disable-next-line no-empty } catch (unused) {} } return null; } function normalizeApiHost(apiHost) { return apiHost.replace(/^https?:\/\//, ''); } export function toFetchOptions(req) { var apiHost = req.apiHost, apiVersion = req.apiVersion, body = req.body, endpoint = req.endpoint, projectId = req.projectId, tag = req.tag, token = req.token; var requestInit = _object_spread({ headers: { 'Content-Type': 'application/json' }, method: endpoint.method || 'GET' }, body !== undefined && { body: body }); var ua = getUserAgent(); if (ua) { requestInit.headers = _object_spread_props(_object_spread({}, requestInit.headers), { 'User-Agent': ua }); } if (token) { requestInit.headers = _object_spread_props(_object_spread({}, requestInit.headers), { Authorization: "bearer ".concat(token) }); } var normalizedApiHost = normalizeApiHost(apiHost); var path = "/".concat(apiVersion).concat(endpoint.path); var host = endpoint.global ? normalizedApiHost : "".concat(projectId, ".").concat(normalizedApiHost); var searchParams = new URLSearchParams(_to_consumable_array(endpoint.searchParams).concat(_to_consumable_array(tag ? [ [ 'tag', tag ] ] : []))).toString(); return { init: requestInit, url: "https://".concat(host, "/").concat(path).concat(searchParams ? "?".concat(searchParams) : '') }; }