index.ts 1.11 KB
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: ''
  })
}