aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/apis/raw/utils.ts
blob: 1fe98c35bed068140f3b33dafb21a77be9654855 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
import client from "./client";

type UrlTestResponse =
  | {
      status: true;
      version: string;
      code: number;
    }
  | {
      status: false;
      error: string;
      code: number;
    };

class RequestUtils {
  async urlTest(protocol: string, url: string, params?: LooseObject) {
    try {
      const result = await client.axios.get<UrlTestResponse>(
        `../test/${protocol}/${url}api/system/status`,
        { params },
      );
      const { data } = result;
      if (data.status && data.version) {
        return data;
      } else {
        throw new Error("Cannot get response, fallback to v3 api");
      }
    } catch (e) {
      const result = await client.axios.get<UrlTestResponse>(
        `../test/${protocol}/${url}api/v3/system/status`,
        { params },
      );
      return result.data;
    }
  }

  async providerUrlTest(protocol: string, url: string, params?: LooseObject) {
    const result = await client.axios.get<UrlTestResponse>(
      `../test/${protocol}/${url}status`,
      { params },
    );
    const { data } = result;
    if (data.status && data.version) {
      return data;
    }
    return result.data;
  }
}

const requestUtils = new RequestUtils();
export default requestUtils;