Fix error on accounts fetcher worker.

This commit is contained in:
Spencer Ofwiti 2021-07-13 18:58:36 +03:00
parent 1411116d24
commit b6e81bcbe1

View File

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