From 3f94723d7a375e0d1335d0207a2c04a957eb3d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=9C=E6=B5=85?= <14177215+unliar@users.noreply.github.com> Date: Wed, 1 Jan 2025 01:33:07 +0800 Subject: [PATCH] fix: (routes/zhihu/utils) getSignedHeader throw error cause request failed and ts type fix (#18001) * refactor: it looks good to me * refactor: it looks good to me --- lib/routes/zhihu/utils.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/routes/zhihu/utils.ts b/lib/routes/zhihu/utils.ts index 00ed64e4abf506..0800c7fbbff321 100644 --- a/lib/routes/zhihu/utils.ts +++ b/lib/routes/zhihu/utils.ts @@ -61,7 +61,7 @@ export const getCookieValueByKey = (key: string) => ?.split(';') .map((e) => e.trim()) .find((e) => e.startsWith(key + '=')) - ?.slice(key.length + 1); + ?.slice(key.length + 1) || ''; export const getSignedHeader = async (url: string, apiPath: string) => { // Because the API of zhihu.com has changed, we must use the value of `d_c0` (extracted from cookies) to calculate @@ -76,7 +76,6 @@ export const getSignedHeader = async (url: string, apiPath: string) => { const response1 = await ofetch.raw('https://static.zhihu.com/zse-ck/v3.js'); const script = await response1._data.text(); const zseCk = script.match(/__g\.ck\|\|"([\w+/=\\]*?)",_=/)?.[1]; - const response2 = zseCk ? await ofetch.raw(url, { headers: { @@ -88,15 +87,13 @@ export const getSignedHeader = async (url: string, apiPath: string) => { }) : null; - const dc0 = (response2 || response1).headers - .getSetCookie() - .find((s) => s.startsWith('d_c0=')) - ?.split(';')[0] - .trim() - .slice('d_c0='.length); - if (!dc0) { - throw new Error('Failed to extract `d_c0` from cookies'); - } + const dc0 = + (response2 || response1).headers + .getSetCookie() + .find((s) => s.startsWith('d_c0=')) + ?.split(';')[0] + .trim() + .slice('d_c0='.length) || ''; return dc0; });