-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
166 lines (151 loc) · 5.77 KB
/
vite.config.ts
File metadata and controls
166 lines (151 loc) · 5.77 KB
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
import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import {defineConfig, loadEnv} from 'vite';
import {Resend} from 'resend';
import {CONTACT_EMAIL, SOCIAL_URLS} from './src/constants';
const DEFAULT_FROM_EMAIL =
'JavaScript Kampala <noreply@mail.javascriptkampala.org>';
/** Default CC when `RESEND_CC_EMAILS` is not set (comma-separated). */
const DEFAULT_CC_EMAILS = [
'javascriptkampala@gmail.com',
'kayondoedward13@gmail.com',
'pujeremy27@gmail.com',
'mukisageofrey@koodeyo.com',
];
function parseCcList(raw: string | undefined): string[] {
if (!raw?.trim()) {
return [...DEFAULT_CC_EMAILS];
}
return raw
.split(',')
.map((s) => s.trim())
.filter(Boolean);
}
function socialLinksBlock(): string {
return [
'Find us online:',
`X: ${SOCIAL_URLS.twitter}`,
`LinkedIn: ${SOCIAL_URLS.linkedin}`,
`GitHub: ${SOCIAL_URLS.github}`,
`YouTube: ${SOCIAL_URLS.youtube}`,
`Email: ${CONTACT_EMAIL}`,
].join('\n');
}
export default defineConfig(({mode}) => {
const env = loadEnv(mode, '.', '');
return {
plugins: [
react(),
tailwindcss(),
{
name: 'resend-contact-api',
configureServer(server) {
server.middlewares.use('/api/contact', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({error: 'Method not allowed'}));
return;
}
try {
const chunks: Buffer[] = [];
for await (const chunk of req) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) as {
type?: 'speaker' | 'sponsor';
payload?: Record<string, string>;
};
const apiKey = env.RESEND_API_KEY || process.env.RESEND_API_KEY;
const toEmail = env.RESEND_TO_EMAIL || process.env.RESEND_TO_EMAIL;
const fromEmail =
env.RESEND_FROM_EMAIL ||
process.env.RESEND_FROM_EMAIL ||
DEFAULT_FROM_EMAIL;
const ccRaw =
env.RESEND_CC_EMAILS || process.env.RESEND_CC_EMAILS;
const ccList = parseCcList(ccRaw);
const toLower = (e: string) => e.toLowerCase();
const cc = ccList.filter((addr) => toLower(addr) !== toLower(toEmail || ''));
if (!apiKey || !toEmail) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({error: 'Resend is not configured'}));
return;
}
const payload = body.payload || {};
const submitterEmail =
typeof payload.email === 'string' ? payload.email.trim() : '';
if (!submitterEmail) {
res.statusCode = 400;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({error: 'Missing email in submission'}));
return;
}
const resend = new Resend(apiKey);
const typeLabel =
body.type === 'sponsor' ? 'Sponsor Inquiry' : 'Speaker Proposal';
const lines = Object.entries(payload).map(
([key, value]) => `${key}: ${value}`,
);
const teamBody = [lines.join('\n'), '', socialLinksBlock()].join(
'\n',
);
await resend.emails.send({
from: fromEmail,
to: [toEmail],
cc: cc.length > 0 ? cc : undefined,
subject: `[${typeLabel}] JavaScript Kampala Website`,
replyTo: submitterEmail,
text: teamBody,
});
const isSponsor = body.type === 'sponsor';
const confirmationSubject = isSponsor
? 'We received your sponsor inquiry — JavaScript Kampala'
: 'We received your speaker proposal — JavaScript Kampala';
const kind = isSponsor ? 'sponsorship inquiry' : 'speaker proposal';
const confirmationText = [
'Hi,',
'',
`Thanks for contacting JavaScript Kampala. We've received your ${kind} and will get back to you shortly.`,
'',
`This address is not monitored for replies. If you need to add anything else, email ${CONTACT_EMAIL}.`,
'',
socialLinksBlock(),
'',
'— JavaScript Kampala',
].join('\n');
await resend.emails.send({
from: fromEmail,
to: [submitterEmail],
subject: confirmationSubject,
text: confirmationText,
});
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ok: true}));
} catch (_error) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({error: 'Failed to send email'}));
}
});
},
},
],
define: {
'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY),
},
resolve: {
alias: {
'@': path.resolve(__dirname, '.'),
},
},
server: {
// HMR is disabled in AI Studio via DISABLE_HMR env var.
// Do not modifyâfile watching is disabled to prevent flickering during agent edits.
hmr: process.env.DISABLE_HMR !== 'true',
},
};
});