-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.ts
81 lines (72 loc) · 2.21 KB
/
handler.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
import { Env } from "./index";
type Period = "overall" | "7day" | "1month" | "3month" | "6month" | "12month";
const validPeriods = [
"overall",
"7day",
"1month",
"3month",
"6month",
"12month",
];
function isPeriod(maybePeriod: string): maybePeriod is Period {
return validPeriods.includes(maybePeriod);
}
export async function handleRequest(
request: Request,
env: Env
): Promise<Response> {
if (request.method === "GET") {
const url = new URL(request.url);
let method = "";
if (url.pathname === "/tracks") {
method = "user.getTopTracks";
} else if (url.pathname === "/artists") {
method = "user.getTopArtists";
} else {
return new Response("Route Not Found", {
status: 404,
statusText: "Route Not Found",
});
}
const period = url.searchParams.get("period") || "overall";
if (!isPeriod(period)) {
return new Response(
"Invalid period, must be omitted or one of the following: overall | 7day | 1month | 3month | 6month | 12month",
{
status: 400,
statusText: "Bad Request",
}
);
}
const searchParams = new URLSearchParams({
method,
user: env.LASTFM_USERNAME,
api_key: env.LASTFM_API_KEY,
format: "json",
period,
limit: "10",
});
const lastFmUrl = `https://ws.audioscrobbler.com/2.0/?${searchParams.toString()}`;
const lastFmResponse = await fetch(lastFmUrl, {
cf: {
// Tell CloudFlare's Global CDN to always cache this fetch regardless of content type
// for a max of 60 seconds before revalidating the resource
cacheTtl: 60, // sets TTL to 60 and cacheEverything setting
},
headers: {
"Content-Type": "application/json;charset=UTF-8",
},
});
// must use Response constructor to inherit all of response's fields
const response = new Response(lastFmResponse.body, lastFmResponse);
if (lastFmResponse.ok) {
//Set cache control headers to cache on browser for 30 minutes
response.headers.set("Cache-Control", "max-age=1800");
}
return response;
}
return new Response("Method Not Allowed", {
status: 405,
statusText: "Method Not Allowed",
});
}