@addev-be/ui 0.4.7 → 0.5.0
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 +1 -1
- package/src/services/HttpService.ts +17 -5
- package/src/services/hooks.ts +7 -3
package/package.json
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Config } from '../config';
|
|
2
|
-
import { Request } from './base';
|
|
3
2
|
import { trimEnd } from 'lodash';
|
|
4
3
|
import { v4 } from 'uuid';
|
|
5
4
|
|
|
@@ -24,18 +23,31 @@ export class HttpService {
|
|
|
24
23
|
return HttpService.instance;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
public sendRequest<TReq, TRes>(
|
|
26
|
+
public sendRequest<TReq, TRes>(
|
|
27
|
+
name: string,
|
|
28
|
+
data: TReq,
|
|
29
|
+
files?: Record<string, File>
|
|
30
|
+
) {
|
|
28
31
|
const id = v4();
|
|
29
32
|
const promise = new Promise<TRes>((resolve, reject) => {
|
|
30
|
-
const request: Request = { id, name, data };
|
|
31
33
|
this.promises[id] = { resolve, reject };
|
|
32
|
-
|
|
34
|
+
|
|
35
|
+
const formData = new FormData();
|
|
36
|
+
formData.append('id', id);
|
|
37
|
+
formData.append('data', JSON.stringify(data));
|
|
38
|
+
if (files) {
|
|
39
|
+
Object.entries(files).forEach(([key, file]) => {
|
|
40
|
+
formData.append(key, file);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log('[HTTP] Sending request:', { id, name, data });
|
|
33
45
|
fetch(`${trimEnd(this.config.httpServerUrl, '/')}/${name}`, {
|
|
34
46
|
method: 'POST',
|
|
35
47
|
headers: {
|
|
36
48
|
'Content-Type': 'application/json',
|
|
37
49
|
},
|
|
38
|
-
body:
|
|
50
|
+
body: formData,
|
|
39
51
|
})
|
|
40
52
|
.then(async (response) => {
|
|
41
53
|
if (!response.ok) {
|
package/src/services/hooks.ts
CHANGED
|
@@ -31,10 +31,14 @@ export const useWebSocketRequestHandler = <TReq, TRes>(
|
|
|
31
31
|
|
|
32
32
|
export const useHttpRequestHandler = <TReq, TRes>(
|
|
33
33
|
name: string
|
|
34
|
-
): ((data: TReq) => Promise<TRes>) =>
|
|
34
|
+
): ((data: TReq, files?: Record<string, File>) => Promise<TRes>) =>
|
|
35
35
|
useCallback(
|
|
36
|
-
(data: TReq) => {
|
|
37
|
-
return HttpService.getInstance().sendRequest<TReq, TRes>(
|
|
36
|
+
(data: TReq, files?: Record<string, File>) => {
|
|
37
|
+
return HttpService.getInstance().sendRequest<TReq, TRes>(
|
|
38
|
+
name,
|
|
39
|
+
data,
|
|
40
|
+
files
|
|
41
|
+
);
|
|
38
42
|
},
|
|
39
43
|
[name]
|
|
40
44
|
);
|