(uri: string)
| 44 | } |
| 45 | |
| 46 | export async function getUriPreview(uri: string): Promise<PreviewResult> { |
| 47 | const url = rewriteUrl(uri); |
| 48 | |
| 49 | const head = await headUri(url.href); |
| 50 | |
| 51 | // If the url is an image content type, return a preview image |
| 52 | if ( |
| 53 | head && |
| 54 | imageContentTypes.some((contentType) => |
| 55 | contentType.includes(head.contentType) |
| 56 | ) |
| 57 | ) { |
| 58 | const previewImage = createPreviewImage(url.href, head); |
| 59 | |
| 60 | return previewImage; |
| 61 | } |
| 62 | |
| 63 | // If the url is a json content type, attempt to request the json and return a preview json |
| 64 | if (head?.contentType.includes("application/json")) { |
| 65 | const response = await safeFetch(url.href, { |
| 66 | headers: { |
| 67 | accept: "application/json", |
| 68 | }, |
| 69 | }); |
| 70 | |
| 71 | if (!response.ok) { |
| 72 | return { error: "No preview available for this URL" }; |
| 73 | } |
| 74 | |
| 75 | const jsonBody = await response.json(); |
| 76 | |
| 77 | return createPreviewJson(url.href, jsonBody); |
| 78 | } |
| 79 | |
| 80 | return await getOpenGraphNinja(url.href); |
| 81 | } |
| 82 | |
| 83 | type HeadInfo = { |
| 84 | contentType: string; |
no test coverage detected