@jetlinks-web/core 1.0.5 → 1.0.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetlinks-web/core",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "module": "index.ts",
@@ -18,8 +18,8 @@
18
18
  "rxjs": "^7.8.1",
19
19
  "@jetlinks-web/constants": "^1.0.1",
20
20
  "@jetlinks-web/types": "^1.0.1",
21
- "@jetlinks-web/router": "^1.0.5",
22
- "@jetlinks-web/utils": "^1.0.4"
21
+ "@jetlinks-web/router": "^1.0.7",
22
+ "@jetlinks-web/utils": "^1.0.5"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/lodash-es": "^4.17.7",
@@ -1,6 +1,5 @@
1
1
  import { TOKEN_KEY, BASE_API } from '@jetlinks-web/constants'
2
2
  import { getToken } from '@jetlinks-web/utils'
3
- import { jumpLogin } from '@jetlinks-web/router'
4
3
  import axios from 'axios'
5
4
  import type {
6
5
  AxiosRequestConfig,
@@ -10,8 +9,9 @@ import type {
10
9
  InternalAxiosRequestConfig,
11
10
  } from 'axios'
12
11
  import { notification as Notification } from 'jetlinks-ui-components'
13
- import { isFunction } from 'lodash-es'
12
+ import { isFunction, merge } from 'lodash-es'
14
13
  import type { AxiosResponseRewrite } from '@jetlinks-web/types'
14
+ import { context } from './context'
15
15
 
16
16
  export interface ContextOptions {
17
17
  filterUrl?: string[]
@@ -20,6 +20,8 @@ export interface ContextOptions {
20
20
  ) => InternalAxiosRequestConfig
21
21
  handleResponse?: (response: AxiosResponse) => AxiosResponse
22
22
  errorHandler?: (error: AxiosError) => void
23
+
24
+ loginInvalid?: () => void
23
25
  }
24
26
 
25
27
  export interface CreateAxiosOptions extends AxiosRequestConfig, ContextOptions {}
@@ -31,7 +33,7 @@ export class Axios {
31
33
  private readonly options: CreateAxiosOptions
32
34
 
33
35
  constructor(options: CreateAxiosOptions) {
34
- this.options = options
36
+ this.options = merge(context, options)
35
37
  this.axiosInstance = axios.create({
36
38
  withCredentials: false,
37
39
  timeout: 1000 * 15,
@@ -47,13 +49,17 @@ export class Axios {
47
49
  )
48
50
  }
49
51
 
52
+ jumpLogin() {
53
+ this.options.loginInvalid?.()
54
+ }
55
+
50
56
  request(config: InternalAxiosRequestConfig) {
51
57
  const token = getToken()
52
58
  const filterUrl = this.options.filterUrl // 不需要token校验接口
53
59
  // 没有token,并且该接口需要token校验
54
60
  if (!token && !filterUrl?.some((url) => config.url?.includes(url))) {
55
61
  // 跳转登录页
56
- jumpLogin()
62
+ this.jumpLogin()
57
63
  return config
58
64
  }
59
65
 
@@ -107,7 +113,7 @@ export class Axios {
107
113
  break;
108
114
  case 401:
109
115
  this.showNotification('用户未登录', status)
110
- jumpLogin()
116
+ this.jumpLogin()
111
117
  break;
112
118
  default:
113
119
  break;
@@ -1,11 +1,9 @@
1
1
  import { Axios } from "./axios";
2
2
  import type { CreateAxiosOptions } from "./axios";
3
- import { merge } from 'lodash-es'
4
- import { context } from './context'
5
3
  export { initRequest } from './context'
6
4
 
7
5
  export const createAxios = (options?: Partial<CreateAxiosOptions>) => {
8
- return new Axios(merge(context, options || {} ))
6
+ return new Axios(options)
9
7
  }
10
8
 
11
9
  export const request = createAxios()