UNPKG

burdy

Version:

Open Source Headless Platform

157 lines (156 loc) 6.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapPublicTags = exports.mapPublicTag = exports.mapTags = exports.mapTag = exports.mapPublicUser = exports.mapUser = exports.mapContentType = exports.mapPublicPostWithMeta = exports.mapPostWithMeta = exports.mapPostContainer = exports.mapPost = exports.mapAsset = exports.mapPublicAsset = exports.getAssetsSrc = void 0; const hooks_1 = __importDefault(require("../../shared/features/hooks")); const object_1 = require("./object"); const post_parser_1 = require("./post.parser"); const getAssetsSrc = (npath) => { var _a; return `${((_a = process.env.BURDY_HOST) === null || _a === void 0 ? void 0 : _a.length) > 0 ? process.env.BURDY_HOST : ''}/api/uploads/${npath}`; }; exports.getAssetsSrc = getAssetsSrc; hooks_1.default.addSyncFilter('assets/public-mapping', (asset) => { var _a; const meta = {}; ((asset === null || asset === void 0 ? void 0 : asset.meta) || []).forEach((m) => { meta[m.key] = m === null || m === void 0 ? void 0 : m.value; }); return { name: asset.name, npath: asset.npath, height: meta === null || meta === void 0 ? void 0 : meta.height, width: meta === null || meta === void 0 ? void 0 : meta.width, alt: meta === null || meta === void 0 ? void 0 : meta.alt, copyright: meta === null || meta === void 0 ? void 0 : meta.copyright, meta, mimeType: asset.mimeType, tags: (0, exports.mapPublicTags)(asset.tags), src: `${((_a = process.env.BURDY_HOST) === null || _a === void 0 ? void 0 : _a.length) > 0 ? process.env.BURDY_HOST : ''}/api/uploads/${asset.npath}`, }; }, { id: 'resolve', priority: 10 }); const mapPublicAsset = (asset) => { if (!asset) return undefined; return hooks_1.default.applySyncFilters('assets/public-mapping', asset); }; exports.mapPublicAsset = mapPublicAsset; const mapAsset = (asset) => { if (!asset) return undefined; return Object.assign(Object.assign({}, asset), { author: (0, exports.mapUser)(asset.author), tags: (0, exports.mapTags)(asset.tags) }); }; exports.mapAsset = mapAsset; const mapPost = (post) => { if (!post) return undefined; return Object.assign(Object.assign({}, post), { author: (0, exports.mapUser)(post.author), tags: (0, exports.mapTags)(post.tags), contentType: (0, exports.mapContentType)(post.contentType) }); }; exports.mapPost = mapPost; const mapPostContainer = (post) => { if (!post) return undefined; return { id: post.id, name: post.name, slug: post.slug, slugPath: post.slugPath, type: post.type, contentType: (0, exports.mapContentType)(post.contentType), tags: (0, exports.mapTags)(post.tags), }; }; exports.mapPostContainer = mapPostContainer; const mapPostWithMeta = (post) => { if (!post) return undefined; return Object.assign(Object.assign({}, post), { contentType: (0, exports.mapContentType)(post.contentType), author: (0, exports.mapUser)(post.author), meta: (0, post_parser_1.parseInternalMetaContent)(post), tags: (0, exports.mapTags)(post.tags) }); }; exports.mapPostWithMeta = mapPostWithMeta; const mapPublicPostWithMeta = (post) => { if (!post) return undefined; const meta = {}; ((post === null || post === void 0 ? void 0 : post.meta) || []).forEach((m) => { meta[m.key] = m === null || m === void 0 ? void 0 : m.value; }); return { id: post.id, type: post.type, name: post.name, slug: post.slug, slugPath: post.slugPath, createdAt: post.createdAt, updatedAt: post.updatedAt, publishedAt: post.publishedAt, publishedFrom: post.publishedFrom, publishedUntil: post.publishedUntil, status: post.status, parent: (0, exports.mapPublicPostWithMeta)(post.parent), contentType: (0, exports.mapContentType)(post.contentType), author: (0, exports.mapPublicUser)(post.author), meta: (0, object_1.unflatten)(meta || {}), tags: (0, exports.mapPublicTags)(post.tags), }; }; exports.mapPublicPostWithMeta = mapPublicPostWithMeta; const mapContentType = (contentType) => { let fields; try { fields = JSON.parse(contentType === null || contentType === void 0 ? void 0 : contentType.fields); } catch (e) { return undefined; } return Object.assign(Object.assign({}, contentType), { author: (0, exports.mapUser)(contentType === null || contentType === void 0 ? void 0 : contentType.author), fields }); }; exports.mapContentType = mapContentType; const mapUser = (user) => { if (!user) return undefined; delete user.password; return user; }; exports.mapUser = mapUser; const mapPublicUser = (user) => { if (!user) return undefined; return { firstName: user === null || user === void 0 ? void 0 : user.firstName, lastName: user === null || user === void 0 ? void 0 : user.lastName, }; }; exports.mapPublicUser = mapPublicUser; const mapTag = (tag) => { if (!tag) return undefined; return Object.assign(Object.assign({}, tag), { author: (0, exports.mapUser)(tag.author) }); }; exports.mapTag = mapTag; const mapTags = (tags) => { if (!tags) return undefined; return tags.map(exports.mapTag); }; exports.mapTags = mapTags; const mapPublicTag = (tag) => { if (!tag) return undefined; return { name: tag.name, slug: tag.slug, slugPath: tag.slugPath, parent: (0, exports.mapPublicTag)(tag.parent), id: tag.id, author: (0, exports.mapPublicUser)(tag.author), }; }; exports.mapPublicTag = mapPublicTag; const mapPublicTags = (tags) => { if (!tags) return undefined; return tags.map(exports.mapPublicTag); }; exports.mapPublicTags = mapPublicTags;