MCPcopy Create free account
hub / github.com/triggerdotdev/jsonhero-web / loader

Function loader

app/routes/j/$id.tsx:41–90  ·  view source on GitHub ↗
({ params, request })

Source from the content-addressed store, hash-verified

39import { getRandomUserAgent } from '~/utilities/getRandomUserAgent'
40
41export 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
92export const action: ActionFunction = async ({ request, params }) => {
93 // Return if the request is not a DELETE

Callers

nothing calls this directly

Calls 5

getDocumentFunction · 0.90
getRandomUserAgentFunction · 0.90
getPathFromRequestFunction · 0.85
getMinimalFromRequestFunction · 0.85
safeFetchFunction · 0.85

Tested by

no test coverage detected