/*
 * @Author: 代码侠
 * @Date: 2022-10-25 11:57:41
 * @Email: achao@achao.cc
 * @LastEditTime: 2022-12-08 20:58:13
 * @Mobile: 18000599588
 */
import TokenStore from "@/stores/Token";

class HttpHelp {
  headers: Headers = new Headers();
  public constructor() {
    const token = TokenStore();
    this.headers.append("Content-Type", "application/json");
    this.headers.append("token", token.token);
  }
  public async get(url: string): Promise<Response> {
    const response = await fetch(url, {
      method: "get",
      headers: this.headers,
    });
    return response;
  }
  public async post(url: string, body: string): Promise<Response> {
    const response = await fetch(url, {
      method: "post",
      headers: this.headers,
      body: body,
    });
    return response;
  }
  public async postForm(url: string, body: FormData): Promise<Response> {
    const token = TokenStore();
    const headers1: Headers = new Headers();
    headers1.append("token", token.token);
    const response = await fetch(url, {
      method: "post",
      headers: headers1,
      body: body,
    });
    return response;
  }
  public async put(url: string, body: string): Promise<Response> {
    const response = await fetch(url, {
      method: "put",
      headers: this.headers,
      body: body,
    });
    return response;
  }
  public async delete(url: string, body: string): Promise<Response> {
    const response = await fetch(url, {
      method: "delete",
      headers: this.headers,
      body: body,
    });
    return response;
  }
  public async upload(url: string, formData: FormData): Promise<Response> {
    const token1 = TokenStore();
    const headers1: Headers = new Headers();
    headers1.append("Content-Type", "application/json");
    headers1.append("token", token1.token);
    const response = await fetch(url, {
      method: "post",
      headers: headers1,
      body: formData,
    });
    return response;
  }
}
export { HttpHelp };