uniswap-interface-uncensored/functions/utils/getRequest.ts
Connor McEwen e6519a7dd1
feat: support redirects for a list of header paths (#7411)
* add country code to meta tag

* use blocked paths header

* proper types

* add test

* Update functions/components/metaTagInjector.ts

Co-authored-by: Zach Pomerantz <zzmp@uniswap.org>

* Update functions/components/metaTagInjector.ts

Co-authored-by: Zach Pomerantz <zzmp@uniswap.org>

* Update src/pages/App.tsx

Co-authored-by: Zach Pomerantz <zzmp@uniswap.org>

* pr suggestions

* skip failing e2e

* revert test change

* take file from main

---------

Co-authored-by: Zach Pomerantz <zzmp@uniswap.org>
2023-10-05 15:25:45 -04:00

43 lines
1.0 KiB
TypeScript

import { MetaTagInjector } from '../components/metaTagInjector'
import Cache from './cache'
import { Data } from './cache'
export async function getMetadataRequest(
res: Promise<Response>,
request: Request,
getData: () => Promise<Data | undefined>
) {
try {
const cachedData = await getRequest(request.url, getData, (data): data is Data => true)
if (cachedData) {
return new HTMLRewriter().on('head', new MetaTagInjector(cachedData, request)).transform(await res)
} else {
return res
}
} catch (e) {
return res
}
}
export async function getRequest<T extends Data>(
url: string,
getData: () => Promise<T | undefined>,
validateData: (data: Data) => data is T
): Promise<T | undefined> {
try {
const cachedData = await Cache.match(url)
if (cachedData && validateData(cachedData)) {
return cachedData
} else {
const data = await getData()
if (!data) {
return undefined
}
await Cache.put(data, url)
return data
}
} catch (e) {
return undefined
}
}