
export interface ReqData {
  [key: string]: string | boolean | number | unknown;
}

const baseUrl = "https://www.xunlianying.vip";

function postForm<T>(url: string, body: { [key: string]: string }): Promise<T> {
  const options = {
    method: "POST",
    body,
    headers: {
      authorization: `${localStorage.getItem("AUTH")}`,
    },
  };
  return fetch(`${baseUrl}${url}`, options as unknown as RequestInit).then((res) => res.json() as unknown as T);
}

export function post<T>(url: string, data: ReqData| FormData, isFormData?: boolean): Promise<T> {
  const header = {
    authorization: `${localStorage.getItem("AUTH")}`,
  };

  const headerJson = {
    authorization: `${localStorage.getItem("AUTH")}`,
    "content-type": "application/json",
  };

  const options = {
    method: "POST",
    body: isFormData !== true ? JSON.stringify(data) : data as FormData,
    headers: isFormData !== true ? { ...header, ...headerJson } : header,
  };
  return fetch(`${baseUrl}${url}`, options).then((res) => res.json()).catch((r: unknown) => r).then((rr) => rr);
}

export interface RetSeek {
  code: string;
  msg: string;
}

export const requestUploadFile1 = async (data: FormData): Promise<RetSeek> => await post("/horse/v1/card/image/save", data, true) as unknown as RetSeek;


export const retUrlTitle = async (url: string): Promise<string> => {
  const r = await fetch(url, {});
  const title = await r.text();
  return title.split("<title>")[1].split("</title>")[0];
};
