Add a contact form to the resume (#1)
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m6s
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m6s
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
47
functions/contact.ts
Normal file
47
functions/contact.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
|
||||
|
||||
interface EmailDetails {
|
||||
fullName: string;
|
||||
organisation: string;
|
||||
email: string;
|
||||
mobile: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface SendEmailWorker {
|
||||
sendEmail(rawMessage: EmailDetails): Promise<Response>;
|
||||
}
|
||||
|
||||
interface Env {
|
||||
SERVICE: SendEmailWorker;
|
||||
}
|
||||
|
||||
export const onRequest: PagesFunction<Env> = (context) => {
|
||||
// Wrap static forms plugin so we can extract the env to use email routing
|
||||
return staticFormsPlugin({
|
||||
respondWith: async ({ formData }) => {
|
||||
const fullName = formData.get("name");
|
||||
const organisation = formData.get("org");
|
||||
const email = formData.get("email");
|
||||
const mobile = formData.get("mobile");
|
||||
const message = formData.get("message");
|
||||
|
||||
// Must have some kind of identifiable information for me to actually care about them.
|
||||
if ((fullName || email) && message) {
|
||||
try {
|
||||
await context.env.SERVICE.sendEmail({
|
||||
fullName,
|
||||
organisation,
|
||||
email,
|
||||
mobile,
|
||||
message,
|
||||
});
|
||||
} catch (e) {
|
||||
return new Response(e);
|
||||
}
|
||||
}
|
||||
|
||||
return Response.redirect("https://michaelpivato.dev");
|
||||
},
|
||||
})(context);
|
||||
};
|
||||
13
functions/tsconfig.json
Normal file
13
functions/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "nodenext",
|
||||
"lib": [
|
||||
"esnext"
|
||||
],
|
||||
"types": [
|
||||
"@cloudflare/workers-types"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user