-
Notifications
You must be signed in to change notification settings - Fork 2
Add runtime type check #66
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 | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,112 +1,108 @@ | ||||||||||||||||||||||||||||||||||
import type { NextApiRequest, NextApiResponse } from 'next' | ||||||||||||||||||||||||||||||||||
import { mailingClient } from '@/api/mailingClient' | ||||||||||||||||||||||||||||||||||
import { appendSpreadsheet } from '@/api/gSheets' | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
interface ContactFormNextApiRequest extends NextApiRequest { | ||||||||||||||||||||||||||||||||||
body: { | ||||||||||||||||||||||||||||||||||
firstName: string | ||||||||||||||||||||||||||||||||||
email: string | ||||||||||||||||||||||||||||||||||
projectName: string | ||||||||||||||||||||||||||||||||||
projectDescription: string | ||||||||||||||||||||||||||||||||||
services: string[] | ||||||||||||||||||||||||||||||||||
budget: string | ||||||||||||||||||||||||||||||||||
deadline: string | ||||||||||||||||||||||||||||||||||
discord: string | undefined | ||||||||||||||||||||||||||||||||||
telegram: string | undefined | ||||||||||||||||||||||||||||||||||
github: string | undefined | ||||||||||||||||||||||||||||||||||
projectLink: string | undefined | ||||||||||||||||||||||||||||||||||
aditionalInformation: string | undefined | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
import { Submit, SubmitSchema } from '@/types/submit' | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
export default async function serverSideCall( | ||||||||||||||||||||||||||||||||||
req: ContactFormNextApiRequest, | ||||||||||||||||||||||||||||||||||
req: NextApiRequest, | ||||||||||||||||||||||||||||||||||
res: NextApiResponse | ||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||
if (req.method !== 'POST') { | ||||||||||||||||||||||||||||||||||
res.status(405) | ||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
const { | ||||||||||||||||||||||||||||||||||
firstName, | ||||||||||||||||||||||||||||||||||
email, | ||||||||||||||||||||||||||||||||||
projectName, | ||||||||||||||||||||||||||||||||||
projectDescription, | ||||||||||||||||||||||||||||||||||
services, | ||||||||||||||||||||||||||||||||||
budget, | ||||||||||||||||||||||||||||||||||
deadline, | ||||||||||||||||||||||||||||||||||
discord, | ||||||||||||||||||||||||||||||||||
telegram, | ||||||||||||||||||||||||||||||||||
github, | ||||||||||||||||||||||||||||||||||
projectLink, | ||||||||||||||||||||||||||||||||||
aditionalInformation, | ||||||||||||||||||||||||||||||||||
} = req.body | ||||||||||||||||||||||||||||||||||
const aditionalInformationHeader = | ||||||||||||||||||||||||||||||||||
discord || telegram || github || projectLink || aditionalInformation | ||||||||||||||||||||||||||||||||||
? 'Aditional information' | ||||||||||||||||||||||||||||||||||
: '' | ||||||||||||||||||||||||||||||||||
const discordHandle = discord ? `Discord handle: ${discord}` : '' | ||||||||||||||||||||||||||||||||||
const telegramHandle = telegram ? `Telegram handle: ${telegram}` : '' | ||||||||||||||||||||||||||||||||||
const githubHandle = github ? `Github Profile: ${github}` : '' | ||||||||||||||||||||||||||||||||||
const projectLinkText = projectLink ? `Project Link: ${projectLink}` : '' | ||||||||||||||||||||||||||||||||||
const aditionalInformationText = aditionalInformation | ||||||||||||||||||||||||||||||||||
? `Additional Information given by the client: ${aditionalInformation}` | ||||||||||||||||||||||||||||||||||
: '' | ||||||||||||||||||||||||||||||||||
const mailData = { | ||||||||||||||||||||||||||||||||||
from: process.env.GMAIL_ACCOUNT as string, | ||||||||||||||||||||||||||||||||||
to: process.env.EMAIL_RECIPIENT as string, | ||||||||||||||||||||||||||||||||||
subject: `Project Request from: ${firstName} | ${projectName}`, | ||||||||||||||||||||||||||||||||||
text: `Who requested: | ||||||||||||||||||||||||||||||||||
First Name: ${firstName} | ||||||||||||||||||||||||||||||||||
E-mail: ${email} | ||||||||||||||||||||||||||||||||||
Project Name: ${projectName} | ||||||||||||||||||||||||||||||||||
Project Description: ${projectDescription} | ||||||||||||||||||||||||||||||||||
Type of services: ${services} | ||||||||||||||||||||||||||||||||||
Budget Range: ${budget} | ||||||||||||||||||||||||||||||||||
Deadline(yyyy-mm-dd): ${deadline} | ||||||||||||||||||||||||||||||||||
const submitData: Submit = req.body | ||||||||||||||||||||||||||||||||||
const { success } = SubmitSchema.safeParse(submitData) | ||||||||||||||||||||||||||||||||||
if (success) { | ||||||||||||||||||||||||||||||||||
const { | ||||||||||||||||||||||||||||||||||
firstName, | ||||||||||||||||||||||||||||||||||
email, | ||||||||||||||||||||||||||||||||||
projectName, | ||||||||||||||||||||||||||||||||||
projectDescription, | ||||||||||||||||||||||||||||||||||
services, | ||||||||||||||||||||||||||||||||||
budget, | ||||||||||||||||||||||||||||||||||
deadline, | ||||||||||||||||||||||||||||||||||
discord, | ||||||||||||||||||||||||||||||||||
telegram, | ||||||||||||||||||||||||||||||||||
github, | ||||||||||||||||||||||||||||||||||
projectLink, | ||||||||||||||||||||||||||||||||||
aditionalInformation, | ||||||||||||||||||||||||||||||||||
} = submitData | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
${aditionalInformationHeader} | ||||||||||||||||||||||||||||||||||
${discordHandle} | ||||||||||||||||||||||||||||||||||
${telegramHandle} | ||||||||||||||||||||||||||||||||||
${githubHandle} | ||||||||||||||||||||||||||||||||||
${projectLinkText} | ||||||||||||||||||||||||||||||||||
${aditionalInformationText} | ||||||||||||||||||||||||||||||||||
`, | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
const date = new Date() | ||||||||||||||||||||||||||||||||||
const sheetsData = { | ||||||||||||||||||||||||||||||||||
timeStamp: date.toUTCString(), | ||||||||||||||||||||||||||||||||||
whoRequested: firstName, | ||||||||||||||||||||||||||||||||||
emailContact: email, | ||||||||||||||||||||||||||||||||||
projectName: projectName, | ||||||||||||||||||||||||||||||||||
projectDescription: projectDescription, | ||||||||||||||||||||||||||||||||||
typeOfServices: services.toString(), | ||||||||||||||||||||||||||||||||||
budgetRange: budget, | ||||||||||||||||||||||||||||||||||
deadline: deadline, | ||||||||||||||||||||||||||||||||||
discordHandle: discord ? discord : '', | ||||||||||||||||||||||||||||||||||
githubHandle: github ? github : '', | ||||||||||||||||||||||||||||||||||
telegramHandle: telegram ? telegram : '', | ||||||||||||||||||||||||||||||||||
projectLink: projectLink ? projectLink : '', | ||||||||||||||||||||||||||||||||||
aditionalInformationText: aditionalInformation ? aditionalInformation : '' | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||
mailingClient.sendMail(mailData, (err) => { | ||||||||||||||||||||||||||||||||||
if (err) { | ||||||||||||||||||||||||||||||||||
console.log(err) | ||||||||||||||||||||||||||||||||||
res.status(500).json({ | ||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||
body: err, | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
res.status(200).json({ | ||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
appendSpreadsheet(sheetsData) | ||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||
console.log(err) | ||||||||||||||||||||||||||||||||||
res.status(400).json({ | ||||||||||||||||||||||||||||||||||
const aditionalInformationHeader = | ||||||||||||||||||||||||||||||||||
discord || telegram || github || projectLink || aditionalInformation | ||||||||||||||||||||||||||||||||||
? 'Aditional information' | ||||||||||||||||||||||||||||||||||
: '' | ||||||||||||||||||||||||||||||||||
const discordHandle = discord ? `Discord handle: ${discord}` : '' | ||||||||||||||||||||||||||||||||||
const telegramHandle = telegram ? `Telegram handle: ${telegram}` : '' | ||||||||||||||||||||||||||||||||||
const githubHandle = github ? `Github Profile: ${github}` : '' | ||||||||||||||||||||||||||||||||||
const projectLinkText = projectLink ? `Project Link: ${projectLink}` : '' | ||||||||||||||||||||||||||||||||||
const aditionalInformationText = aditionalInformation | ||||||||||||||||||||||||||||||||||
? `Additional Information given by the client: ${aditionalInformation}` | ||||||||||||||||||||||||||||||||||
: '' | ||||||||||||||||||||||||||||||||||
const mailData = { | ||||||||||||||||||||||||||||||||||
from: process.env.GMAIL_ACCOUNT as string, | ||||||||||||||||||||||||||||||||||
to: process.env.EMAIL_RECIPIENT as string, | ||||||||||||||||||||||||||||||||||
subject: `Project Request from: ${firstName} | ${projectName}`, | ||||||||||||||||||||||||||||||||||
text: `Who requested: | ||||||||||||||||||||||||||||||||||
First Name: ${firstName} | ||||||||||||||||||||||||||||||||||
E-mail: ${email} | ||||||||||||||||||||||||||||||||||
Project Name: ${projectName} | ||||||||||||||||||||||||||||||||||
Project Description: ${projectDescription} | ||||||||||||||||||||||||||||||||||
Type of services: ${services} | ||||||||||||||||||||||||||||||||||
Budget Range: ${budget} | ||||||||||||||||||||||||||||||||||
Deadline(yyyy-mm-dd): ${deadline} | ||||||||||||||||||||||||||||||||||
${aditionalInformationHeader} | ||||||||||||||||||||||||||||||||||
${discordHandle} | ||||||||||||||||||||||||||||||||||
${telegramHandle} | ||||||||||||||||||||||||||||||||||
${githubHandle} | ||||||||||||||||||||||||||||||||||
${projectLinkText} | ||||||||||||||||||||||||||||||||||
${aditionalInformationText} | ||||||||||||||||||||||||||||||||||
`, | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
const date = new Date() | ||||||||||||||||||||||||||||||||||
const sheetsData = { | ||||||||||||||||||||||||||||||||||
timeStamp: date.toUTCString(), | ||||||||||||||||||||||||||||||||||
whoRequested: firstName, | ||||||||||||||||||||||||||||||||||
emailContact: email, | ||||||||||||||||||||||||||||||||||
projectName: projectName, | ||||||||||||||||||||||||||||||||||
projectDescription: projectDescription, | ||||||||||||||||||||||||||||||||||
typeOfServices: services.toString(), | ||||||||||||||||||||||||||||||||||
budgetRange: budget, | ||||||||||||||||||||||||||||||||||
deadline: deadline, | ||||||||||||||||||||||||||||||||||
discordHandle: discord ? discord : '', | ||||||||||||||||||||||||||||||||||
githubHandle: github ? github : '', | ||||||||||||||||||||||||||||||||||
telegramHandle: telegram ? telegram : '', | ||||||||||||||||||||||||||||||||||
projectLink: projectLink ? projectLink : '', | ||||||||||||||||||||||||||||||||||
aditionalInformationText: aditionalInformation | ||||||||||||||||||||||||||||||||||
? aditionalInformation | ||||||||||||||||||||||||||||||||||
: '', | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||
mailingClient.sendMail(mailData, (err) => { | ||||||||||||||||||||||||||||||||||
if (err) { | ||||||||||||||||||||||||||||||||||
console.log(err) | ||||||||||||||||||||||||||||||||||
res.status(500).json({ | ||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||
body: err, | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
res.status(200).json({ | ||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
appendSpreadsheet(sheetsData) | ||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||
console.log(err) | ||||||||||||||||||||||||||||||||||
res.status(400).json({ | ||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+96
to
+101
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. In case of an error in the execution of the endpoint is always good to also send the error message. Getting the message with the correct typing from an error response is a bit tricky, but here's a solution:
Suggested change
|
||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
res.status(422).json({ | ||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||
message: 'Invalid body format', | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||
import { z } from 'zod' | ||||||
|
||||||
const nonEmptyStringContraint = z.string().min(1) | ||||||
const EmptyStringContraint = z.string() | ||||||
|
||||||
export const SubmitSchema = z.object({ | ||||||
firstName: nonEmptyStringContraint, | ||||||
email: nonEmptyStringContraint, | ||||||
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. Zod string objects has a built-in method for checking the email syntax:
Suggested change
|
||||||
projectName: nonEmptyStringContraint, | ||||||
projectDescription: nonEmptyStringContraint, | ||||||
services: z.array(nonEmptyStringContraint), | ||||||
budget: nonEmptyStringContraint, | ||||||
deadline: nonEmptyStringContraint, | ||||||
discord: EmptyStringContraint, | ||||||
telegram: EmptyStringContraint, | ||||||
github: EmptyStringContraint, | ||||||
projectLink: EmptyStringContraint, | ||||||
aditionalInformation: EmptyStringContraint, | ||||||
}) | ||||||
|
||||||
export type Submit = z.infer<typeof SubmitSchema> |
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.
To keep it DRY, I recommend creating a helper function that takes the optional data variable and its message and returns an empty string, and replaces all of these ternary operators.
something like that