-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathquotes.ts
35 lines (27 loc) · 1.14 KB
/
quotes.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
import { json } from "@solidjs/router";
import { z } from 'zod';
import { getQuotes } from '../../lib/utils/query';
import type { APIEvent } from "@solidjs/start/server";
import type { QuoteResult, CacheStrategy, QuoteCacheType } from '../../types';
const cacheStrategies: Record<QuoteCacheType, CacheStrategy | undefined> = {
'SWR': { swr: 30 },
'TTL': { ttl: 30 },
'NO CACHING': undefined,
'TTL + SWR': { ttl: 30, swr: 60 },
};
export async function GET({ request }: APIEvent) {
const url = new URL(request.url);
const cache = decodeURIComponent(url.searchParams.get('cache') || '') as QuoteCacheType;
const parser = z.enum(["TTL", "SWR", "TTL + SWR", "NO CACHING"]);
const parsedOutput = parser.safeParse(cache);
if (!parsedOutput.success) {
return json({ error: 'Invalid search parameter.' }, { status: 400 });
}
const cacheType: QuoteCacheType = parsedOutput.data;
const cacheStrategy = cacheStrategies[cacheType];
if (cacheStrategy === undefined && cacheType !== 'NO CACHING') {
return json({ error: 'Invalid cache strategy.' }, { status: 400 });
}
const data = await getQuotes(cacheStrategy);
return json(data);
}