UNPKG

@jmuchovej/paperpile-notion

Version:
84 lines (83 loc) 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StrictConfig = exports.newConfig = void 0; const tslib_1 = require("tslib"); const lodash_1 = (0, tslib_1.__importDefault)(require("lodash")); const _hexChars = lodash_1.default.split("1234567890abcdef", ""); const isValidPageID = (pageID) => { const is32char = pageID.replaceAll("-", "").length === 32; const set = lodash_1.default.chain(pageID).split("").uniq().value(); const valid = lodash_1.default.isEmpty(lodash_1.default.difference(set, _hexChars)) && is32char; return valid ? pageID : undefined; }; const getDBbyName = async (notion, name) => { const { results } = await notion.search({ query: name, page_size: 1, filter: { property: "object", value: "database" }, }); return results[0].id.replaceAll("-", ""); }; const newConfig = async (config, notion) => { let { status, topics, fields, methods, folders, icons, } = config; status.prefix = status.prefix ?? "status:"; status.colname = status.colname ?? "Status"; topics.prefix = topics.prefix ?? "topic:"; topics.colname = topics.colname ?? "Topics"; fields.prefix = fields.prefix ?? "field:"; fields.colname = fields.colname ?? "Fields"; methods.prefix = methods.prefix ?? "method:"; methods.colname = methods.colname ?? "Methods"; folders.prefix = folders.prefix ?? "folder:"; folders.colname = folders.colname ?? "Folders"; let databases = await setupDBs(config, notion); return new StrictConfig({ databases, status, topics, fields, methods, folders, icons, }); }; exports.newConfig = newConfig; const setupDBs = async ({ databases }, notion) => { let { authors, articles } = databases; if (lodash_1.default.isString(authors)) { let dbID = isValidPageID(authors) ?? await getDBbyName(notion, authors); databases.authors = { databaseID: dbID, articleRef: "Articles", primaryKey: "name", }; } else if (!lodash_1.default.isNil(authors)) { databases.authors = undefined; } if (lodash_1.default.isString(articles)) { let dbID = isValidPageID(articles) ?? await getDBbyName(notion, articles); databases.articles = { databaseID: dbID, authorRef: "Authors", primaryKey: "ID", }; } if (lodash_1.default.isString(databases.articles) || lodash_1.default.isString(databases.authors)) { throw new Error("Failed to convert string to object. Please open a bug report on GitHub."); } // @ts-expect-error TODO sort out why this causes an error... return databases; }; class StrictConfig { constructor({ databases, status, topics, fields, methods, folders, icons, }) { this.databases = databases; this.status = status; this.topics = topics; this.fields = fields; this.methods = methods; this.folders = folders; this.icons = icons; } get hasAuthorDB() { return !lodash_1.default.isNil(this.databases.authors); } get authorType() { return this.hasAuthorDB ? "relation" : "multi_select"; } } exports.StrictConfig = StrictConfig;