广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
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
import type { AxiosHeaderValue, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import type { ResponseType } from './type';
 
export function getContentType(config: InternalAxiosRequestConfig) {
  const contentType: AxiosHeaderValue = config.headers?.['Content-Type'] || 'application/json';
 
  return contentType;
}
 
/**
 * check if http status is success
 *
 * @param status
 */
export function isHttpSuccess(status: number) {
  const isSuccessCode = status >= 200 && status < 300;
  return isSuccessCode || status === 304;
}
 
/**
 * is response json
 *
 * @param response axios response
 */
export function isResponseJson(response: AxiosResponse) {
  const { responseType } = response.config;
 
  return responseType === 'json' || responseType === undefined;
}
 
export async function transformResponse(response: AxiosResponse) {
  const responseType: ResponseType = (response.config?.responseType as ResponseType) || 'json';
  if (responseType === 'json') return;
 
  const isJson = response.headers['content-type']?.includes('application/json');
  if (!isJson) return;
 
  if (responseType === 'blob') {
    await transformBlobToJson(response);
  }
 
  if (responseType === 'arrayBuffer') {
    await transformArrayBufferToJson(response);
  }
}
 
export async function transformBlobToJson(response: AxiosResponse) {
  try {
    let data = response.data;
 
    if (typeof data === 'string') {
      data = JSON.parse(data);
    }
 
    if (Object.prototype.toString.call(data) === '[object Blob]') {
      const json = await data.text();
      data = JSON.parse(json);
    }
 
    response.data = data;
  } catch {}
}
 
export async function transformArrayBufferToJson(response: AxiosResponse) {
  try {
    let data = response.data;
 
    if (typeof data === 'string') {
      data = JSON.parse(data);
    }
 
    if (Object.prototype.toString.call(data) === '[object ArrayBuffer]') {
      const json = new TextDecoder().decode(data);
      data = JSON.parse(json);
    }
 
    response.data = data;
  } catch {}
}