@pagenote/notion-database
Version:
make notion as a real-database for server
75 lines • 3.17 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const client_1 = require("@notionhq/client");
const notion_to_md_1 = require("notion-to-md");
const page_1 = require("./notion-helper/page");
function resolveEmpty() {
return Promise.resolve(undefined);
}
class Page {
constructor(option) {
this.shortPageId = option.pageId;
this.notion =
option.notion ||
new client_1.Client({
auth: option.token
});
this.n2m = new notion_to_md_1.NotionToMarkdown({ notionClient: this.notion });
}
properties() {
return this.notion.pages
.retrieve({
page_id: this.shortPageId
})
.then(function (res) {
const pageObject = res;
const properties = pageObject === null || pageObject === void 0 ? void 0 : pageObject.properties;
if (!properties) {
return undefined;
}
return (0, page_1.formatNotionPageDocument)(res);
});
}
blocks() {
return (0, client_1.collectPaginatedAPI)(this.notion.blocks.children.list, {
block_id: this.shortPageId
});
}
markdown() {
return this.n2m.pageToMarkdown(this.shortPageId).then(res => {
return this.n2m.toMarkdownString(res);
});
}
detail(parts = ['properties', 'markdown']) {
return Promise.allSettled([
parts.includes('properties') ? this.properties() : resolveEmpty(),
parts.includes('blocks') ? this.blocks() : resolveEmpty(),
parts.includes('markdown') ? this.markdown() : resolveEmpty()
]).then(([property, blocks, markdown]) => __awaiter(this, void 0, void 0, function* () {
const result = property.status === 'fulfilled' ? property.value : {};
if (result && parts.includes('markdown')) {
result.__markdown =
markdown.status === 'fulfilled' ? markdown.value : undefined;
}
if (result && parts.includes('blocks')) {
if (blocks.status === 'fulfilled') {
result.__blocks = blocks.value;
// const res = await this.n2m.blocksToMarkdown(blocks.value)
// console.log('转化结果',res)
}
}
return result;
}));
}
}
exports.default = Page;
//# sourceMappingURL=Page.js.map