forked from gskril/ens-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
82 lines (67 loc) · 2.34 KB
/
utils.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
82
import { addEnsContracts, ensPublicActions } from '@ensdomains/ensjs';
import { createPublicClient, http, sha256 } from 'viem';
import { mainnet } from 'viem/chains';
import { z } from 'zod';
import { defaultCoinKeys, defaultTextKeys } from './constants';
import { IRequest } from 'itty-router/Router';
export function getPublicClient(env: Env) {
return createPublicClient({
transport: http(env.ETH_RPC),
chain: addEnsContracts(mainnet),
batch: {
multicall: {
batchSize: 10_240,
},
},
}).extend(ensPublicActions);
}
export const commaSeparatedListSchema = (type: 'string' | 'number') => {
return z.string().refine((value) => {
const values = value.split(',');
if (type === 'string') {
return values.every((v) => !!v.trim());
} else {
return values.every((v) => !isNaN(Number(v)));
}
});
};
export function parseKeysFromParams({
texts,
coins,
}: {
texts?: string | undefined;
coins?: string | undefined;
}) {
const requestedTextKeys = texts?.split(',').map((key) => key.trim()) || [];
const requestedCoinKeys = coins?.split(',').map((key) => Number(key)) || [];
const textKeys = defaultTextKeys.concat(requestedTextKeys);
const coinKeys = defaultCoinKeys.concat(requestedCoinKeys);
return { textKeys, coinKeys };
}
export async function checkCache(key: string, request: IRequest, body?: unknown) {
const bodyHash = sha256(new TextEncoder().encode(JSON.stringify(body)));
const cacheUrl = new URL(request.url);
cacheUrl.pathname = cacheUrl.pathname + bodyHash;
// Always use GET method to enable caching
const cacheKey = new Request(cacheUrl, { method: 'GET', headers: request.headers });
const cache = await caches.open(key);
// Check whether the value is already available in the cache
const response = await cache.match(cacheKey);
return { cache, cacheKey, response };
}
export function cacheAndCreateResponse(
ctx: ExecutionContext,
cache: Cache,
cacheKey: Request,
data: any
) {
const response = Response.json(data);
// Cache the response for 10 minutes. If the same data is requested witin the
// next 50 mins, serve the stale response while revalidating in the background
response.headers.append(
'Cache-Control',
'public, max-age=600, stale-while-revalidate=3000'
);
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
}