-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy path+server.ts
36 lines (26 loc) · 1.1 KB
/
+server.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
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getQuotes } from '$lib/utils/query';
// Import types from your custom types file
import type { QuoteResult, CacheStrategy, QuoteCacheType } from '$lib/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({ url }: { url: URL }) {
const cache = decodeURIComponent(url.searchParams.get('cache') || '') as QuoteCacheType;
const parser = z.enum(["TTL", "SWR", "TTL + SWR", "No caching"]);
const parsedOutput = await parser.safeParseAsync(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);
}