From 765f1986c7c767e59594832865c07daee731e94d Mon Sep 17 00:00:00 2001 From: dtran Date: Wed, 21 Feb 2024 12:40:02 -0500 Subject: [PATCH] Fixed stock card data fetching --- package.json | 2 +- src/app/api/alphavantage/[fn]/[ticker]/route.ts | 15 ++++++++++----- src/hooks/useGetStockCompany.ts | 2 +- src/hooks/useGetStockQuote.ts | 2 +- src/utils/Alphavantage.ts | 6 +++--- yarn.lock | 8 ++++---- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index e311b26..3563f5f 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "sharp": "^0.30.5", "storybook": "^7.3.1", "typewriter-effect": "^2.18.2", - "utils-toolkit": "^0.2.2", + "utils-toolkit": "^0.2.3", "winston": "^3.10.0" }, "devDependencies": { diff --git a/src/app/api/alphavantage/[fn]/[ticker]/route.ts b/src/app/api/alphavantage/[fn]/[ticker]/route.ts index 7f9a286..a0cc938 100644 --- a/src/app/api/alphavantage/[fn]/[ticker]/route.ts +++ b/src/app/api/alphavantage/[fn]/[ticker]/route.ts @@ -74,6 +74,7 @@ const processCompanyData = (data: CompanyQR) => { } = data; return NextResponse.json({ + success: true, data: { name, exchange, @@ -99,6 +100,7 @@ const processQuoteData = (data: QuoteQR) => { } = data; return NextResponse.json({ + success: true, data: { price: parseFloat(price), change: parseFloat(change), @@ -110,10 +112,10 @@ const processQuoteData = (data: QuoteQR) => { const processResponse = (fn: AlphavantageFn, data: QuoteQR | CompanyQR | undefined) => { if (!data) { - return NextResponse.json({ error: "No data returned" }, { status: 500 }); + return NextResponse.json({ success: false, error: "No data returned" }); } if ("Information" in data) { - return NextResponse.json({ error: "Alpha Vantage daily request limit reached." }, { status: 500 }); + return NextResponse.json({ success: false, error: "Alpha Vantage daily request limit reached." }); } switch (fn) { @@ -124,7 +126,7 @@ const processResponse = (fn: AlphavantageFn, data: QuoteQR | CompanyQR | undefin // * This should never happen default: - return NextResponse.json({ error: "Invalid function" }, { status: 400 }); + return NextResponse.json({ success: false, error: "Invalid function" }, { status: 400 }); } }; @@ -133,12 +135,15 @@ export async function GET(_req: Request, { params }: { params: { fn: string; tic const alphavantageFn = getFunctionFromFn(fn); if (!alphavantageFn.some) { - return NextResponse.json({ error: "Invalid function" }, { status: 400 }); + return NextResponse.json({ success: false, error: "Invalid function" }, { status: 400 }); } const alphavantageUrl = getBaseAlphavantageUrl(ticker as string, alphavantageFn.coalesce()); if (!alphavantageUrl.some) { - return NextResponse.json({ error: "Env variables not set, this is a problem with the server" }, { status: 500 }); + return NextResponse.json( + { success: false, error: "Env variables not set, this is a problem with the server" }, + { status: 500 } + ); } try { diff --git a/src/hooks/useGetStockCompany.ts b/src/hooks/useGetStockCompany.ts index 58173c0..4345405 100644 --- a/src/hooks/useGetStockCompany.ts +++ b/src/hooks/useGetStockCompany.ts @@ -17,7 +17,7 @@ const useGetStockCompany = (symbol: string) => { staleTime: 1000 * 60 * 60 * 24 * 7, // 1 week }); - return { data, isFetching, error }; + return { data: data?.coalesce(), isFetching, error }; }; export default useGetStockCompany; diff --git a/src/hooks/useGetStockQuote.ts b/src/hooks/useGetStockQuote.ts index 375f4dc..b8e36ea 100644 --- a/src/hooks/useGetStockQuote.ts +++ b/src/hooks/useGetStockQuote.ts @@ -17,7 +17,7 @@ const useGetStockQuote = (symbol: string) => { staleTime: 1000 * 60 * 10, // 10 minutes }); - return { data, isFetching, error }; + return { data: data?.coalesce(), isFetching, error }; }; export default useGetStockQuote; diff --git a/src/utils/Alphavantage.ts b/src/utils/Alphavantage.ts index 8374a4c..bc67f81 100644 --- a/src/utils/Alphavantage.ts +++ b/src/utils/Alphavantage.ts @@ -32,14 +32,14 @@ export const queryAlphavantage = async (ticker: string, fn: Alphava const alphavantageUrl = getAPIAlphavantageUrl(ticker, fn); if (!alphavantageUrl.some) { - return Result(new Error("Invalid Alphavantage function provided")); + return Result, Error>(new Error("Invalid Alphavantage function provided")); } try { const { data, status, statusText } = await axios.get>(alphavantageUrl.coalesce()); if (status !== 200) { - return Result(new Error(`[${status}] ${statusText}`)); + return Result, Error>(new Error(`[${status}] ${statusText}`)); } return consumeApiResponse(data); @@ -48,6 +48,6 @@ export const queryAlphavantage = async (ticker: string, fn: Alphava console.error("Something went wrong with axios: ", error.toJSON()); } - return Result(error as Error); + return Result, Error>(error as Error); } }; diff --git a/yarn.lock b/yarn.lock index 4021001..2babe4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12521,10 +12521,10 @@ utils-merge@1.0.1: resolved "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz" integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== -utils-toolkit@^0.2.2: - version "0.2.2" - resolved "https://registry.yarnpkg.com/utils-toolkit/-/utils-toolkit-0.2.2.tgz#6989c8499946e41e44e92b6f5aaa67ab3176a2d4" - integrity sha512-0YjbpWFVLxJjeay8HDqGnfcZ/rqB7AG6+ytmJQAHmfYVOfxzsJHg0jA+hmnAXp+gJO8DW643qAOw9CsitQ9tzg== +utils-toolkit@^0.2.3: + version "0.2.3" + resolved "https://registry.yarnpkg.com/utils-toolkit/-/utils-toolkit-0.2.3.tgz#84820d36ff57d93f4843d86040fc94853511a0c3" + integrity sha512-E2808YEQUzmrPFL4Y2x+foptTQDV1/HPcQiLc4vPSxBl5+2qpHtnnfT9CgFSawNEfdg6nhrS8zHaycwBMdlCWQ== dependencies: clsx "^2.0.0" tailwind-merge "^1.14.0"