forked from hyperledger/firefly-ui
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfetches.ts
128 lines (119 loc) · 3.63 KB
/
fetches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Dispatch, SetStateAction } from 'react';
import {
FF_Paths,
ITokenApproval,
ITokenApprovalWithPoolName,
ITokenBalance,
ITokenBalanceWithPool,
ITokenPool,
ITokenTransfer,
ITokenTransferWithPool,
} from '../interfaces';
import { parse, isSafeNumber, LosslessNumber } from 'lossless-json';
export const fetchWithCredentials = (
resource: string,
options?: RequestInit
): Promise<Response> => {
return fetch(
`${window.location.protocol}//${window.location.hostname}:${window.location.port}${resource}`,
{ ...options, credentials: 'include' }
);
};
export function parseLargeNumbersAsStrings(value: any) {
return isSafeNumber(value, { approx: false })
? parseFloat(value) // Smaller numbers are kept as Javascript numbers
: new LosslessNumber(value).toString(); // Large numbers are safely stringified
}
export const fetchCatcher = async (resource: string): Promise<any> => {
const response = await fetchWithCredentials(resource);
if (!response.ok) {
} else {
return parse(await response.text(), null, parseLargeNumbersAsStrings);
}
};
export const summarizeFetchError = async (
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
errOrResponse: any
): Promise<string> => {
console.log('Fetch error', errOrResponse);
let message = 'Fetch failed';
if (errOrResponse.status) {
message += ` [${errOrResponse.status}]`;
}
if (errOrResponse.message) {
message += `: ${errOrResponse.message}`;
}
if (typeof errOrResponse.json === 'function') {
let jsonData: any;
try {
jsonData = await errOrResponse.json();
} catch (err1) {
console.log('Failed to parse response as JSON: ' + err1);
}
if (jsonData?.error) {
message += `: ${jsonData.error}`;
} else {
try {
message += `: ${await errOrResponse.text()}`;
} catch (err2) {
console.log('Failed to get response as text: ' + err2);
}
}
}
return message;
};
export const fetchPool = async (
namespace: string,
id: string,
poolCache: Map<string, ITokenPool>,
setPoolCache: Dispatch<SetStateAction<Map<string, ITokenPool>>>
): Promise<ITokenPool | undefined> => {
if (poolCache.has(id)) {
return poolCache.get(id);
}
const response = await fetchWithCredentials(
`${FF_Paths.nsPrefix}/${namespace}${FF_Paths.tokenPools}/${id}`
);
if (!response.ok) {
return undefined;
}
const pool: ITokenPool = await response.json();
setPoolCache((poolCache) => new Map(poolCache.set(pool.id, pool)));
return pool;
};
export const fetchPoolObjectFromTransfer = async (
transfer: ITokenTransfer,
ns: string,
poolCache: Map<string, ITokenPool>,
setPoolCache: Dispatch<SetStateAction<Map<string, ITokenPool>>>
): Promise<ITokenTransferWithPool> => {
const pool = await fetchPool(ns, transfer.pool, poolCache, setPoolCache);
return {
...transfer,
poolObject: pool,
};
};
export const fetchPoolObjectFromBalance = async (
balance: ITokenBalance,
ns: string,
poolCache: Map<string, ITokenPool>,
setPoolCache: Dispatch<SetStateAction<Map<string, ITokenPool>>>
): Promise<ITokenBalanceWithPool> => {
const pool = await fetchPool(ns, balance.pool, poolCache, setPoolCache);
return {
...balance,
poolObject: pool,
};
};
export const fetchPoolNameFromApproval = async (
approval: ITokenApproval,
ns: string,
poolCache: Map<string, ITokenPool>,
setPoolCache: Dispatch<SetStateAction<Map<string, ITokenPool>>>
): Promise<ITokenApprovalWithPoolName> => {
const pool = await fetchPool(ns, approval.pool, poolCache, setPoolCache);
return {
...approval,
poolName: pool ? pool.name : approval.pool,
};
};