@vyr/service-rpc 0.0.25 → 0.0.27

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,12 +1,11 @@
1
1
  {
2
2
  "name": "@vyr/service-rpc",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "description": "",
5
5
  "main": "./src/index.ts",
6
6
  "author": "",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "vue": "3.5.22",
10
9
  "axios": "^1.7.4"
11
10
  },
12
11
  "files": [
package/src/RpcService.ts CHANGED
@@ -1,12 +1,10 @@
1
- import axios from 'axios'
2
1
  import { Serialization, Listener } from '@vyr/engine'
3
2
  import { Service } from '@vyr/service'
4
3
  import { path, tokenKey, topic, Confirm, Message } from '@vyr/service-rpc-universal'
4
+ import { request } from './request'
5
5
 
6
6
  interface RpcServiceConfig {
7
7
  token: string
8
- notify: (msg: string) => void
9
- confirm: (options: Partial<Confirm>, value?: string) => Promise<Confirm>
10
8
  }
11
9
 
12
10
  const _logout = () => {
@@ -17,10 +15,14 @@ const _logout = () => {
17
15
  }
18
16
 
19
17
  class RpcService extends Service {
18
+ static notify = (msg: string) => { }
19
+ static confirm = async (options: Partial<Confirm>, value?: string) => {
20
+ return options as Confirm
21
+ }
20
22
 
21
23
  static async login(name: string, password: string) {
22
24
  const data = new Message.rpc.login.RequestMessage(name, password)
23
- const res = await axios.post<InstanceType<typeof Message['rpc']['login']['NoticeMessage']>>(Message.rpc.login.Method, data)
25
+ const res = await request.post<InstanceType<typeof Message['rpc']['login']['NoticeMessage']>>(Message.rpc.login.Method, data)
24
26
  window.localStorage.setItem(tokenKey, res.data.params.token)
25
27
 
26
28
  const location = new URL(window.location.href)
@@ -30,7 +32,7 @@ class RpcService extends Service {
30
32
 
31
33
  static async getUser() {
32
34
  const data = new Message.rpc.getUser.RequestMessage()
33
- const res = await axios.get<InstanceType<typeof Message['rpc']['getUser']['NoticeMessage']>>(Message.rpc.getUser.Method, data)
35
+ const res = await request.get<InstanceType<typeof Message['rpc']['getUser']['NoticeMessage']>>(Message.rpc.getUser.Method, data)
34
36
  return res.data.params.user
35
37
  }
36
38
 
@@ -42,21 +44,19 @@ class RpcService extends Service {
42
44
  }
43
45
 
44
46
  private _listener = new Listener<{ [k: string]: (msg: Message) => void }>()
45
- private confirm
46
47
  private socket: any
47
48
  private needTrigger = false
48
49
  private msgQueue: Message[] = []
49
50
 
50
51
  constructor(name: string, config: RpcServiceConfig) {
51
52
  super(name)
52
- this.confirm = config.confirm
53
53
  const params = { path, extraHeaders: { authorization: 'Bearer ' + config.token }, autoConnect: false }
54
54
  //@ts-ignore
55
55
  this.socket = new window.io(location.href, params)
56
56
 
57
57
  this.socket.on(topic, (content: string) => {
58
58
  const msg = Serialization.parse(content) as Message
59
- if (msg.message !== undefined) return config.notify(msg.message)
59
+ if (msg.message !== undefined) return RpcService.notify(msg.message)
60
60
  this._delayTrigger(msg)
61
61
  })
62
62
  this.socket.on('disconnect', () => {
@@ -67,7 +67,7 @@ class RpcService extends Service {
67
67
  }
68
68
 
69
69
  private _onConfirm = async (msg: InstanceType<typeof Message['rpc']['confirm']['NoticeMessage']>) => {
70
- const result = await this.confirm(msg.params.config)
70
+ const result = await RpcService.confirm(msg.params.config)
71
71
  msg.params.config.value = result.value
72
72
  msg.params.config.success = result.success
73
73
  this.send(msg)
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from '@vyr/service-rpc-universal'
2
2
  export * from './locale'
3
+ export * from './request'
3
4
  export * from './RpcService'
@@ -1,12 +1,16 @@
1
1
  import { LanguageProvider } from '@vyr/locale'
2
2
 
3
3
  interface ZhCNLanguageProvider extends LanguageProvider {
4
-
4
+ 'request.error.general': string
5
+ 'request.error.401': string
5
6
  }
6
7
 
7
8
  const zhCnLanguageProvider: ZhCNLanguageProvider = {
8
9
  id: 'zh_CN',
9
10
  name: '@vyr/service-rpc',
11
+
12
+ 'request.error.general': '服务错误,请稍后重试!',
13
+ 'request.error.401': '无效的会话,或者会话已过期,请重新登录。'
10
14
  }
11
15
 
12
16
  export {
@@ -0,0 +1,50 @@
1
+ import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios'
2
+ import { language } from '../locale';
3
+ import { RpcService } from '../RpcService';
4
+
5
+ const request = axios.create({
6
+ baseURL: '',
7
+ })
8
+
9
+ // 请求拦截器
10
+ request.interceptors.request.use(
11
+ (config: InternalAxiosRequestConfig) => {
12
+ const token = RpcService.getToken()
13
+
14
+ if (token) {
15
+ config.headers['Authorization'] = 'Bearer ' + token;
16
+ }
17
+
18
+ // FormData数据去请求头Content-Type
19
+ if (config.data instanceof FormData) {
20
+ delete config.headers['Content-Type'];
21
+ }
22
+ return config;
23
+ },
24
+ (error: any) => {
25
+ return Promise.reject(error);
26
+ }
27
+ );
28
+
29
+ // 响应拦截器
30
+ request.interceptors.response.use(
31
+ (response: AxiosResponse) => {
32
+ return response
33
+ },
34
+ (error: any) => {
35
+ let message = language.get('request.error.general')
36
+ const { response, status } = error;
37
+
38
+ if (status === 401) {
39
+ message = language.get('request.error.401')
40
+ } else if (response.data !== null && typeof response.data === 'object') {
41
+ if (response.data.message) message = response.data.message
42
+ }
43
+ RpcService.notify(message)
44
+ return Promise.reject(error);
45
+ }
46
+ )
47
+
48
+ export {
49
+ request
50
+ }