@remcostoeten/fync
Version:
Unified TypeScript library for 9 popular APIs with consistent functional architecture
209 lines • 8.87 kB
JavaScript
import { createApiBuilder, defineResource } from "../core";
const NOTION_API_BASE = "https://api.notion.com/v1";
const pagesResource = defineResource({
name: "pages",
basePath: "/pages",
methods: {
getPage: { path: "/{page_id}" },
createPage: { path: "", method: "POST" },
updatePage: { path: "/{page_id}", method: "PATCH" },
getPageProperty: { path: "/{page_id}/properties/{property_id}" },
},
});
const databasesResource = defineResource({
name: "databases",
basePath: "/databases",
methods: {
getDatabase: { path: "/{database_id}" },
createDatabase: { path: "", method: "POST" },
updateDatabase: { path: "/{database_id}", method: "PATCH" },
queryDatabase: { path: "/{database_id}/query", method: "POST" },
},
});
const blocksResource = defineResource({
name: "blocks",
basePath: "/blocks",
methods: {
getBlock: { path: "/{block_id}" },
updateBlock: { path: "/{block_id}", method: "PATCH" },
deleteBlock: { path: "/{block_id}", method: "DELETE" },
getBlockChildren: { path: "/{block_id}/children" },
appendBlockChildren: { path: "/{block_id}/children", method: "PATCH" },
},
});
const usersResource = defineResource({
name: "users",
basePath: "/users",
methods: {
getUser: { path: "/{user_id}" },
getCurrentUser: { path: "/me" },
listUsers: { path: "" },
},
});
const commentsResource = defineResource({
name: "comments",
basePath: "/comments",
methods: {
createComment: { path: "", method: "POST" },
getComments: { path: "" },
},
});
const searchResource = defineResource({
name: "search",
basePath: "/search",
methods: {
search: { path: "", method: "POST" },
},
});
const resources = {
pages: pagesResource,
databases: databasesResource,
blocks: blocksResource,
users: usersResource,
comments: commentsResource,
search: searchResource,
};
const buildNotion = createApiBuilder({
baseUrl: NOTION_API_BASE,
auth: { type: "bearer" },
headers: {
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
},
});
export function Notion(config) {
const configWithHeaders = config.notionVersion ?
{ ...config, headers: { "Notion-Version": config.notionVersion } } :
config;
const base = buildNotion(configWithHeaders, resources);
const notion = base;
notion.getPage = function (pageId) {
return base.pages.getPage({ page_id: pageId });
};
notion.createPage = function (data) {
return base.pages.createPage(data);
};
notion.updatePage = function (pageId, data) {
return base.pages.updatePage(data, { page_id: pageId });
};
notion.archivePage = function (pageId) {
return base.pages.updatePage({ archived: true }, { page_id: pageId });
};
notion.getPageProperty = function (pageId, propertyId) {
return base.pages.getPageProperty({ page_id: pageId, property_id: propertyId });
};
notion.getDatabase = function (databaseId) {
return base.databases.getDatabase({ database_id: databaseId });
};
notion.createDatabase = function (data) {
return base.databases.createDatabase(data);
};
notion.updateDatabase = function (databaseId, data) {
return base.databases.updateDatabase(data, { database_id: databaseId });
};
notion.queryDatabase = function (databaseId, query) {
return base.databases.queryDatabase(query || {}, { database_id: databaseId });
};
notion.getBlock = function (blockId) {
return base.blocks.getBlock({ block_id: blockId });
};
notion.updateBlock = function (blockId, data) {
return base.blocks.updateBlock(data, { block_id: blockId });
};
notion.deleteBlock = function (blockId) {
return base.blocks.deleteBlock({ block_id: blockId });
};
notion.getBlockChildren = function (blockId, startCursor, pageSize) {
const params = { block_id: blockId };
if (startCursor)
params.start_cursor = startCursor;
if (pageSize)
params.page_size = pageSize;
return base.blocks.getBlockChildren(params);
};
notion.appendBlockChildren = function (blockId, children) {
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.getUser = function (userId) {
return base.users.getUser({ user_id: userId });
};
notion.getCurrentUser = function () {
return base.users.getCurrentUser({});
};
notion.listUsers = function (startCursor, pageSize) {
const params = {};
if (startCursor)
params.start_cursor = startCursor;
if (pageSize)
params.page_size = pageSize;
return base.users.listUsers(params);
};
notion.createComment = function (data) {
return base.comments.createComment(data);
};
notion.getComments = function (blockId, startCursor, pageSize) {
const params = { block_id: blockId };
if (startCursor)
params.start_cursor = startCursor;
if (pageSize)
params.page_size = pageSize;
return base.comments.getComments(params);
};
notion.search = function (query, options) {
return base.search.search({ query, ...options });
};
notion.searchPages = function (query, options) {
return base.search.search({ query, filter: { property: "object", value: "page" }, ...options });
};
notion.searchDatabases = function (query, options) {
return base.search.search({ query, filter: { property: "object", value: "database" }, ...options });
};
notion.createRichText = function (text) {
return [{ type: "text", text: { content: text, link: null }, plain_text: text, href: null }];
};
notion.createPageInDatabase = function (databaseId, properties, content) {
return base.pages.createPage({ parent: { database_id: databaseId }, properties, children: content || [] });
};
notion.createPageWithContent = function (parent, title, content) {
return base.pages.createPage({ parent, properties: { title: { title: notion.createRichText(title) } }, children: content });
};
notion.addTextBlock = function (blockId, text, type) {
const blockType = type || "paragraph";
const children = [{ type: blockType, [blockType]: { rich_text: notion.createRichText(text) } }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.addToDoBlock = function (blockId, text, checked) {
const children = [{ type: "to_do", to_do: { rich_text: notion.createRichText(text), checked: checked || false } }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.addBulletedListItem = function (blockId, text) {
const children = [{ type: "bulleted_list_item", bulleted_list_item: { rich_text: notion.createRichText(text) } }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.addNumberedListItem = function (blockId, text) {
const children = [{ type: "numbered_list_item", numbered_list_item: { rich_text: notion.createRichText(text) } }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.addToggleBlock = function (blockId, text, children) {
const block = { type: "toggle", toggle: { rich_text: notion.createRichText(text), children: children || [] } };
return base.blocks.appendBlockChildren({ children: [block] }, { block_id: blockId });
};
notion.addCalloutBlock = function (blockId, text, emoji) {
const block = { type: "callout", callout: { rich_text: notion.createRichText(text), icon: emoji ? { type: "emoji", emoji } : undefined } };
return base.blocks.appendBlockChildren({ children: [block] }, { block_id: blockId });
};
notion.addQuoteBlock = function (blockId, text) {
const children = [{ type: "quote", quote: { rich_text: notion.createRichText(text) } }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.addCodeBlock = function (blockId, code, language) {
const children = [{ type: "code", code: { rich_text: notion.createRichText(code), language: language || "plain text" } }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
notion.addDividerBlock = function (blockId) {
const children = [{ type: "divider", divider: {} }];
return base.blocks.appendBlockChildren({ children }, { block_id: blockId });
};
return notion;
}
//# sourceMappingURL=index.js.map