-
Notifications
You must be signed in to change notification settings - Fork 0
Implement sum #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { sum } from "@/pages/utils/sum"; | ||
|
||
export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function should have return type e.g export default function handler(req: NextApiRequest, res: NextApiResponse): void { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I appreciate the return type but I think this is not necessary. |
||
const { num1, num2 } = req.query; | ||
const result = sum(num1, num2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide more context to this? or what do you really mean to do with this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add try catch handler and handle statusCode.
|
||
|
||
res.status(200).json({ total: result }); | ||
Comment on lines
+5
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @dorelljames. Although my understanding of Typescript may not be very strong, I have made an effort to improve its readability of this. I would greatly appreciate it if you could update it to this version. const total = sum(num1, num2);
res.status(200).json({ total }); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I like your idea. Let's do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be good if we also return a message and status code. Like: so that when using this API from the front-end side, it's easy to trace the status code or return the message from the front-end side. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function sum(a: number, b: number, format: "html" | "text" = "html") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly create a typing that contains the format value as shown below type Format = "html" | "text" And use the format type in the format parameter as a type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Will do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's best to add a comment for this function e.g jsdocs to have descriptions for the functionality of this function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. lets do that. Can you give me reference to JSDocs and how we can do that? Maybe you can also provide an example or reference link for me to check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's best to rename the function to "getSum" or "calculateSum" in order to know that this function calculates the sum There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I think this aligns with our coding standards / style guide. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function should have return type e.g export function sum(a: number, b: number, format: "html" | "text" = "html"): string | number { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we only output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's better to rename parameter to "num1" or "num2" to make it more readable compared to a and b. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I agree. Lets do that.
dorelljames marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const result = a + b; | ||
if (format === "html") { | ||
return `<span>${result}</span>`; | ||
} else { | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be much more readable to implement this code block as shown below? if(format === "text") return result: return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to expand @DAJAKMPM no need for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use alias for sum
sum: calculateSum