Skip to content

Commit

Permalink
Fixed stock card data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
dtran421 committed Feb 21, 2024
1 parent ab1390f commit 765f198
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 10 additions & 5 deletions src/app/api/alphavantage/[fn]/[ticker]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const processCompanyData = (data: CompanyQR) => {
} = data;

return NextResponse.json({
success: true,
data: {
name,
exchange,
Expand All @@ -99,6 +100,7 @@ const processQuoteData = (data: QuoteQR) => {
} = data;

return NextResponse.json({
success: true,
data: {
price: parseFloat(price),
change: parseFloat(change),
Expand All @@ -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) {
Expand All @@ -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 });
}
};

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useGetStockCompany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/hooks/useGetStockQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 3 additions & 3 deletions src/utils/Alphavantage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ export const queryAlphavantage = async <T = unknown>(ticker: string, fn: Alphava
const alphavantageUrl = getAPIAlphavantageUrl(ticker, fn);

if (!alphavantageUrl.some) {
return Result<T, Error>(new Error("Invalid Alphavantage function provided"));
return Result<Option<T>, Error>(new Error("Invalid Alphavantage function provided"));
}

try {
const { data, status, statusText } = await axios.get<ApiResponse<T>>(alphavantageUrl.coalesce());

if (status !== 200) {
return Result<T, Error>(new Error(`[${status}] ${statusText}`));
return Result<Option<T>, Error>(new Error(`[${status}] ${statusText}`));
}

return consumeApiResponse<T>(data);
Expand All @@ -48,6 +48,6 @@ export const queryAlphavantage = async <T = unknown>(ticker: string, fn: Alphava
console.error("Something went wrong with axios: ", error.toJSON());
}

return Result<T, Error>(error as Error);
return Result<Option<T>, Error>(error as Error);
}
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12521,10 +12521,10 @@ [email protected]:
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"
Expand Down

0 comments on commit 765f198

Please sign in to comment.