diff --git a/scripts/apis/creg/creg.js b/scripts/apis/creg/creg.js index 1fd1fedb..c2ba8314 100644 --- a/scripts/apis/creg/creg.js +++ b/scripts/apis/creg/creg.js @@ -114,7 +114,7 @@ export async function getSchools(lat, long) { } /** - * Gets the market trends for . + * Gets the market trends for a listing. * * @param {string} listingId - The ID of the listing. * @param {string} lat latitude @@ -134,3 +134,19 @@ export async function getMarketTrends(listingId, lat, long, zipcode) { }); }); } + +/** + * Gets the price history for a listing. + * + * @param {string} listingId - The ID of the listing. + * @return {Promise} resolving the economic details + */ +export async function getPriceHistory(listingId) { + return new Promise((resolve) => { + const worker = new Worker(`${window.hlx.codeBasePath}/scripts/apis/creg/workers/pricehistory.js`, { type: 'module' }); + worker.onmessage = (e) => resolve(e.data); + worker.postMessage({ + listingId, + }); + }); +} diff --git a/scripts/apis/creg/workers/pricehistory.js b/scripts/apis/creg/workers/pricehistory.js new file mode 100644 index 00000000..43225514 --- /dev/null +++ b/scripts/apis/creg/workers/pricehistory.js @@ -0,0 +1,19 @@ +/** + * Handle the Worker event. Fetches price history for a provided listing id. + * + * @param {Object} event the worker event. + * @param {string} event.data.api the URL to fetch. + * @param {string} event.data.id listing id. + */ +onmessage = async (event) => { + const { listingId } = event.data; + + try { + const response = await fetch(`/v1/pricehistory/${listingId}`); + const data = response.ok ? await response.json() : undefined; + + postMessage(data); + } catch (error) { + postMessage({}); + } +};