({ params, request })
| 39 | import { getRandomUserAgent } from '~/utilities/getRandomUserAgent' |
| 40 | |
| 41 | export const loader: LoaderFunction = async ({ params, request }) => { |
| 42 | invariant(params.id, "expected params.id"); |
| 43 | |
| 44 | const doc = await getDocument(params.id); |
| 45 | |
| 46 | if (!doc) { |
| 47 | throw new Response("Not Found", { |
| 48 | status: 404, |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | const path = getPathFromRequest(request); |
| 53 | const minimal = getMinimalFromRequest(request); |
| 54 | |
| 55 | if (doc.type == "url") { |
| 56 | console.log(`Fetching ${doc.url}...`); |
| 57 | |
| 58 | const jsonResponse = await safeFetch(doc.url, { |
| 59 | headers: { |
| 60 | "User-Agent": getRandomUserAgent(), |
| 61 | }, |
| 62 | }); |
| 63 | |
| 64 | if (!jsonResponse.ok) { |
| 65 | const jsonResponseText = await jsonResponse.text(); |
| 66 | const error = `Failed to fetch ${doc.url}. HTTP status: ${jsonResponse.status} (${jsonResponseText}})`; |
| 67 | console.error(error); |
| 68 | |
| 69 | throw new Response(error, { |
| 70 | status: jsonResponse.status, |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | const json = await jsonResponse.json(); |
| 75 | |
| 76 | return { |
| 77 | doc, |
| 78 | json, |
| 79 | path, |
| 80 | minimal, |
| 81 | }; |
| 82 | } else { |
| 83 | return { |
| 84 | doc, |
| 85 | json: JSON.parse(doc.contents), |
| 86 | path, |
| 87 | minimal, |
| 88 | }; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | export const action: ActionFunction = async ({ request, params }) => { |
| 93 | // Return if the request is not a DELETE |
nothing calls this directly
no test coverage detected