summaryrefslogtreecommitdiff
path: root/extension/react-app/src/util/api.ts
blob: bdec1d2006dece820666e3dbdf00876a204df5c4 (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
import {
  Configuration,
  DebugApi,
  UnittestApi,
  ChatApi,
} from "../../../src/client";
import { useSelector } from "react-redux";
import { useEffect, useState } from "react";
import { RootStore } from "../redux/store";

export function useApi() {
  const apiUrl = useSelector((state: RootStore) => state.config.apiUrl);
  const vscMachineId = useSelector(
    (state: RootStore) => state.config.vscMachineId
  );
  const [debugApi, setDebugApi] = useState<DebugApi>();
  const [unittestApi, setUnittestApi] = useState<UnittestApi>();
  const [chatApi, setChatApi] = useState<ChatApi>();

  useEffect(() => {
    if (apiUrl && vscMachineId) {
      let config = new Configuration({
        basePath: apiUrl,
        fetchApi: fetch,
        middleware: [
          {
            pre: async (context) => {
              context.init.headers = {
                ...context.init.headers,
                "x-vsc-machine-id": vscMachineId,
              };
            },
          },
        ],
      });
      setDebugApi(new DebugApi(config));
      setUnittestApi(new UnittestApi(config));
      setChatApi(new ChatApi(config));
    }
  }, [apiUrl, vscMachineId]);

  return { debugApi, unittestApi, chatApi };
}