@addev-be/ui 0.1.26 → 0.2.1
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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Config } from '../config';
|
|
2
|
+
import { Request } from './base';
|
|
3
|
+
import { trimEnd } from 'lodash';
|
|
4
|
+
import { v4 } from 'uuid';
|
|
5
|
+
|
|
6
|
+
export class HttpService {
|
|
7
|
+
private static instance: HttpService;
|
|
8
|
+
private config: Config;
|
|
9
|
+
private promises: {
|
|
10
|
+
[id: string]: {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
resolve: (value: any) => void;
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
reject: (reason?: any) => void;
|
|
15
|
+
};
|
|
16
|
+
} = {};
|
|
17
|
+
|
|
18
|
+
constructor(config: Config) {
|
|
19
|
+
HttpService.instance = this;
|
|
20
|
+
this.config = config;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public static getInstance() {
|
|
24
|
+
return HttpService.instance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public sendRequest<TReq, TRes>(name: string, data: TReq) {
|
|
28
|
+
const id = v4();
|
|
29
|
+
const promise = new Promise<TRes>((resolve, reject) => {
|
|
30
|
+
const request: Request = { id, name, data };
|
|
31
|
+
this.promises[id] = { resolve, reject };
|
|
32
|
+
console.log('[HTTP] Sending request:', request);
|
|
33
|
+
fetch(`${trimEnd(this.config.httpServerUrl, '/')}/${name}`, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: {
|
|
36
|
+
'Content-Type': 'application/json',
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify(request),
|
|
39
|
+
})
|
|
40
|
+
.then(async (response) => {
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
throw new Error('HTTP request failed');
|
|
43
|
+
}
|
|
44
|
+
const body = await response.text();
|
|
45
|
+
this.parseMessage(body);
|
|
46
|
+
})
|
|
47
|
+
.catch((error) => {
|
|
48
|
+
reject(error);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
return promise;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private parseMessage(message: string) {
|
|
55
|
+
try {
|
|
56
|
+
const response = JSON.parse(message);
|
|
57
|
+
if (!response.id) {
|
|
58
|
+
throw new Error('Received message is not valid');
|
|
59
|
+
}
|
|
60
|
+
if (this.promises[response.id]) {
|
|
61
|
+
console.log('[HTTP] Received response:', response);
|
|
62
|
+
if (
|
|
63
|
+
typeof response.data?.status === 'number' &&
|
|
64
|
+
response.data.status < 0
|
|
65
|
+
) {
|
|
66
|
+
this.promises[response.id].reject(new Error(response.data.message));
|
|
67
|
+
} else {
|
|
68
|
+
this.promises[response.id].resolve(response.data);
|
|
69
|
+
}
|
|
70
|
+
delete this.promises[response.id];
|
|
71
|
+
} else {
|
|
72
|
+
console.log('[HTTP] Received request:', response);
|
|
73
|
+
// TODO : Implement request handling
|
|
74
|
+
throw new Error('Not yet implemented');
|
|
75
|
+
}
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('[HTTP] Error parsing message:', error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useWebSocketRequestHandler } from './hooks';
|
|
2
2
|
|
|
3
3
|
export type FieldDTO<T extends string = string> = {
|
|
4
4
|
fieldName?: T;
|
|
@@ -93,9 +93,9 @@ export type AdvancedRequestFieldDTO =
|
|
|
93
93
|
|
|
94
94
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
95
|
export const useAdvancedRequestHandler = <T = any>() =>
|
|
96
|
-
|
|
96
|
+
useWebSocketRequestHandler<AdvancedRequestDTO, AdvancedResponseDTO<T>>(
|
|
97
97
|
'AdvancedRequest'
|
|
98
98
|
);
|
|
99
99
|
|
|
100
100
|
export const useRawAdvancedRequestHandler = () =>
|
|
101
|
-
|
|
101
|
+
useWebSocketRequestHandler<AdvancedRequestDTO, string>('RawAdvancedRequest');
|
package/src/services/hooks.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
|
+
import { HttpService } from './HttpService';
|
|
1
2
|
import { WebSocketService } from './WebSocketService';
|
|
2
3
|
import { useCallback } from 'react';
|
|
3
4
|
|
|
4
|
-
export const
|
|
5
|
+
export const useWebSocketRequestHandler = <TReq, TRes>(
|
|
5
6
|
name: string
|
|
6
|
-
): ((data: TReq) => Promise<TRes>) =>
|
|
7
|
-
|
|
7
|
+
): ((data: TReq) => Promise<TRes>) =>
|
|
8
|
+
useCallback(
|
|
8
9
|
(data: TReq) => {
|
|
9
10
|
return WebSocketService.getInstance().sendRequest<TReq, TRes>(name, data);
|
|
10
11
|
},
|
|
11
12
|
[name]
|
|
12
13
|
);
|
|
13
|
-
|
|
14
|
+
|
|
15
|
+
export const useHttpRequestHandler = <TReq, TRes>(
|
|
16
|
+
name: string
|
|
17
|
+
): ((data: TReq) => Promise<TRes>) =>
|
|
18
|
+
useCallback(
|
|
19
|
+
(data: TReq) => {
|
|
20
|
+
return HttpService.getInstance().sendRequest<TReq, TRes>(name, data);
|
|
21
|
+
},
|
|
22
|
+
[name]
|
|
23
|
+
);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useWebSocketRequestHandler } from './hooks';
|
|
2
2
|
|
|
3
3
|
export type FieldDTO<T extends string = string> = {
|
|
4
4
|
fieldName?: T;
|
|
@@ -91,6 +91,8 @@ type SqlRequestHandler<T> = (
|
|
|
91
91
|
export const useSqlRequestHandler = <T = any>(
|
|
92
92
|
name: string
|
|
93
93
|
): [SqlRequestHandler<T>, SqlRequestHandler<{ Id: string }>] => [
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
useWebSocketRequestHandler<SqlRequestDTO, SqlResponseDTO<T>>(name),
|
|
95
|
+
useWebSocketRequestHandler<SqlRequestDTO, SqlResponseDTO<{ Id: string }>>(
|
|
96
|
+
name
|
|
97
|
+
),
|
|
96
98
|
];
|