refactor fetch fn in ui, fix error detection

feat/vaults
Tomáš Mládek 2021-12-27 13:13:11 +01:00
parent a43a9d6caf
commit 9d06e25d11
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
1 changed files with 11 additions and 8 deletions

View File

@ -6,17 +6,20 @@ export function useSWR<D = unknown, E = Error>(
key: SWRKey | undefined | (() => SWRKey | undefined)
) {
const data = writable<D | undefined>();
const error = writable<D | undefined>();
const error = writable<E | undefined>();
function doFetch() {
async function doFetch() {
let keyString = typeof key === "string" ? key : key();
fetch(keyString)
.then(async (response) => {
try {
const response = await fetch(keyString);
if (response.ok) {
data.set(await response.json());
})
.catch((err) => {
error.set(err);
});
} else {
throw new Error(await response.json());
}
} catch (err) {
error.set(err);
}
}
doFetch();