Skip to content

Commit c53dcef

Browse files
committed
build(projects): update
1 parent 898a932 commit c53dcef

File tree

7 files changed

+91
-14
lines changed

7 files changed

+91
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"qs": "^6.11.0",
3434
"simple-git-hooks": "^2.8.1",
3535
"tsup": "^6.5.0",
36+
"tsx": "^3.12.2",
3637
"typescript": "^4.9.4"
3738
},
3839
"simple-git-hooks": {

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Request {
1616

1717
requestConfig: RequiredRequestConfig;
1818

19-
constructor(axiosConfig: AxiosRequestConfig, requestConfig?: RequestConfig) {
19+
constructor(axiosConfig?: AxiosRequestConfig, requestConfig?: RequestConfig) {
2020
this.axiosInstance = axios.create(axiosConfig);
2121
this.setInterceptor();
2222

@@ -72,10 +72,8 @@ class Request {
7272
* 创建请求
7373
* @param axiosConfig axios配置
7474
* @param requestConfig 请求配置
75-
* @param isRaw 是否返回原始的axios实例, (默认返回自定义的请求实例)
7675
*/
77-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
78-
export function createRequest(axiosConfig: AxiosRequestConfig, requestConfig?: RequestConfig, isRaw = false) {
76+
export function createRequest(axiosConfig?: AxiosRequestConfig, requestConfig?: RequestConfig) {
7977
const request = new Request(axiosConfig, requestConfig);
8078

8179
return request.axiosInstance;

src/shared/common.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AxiosRequestConfig, AxiosInstance } from 'axios';
1+
import type { AxiosRequestConfig, RawAxiosRequestHeaders } from 'axios';
22
import { CONTENT_TYPE, type ContentTypeValue } from './constant';
33
import type { RequestConfig, RequiredRequestConfig } from '../types';
44

@@ -14,9 +14,7 @@ export function createDefaultRequestConfig(requestConfig?: RequestConfig) {
1414
return responseData[codeKey] === BACKEND_SUCCESS_CODE;
1515
},
1616
onBackendFail: async () => {},
17-
onError: async ({ error }) => {
18-
window.console.log(`code: ${error.code}, msg: ${error.msg}`);
19-
}
17+
onError: async () => {}
2018
};
2119

2220
Object.assign(configs, requestConfig);
@@ -25,7 +23,9 @@ export function createDefaultRequestConfig(requestConfig?: RequestConfig) {
2523
}
2624

2725
export function getRequestHeaderContentType(config: AxiosRequestConfig) {
28-
const contentType = (config?.headers?.['Content-Type'] || CONTENT_TYPE.json) as ContentTypeValue;
26+
const headerContentType = (config?.headers as RawAxiosRequestHeaders)?.['Content-Type'];
27+
28+
const contentType = (headerContentType || CONTENT_TYPE.json) as ContentTypeValue;
2929

3030
return contentType;
3131
}
@@ -38,6 +38,3 @@ export function isHttpSuccess(status: number) {
3838
const isSuccessCode = status >= 200 && status < 300;
3939
return isSuccessCode || status === 304;
4040
}
41-
42-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
43-
export function getCustomRequestInstance(instance: AxiosInstance) {}

src/shared/error.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
BLANK_AXIOS_ERROR_CODE,
1010
ERROR_STATUS
1111
} from './constant';
12+
import { getNetworkIsOnline } from './network';
1213
import type { ErrorStatus } from './constant';
1314
import type { CustomError, RawError, RequestError, RequestConfig } from '../types';
1415

@@ -30,7 +31,7 @@ export function handleAxiosError<T, D>(rawError: RawError<T, D>): RequestError<T
3031
const actions: [boolean, () => void][] = [
3132
[
3233
// 网路错误
33-
!window.navigator.onLine || rawError.message === 'Network Error',
34+
!getNetworkIsOnline() || rawError.message === 'Network Error',
3435
() => {
3536
updateCustomError(error, { code: NETWORK_ERROR_CODE, msg: NETWORK_ERROR_MSG });
3637
}
@@ -78,7 +79,7 @@ export function handleHttpError<T, D>(response: AxiosResponse<T, D>): RequestErr
7879
msg: DEFAULT_REQUEST_ERROR_MSG
7980
};
8081

81-
if (!window.navigator.onLine) {
82+
if (!getNetworkIsOnline()) {
8283
// 网路错误
8384
updateCustomError(error, { code: NETWORK_ERROR_CODE, msg: NETWORK_ERROR_MSG });
8485
} else {

src/shared/network.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { networkInterfaces } from 'node:os';
2+
3+
export function getNetworkIsOnline() {
4+
const isWindow = typeof window === 'object';
5+
6+
const windowNetworkOnline = isWindow && window.navigator.onLine;
7+
8+
console.log(networkInterfaces());
9+
10+
return windowNetworkOnline || false;
11+
}

test/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createRequest } from '../src';
2+
3+
const request = createRequest(
4+
{},
5+
{
6+
codeKey: 'code',
7+
dataKey: 'data',
8+
msgKey: 'msg',
9+
onBackendSuccess(responseData) {
10+
const isObject = Object.prototype.toString.call(responseData) === '[Object object]';
11+
12+
/** 后端自定义的成功code */
13+
const SUCCESS_CODE = '0000';
14+
15+
const hasCodeKey = Object.keys(responseData).includes(this.codeKey);
16+
17+
const isFailedByCode = responseData.code !== SUCCESS_CODE;
18+
19+
return !(isObject && hasCodeKey && isFailedByCode) || true;
20+
}
21+
}
22+
);
23+
24+
async function fetchSearchBook() {
25+
const data = await request('https://www.baidu.com', { responseType: 'arraybuffer' }).catch(err => {
26+
console.log(err);
27+
});
28+
console.log('data: ', data);
29+
}
30+
31+
fetchSearchBook();

0 commit comments

Comments
 (0)