diff --git a/src/routes/market-news/+layout.server.ts b/src/routes/market-news/+layout.server.ts new file mode 100644 index 00000000..267ae8ff --- /dev/null +++ b/src/routes/market-news/+layout.server.ts @@ -0,0 +1,66 @@ +export const load = async ({ locals }) => { + const { apiURL, apiKey } = locals; + + const getStockNews = async () => { + const postData = { newsType: "stock-news" }; + // make the POST request to the endpoint + const response = await fetch(apiURL + "/market-news", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = await response.json(); + + return output; + }; + + + const getMarketNews = async () => { + const postData = { newsType: "general-news" }; + + // make the POST request to the endpoint + const response = await fetch(apiURL + "/market-news", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = await response.json(); + + return output; + }; + + const getPressNews = async () => { + const postData = { newsType: "press-news" }; + + // make the POST request to the endpoint + const response = await fetch(apiURL + "/market-news", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = await response.json(); + + return output; + }; + + + + + return { + getStockNews: await getStockNews(), + getMarketNews: await getMarketNews(), + getPressNews: await getPressNews(), + }; +}; diff --git a/src/routes/market-news/+page.server.ts b/src/routes/market-news/+page.server.ts deleted file mode 100644 index caf95068..00000000 --- a/src/routes/market-news/+page.server.ts +++ /dev/null @@ -1,24 +0,0 @@ -export const load = async ({ locals }) => { - const getData = async () => { - const { apiURL, apiKey } = locals; - const postData = { newsType: "stock-news" }; - // make the POST request to the endpoint - const response = await fetch(apiURL + "/market-news", { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-API-KEY": apiKey, - }, - body: JSON.stringify(postData), - }); - - const output = await response.json(); - - return output; - }; - - // Make sure to return a promise - return { - getData: await getData(), - }; -}; diff --git a/src/routes/market-news/+page.svelte b/src/routes/market-news/+page.svelte index 6d0524df..5ab9e193 100644 --- a/src/routes/market-news/+page.svelte +++ b/src/routes/market-news/+page.svelte @@ -1,10 +1,12 @@