Skip to content

Commit 29504d0

Browse files
committed
feat: add cache to check endpoint
1 parent 24f802f commit 29504d0

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

packages/api/src/routes/nfts-check.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,50 @@ import { DBClient } from '../utils/db-client'
55
import { parseCid } from '../utils/utils.js'
66
import { toCheckNftResponse } from '../utils/db-transforms.js'
77

8+
const CACHE_MAX_AGE_NO_DEAL_YET = 10 * 60 // in seconds (10 minutes)
89
const db = new DBClient(database.url, secrets.database)
910

1011
/** @type {import('../bindings').Handler} */
1112
export const nftCheck = async (event, { params }) => {
13+
const cache = await caches.open('nft:check')
14+
// @ts-ignore match function not found in type https://developer.mozilla.org/en-US/docs/Web/API/Cache
15+
let res = await caches.match(event.request.url)
16+
17+
if (res) {
18+
return res
19+
}
20+
1221
const cid = parseCid(params.cid)
1322
const content = await db.getContent(cid.contentCid)
1423

15-
if (content) {
16-
return new JSONResponse({
17-
ok: true,
18-
value: toCheckNftResponse(cid.sourceCid, content),
19-
})
20-
} else {
24+
if (!content) {
2125
throw new HTTPError('NFT not found', 404)
2226
}
27+
28+
const nftCheckResponseValue = toCheckNftResponse(cid.sourceCid, content)
29+
const headers =
30+
!!nftCheckResponseValue.deals.find((d) => d.status === 'active') &&
31+
nftCheckResponseValue.pin.status === 'pinned'
32+
? {
33+
// cache status response with max age
34+
'Cache-Control': `public, max-age=${CACHE_MAX_AGE_NO_DEAL_YET}`,
35+
}
36+
: undefined // cache default
37+
38+
res = new JSONResponse(
39+
{
40+
ok: true,
41+
value: nftCheckResponseValue,
42+
},
43+
{
44+
headers,
45+
}
46+
)
47+
48+
// Cache if pin status is pinned
49+
if (nftCheckResponseValue.pin.status === 'pinned') {
50+
event.waitUntil(cache.put(event.request, res.clone()))
51+
}
52+
53+
return res
2354
}

packages/api/src/utils/json-response.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ export class JSONResponse extends Response {
55
* @param {ResponseInitializerDict} [init]
66
*/
77
constructor(body, init = {}) {
8-
const headers = {
8+
super(JSON.stringify(body), {
9+
...init,
910
headers: {
1011
'content-type': 'application/json;charset=UTF-8',
12+
...init.headers,
1113
},
12-
}
13-
super(JSON.stringify(body), { ...init, ...headers })
14+
})
1415
}
1516
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,9 @@
10581058
integrity sha512-hh7qzfT0+1rkKiZrZnttRZxjZLzcHHZNQ7XmzA8De0YJxhg/tEovmczM1AjuGZJr8sr69gfOFtfZgqz2s1/p5Q==
10591059

10601060
"@cloudflare/workers-types@^3.3.1":
1061-
version "3.4.0"
1062-
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.4.0.tgz#80311e14df2f7f8c2cfcdce945b4f4ad8f9b03b1"
1063-
integrity sha512-i/3czUrt6YqbOWl44OtIqd0cSZvEVXp/1oD/DZylC4PHZL3q/BhbamdEVeVhc/HPk4iD/7MZ2HGaIMO4Z4b12A==
1061+
version "3.3.1"
1062+
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.3.1.tgz#8847543bda320472252708c29aaf7bac3374e814"
1063+
integrity sha512-GJFDgWd8ZHlr/m+Q2mp4xUl0/FIPpR6kf0Ix2C78E9HeJyUCW0c0w2EKCvgEDFFKB2rkzIX3eBqZCnLsbsw8zQ==
10641064

10651065
"@concordance/react@^2.0.0":
10661066
version "2.0.0"

0 commit comments

Comments
 (0)