index.ts
1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { RequestBodyEnum } from "@/enums/httpEnum"
import { RequestConfigType, RequestGlobalConfigType } from "@/store/modules/chartEditStore/chartEditStore.d"
import { defHttp } from "@/utils/external/http/axios"
export const isFullUrl = (url = '') => {
try {
new URL(url)
return true
} catch (error) {
return false
}
}
export const getUrl = (url = '') => {
const isFullUrlFlag = isFullUrl(url)
const { origin } = window.location
return isFullUrlFlag ? new URL(url) : { pathname: url, origin }
}
export const customRequest = async (request: RequestConfigType) => {
const { requestHttpType, requestParams, requestParamsBodyType, requestUrl } = request as RequestGlobalConfigType & RequestConfigType
const { Params, Header, Body } = requestParams
const { origin, pathname } = getUrl(requestUrl!)
const body = Body[requestParamsBodyType as Exclude<'NONE', keyof typeof RequestBodyEnum>]
return defHttp.request<any>({
url: pathname,
baseURL: origin,
method: requestHttpType,
params: Params,
data: body,
headers: Header
}, {
joinPrefix: false,
apiUrl: ''
})
}