-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
175 lines (161 loc) · 5.65 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { Options as SMTPTransportOptions } from "nodemailer/lib/smtp-transport";
import type { SendMailOptions } from "nodemailer";
import * as nodemailer from "nodemailer";
export type ValidationRule = {
field: string;
validator: (value: string | undefined) => boolean;
errorMessage: string;
};
export const validationRules: ValidationRule[] = [
{
field: "email",
validator: (value) => typeof value === "string" && value.trim().length > 0,
errorMessage: "is empty",
},
{
field: "subject",
validator: (value) => typeof value === "string" && value.trim().length > 0,
errorMessage: "is empty",
},
{
field: "message",
validator: (value) => typeof value === "string" && value.trim().length > 0,
errorMessage: "is empty",
},
// Add other fields and validations as needed
];
/**
* Validates the payload against a set of predefined rules.
* Edit `validationRules` in utils/index.ts to match the fields in your form.
* @see {@link validationRules} - The array of validation rules in utils/index.ts.
* @param {Record<string, string | undefined> | undefined} payload - The payload to validate.
* @returns {{isValid: boolean, errorMessages: Record<string, string>}} - Returns an object with `isValid` indicating if the payload passes all validation rules, and `errorMessages` providing details about each failed validation.
*/
export const isValid = (
payload: Record<string, string | undefined> | undefined
): { isValid: boolean; errorMessages: Record<string, string> } => {
const errorMessages: Record<string, string> = {};
if (!payload) {
return {
isValid: false,
errorMessages: { payload: "is empty" },
};
}
validationRules.forEach(({ field, validator, errorMessage }) => {
if (!validator(payload[field])) {
errorMessages[field] = errorMessage;
}
});
const isValid = Object.keys(errorMessages).length === 0;
return { isValid, errorMessages };
};
/**
* Trims a string and ensures it is not empty.
* @param {unknown} value - The value to process.
* @returns {string | undefined} - Returns the trimmed string or undefined if the input is not a string or is empty.
*/
export const getTrimmedNonEmptyString = (
value: unknown
): string | undefined => {
if (typeof value === "string") {
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
return undefined;
};
/**
* Parses the request body and returns an object with key-value pairs.
* @param {string | null} body - The body of the request to parse.
* @returns {Record<string, string> | undefined} Returns an object with key-value pairs if the body can be parsed, otherwise undefined.
*/
export const parseRequestBody = (
body: string | null
): Record<string, string> | undefined => {
if (!body) {
return undefined;
}
try {
const parsed = JSON.parse(body);
if (
typeof parsed !== "object" ||
parsed === null ||
Array.isArray(parsed)
) {
return undefined; // Ensure parsed is an object
}
const result = Object.keys(parsed).reduce((acc, key) => {
const trimmedValue = getTrimmedNonEmptyString(parsed[key]);
if (trimmedValue !== undefined) {
acc[key] = trimmedValue;
}
return acc;
}, {} as Record<string, string>);
return Object.keys(result).length > 0 ? result : undefined;
} catch {
const params = new URLSearchParams(body);
const result = Array.from(params.keys()).reduce((acc, key) => {
const trimmedValue = getTrimmedNonEmptyString(params.get(key));
if (trimmedValue !== undefined) {
acc[key] = trimmedValue;
}
return acc;
}, {} as Record<string, string>);
return Object.keys(result).length > 0 ? result : undefined;
}
};
/**
* Sends an email using the provided payload and configuration.
* The email configuration is defined in example.ts as `emailConfig`.
* @param {RequestPayload} payload - The payload containing the email details.
* @param {string} origin - The origin of the request.
* @param {boolean} isJson - Indicates if the response should be in JSON format (if the form is submitted with or without js).
* @param {Record<string, string>} headers - The headers to include in the response.
* @param {SMTPTransportOptions} emailConfig - Defined at top of example.ts based on env variables for the SMTP transport configuration.
* @returns {Promise<{statusCode: number, headers: Record<string, string>, body: string}>} - Returns a promise that resolves to the response object.
*/
export const sendEmail = async (
payload: Record<string, string>,
origin: string,
isJson: boolean,
headers: Record<string, string>,
emailConfig: SMTPTransportOptions
): Promise<{
statusCode: number;
headers: Record<string, string>;
body: string;
}> => {
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: emailConfig.auth.user,
to: payload.email,
subject: payload.subject,
html: `<p>${payload.message}</p>`,
} satisfies SendMailOptions;
try {
await transporter.sendMail(mailOptions);
return isJson
? {
statusCode: 200,
headers,
body: JSON.stringify({ message: "Email sent successfully" }),
}
: {
statusCode: 303,
headers: { ...headers, Location: `${origin}/#success` },
body: "",
};
} catch (error) {
console.error("Error sending email: ", error);
return isJson
? {
statusCode: 500,
headers,
body: JSON.stringify({ error: "Failed to send email" }),
}
: {
statusCode: 303,
headers: { ...headers, Location: `${origin}/#error` },
body: "",
};
}
};