diff --git a/src/app/_workers/fetch-accounts.worker.ts b/src/app/_workers/fetch-accounts.worker.ts index ebce013..519b760 100644 --- a/src/app/_workers/fetch-accounts.worker.ts +++ b/src/app/_workers/fetch-accounts.worker.ts @@ -3,7 +3,6 @@ import { Envelope, Syncable, User } from 'cic-client-meta'; import { add0x } from '@src/assets/js/ethtx/dist/hex'; import { personValidation, vcardValidation } from '@app/_helpers/schema-validation'; -// const vCard = require('vcard-parser'); import * as vCard from 'vcard-parser'; const headers = { @@ -16,29 +15,36 @@ const options = { addEventListener('message', async ({ data }) => { if (data.addresses instanceof Array) { for (const accountAddress of data.addresses) { - const account = await getAccountByAddress(accountAddress, data.url); - postMessage(account); + try { + const account = await getAccountByAddress(accountAddress, data.url); + postMessage(account); + } catch (error) { + throw Error(error); + } } } }); async function getAccountByAddress(accountAddress: string, metaUrl: string): Promise { const userKey = await User.toKey(add0x(accountAddress)); - return new Promise((resolve, reject) => { - fetch(`${metaUrl}/${userKey}`, options).then(async (response) => { - if (!response.ok) { - return reject({ - status: response.status, - statusText: response.statusText, - }); + const response = await fetch(`${metaUrl}/${userKey}`, options) + .then((res) => { + if (res.ok) { + return res.json(); } else { - const account: Syncable = Envelope.fromJSON(JSON.stringify(response.text())).unwrap(); - const accountInfo = account.m.data; - await personValidation(accountInfo); - accountInfo.vcard = vCard.parse(atob(accountInfo.vcard)); - await vcardValidation(accountInfo.vcard); - return resolve(accountInfo); + return Promise.reject({ + status: res.status, + statusText: res.statusText, + }); } + }) + .catch((error) => { + throw Error(`${error.status}: ${error.statusText}`); }); - }); + const account: Syncable = Envelope.fromJSON(JSON.stringify(response)).unwrap(); + const accountInfo = account.m.data; + await personValidation(accountInfo); + accountInfo.vcard = vCard.parse(atob(accountInfo.vcard)); + await vcardValidation(accountInfo.vcard); + return accountInfo; }