cosey 0.2.15 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import elTableColumnProps from 'element-plus/es/components/table/src/table-colum
3
3
  const tableColumnProps = {
4
4
  ...elTableColumnProps,
5
5
  slots: {
6
- type: [String, Object]
6
+ type: [String, Object, Function]
7
7
  },
8
8
  renderer: {
9
9
  type: [String, Object],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cosey",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "基于 Vue3 + vite 的后台管理系统框架",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/request/Http.d.ts CHANGED
@@ -1,18 +1,19 @@
1
- import { AxiosInstance, AxiosRequestConfig } from 'axios';
1
+ import { AxiosInstance, type AxiosRequestConfig } from 'axios';
2
+ import { type HttpConfig } from '../config/http';
2
3
  export declare class Http {
3
- axiosFactory: () => AxiosInstance;
4
+ axiosFactory: (httpConfig?: HttpConfig) => AxiosInstance;
4
5
  axiosIns: AxiosInstance | null;
5
6
  controller: AbortController;
6
7
  config: AxiosRequestConfig | null;
7
- constructor(axiosFactory: () => AxiosInstance);
8
+ constructor(axiosFactory: (httpConfig?: HttpConfig) => AxiosInstance);
8
9
  abort(): void;
9
- _request<T = any, D = any>(config: AxiosRequestConfig<D>): Promise<T>;
10
- request<T = any, D = any>(config: AxiosRequestConfig<D>): Promise<T>;
11
- get<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
12
- delete<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
13
- head<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
14
- options<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T>;
15
- post<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>;
16
- put<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>;
17
- patch<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>;
10
+ _request<T = any, D = any>(config: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
11
+ request<T = any, D = any>(config: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
12
+ get<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
13
+ delete<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
14
+ head<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
15
+ options<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
16
+ post<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
17
+ put<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
18
+ patch<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>, httpConfig?: HttpConfig): Promise<T>;
18
19
  }
package/request/Http.js CHANGED
@@ -12,69 +12,90 @@ class Http {
12
12
  this.controller.abort();
13
13
  }
14
14
  }
15
- _request(config) {
15
+ _request(config, httpConfig) {
16
16
  if (!config.signal) {
17
17
  config.signal = this.controller.signal;
18
18
  }
19
19
  if (!this.axiosIns) {
20
- this.axiosIns = this.axiosFactory();
20
+ this.axiosIns = this.axiosFactory(httpConfig);
21
21
  }
22
22
  return this.axiosIns.request(config);
23
23
  }
24
- request(config) {
25
- return this._request(config);
24
+ request(config, httpConfig) {
25
+ return this._request(config, httpConfig);
26
26
  }
27
- get(url, config) {
28
- return this._request({
29
- ...config,
30
- url,
31
- method: "get"
32
- });
27
+ get(url, config, httpConfig) {
28
+ return this._request(
29
+ {
30
+ ...config,
31
+ url,
32
+ method: "get"
33
+ },
34
+ httpConfig
35
+ );
33
36
  }
34
- delete(url, config) {
35
- return this._request({
36
- ...config,
37
- url,
38
- method: "delete"
39
- });
37
+ delete(url, config, httpConfig) {
38
+ return this._request(
39
+ {
40
+ ...config,
41
+ url,
42
+ method: "delete"
43
+ },
44
+ httpConfig
45
+ );
40
46
  }
41
- head(url, config) {
42
- return this._request({
43
- ...config,
44
- url,
45
- method: "head"
46
- });
47
+ head(url, config, httpConfig) {
48
+ return this._request(
49
+ {
50
+ ...config,
51
+ url,
52
+ method: "head"
53
+ },
54
+ httpConfig
55
+ );
47
56
  }
48
- options(url, config) {
49
- return this._request({
50
- ...config,
51
- url,
52
- method: "options"
53
- });
57
+ options(url, config, httpConfig) {
58
+ return this._request(
59
+ {
60
+ ...config,
61
+ url,
62
+ method: "options"
63
+ },
64
+ httpConfig
65
+ );
54
66
  }
55
- post(url, data, config) {
56
- return this._request({
57
- ...config,
58
- url,
59
- data,
60
- method: "post"
61
- });
67
+ post(url, data, config, httpConfig) {
68
+ return this._request(
69
+ {
70
+ ...config,
71
+ url,
72
+ data,
73
+ method: "post"
74
+ },
75
+ httpConfig
76
+ );
62
77
  }
63
- put(url, data, config) {
64
- return this._request({
65
- ...config,
66
- url,
67
- data,
68
- method: "put"
69
- });
78
+ put(url, data, config, httpConfig) {
79
+ return this._request(
80
+ {
81
+ ...config,
82
+ url,
83
+ data,
84
+ method: "put"
85
+ },
86
+ httpConfig
87
+ );
70
88
  }
71
- patch(url, data, config) {
72
- return this._request({
73
- ...config,
74
- url,
75
- data,
76
- method: "patch"
77
- });
89
+ patch(url, data, config, httpConfig) {
90
+ return this._request(
91
+ {
92
+ ...config,
93
+ url,
94
+ data,
95
+ method: "patch"
96
+ },
97
+ httpConfig
98
+ );
78
99
  }
79
100
  }
80
101
 
@@ -1,6 +1,7 @@
1
1
  import { type CreateAxiosDefaults } from 'axios';
2
2
  import { Http } from './Http';
3
- export declare function useRequest(config?: CreateAxiosDefaults): {
3
+ import { type HttpConfig } from '../config/http';
4
+ export declare function useRequest(config?: CreateAxiosDefaults, useHttpConfig?: HttpConfig): {
4
5
  fn: <T extends (http: Http) => (...args: any[]) => any, R = {
5
6
  (...args: Parameters<ReturnType<T>>): ReturnType<ReturnType<T>>;
6
7
  abort: () => void;
@@ -1,7 +1,7 @@
1
1
  import { useRouter, useRoute } from 'vue-router';
2
2
  import axios, { AxiosError } from 'axios';
3
3
  import { ElMessage } from 'element-plus';
4
- import { defaults, pick, get } from 'lodash-es';
4
+ import { merge, defaults, pick, get } from 'lodash-es';
5
5
  import { useUserStore } from '../store/user.js';
6
6
  import { TOKEN_NAME, ROUTER_TO } from '../constant.js';
7
7
  import { Http } from './Http.js';
@@ -9,25 +9,25 @@ import { useGlobalConfig } from '../config/index.js';
9
9
  import { usePersist } from '../hooks/usePersist.js';
10
10
  import { isObject } from '../utils/is.js';
11
11
 
12
- function useRequest(config = {}) {
12
+ function useRequest(config = {}, useHttpConfig) {
13
13
  const persist = usePersist();
14
14
  const userStore = useUserStore();
15
15
  const router = useRouter();
16
16
  const route = useRoute();
17
17
  const { http: httpConfig, router: routerConfig } = useGlobalConfig();
18
- const handleError = (resData) => {
19
- const httpPath = httpConfig.path;
18
+ const handleError = (resData, mergedHttpConfig) => {
19
+ const httpPath = mergedHttpConfig.path;
20
20
  const code = get(resData, httpPath.code);
21
21
  const message = get(resData, httpPath.message) || "Error";
22
- const data = get(resData, httpPath.data);
23
- if (code !== httpConfig.code.success) {
22
+ const data = httpPath.data ? get(resData, httpPath.data) : resData;
23
+ if (code !== mergedHttpConfig.code.success) {
24
24
  ElMessage.error({
25
25
  message,
26
- duration: httpConfig.errorDuration
26
+ duration: mergedHttpConfig.errorDuration
27
27
  });
28
- if (code === httpConfig.code.forbidden) {
28
+ if (code === mergedHttpConfig.code.forbidden) {
29
29
  router.push(routerConfig.homePath);
30
- } else if (code === httpConfig.code.unauthorized) {
30
+ } else if (code === mergedHttpConfig.code.unauthorized) {
31
31
  if (persist.get(TOKEN_NAME)) {
32
32
  userStore.logout(persist.get(ROUTER_TO) || route.fullPath);
33
33
  }
@@ -36,23 +36,24 @@ function useRequest(config = {}) {
36
36
  }
37
37
  return data;
38
38
  };
39
- const createAxios = () => {
39
+ const createAxios = (createHttpConfig) => {
40
+ const mergedHttpConfig = merge({}, httpConfig, useHttpConfig, createHttpConfig);
40
41
  const axiosIns = axios.create(
41
- defaults(config, pick(httpConfig, ["baseURL", "timeout", "headers"]))
42
+ defaults(config, pick(mergedHttpConfig, ["baseURL", "timeout", "headers"]))
42
43
  );
43
44
  axiosIns.interceptors.request.use(
44
45
  (config2) => {
45
- Object.assign(config2, httpConfig.headers);
46
+ Object.assign(config2, mergedHttpConfig.headers);
46
47
  const token = persist.get(TOKEN_NAME);
47
48
  if (token) {
48
- config2.headers[httpConfig.authHeaderKey] = httpConfig.authScheme ? `${httpConfig.authScheme} ${token}` : token;
49
+ config2.headers[mergedHttpConfig.authHeaderKey] = mergedHttpConfig.authScheme ? `${mergedHttpConfig.authScheme} ${token}` : token;
49
50
  }
50
51
  return config2;
51
52
  },
52
53
  (error) => {
53
54
  ElMessage.error({
54
55
  message: error,
55
- duration: httpConfig.errorDuration
56
+ duration: mergedHttpConfig.errorDuration
56
57
  });
57
58
  return Promise.reject(error);
58
59
  }
@@ -60,15 +61,15 @@ function useRequest(config = {}) {
60
61
  axiosIns.interceptors.response.use(
61
62
  (response) => {
62
63
  const { data: resData } = response;
63
- return handleError(resData);
64
+ return handleError(resData, mergedHttpConfig);
64
65
  },
65
66
  (error) => {
66
- if (error instanceof AxiosError && error.response && isObject(error.response.data) && get(error.response.data, httpConfig.path.code)) {
67
- return handleError(error.response.data);
67
+ if (error instanceof AxiosError && error.response && isObject(error.response.data) && get(error.response.data, mergedHttpConfig.path.code)) {
68
+ return handleError(error.response.data, mergedHttpConfig);
68
69
  } else {
69
70
  ElMessage.error({
70
71
  message: error.message,
71
- duration: httpConfig.errorDuration
72
+ duration: mergedHttpConfig.errorDuration
72
73
  });
73
74
  return Promise.reject(error);
74
75
  }