@sanity/form-builder
Version:
Sanity form builder
116 lines (113 loc) • 5.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createGetReferenceInfo = createGetReferenceInfo;
var _keyBy2 = _interopRequireDefault(require("lodash/keyBy"));
var _operators = require("rxjs/operators");
var _rxjs = require("rxjs");
var _internal = require("@sanity/base/_internal");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(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(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(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 _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* eslint-disable max-nested-callbacks */
var REQUEST_TAG_BASE = 'cross-dataset-refs';
var AVAILABILITY_READABLE = {
available: true,
reason: _internal.AvailabilityReason.READABLE
};
var AVAILABILITY_PERMISSION_DENIED = {
available: false,
reason: _internal.AvailabilityReason.PERMISSION_DENIED
};
var AVAILABILITY_NOT_FOUND = {
available: false,
reason: _internal.AvailabilityReason.NOT_FOUND
};
/**
* Takes a client instance and returns a function that can be called to retrieve reference information
* @param client
*/
function createGetReferenceInfo(client) {
var _client$config = client.config(),
dataset = _client$config.dataset,
projectId = _client$config.projectId;
/**
* Takes an id and a reference schema type, returns metadata about it
* Assumption: _id is always published id
* @param id
* @param referenceType
*/
return function getReferenceInfo(doc,
// pass {_id, _type} instead and we can skip the `fetchType`
referenceType) {
return (doc._type ? (0, _rxjs.of)(doc) : (0, _internal.observeDocumentTypeFromId)(doc._id, {
dataset,
projectId
}).pipe((0, _operators.map)(docType => ({
_id: doc._id,
_type: docType
})))).pipe((0, _operators.switchMap)(resolvedDoc => {
if (!resolvedDoc._type) {
// we still can't read the type of the referenced document. This may be due to either 1) lack of access 2) lack of existence
// we want to display a reason to the end user, so we're fetching metadata about it
return fetchDocumentAvailability(client, doc._id).pipe((0, _operators.map)(availability => ({
id: doc._id,
type: null,
availability,
preview: {
published: undefined
}
})));
}
var refSchemaType = referenceType.to.find(candidate => candidate.type === resolvedDoc._type);
var previewPaths = [...((0, _internal.getPreviewPaths)(refSchemaType.preview) || []), ['_updatedAt'], ['_createdAt']];
var publishedPreview$ = (0, _internal.observePaths)(doc._id, previewPaths, {
projectId,
dataset
}).pipe((0, _operators.map)(result => result ? (0, _internal.prepareForPreview)(result, refSchemaType) : result));
return (0, _rxjs.combineLatest)([publishedPreview$]).pipe((0, _operators.map)(_ref => {
var _ref2 = _slicedToArray(_ref, 1),
publishedPreview = _ref2[0];
return {
type: resolvedDoc._type,
id: doc._id,
availability: AVAILABILITY_READABLE,
preview: {
published: publishedPreview
}
};
}));
}));
};
}
function fetchDocumentAvailability(client, id) {
var requestOptions = {
uri: client.getDataUrl('doc', id),
json: true,
query: {
excludeContent: 'true'
},
tag: "".concat(REQUEST_TAG_BASE, ".availability")
};
return client.observable.request(requestOptions).pipe((0, _operators.map)(response => {
var omitted = (0, _keyBy2.default)(response.omitted || [], entry => entry.id);
var omittedEntry = omitted[id];
if (!omittedEntry) {
// it's not omitted, so it exists and is readable
return AVAILABILITY_READABLE;
}
// omitted because it doesn't exist
if (omittedEntry.reason === 'existence') {
return AVAILABILITY_NOT_FOUND;
}
if (omittedEntry.reason === 'permission') {
// omitted because it's not readable
return AVAILABILITY_PERMISSION_DENIED;
}
return null;
}));
}