refactor(fetch): convert baseOptions to a function for dynamic request options

This commit is contained in:
twwu 2025-09-17 22:37:04 +08:00
parent 73d4bb596a
commit 55f96a4266
2 changed files with 5 additions and 3 deletions

View File

@ -23,7 +23,7 @@ import type {
} from '@/types/workflow'
import { removeAccessToken } from '@/app/components/share/utils'
import type { FetchOptionType, ResponseError } from './fetch'
import { ContentType, base, baseOptions, getAccessToken } from './fetch'
import { ContentType, base, getAccessToken, getBaseOptions } from './fetch'
import { asyncRunSafe } from '@/utils'
import type {
DataSourceNodeCompletedResponse,
@ -400,6 +400,7 @@ export const ssePost = async (
const token = localStorage.getItem('console_token')
const baseOptions = getBaseOptions()
const options = Object.assign({}, baseOptions, {
method: 'POST',
signal: abortController.signal,

View File

@ -111,7 +111,7 @@ const baseClient = ky.create({
timeout: TIME_OUT,
})
export const baseOptions: RequestInit = {
export const getBaseOptions = (): RequestInit => ({
method: 'GET',
mode: 'cors',
credentials: 'include', // always send cookies、HTTP Basic authentication.
@ -119,9 +119,10 @@ export const baseOptions: RequestInit = {
'Content-Type': ContentType.json,
}),
redirect: 'follow',
}
})
async function base<T>(url: string, options: FetchOptionType = {}, otherOptions: IOtherOptions = {}): Promise<T> {
const baseOptions = getBaseOptions()
const { params, body, headers, ...init } = Object.assign({}, baseOptions, options)
const {
isPublicAPI = false,