ApiCall.ts 941 Bytes
Newer Older
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
class ApiCall<T> {
  public loading: boolean;
  public error: string;
  public timestamp: Date | number; // last successful execution (helps with invalidating cached data)
  public data: T;
  public progress?: number;


  public static start = () => ({
    loading: true,
    error: null,
  })

  public static progress = (progress) => ({
    loading: true,
    error: null,
    progress,
  })

  public static success = (data) => ({
    loading: false,
    data,
    timestamp: Date.now(),
    error: null,
  })

  public static error = (error) => {
    console.log('ApiCall error', error);
    return {
      loading: false,
      data: null,
      timestamp: Date.now(),
33
34
35
36
37
      error: {
        status: error.response.status,
        data: error.response && error.response.data,
        message: `${error.message} ${error.response && error.response.data && error.response.data.error || ''}`,
      },
38
39
40
41
42
    };
  }
}

export default ApiCall;