From d5ae329e2c3a1e18b5fd5fc3507791be9da9b4ed Mon Sep 17 00:00:00 2001 From: anime Date: Sat, 7 Dec 2024 12:53:10 +0800 Subject: [PATCH] =?UTF-8?q?feat&fix(=E6=94=AF=E6=8C=81=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E6=95=B0=E6=8D=AE=E8=AE=BF=E9=97=AE?= =?UTF-8?q?):=20=E4=BD=BF=E7=94=A8react-notion-x=E8=A6=81=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F=E8=BF=9B=E8=A1=8CNotionAPI=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.config.js | 3 ++- lib/notion/getNotionAPI.js | 10 ++++++++++ lib/notion/getPageProperties.js | 3 ++- lib/notion/getPostBlocks.js | 13 +++---------- 4 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 lib/notion/getNotionAPI.js diff --git a/blog.config.js b/blog.config.js index fca92364ed6..9f94950d8bc 100644 --- a/blog.config.js +++ b/blog.config.js @@ -551,7 +551,8 @@ const BLOG = { process.env.NEXT_PUBLIC_DESCRIPTION || '这是一个由NotionNext生成的站点', // 站点描述,被notion中的页面描述覆盖 // 开发相关 - NOTION_ACCESS_TOKEN: process.env.NOTION_ACCESS_TOKEN || '', // Useful if you prefer not to make your database public + NOTION_ACTIVE_USER: process.env.NOTION_ACTIVE_USER || '', + NOTION_TOKEN_V2: process.env.NOTION_TOKEN_V2 || '', // Useful if you prefer not to make your database public DEBUG: process.env.NEXT_PUBLIC_DEBUG || false, // 是否显示调试按钮 ENABLE_CACHE: process.env.ENABLE_CACHE || diff --git a/lib/notion/getNotionAPI.js b/lib/notion/getNotionAPI.js new file mode 100644 index 00000000000..c2cbb6a63a4 --- /dev/null +++ b/lib/notion/getNotionAPI.js @@ -0,0 +1,10 @@ +import { NotionAPI } from 'notion-client' +import BLOG from '@/blog.config' + +export default function getNotionAPI() { + return new NotionAPI({ + activeUser: BLOG.NOTION_ACTIVE_USER || null, + authToken: BLOG.NOTION_TOKEN_V2 || null, + userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone + }) +} diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index 6342818474a..5d5e7ff6a38 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -12,6 +12,7 @@ import { } from '../utils' import { extractLangPrefix } from '../utils/pageId' import { mapImgUrl } from './mapImage' +import getNotionAPI from '@/lib/notion/getNotionAPI' /** * 获取页面元素成员属性 @@ -56,7 +57,7 @@ export default async function getPageProperties( case 'person': { const rawUsers = val.flat() const users = [] - const api = new NotionAPI({ authToken }) + const api = getNotionAPI() for (let i = 0; i < rawUsers.length; i++) { if (rawUsers[i][0][1]) { diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index 43d9c430513..8833e212e5e 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -2,6 +2,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' /** * 获取文章内容 @@ -43,11 +44,7 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) { retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '' ) try { - const authToken = BLOG.NOTION_ACCESS_TOKEN || null - const api = new NotionAPI({ - authToken, - userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone - }) + const api = getNotionAPI() const start = new Date().getTime() const pageData = await api.getPage(id) const end = new Date().getTime() @@ -168,11 +165,7 @@ export const fetchInBatches = async (ids, batchSize = 100) => { ids = [ids] } - const authToken = BLOG.NOTION_ACCESS_TOKEN || null - const api = new NotionAPI({ - authToken, - userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone - }) + const api = getNotionAPI() let fetchedBlocks = {} for (let i = 0; i < ids.length; i += batchSize) {