Skip to content

Commit d3e9698

Browse files
committed
found a cache event handler
1 parent 2176075 commit d3e9698

1 file changed

Lines changed: 60 additions & 55 deletions

File tree

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,69 @@
11
import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config'
22

3-
export default defineEventHandler(async event => {
4-
let owner = getRouterParam(event, 'owner')
5-
let repo = getRouterParam(event, 'repo')
3+
export default defineCachedEventHandler(
4+
async event => {
5+
let owner = getRouterParam(event, 'owner')
6+
let repo = getRouterParam(event, 'repo')
67

7-
let cachedFetch: CachedFetchFunction
8-
if (event.context.cachedFetch) {
9-
cachedFetch = event.context.cachedFetch
10-
} else {
11-
// Fallback: return a function that uses regular $fetch
12-
// (shouldn't happen in normal operation)
13-
cachedFetch = async <T = unknown>(
14-
url: string,
15-
options: Parameters<typeof $fetch>[1] = {},
16-
_ttl?: number,
17-
): Promise<CachedFetchResult<T>> => {
18-
const data = (await $fetch<T>(url, options)) as T
19-
return { data, isStale: false, cachedAt: null }
8+
let cachedFetch: CachedFetchFunction
9+
if (event.context.cachedFetch) {
10+
cachedFetch = event.context.cachedFetch
11+
} else {
12+
// Fallback: return a function that uses regular $fetch
13+
// (shouldn't happen in normal operation)
14+
cachedFetch = async <T = unknown>(
15+
url: string,
16+
options: Parameters<typeof $fetch>[1] = {},
17+
_ttl?: number,
18+
): Promise<CachedFetchResult<T>> => {
19+
const data = (await $fetch<T>(url, options)) as T
20+
return { data, isStale: false, cachedAt: null }
21+
}
2022
}
21-
}
2223

23-
try {
24-
// Tangled doesn't have a public JSON API, but we can scrape the star count
25-
// from the HTML page (it's in the hx-post URL as countHint=N)
26-
const { data: html } = await cachedFetch<string>(
27-
`https://tangled.org/${owner}/${repo}`,
28-
{
29-
headers: { 'User-Agent': 'npmx', 'Accept': 'text/html' },
30-
},
31-
60 * 10,
32-
)
33-
// Extracts the at-uri used in atproto
34-
const atUriMatch = html.match(/data-star-subject-at="([^"]+)"/)
35-
// Extract star count from: hx-post="/star?subject=...&countHint=23"
36-
const starMatch = html.match(/countHint=(\d+)/)
37-
//We'll set the stars from tangled's repo page and may override it with constellation if successful
38-
let stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0
39-
let forks = 0
40-
const atUri = atUriMatch?.[1]
24+
try {
25+
// Tangled doesn't have a public JSON API, but we can scrape the star count
26+
// from the HTML page (it's in the hx-post URL as countHint=N)
27+
const { data: html } = await cachedFetch<string>(
28+
`https://tangled.org/${owner}/${repo}`,
29+
{
30+
headers: { 'User-Agent': 'npmx', 'Accept': 'text/html' },
31+
},
32+
CACHE_MAX_AGE_ONE_MINUTE * 10,
33+
)
34+
// Extracts the at-uri used in atproto
35+
const atUriMatch = html.match(/data-star-subject-at="([^"]+)"/)
36+
// Extract star count from: hx-post="/star?subject=...&countHint=23"
37+
const starMatch = html.match(/countHint=(\d+)/)
38+
//We'll set the stars from tangled's repo page and may override it with constellation if successful
39+
let stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0
40+
let forks = 0
41+
const atUri = atUriMatch?.[1]
4142

42-
if (atUri) {
43-
try {
44-
const constellation = new Constellation(cachedFetch)
45-
//Get counts of records that reference this repo in the atmosphere using constellation
46-
const { data: allLinks } = await constellation.getAllLinks(atUri)
47-
stars = allLinks.links['sh.tangled.feed.star']?.['.subject']?.distinct_dids ?? stars
48-
forks = allLinks.links['sh.tangled.repo']?.['.source']?.distinct_dids ?? 0
49-
} catch {
50-
//failing silently since this is just an enhancement to the information already showing
43+
if (atUri) {
44+
try {
45+
const constellation = new Constellation(cachedFetch)
46+
//Get counts of records that reference this repo in the atmosphere using constellation
47+
const { data: allLinks } = await constellation.getAllLinks(atUri)
48+
stars = allLinks.links['sh.tangled.feed.star']?.['.subject']?.distinct_dids ?? stars
49+
forks = allLinks.links['sh.tangled.repo']?.['.source']?.distinct_dids ?? 0
50+
} catch {
51+
//failing silently since this is just an enhancement to the information already showing
52+
}
5153
}
52-
}
5354

54-
return {
55-
stars,
56-
forks,
57-
}
58-
} catch {
59-
return {
60-
stars: 0,
61-
forks: 0,
55+
return {
56+
stars,
57+
forks,
58+
}
59+
} catch {
60+
return {
61+
stars: 0,
62+
forks: 0,
63+
}
6264
}
63-
}
64-
})
65+
},
66+
{
67+
maxAge: CACHE_MAX_AGE_ONE_MINUTE * 10,
68+
},
69+
)

0 commit comments

Comments
 (0)