"src/diffusers/schedulers/scheduling_euler_discrete_flax.py" did not exist on "d7b692083c794b4047930cd84c17c0da3272510b"
http.ts 2 KB
Newer Older
dechen lin's avatar
dechen lin committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import Cookies from "js-cookie";
import { message } from "antd";

interface ApiResponse<T> {
  code: number;
  msg: string;
  data: T;
}

interface ErrorResponse {
  code: number;
  msg: string;
  msgZh: string;
}

export interface ProcessedResponse<T> {
  data: T;
  error: null | Pick<ErrorResponse, "msg" | "msgZh">;
}

interface CustomAxiosInstance
  extends Omit<AxiosInstance, "get" | "post" | "put" | "delete"> {
  get<T, R = AxiosResponse<ProcessedResponse<T>>>(
    url: string,
    config?: AxiosRequestConfig
  ): Promise<R>;
  post<T, R = AxiosResponse<ProcessedResponse<T>>>(
    url: string,
    data?: unknown,
    config?: AxiosRequestConfig
  ): Promise<R>;
  put<T, R = AxiosResponse<ProcessedResponse<T>>>(
    url: string,
    data?: unknown,
    config?: AxiosRequestConfig
  ): Promise<R>;
  delete<T, R = AxiosResponse<ProcessedResponse<T>>>(
    url: string,
    config?: AxiosRequestConfig
  ): Promise<R>;
}

const instance: CustomAxiosInstance = axios.create({
  baseURL: "",
  timeout: 10000,
  headers: {
    "Content-Type": "application/json",
  },
});

const processResponse = <T>(
  response: AxiosResponse<ApiResponse<T>>
): ProcessedResponse<T> => {
  if (response.data.code === 200) {
    return {
      data: response.data.data,
      error: null,
    };
  } else {
    return {
      data: response.data.data || ({} as T),
      error: {
        msg: response.data.msg,
        msgZh: (response.data as unknown as ErrorResponse).msgZh,
      },
    };
  }
};

instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  <T>(
    response: AxiosResponse<ApiResponse<T>>
  ): AxiosResponse<ProcessedResponse<T>> => {
    return { ...response, data: processResponse(response) };
  },
  (error) => {
    message.error(error?.response?.data?.msg || "Error");
    return Promise.reject(error);
  }
);

export default instance;