Skip to content

Commit

Permalink
fix: 重定向时,重定向的请求时需加上cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
gxr404 committed Jan 9, 2024
1 parent c8e682b commit b18f9fa
Showing 1 changed file with 46 additions and 38 deletions.
84 changes: 46 additions & 38 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from 'axios'
import { randUserAgent } from './utils'
import { DEFAULT_COOKIE_KEY, DEFAULT_DOMAIN } from './constant'

import type { ArticleResponse } from './types/ArticleResponse'
import type { KnowledgeBase } from './types/KnowledgeBaseResponse'
import { DEFAULT_COOKIE_KEY, DEFAULT_DOMAIN } from './constant'

import type { AxiosRequestConfig } from 'axios'

interface IKnowledgeBaseInfo {
bookId?: number
Expand All @@ -18,7 +18,6 @@ interface IKnowledgeBaseInfo {
interface IReqHeader {
[key: string]: string
}

interface GetHeaderParams {
key?:string,
token?: string
Expand All @@ -35,35 +34,45 @@ function getHeaders(params: GetHeaderParams): IReqHeader {
return headers
}

interface CookieInfo {
key?:string,
token?: string
function genCommonOptions(params: GetHeaderParams): AxiosRequestConfig {
return {
headers: getHeaders(params),
beforeRedirect: (options) => {
// 语雀免费非企业空间会重定向如: www.yuque.com -> gxr404.yuque.com
// 此时axios自动重定向并不会带上cookie
options.headers = {
...(options?.headers || {}),
...getHeaders(params)
}
}
}
}

type TGetKnowledgeBaseInfo = (url: string, {key, token}: CookieInfo) => Promise<IKnowledgeBaseInfo>

type TGetKnowledgeBaseInfo = (url: string, headerParams: GetHeaderParams) => Promise<IKnowledgeBaseInfo>
/** 获取知识库数据信息 */
export const getKnowledgeBaseInfo: TGetKnowledgeBaseInfo = (url, {key, token}) => {
export const getKnowledgeBaseInfo: TGetKnowledgeBaseInfo = (url, headerParams) => {
const knowledgeBaseReg = /decodeURIComponent\("(.+)"\)\);/m
return axios.get<string>(url, {
headers: getHeaders({key, token})
}).then(({data = '', status}) => {
if (status === 200) return data
return ''
}).then(html => {
const data = knowledgeBaseReg.exec(html) ?? ''
if (!data[1]) return {}
const jsonData: KnowledgeBase.Response = JSON.parse(decodeURIComponent(data[1]))
if (!jsonData.book) return {}
const info = {
bookId: jsonData.book.id,
bookSlug: jsonData.book.slug,
tocList: jsonData.book.toc || [],
bookName: jsonData.book.name || '',
bookDesc: jsonData.book.description || '',
host: jsonData.space?.host || DEFAULT_DOMAIN,
}
return info
})
return axios.get<string>(url, genCommonOptions(headerParams))
.then(({data = '', status}) => {
if (status === 200) return data
return ''
})
.then(html => {
const data = knowledgeBaseReg.exec(html) ?? ''
if (!data[1]) return {}
const jsonData: KnowledgeBase.Response = JSON.parse(decodeURIComponent(data[1]))
if (!jsonData.book) return {}
const info = {
bookId: jsonData.book.id,
bookSlug: jsonData.book.slug,
tocList: jsonData.book.toc || [],
bookName: jsonData.book.name || '',
bookDesc: jsonData.book.description || '',
host: jsonData.space?.host || DEFAULT_DOMAIN,
}
return info
})
}

interface GetMdDataParams {
Expand All @@ -89,14 +98,13 @@ export const getDocsMdData: TGetMdData = (params) => {
}
const query = new URLSearchParams(queryParams).toString()
apiUrl = `${apiUrl}?${query}`
return axios.get<ArticleResponse.RootObject>(apiUrl, {
headers: getHeaders({token, key}),
}).then(({data, status}) => {
const res = {
apiUrl,
httpStatus: status,
response: data
}
return res
})
return axios.get<ArticleResponse.RootObject>(apiUrl, genCommonOptions({token, key}))
.then(({data, status}) => {
const res = {
apiUrl,
httpStatus: status,
response: data
}
return res
})
}

0 comments on commit b18f9fa

Please sign in to comment.