-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkiwi.ts
66 lines (54 loc) · 1.87 KB
/
kiwi.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
import { serve, serveStatic } from "https://deno.land/x/[email protected]/mod.ts";
import {
Calendar,
Event,
EventConfig,
} from "https://deno.land/x/[email protected]/mod.ts";
import { z } from "https://deno.land/x/[email protected]/mod.ts";
const buildICS = (config: EventConfig) => {
const event = new Event(config);
const calendar = new Calendar([event]);
const ics = calendar.toString();
return ics;
};
const dateSchema = z.preprocess((arg) => {
if (typeof arg == "string" || arg instanceof Date) return new Date(arg);
}, z.date());
const stringSchema = z.string();
const handleEvent = async (req: Request) => {
const form = await req.formData();
const title = form.get("title") || "Event Title";
const desc = form.get("desc") || "Description";
const now = new Date();
const then = new Date();
then.setHours(now.getHours() + 1);
const beginDate = form.get("start") || now;
const endDate = form.get("end") || then;
const _beginDate = dateSchema.safeParse(beginDate);
const _endDate = dateSchema.safeParse(endDate);
const _title = stringSchema.safeParse(title);
const _desc = stringSchema.safeParse(desc);
if (
_beginDate.success &&
_endDate.success &&
_title.success &&
_desc.success
) {
const ics = buildICS({
title: _title.data,
desc: _desc.data,
beginDate: _beginDate.data,
endDate: _endDate.data,
});
return new Response(ics, { headers: { "Content-Type": "text/calendar" } });
}
return new Response("Invalid form", { status: 400 });
};
serve({
"/": (req) => Response.redirect(`${req.url}cal`, 302),
"/cal": serveStatic("view.html", { baseUrl: import.meta.url }),
"/view.js": serveStatic("view.js", { baseUrl: import.meta.url }),
"/mvp.css": serveStatic("mvp.css", { baseUrl: import.meta.url }),
"/style.css": serveStatic("style.css", { baseUrl: import.meta.url }),
"/event": handleEvent,
});