uniswap-interface-uncensored/functions/tokens/[[index]].ts
Brendan Wong ef28667d13
feat: cloudflare worker to inject meta tags (#6901)
* feat: add token and nft injection

* feat: basic tests

* fix: get jest configured properly

* fix: change timeout

* fix: uninstall port ready

* fix: readd port ready

* fix: local tests work

* Update yarn.lock

* add lint disable for setup files

* fix: update dependencies

* fix: basic test suite for nfts/tokens

* feat: collection data

* fix: make tests more comprehensive

* fix: change matches to contains

* fix: tests for twitter alt image tag

* fix: image gen

* fix: add patch-package

* fix: update yarn install

* feat: basic image gen for nfts and collections

* fix: remove vibrant attempt

* use watermark asset

* dynamically grab color

* modularize code and prototype for token preview

* refactor code

* finalize css

* fix color grabber

* update tests

* fix up css

* refactor code a bit more

* remove console logs

* tests

* update tests

* update images based on design feedback

* network logos

* update lint

* slight refactoring

* more refactoring

* fix packages

* Update yarn.lock

* remove dynamically generated image stuff

* cleanup return values

* Create README.md

* Revert "Create README.md"

This reverts commit 7a91c98d384995fba914c9bf9a2fb3072793621f.

* First round of feedback

* comments

* feedback round 2

* final feedback

* final final feedback

* nest twitter:image:alt in image check

* better title handling

---------

Co-authored-by: Zach Pomerantz <zzmp@uniswap.org>
2023-07-18 12:35:29 -04:00

28 lines
949 B
TypeScript

/* eslint-disable import/no-unused-modules */
import { MetaTagInjector } from '../components/metaTagInjector'
import getToken from '../utils/getToken'
const convertTokenAddress = (tokenAddress: string) => {
return tokenAddress && tokenAddress === 'NATIVE' ? '0x0000000000000000000000000000000000000000' : tokenAddress
}
export const onRequest: PagesFunction = async ({ params, request, next }) => {
const { index } = params
const networkName = index[0]?.toString().toUpperCase()
const tokenAddress = convertTokenAddress(index[1]?.toString())
if (!tokenAddress) {
return next()
}
const tokenPromise = getToken(networkName, tokenAddress, request.url)
const resPromise = next()
try {
const [data, res] = await Promise.all([tokenPromise, resPromise])
if (!data) {
return resPromise
}
return new HTMLRewriter().on('head', new MetaTagInjector(data)).transform(res)
} catch (e) {
return resPromise
}
}