@vyr/service-gateway 0.0.34

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 ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@vyr/service-gateway",
3
+ "version": "0.0.34",
4
+ "description": "",
5
+ "main": "./src/index.ts",
6
+ "author": "",
7
+ "license": "MIT",
8
+ "dependencies": {
9
+ "axios": "^1.7.4"
10
+ },
11
+ "files": [
12
+ "package.json",
13
+ "src/"
14
+ ]
15
+ }
@@ -0,0 +1,78 @@
1
+ import { Serialization, Listener, DeserializationObject } from '@vyr/engine'
2
+ import { Service } from '@vyr/service'
3
+ import { path, topic, Confirm, api } from '@vyr/gateway'
4
+ import { bindExecutor, confirm, notify } from './executor'
5
+ import { setAuthorization } from './request'
6
+
7
+ interface RpcServiceConfig {
8
+ token: string
9
+ }
10
+
11
+ class GatewayService extends Service {
12
+ static notify = notify
13
+ static confirm = confirm
14
+ static client = ''
15
+
16
+ private _listener = new Listener<{ [k: string]: (data: DeserializationObject) => void }>()
17
+ private socket: any
18
+ private needTrigger = false
19
+ private queue: DeserializationObject[] = []
20
+
21
+ get url(): string {
22
+ return this.socket.uri
23
+ }
24
+
25
+ constructor(name: string, config: RpcServiceConfig) {
26
+ super(name)
27
+ const params = { path, extraHeaders: {}, autoConnect: false }
28
+ setAuthorization(params.extraHeaders, config.token)
29
+ //@ts-ignore
30
+ this.socket = new window.io(location.href, params)
31
+ }
32
+
33
+ async ready() {
34
+ bindExecutor(this)
35
+ this.socket.on(topic, (content: string) => {
36
+ const item = Serialization.parse(content) as DeserializationObject
37
+ this._delayTrigger(item)
38
+ })
39
+ this.socket.on('disconnect', () => {
40
+ const data = { client: GatewayService.client }
41
+ this.trigger(api.system.disconnect.path, data)
42
+ })
43
+ }
44
+
45
+ async start() {
46
+ await this.socket.connect()
47
+ }
48
+
49
+ trigger<T extends DeserializationObject = DeserializationObject>(api: string, data: T, other: DeserializationObject = {}) {
50
+ this._listener.trigger(api, data, other)
51
+ }
52
+ listen<T extends DeserializationObject = DeserializationObject>(api: string, cb: (data: T, ...others: any[]) => void) {
53
+ this._listener.listen(api, cb)
54
+ }
55
+ unlisten<T extends DeserializationObject = DeserializationObject>(api: string, cb: (data: T, ...others: any[]) => void) {
56
+ this._listener.unlisten(api, cb)
57
+ }
58
+
59
+ private _delayTrigger(item: DeserializationObject) {
60
+ this.queue.push(item)
61
+ if (this.needTrigger === true) return
62
+ this.needTrigger = true
63
+ setTimeout(() => this._executeQueue(), 160)
64
+ }
65
+ private _executeQueue() {
66
+ this.needTrigger = false
67
+ for (const { api, data, ...other } of this.queue) {
68
+ try {
69
+ this.trigger(api, data, other)
70
+ } catch (error) {
71
+ console.error(error)
72
+ }
73
+ }
74
+ this.queue.length = 0
75
+ }
76
+ }
77
+
78
+ export { GatewayService }
@@ -0,0 +1,37 @@
1
+ import z from 'zod'
2
+ import { api, Confirm } from '@vyr/gateway'
3
+ import { request } from '../request'
4
+ import { GatewayService } from '../GatewayService'
5
+
6
+ const notify = (data: string) => { }
7
+
8
+ const confirm = async (options: Partial<Confirm>, value?: string) => {
9
+ return options as Confirm
10
+ }
11
+
12
+ const connection = async (data: z.infer<typeof api.system.connection.noticeSchema>) => {
13
+ GatewayService.client = data.client
14
+ }
15
+
16
+ const onConfirm = async (data: z.infer<typeof api.system.confirm.noticeSchema>) => {
17
+ const result = await GatewayService.confirm(data.data)
18
+ data.data.value = result.value
19
+ data.data.success = result.success
20
+ await request({
21
+ url: api.system.confirm.path,
22
+ method: api.system.confirm.method,
23
+ data: data
24
+ })
25
+ }
26
+
27
+ const bindExecutor = (service: GatewayService) => {
28
+ service.listen(api.system.connection.path, connection)
29
+ service.listen(api.system.confirm.path, onConfirm)
30
+ }
31
+
32
+ export {
33
+ bindExecutor,
34
+ onConfirm,
35
+ notify,
36
+ confirm,
37
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from '@vyr/gateway'
2
+ export * from './locale'
3
+ export * from './request'
4
+ export * from './GatewayService'
@@ -0,0 +1,10 @@
1
+ import { Locale } from "@vyr/locale";
2
+ import { zhCnLanguageProvider, ZhCNLanguageProvider } from "./LanguageProvider";
3
+
4
+ Locale.register(zhCnLanguageProvider)
5
+
6
+ const language = Locale.getLanguage<ZhCNLanguageProvider>(zhCnLanguageProvider.name)
7
+
8
+ export {
9
+ language
10
+ }
@@ -0,0 +1,19 @@
1
+ import { LanguageProvider } from '@vyr/locale'
2
+
3
+ interface ZhCNLanguageProvider extends LanguageProvider {
4
+ 'request.error.general': string
5
+ 'request.error.401': string
6
+ }
7
+
8
+ const zhCnLanguageProvider: ZhCNLanguageProvider = {
9
+ id: 'zh_CN',
10
+ name: '@vyr/service-gateway',
11
+
12
+ 'request.error.general': '服务错误,请稍后重试!',
13
+ 'request.error.401': '无效的会话,或者会话已过期,请重新登录。'
14
+ }
15
+
16
+ export {
17
+ ZhCNLanguageProvider,
18
+ zhCnLanguageProvider,
19
+ }
@@ -0,0 +1,2 @@
1
+ export * from './LanguageProvider'
2
+ export * from './Language'
@@ -0,0 +1,57 @@
1
+ import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios'
2
+ import { ObjectUtils } from '@vyr/engine';
3
+ import { UserService } from '@vyr/service-user'
4
+ import { language } from '../locale';
5
+ import { GatewayService } from '../GatewayService';
6
+
7
+ const request = axios.create({
8
+ baseURL: '',
9
+ })
10
+
11
+ const setClient = (headers: any) => {
12
+ headers['Client'] = GatewayService.client
13
+ }
14
+
15
+ const setAuthorization = (headers: any, token: string) => {
16
+ headers['Authorization'] = `Bearer ${token}`
17
+ }
18
+
19
+ request.interceptors.request.use(
20
+ (config: InternalAxiosRequestConfig) => {
21
+ const token = UserService.getToken()
22
+ if (token) setAuthorization(config.headers, token)
23
+ if (config.data instanceof FormData) delete config.headers['Content-Type']
24
+ setClient(config.headers)
25
+
26
+ return config
27
+ },
28
+ (error: any) => {
29
+ return Promise.reject(error);
30
+ }
31
+ );
32
+
33
+ request.interceptors.response.use(
34
+ (response: AxiosResponse) => {
35
+ return response
36
+ },
37
+ (error: any) => {
38
+ let message = language.get('request.error.general')
39
+ const { response, status } = error;
40
+ console.log(error)
41
+
42
+ if (status === 401) {
43
+ message = language.get('request.error.401')
44
+ setTimeout(UserService.logout, 1000);
45
+ } else if (ObjectUtils.isObject(response?.data)) {
46
+ if (response.data.message) message = response.data.message
47
+ }
48
+ GatewayService.notify(message)
49
+ return Promise.reject(error);
50
+ }
51
+ )
52
+
53
+ export {
54
+ request,
55
+ setClient,
56
+ setAuthorization,
57
+ }