Skip to content

Commit

Permalink
fix: (routes/zhihu/utils) getSignedHeader throw error cause request f…
Browse files Browse the repository at this point in the history
…ailed and ts type fix (DIYgod#18001)

* refactor: it looks good to me

* refactor: it looks good to me
  • Loading branch information
unliar authored Dec 31, 2024
1 parent 4561197 commit 3f94723
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/routes/zhihu/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {
Expand All @@ -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;
});
Expand Down

0 comments on commit 3f94723

Please sign in to comment.