forked from sebbo2002/ical-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-nextjs13.js
41 lines (36 loc) · 1.13 KB
/
example-nextjs13.js
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
// app/api/calendar/route.ts
import icalendar from 'ical-generator';
import moment from 'moment';
export async function GET(req) {
if (req.method !== 'GET') {
return new Response('Method Not Allowed', {
headers: { Allow: 'GET' },
status: 405,
});
}
const filename = 'calendar.ics';
try {
const calendar = icalendar({
prodId: '//superman-industries.com//ical-generator//EN',
events: [
{
start: moment(),
end: moment().add(1, 'hour'),
summary: 'Example Event',
description: 'It works ;)',
url: 'https://example.com'
}
]
});
return new Response(calendar.toString(), {
headers: {
'Content-Type': 'text/calendar; charset=utf-8',
'Content-Disposition': `attachment; filename='${filename}'`,
},
status: 200,
});
} catch (err) {
console.error(err);
return new Response(JSON.stringify(err), { status: 500 });
}
}