UNPKG

@pagenote/notion-database

Version:

make notion as a real-database for server

108 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatNotionPageDocument = exports.updateTableInfo = exports.updatePageInfo = void 0; const notionType_1 = require("../types/notionType"); const transformer_1 = require("../transformer"); function getIcon(info) { let icon = undefined; if (info.emoji) { icon = { emoji: info.emoji, type: 'emoji' }; } else if (info.icon) { icon = { external: { url: info.icon }, type: 'external' }; } return icon; } /** * 更新页面的基础展示信息(非property):图标、封面、 */ function updatePageInfo(notion, pageId, info) { if (!pageId) { throw Error('shortPageId is required'); } notion.pages .update({ page_id: pageId, icon: getIcon({ icon: info.icon, emoji: info.emoji }), cover: info.cover ? { type: 'external', external: { url: info.cover } } : undefined }) .catch(function (reason) { console.warn(reason); }); } exports.updatePageInfo = updatePageInfo; /** * 更新表格(database)的基础信息:图标、摘要 */ function updateTableInfo(notion, databaseId, info) { notion.databases .update({ database_id: databaseId, description: info.description ? [ { text: { content: info.description } } ] : undefined, icon: getIcon(info) }) .catch(function (reason) { console.warn(reason); }); } exports.updateTableInfo = updateTableInfo; function getIconString(input) { switch (input === null || input === void 0 ? void 0 : input.type) { case 'emoji': return input.emoji; case 'external': return input.external.url; case 'file': return input.file.url; default: return ''; } } /** * 将 notion 接口返回的数据结构,转换为 js 普通对象 */ function notionPropertiesToData(notionProperties) { var _a; const result = {}; for (let i in notionProperties) { const { type } = notionProperties[i]; const value = (_a = transformer_1.default[type]) === null || _a === void 0 ? void 0 : _a.propertyToObject(notionProperties[i]); // @ts-ignore result[i] = value; if (type === notionType_1.SupportNotionTypes.title) { result.__notion_primary = value; } } return result; } /** * 将 notion 返回的一个 page 对象,解析为js 属性对象 */ function formatNotionPageDocument(pageObject) { const result = notionPropertiesToData(pageObject.properties); const notionObject = Object.assign({ __notion_id: pageObject.id, __notion_icon: getIconString(pageObject === null || pageObject === void 0 ? void 0 : pageObject.icon), __notion_cover: getIconString(pageObject.cover), __notion_url: pageObject.url, __notion_primary: result.__notion_primary || '' }, result); return notionObject; } exports.formatNotionPageDocument = formatNotionPageDocument; //# sourceMappingURL=page.js.map