fix(project): sTATUS_CODE
This commit is contained in:
44
api/fetch.ts
44
api/fetch.ts
@@ -26,6 +26,7 @@ interface OptionsProps {
|
||||
method: string
|
||||
body?: BodyInit
|
||||
data?: any
|
||||
responseType?: "blob" | "arraybuffer" | "stream"
|
||||
needCredentials?: boolean
|
||||
isUpload?: boolean
|
||||
isDownload?: boolean
|
||||
@@ -52,11 +53,23 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
},
|
||||
}
|
||||
|
||||
const getResponseType = (requestOptions: OptionsProps) => {
|
||||
return (
|
||||
requestOptions.responseType ||
|
||||
(requestOptions.headerAttr?.responseType as
|
||||
| "blob"
|
||||
| "arraybuffer"
|
||||
| "stream"
|
||||
| undefined)
|
||||
)
|
||||
}
|
||||
|
||||
const createSsrRequest: (
|
||||
options: OptionsProps,
|
||||
token?: string,
|
||||
refreshToken?: string
|
||||
) => Request = (options) => {
|
||||
const responseType = getResponseType(options)
|
||||
// 设置请求头
|
||||
let myHeaders = new Headers()
|
||||
if (!options.isUpload && !options.isFormData && !options.notJson) {
|
||||
@@ -79,7 +92,13 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
cache: "default",
|
||||
body: options.isFormData
|
||||
? options.body
|
||||
: (JSON.stringify({ data: options.body, ...options }) as BodyInit),
|
||||
: (JSON.stringify({
|
||||
data: options.body,
|
||||
...options,
|
||||
options: {
|
||||
responseType,
|
||||
},
|
||||
}) as BodyInit),
|
||||
}
|
||||
const pathList = options.url.split("/")
|
||||
const request = new Request(
|
||||
@@ -108,6 +127,7 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
}
|
||||
if (options.headerAttr) {
|
||||
Object.entries(options.headerAttr).map(([key, value]) => {
|
||||
if (key === "responseType") return
|
||||
myHeaders.append(key, value)
|
||||
})
|
||||
}
|
||||
@@ -151,9 +171,12 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
return new Promise((resolved, rejected) => {
|
||||
fetch(request)
|
||||
.then(async (response) => {
|
||||
const responseType = getResponseType(options)
|
||||
if (response.status === 200) {
|
||||
if (options.isDownload) {
|
||||
return response.blob()
|
||||
} else if (responseType === "stream") {
|
||||
return response
|
||||
} else {
|
||||
return response.headers
|
||||
.get("Content-Type")
|
||||
@@ -187,6 +210,8 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
})
|
||||
const retry_request = createNewRequest(options, access_token)
|
||||
const retry_response = await fetch(retry_request)
|
||||
const responseType = getResponseType(options)
|
||||
if (responseType === "stream") return retry_response
|
||||
return options.isDownload
|
||||
? retry_response.blob()
|
||||
: retry_response.json()
|
||||
@@ -257,6 +282,13 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
}
|
||||
})
|
||||
.then(async (response) => {
|
||||
if (
|
||||
getResponseType(options) === "stream" &&
|
||||
response instanceof Response
|
||||
) {
|
||||
resolved(response as T)
|
||||
return
|
||||
}
|
||||
const code = response?.code
|
||||
const errInfo = response?.message || ""
|
||||
if (response) {
|
||||
@@ -279,6 +311,16 @@ async function httpfetch<T>(options: OptionsProps): Promise<T> {
|
||||
: retry_response.json()
|
||||
} catch (error) {
|
||||
rejected(error)
|
||||
try {
|
||||
await deleteCookie()
|
||||
} catch (error) {
|
||||
rejected(error)
|
||||
} finally {
|
||||
// 清空身份信息并跳转至登录页面
|
||||
removeUserInfo()
|
||||
logout()
|
||||
window.location.href = "/login"
|
||||
}
|
||||
}
|
||||
} else if (status === 400) {
|
||||
const { message: errInfo } = response
|
||||
|
||||
@@ -123,8 +123,8 @@ export async function POST(req: any) {
|
||||
if (!sessionResult.ok) {
|
||||
return attachServerTiming(
|
||||
NextResponse.json(
|
||||
{ message: sessionResult.message || "未获取到用户信息", code: 401 },
|
||||
{ status: 401 }
|
||||
{ message: sessionResult.message || "未获取到用户信息", code: 406 },
|
||||
{ status: 406 }
|
||||
),
|
||||
{
|
||||
...timing,
|
||||
|
||||
@@ -39,6 +39,7 @@ export const STATUS_CODE = new Map([
|
||||
[190, "Embedding生成中"],
|
||||
[191, "Embedding生成失败"],
|
||||
[199, "审核失败"],
|
||||
[200, "已准备"],
|
||||
[201, "试标阶段"], // 试标中
|
||||
[202, "试标阶段"], // 试标完成
|
||||
[203, "试标阶段"], // 试标确认
|
||||
|
||||
Reference in New Issue
Block a user