UNPKG

@sanity/data-aspects

Version:

Sanity plugin which controls how your data is presented

109 lines (108 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _startCase2 = _interopRequireDefault(require("lodash/startCase")); var _dataAspects = _interopRequireDefault(require("config:@sanity/data-aspects")); var _generateHelpUrl = require("@sanity/generate-help-url"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var bundledTypeNames = ['geopoint', 'richDate', 'date', 'sanity.imageAsset', 'sanity.fileAsset']; var BUNDLED_DOC_TYPES = ['sanity.imageAsset', 'sanity.fileAsset']; function isDocumentType(type) { return type.type && type.type.name === 'document'; } function isObjectType(type) { return type.type !== null && type.jsonType === 'object'; } function isBundledDocType(typeName) { return BUNDLED_DOC_TYPES.includes(typeName); } function schemaHasUserDefinedDocumentTypes(schema) { return schema.getTypeNames().some(typeName => { return !isBundledDocType(typeName) && isDocumentType(schema.get(typeName)); }); } var hasWarned = false; function warnAboutHiddenTypes() { if (hasWarned) { return; } hasWarned = true; // eslint-disable-next-line no-console, prefer-template console.warn("👋 Hi there! Looks like you have hidden types configured in your studio's config/@sanity/data-aspects.json" + " This config is now obsolete and should be removed. Read more at ".concat((0, _generateHelpUrl.generateHelpUrl)('toplevel-objects-to-document-type'))); } class DataAspectsResolver { constructor(schema) { this.schema = schema; this.config = Object.assign({ hiddenTypes: [], typeOptions: {} }, _dataAspects.default || {}); } getConfig() { return this.config; } getField(type, fieldName) { return type.fields.filter(currField => currField.name === fieldName); } getType(typeName) { return this.schema.get(typeName); } inferTypesLegacy() { return (this.schema.getTypeNames() || []).filter(typeName => { // Exclude types which come bundled with Sanity return !bundledTypeNames.includes(typeName) && // Exclude types which are explicitly named in (legacy) hiddenTypes config !this.config.hiddenTypes.includes(typeName) && // Only include if its an object type isObjectType(this.getType(typeName)); }); } getDocumentTypes() { if (this.config.hiddenTypes.length > 0) { warnAboutHiddenTypes(); } return this.schema.getTypeNames().filter(typeName => !isBundledDocType(typeName) && isDocumentType(this.schema.get(typeName))); } getInferredTypes() { return schemaHasUserDefinedDocumentTypes(this.schema) ? this.getDocumentTypes() : this.inferTypesLegacy(); } getDisplayName(typeName) { var typeOption = this.config.typeOptions[typeName] || {}; var type = this.getType(typeName); return typeOption.displayName || type.title || (0, _startCase2.default)(typeName); } getIcon(typeName) { return this.getType(typeName).icon; } getListConstraints(typeName) { var typeOption = this.config.typeOptions[typeName]; if (!typeOption) { return ''; } var constraints = []; if (typeOption.order) { // prefix order items with . because that's what gql requires //constraints.push(typeOptions.order) } if (typeOption.limit || typeOption.limit == 0) { constraints.push("limit: ".concat(typeOption.limit)); } if (typeOption.offset) { constraints.push("offset: ".concat(typeOption.offset)); } return constraints.filter(Boolean).join(', '); } getListQuery(options) { var typeName = options.typeName, keyForId = options.keyForId, keyForDisplayFieldName = options.keyForDisplayFieldName; var fieldName = this.getItemDisplayField(typeName); var constraints = this.getListConstraints(typeName); var selection = "\"".concat(keyForId, "\": _id, \"").concat(keyForDisplayFieldName, "\": ").concat(fieldName); return "".concat(this.schema.name, ".").concat(typeName, " [").concat(constraints, "] {").concat(selection, "}"); } } var _default = DataAspectsResolver; exports.default = _default;