gatsby-source-sanity
Version:
Gatsby source plugin for building websites using Sanity.io as a backend.
96 lines • 3.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const mutator_1 = require("@sanity/mutator");
const graphql_1 = require("gatsby/graphql");
const documentIds_1 = require("./documentIds");
const unprefixDraftId_1 = require("./unprefixDraftId");
const scalarTypeNames = graphql_1.specifiedScalarTypes.map(def => def.name).concat(['JSON', 'Date']);
// Movie => SanityMovie
const typePrefix = 'Sanity';
// Node fields used internally by Gatsby.
exports.RESTRICTED_NODE_FIELDS = ['id', 'children', 'parent', 'fields', 'internal'];
// Transform a Sanity document into a Gatsby node
function processDocument(doc, options) {
const { createNode, createNodeId, createContentDigest, overlayDrafts, skipCreate } = options;
const rawAliases = getRawAliases(doc, options);
const safe = prefixConflictingKeys(doc);
const withRefs = rewriteNodeReferences(safe, options);
const node = Object.assign(Object.assign(Object.assign({}, withRefs), rawAliases), { id: documentIds_1.safeId(overlayDrafts ? unprefixDraftId_1.unprefixDraftId(doc._id) : doc._id, createNodeId), parent: null, children: [], internal: {
type: getTypeName(doc._type),
contentDigest: createContentDigest(JSON.stringify(withRefs)),
} });
if (!skipCreate) {
createNode(node);
}
return node;
}
exports.processDocument = processDocument;
// movie => SanityMovie
// blog_post => SanityBlogPost
// sanity.imageAsset => SanityImageAsset
function getTypeName(type) {
if (!type) {
return type;
}
const typeName = lodash_1.startCase(type);
if (scalarTypeNames.includes(typeName)) {
return typeName;
}
return `${typePrefix}${typeName.replace(/\s+/g, '').replace(/^Sanity/, '')}`;
}
exports.getTypeName = getTypeName;
// {foo: 'bar', children: []} => {foo: 'bar', sanityChildren: []}
function prefixConflictingKeys(obj) {
// Will be overwritten, but initialize for type safety
const initial = { _id: '', _type: '' };
return Object.keys(obj).reduce((target, key) => {
const targetKey = getConflictFreeFieldName(key);
target[targetKey] = obj[key];
return target;
}, initial);
}
function getConflictFreeFieldName(fieldName) {
return exports.RESTRICTED_NODE_FIELDS.includes(fieldName)
? `${lodash_1.camelCase(typePrefix)}${lodash_1.upperFirst(fieldName)}`
: fieldName;
}
exports.getConflictFreeFieldName = getConflictFreeFieldName;
function getRawAliases(doc, options) {
const { typeMap } = options;
const typeName = getTypeName(doc._type);
const type = typeMap.objects[typeName];
if (!type) {
return {};
}
const initial = {};
return Object.keys(type.fields).reduce((acc, fieldName) => {
const field = type.fields[fieldName];
const namedType = field.namedType.name.value;
if (field.aliasFor) {
const aliasName = '_' + lodash_1.camelCase(`raw_data_${field.aliasFor}`);
acc[aliasName] = doc[field.aliasFor];
return acc;
}
if (typeMap.scalars.includes(namedType)) {
return acc;
}
const aliasName = '_' + lodash_1.camelCase(`raw_data_${fieldName}`);
acc[aliasName] = doc[fieldName];
return acc;
}, initial);
}
// Tranform Sanity refs ({_ref: 'foo'}) to Gatsby refs ({_ref: 'someOtherId'})
function rewriteNodeReferences(doc, options) {
const { createNodeId } = options;
const refs = mutator_1.extractWithPath('..[_ref]', doc);
if (refs.length === 0) {
return doc;
}
const newDoc = lodash_1.cloneDeep(doc);
refs.forEach(match => {
lodash_1.set(newDoc, match.path, documentIds_1.safeId(match.value, createNodeId));
});
return newDoc;
}
//# sourceMappingURL=normalize.js.map