-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathroute.ts
65 lines (49 loc) · 1.49 KB
/
route.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
import { addQuote, getQuotes } from "@/lib/utils/query";
import { z } from "zod";
export const runtime = "edge";
// disabling caching
export const fetchCache = "force-no-store";
export const revalidate = 0;
export async function GET(request: Request) {
const url = new URL(request.url);
const searchParams = new URLSearchParams(url.search);
const parser = z.enum(["TTL", "SWR", "BOTH", "NONE"]);
const parsedOutput = await parser.safeParseAsync(searchParams.get("cache"));
if (!parsedOutput.success) {
return new Response(JSON.stringify("Invalid search parameter."), {
status: 400,
});
}
let map = new Map();
// When TTL is selected
map.set("TTL", {
ttl: 30,
});
// When SWR is selected
map.set("SWR", {
swr: 30,
});
// When TTL + SWR is selected
map.set("BOTH", {
ttl: 30,
swr: 60,
});
// This ensures no caching is performed and only the Accelerate connection pool is used
map.set("NONE", undefined);
const data = await getQuotes(map.get(parsedOutput.data));
return new Response(JSON.stringify(data), {
headers: { "Content-Type": "application/json" },
status: 200,
});
}
export async function POST(request: Request) {
const response = await fetch("https://api.quotable.io/random", {
cache: "no-cache",
});
const data = await response.json();
await addQuote(data.content);
return new Response(JSON.stringify({ quote: data.content }), {
headers: { "Content-Type": "application/json" },
status: 201,
});
}