Conversation
As per Rabby's suggestion, I agree this makes more sense.
| if (format === "html") { | ||
| return `<span>${result}</span>`; | ||
| } else { | ||
| return result; |
There was a problem hiding this comment.
Would it be much more readable to implement this code block as shown below?
if(format === "text") return result:
return <span>${result}</span>
There was a problem hiding this comment.
How about we remove the else statement in this case? I think the code you provided behaves the same way with my implementation but I agree that it makes it cleaner if we remove the else at this point.
| @@ -0,0 +1,8 @@ | |||
| export function sum(a: number, b: number, format: "html" | "text" = "html") { | |||
There was a problem hiding this comment.
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.
Good catch! Will do.
| @@ -0,0 +1,8 @@ | |||
| export function sum(a: number, b: number, format: "html" | "text" = "html") { | |||
There was a problem hiding this comment.
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.
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?
| @@ -0,0 +1,8 @@ | |||
| export function sum(a: number, b: number, format: "html" | "text" = "html") { | |||
There was a problem hiding this comment.
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.
OK. I think this aligns with our coding standards / style guide.
| @@ -0,0 +1,8 @@ | |||
| export function sum(a: number, b: number, format: "html" | "text" = "html") { | |||
There was a problem hiding this comment.
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.
Yes. I agree. Lets do that.
| const { num1, num2 } = req.query; | ||
| const result = sum(num1, num2); | ||
|
|
||
| res.status(200).json({ total: result }); |
There was a problem hiding this comment.
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.
Yes, I like your idea. Let's do that.
|
|
||
| export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
| const { num1, num2 } = req.query; | ||
| const result = sum(num1, num2); |
There was a problem hiding this comment.
const sum = calculateSum(num1, num2)
res.status(200).json({ sum })
There was a problem hiding this comment.
Can you provide more context to this? or what do you really mean to do with this?
| @@ -0,0 +1,9 @@ | |||
| import type { NextApiRequest, NextApiResponse } from "next"; | |||
| import { sum } from "@/pages/utils/sum"; | |||
|
Add decriprion and link issue pls. |
| @@ -0,0 +1,8 @@ | |||
| export function sum(a: number, b: number, format: "html" | "text" = "html") { | |||
There was a problem hiding this comment.
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.
I believe we only output string here so this makes the type incorrect. Don't you think?
| 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.
function should have return type e.g
export default function handler(req: NextApiRequest, res: NextApiResponse): void {There was a problem hiding this comment.
I appreciate the return type but I think this is not necessary.
| if (format === "html") { | ||
| return `<span>${result}</span>`; | ||
| } else { | ||
| return result; |
There was a problem hiding this comment.
to expand @DAJAKMPM no need for else statement
| const { num1, num2 } = req.query; | ||
| const result = sum(num1, num2); | ||
|
|
||
| res.status(200).json({ total: result }); |
There was a problem hiding this comment.
It will be good if we also return a message and status code. Like:
res.status(200).json({ statusCode: 200, message: "Success", total: result });
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.
|
|
||
| export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
| const { num1, num2 } = req.query; | ||
| const result = sum(num1, num2); |
There was a problem hiding this comment.
Add try catch handler and handle statusCode.
try {
const { num1, num2 } = req.query;
const result = sum(num1, num2);
res.status(200).json({ statusCode: 200, message: "Success", total: result });
} catch (err) {
res.status(400).json({ statusCode:400, message: "Error when summing up", total: null });
}
...