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 { 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) {
try {
const account = await getAccountByAddress(accountAddress, data.url);
postMessage(account);
} catch (error) {
throw Error(error);
}
}
}
});
async function getAccountByAddress(accountAddress: string, metaUrl: string): Promise<any> {
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();
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 resolve(accountInfo);
}
});
});
return accountInfo;
}