|
| 1 | +import { parse } from 'valibot' |
| 2 | +import { handleApiError } from '#server/utils/error-handler' |
| 3 | +import { |
| 4 | + CACHE_MAX_AGE_ONE_MINUTE, |
| 5 | + BLUESKY_API, |
| 6 | + BLUESKY_EMBED_BASE_ROUTE, |
| 7 | + ERROR_BLUESKY_EMBED_FAILED, |
| 8 | + BLUESKY_URL_EXTRACT_REGEX, |
| 9 | +} from '#shared/utils/constants' |
| 10 | +import { type BlueskyOEmbedResponse, BlueskyOEmbedRequestSchema } from '#shared/schemas/atproto' |
| 11 | +import { Client } from '@atproto/lex' |
| 12 | +import * as com from '#shared/types/lexicons/com' |
| 13 | + |
| 14 | +export default defineCachedEventHandler( |
| 15 | + async (event): Promise<BlueskyOEmbedResponse> => { |
| 16 | + try { |
| 17 | + const query = getQuery(event) |
| 18 | + const { url, colorMode } = parse(BlueskyOEmbedRequestSchema, query) |
| 19 | + |
| 20 | + /** |
| 21 | + * INFO: Extract handle and post ID from https://bsky.app/profile/HANDLE/post/POST_ID |
| 22 | + * Casting type here because the schema has already validated the URL format before this line runs. |
| 23 | + * If the schema passes, this regex is mathematically guaranteed to match and contain both capture groups. |
| 24 | + * Match returns ["profile/danielroe.dev/post/123", "danielroe.dev", "123"] — only want the two capture groups, the full match string is discarded. |
| 25 | + */ |
| 26 | + const [, handle, postId] = url.match(BLUESKY_URL_EXTRACT_REGEX)! as [ |
| 27 | + string, |
| 28 | + `${string}.${string}`, |
| 29 | + string, |
| 30 | + ] |
| 31 | + |
| 32 | + const client = new Client({ service: BLUESKY_API }) |
| 33 | + const { did } = await client.call(com.atproto.identity.resolveHandle, { handle }) |
| 34 | + |
| 35 | + // INFO: Construct the embed URL with the DID |
| 36 | + const embedUrl = `${BLUESKY_EMBED_BASE_ROUTE}/embed/${did}/app.bsky.feed.post/${postId}?colorMode=${colorMode}` |
| 37 | + |
| 38 | + return { |
| 39 | + embedUrl, |
| 40 | + did, |
| 41 | + postId, |
| 42 | + handle, |
| 43 | + } |
| 44 | + } catch (error) { |
| 45 | + handleApiError(error, { |
| 46 | + statusCode: 502, |
| 47 | + message: ERROR_BLUESKY_EMBED_FAILED, |
| 48 | + }) |
| 49 | + } |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'bluesky-oembed', |
| 53 | + maxAge: CACHE_MAX_AGE_ONE_MINUTE, |
| 54 | + getKey: event => { |
| 55 | + const { url, colorMode } = getQuery(event) |
| 56 | + return `oembed:${url}:${colorMode ?? 'system'}` |
| 57 | + }, |
| 58 | + }, |
| 59 | +) |
0 commit comments