UNPKG

@sanity/migrate

Version:

Tooling for running data migrations on Sanity.io projects

144 lines (143 loc) 5.9 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 { createClient } from '@sanity/client'; import { limitClientConcurrency } from './limitClientConcurrency.js'; export function createContextClient(config) { return restrictClient(limitClientConcurrency(createClient(_object_spread_props(_object_spread({}, config), { requestTagPrefix: 'sanity.migration', useCdn: false })))); } var ALLOWED_PROPERTIES = [ 'fetch', 'clone', 'config', 'withConfig', 'getDocument', 'getDocuments', 'users', 'projects' ]; function restrictClient(client) { return new Proxy(client, { get: function(target, property) { switch(property){ case 'clone': { return function() { for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){ args[_key] = arguments[_key]; } var _target; return restrictClient((_target = target).clone.apply(_target, _to_consumable_array(args))); }; } case 'config': { return function() { for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){ args[_key] = arguments[_key]; } var _target; var result = (_target = target).config.apply(_target, _to_consumable_array(args)); // if there is a config, it returns a client so we need to wrap again if (args[0]) return restrictClient(result); return result; }; } case 'withConfig': { return function() { for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){ args[_key] = arguments[_key]; } var _target; return restrictClient((_target = target).withConfig.apply(_target, _to_consumable_array(args))); }; } default: { if (typeof property === 'string' && ALLOWED_PROPERTIES.includes(property)) { return target[property]; } throw new Error('Client method "'.concat(String(property), '" can not be called during a migration. Only ').concat(ALLOWED_PROPERTIES.join(', '), " are allowed.")); } } } }); }