From 019f5e470f2359e4ac146b496fb80c0fc7a4940d Mon Sep 17 00:00:00 2001 From: anime Date: Sat, 4 Jan 2025 01:00:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BD=BF=E7=94=A8=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=9A=84=20notionAPI):=20=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E5=A4=9A=E6=AC=A1=E5=88=9B=E5=BB=BANotionAPI=EF=BC=8C=E6=9C=89?= =?UTF-8?q?=E6=95=88=E5=88=A9=E7=94=A8=E5=86=85=E7=BD=AE=E7=9A=84concurren?= =?UTF-8?q?cy=E5=B9=B6=E5=8F=91=E8=AF=B7=E6=B1=82=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getNotionAPI.js | 10 +++++++--- lib/notion/getPageProperties.js | 6 ++---- lib/notion/getPostBlocks.js | 10 +++------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/notion/getNotionAPI.js b/lib/notion/getNotionAPI.js index c2cbb6a6..757534a3 100644 --- a/lib/notion/getNotionAPI.js +++ b/lib/notion/getNotionAPI.js @@ -1,10 +1,14 @@ -import { NotionAPI } from 'notion-client' +import { NotionAPI as NotionLibrary } from 'notion-client' import BLOG from '@/blog.config' -export default function getNotionAPI() { - return new NotionAPI({ +const notionAPI = getNotionAPI() + +function getNotionAPI() { + return new NotionLibrary({ activeUser: BLOG.NOTION_ACTIVE_USER || null, authToken: BLOG.NOTION_TOKEN_V2 || null, userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone }) } + +export default notionAPI diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index 5d5e7ff6..571c1b8c 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -1,5 +1,4 @@ import BLOG from '@/blog.config' -import { NotionAPI } from 'notion-client' import { getDateValue, getTextContent } from 'notion-utils' import formatDate from '../utils/formatDate' // import { createHash } from 'crypto' @@ -12,7 +11,7 @@ import { } from '../utils' import { extractLangPrefix } from '../utils/pageId' import { mapImgUrl } from './mapImage' -import getNotionAPI from '@/lib/notion/getNotionAPI' +import notionAPI from '@/lib/notion/getNotionAPI' /** * 获取页面元素成员属性 @@ -57,12 +56,11 @@ export default async function getPageProperties( case 'person': { const rawUsers = val.flat() const users = [] - const api = getNotionAPI() for (let i = 0; i < rawUsers.length; i++) { if (rawUsers[i][0][1]) { const userId = rawUsers[i][0] - const res = await api.getUsers(userId) + const res = await notionAPI.getUsers(userId) const resValue = res?.recordMapWithRoles?.notion_user?.[userId[1]]?.value const user = { diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index d5896ddc..16a36534 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -1,8 +1,7 @@ import BLOG from '@/blog.config' import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' -import { NotionAPI } from 'notion-client' import { deepClone, delay } from '../utils' -import getNotionAPI from '@/lib/notion/getNotionAPI' +import notionAPI from '@/lib/notion/getNotionAPI' /** * 获取文章内容 @@ -43,9 +42,8 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) { retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '' ) try { - const api = getNotionAPI() const start = new Date().getTime() - const pageData = await api.getPage(id) + const pageData = await notionAPI.getPage(id) const end = new Date().getTime() console.log('[API<<--响应]', `耗时:${end - start}ms - from:${from}`) return pageData @@ -163,14 +161,12 @@ export const fetchInBatches = async (ids, batchSize = 100) => { ids = [ids] } - const api = getNotionAPI() - let fetchedBlocks = {} for (let i = 0; i < ids.length; i += batchSize) { const batch = ids.slice(i, i + batchSize) console.log('[API-->>请求] Fetching missing blocks', batch, ids.length) const start = new Date().getTime() - const pageChunk = await api.getBlocks(batch) + const pageChunk = await notionAPI.getBlocks(batch) const end = new Date().getTime() console.log( `[API<<--响应] 耗时:${end - start}ms Fetching missing blocks count:${ids.length} `