Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 460 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 460 Rev 467
Línea 4... Línea 4...
4
  #client
4
  #client
5
  #abortController
5
  #abortController
Línea 6... Línea 6...
6
 
6
 
7
  constructor(baseURL, timeout = 5000) {
7
  constructor(baseURL, timeout = 5000) {
-
 
8
    this.#client = axios({ baseURL, timeout })
-
 
9
 
-
 
10
    this.#client.interceptors.request.use(
-
 
11
      async (request) => {
-
 
12
        const token = window.localStorage.getItem("jwt");
-
 
13
 
-
 
14
        if (token) {
-
 
15
          request.headers["Authorization"] = "Bearer " + token;
-
 
16
        }
-
 
17
 
-
 
18
        if (["post", "put", "delete"].includes(request.method)) {
-
 
19
          try {
-
 
20
            const resp = await this.get("/csrf");
-
 
21
            const { data, success } = resp.data
-
 
22
 
-
 
23
            if (success) {
-
 
24
              request.headers["X-CSRF-TOKEN"] = data;
-
 
25
              return request;
-
 
26
            }
-
 
27
 
-
 
28
          } catch (err) {
-
 
29
            throw new Error(
-
 
30
              `Axios problem with request during pre-flight phase: ${err}.`
-
 
31
            );
-
 
32
          }
-
 
33
        }
-
 
34
      },
-
 
35
      (error) => {
-
 
36
        return Promise.reject(error);
-
 
37
      }
-
 
38
    );
8
    this.#client = axios({ baseURL, timeout })
39
 
9
    this.#abortController = new AbortController()
40
    this.#abortController = new AbortController()
Línea 10... Línea 41...
10
  }
41
  }
11
 
42