import type { CustomRequestOptions } from '@/http/types'
import {http as httpUtils} from "@/utils/http";

export function http<T>(options: CustomRequestOptions) {
    // 1. 返回 Promise 对象
    return httpUtils<T>(options)
}

/**
 * GET 请求
 * @param url 后台地址
 * @param query 请求query参数
 * @param header 请求头，默认为json格式
 * @returns
 */
export function httpGet<T>(url: string, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
    return http<T>({
        url,
        query,
        method: 'GET',
        header,
        ...options,
    })
}

/**
 * POST 请求
 * @param url 后台地址
 * @param data 请求body参数
 * @param query 请求query参数，post请求也支持query，很多微信接口都需要
 * @param header 请求头，默认为json格式
 * @returns
 */
export function httpPost<T>(url: string, data?: Record<string, any>, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
    return http<T>({
        url,
        query,
        data,
        method: 'POST',
        header,
        ...options,
    })
}

http.get = httpGet
http.post = httpPost

// 支持与 alovaJS 类似的API调用
http.Get = httpGet
http.Post = httpPost
