({ request, context })
| 8 | } from "~/jsonDoc.server"; |
| 9 | |
| 10 | export let loader: LoaderFunction = async ({ request, context }) => { |
| 11 | const url = new URL(request.url); |
| 12 | const jsonUrl = url.searchParams.get("url"); |
| 13 | const base64EncodedJson = url.searchParams.get("j"); |
| 14 | const ttl = url.searchParams.get("ttl"); |
| 15 | const readOnly = url.searchParams.get("readonly"); |
| 16 | const title = url.searchParams.get("title"); |
| 17 | const injest = url.searchParams.get("injest"); |
| 18 | |
| 19 | if (!jsonUrl && !base64EncodedJson) { |
| 20 | return redirect("/"); |
| 21 | } |
| 22 | |
| 23 | const options: CreateJsonOptions = {}; |
| 24 | |
| 25 | if (typeof ttl === "string") { |
| 26 | invariant(ttl.match(/^\d+$/), "ttl must be a number"); |
| 27 | |
| 28 | options.ttl = parseInt(ttl, 10); |
| 29 | |
| 30 | invariant(options.ttl >= 60, "ttl must be at least 60 seconds"); |
| 31 | } |
| 32 | |
| 33 | if (typeof readOnly === "string") { |
| 34 | options.readOnly = readOnly === "true"; |
| 35 | } |
| 36 | |
| 37 | if (typeof injest === "string") { |
| 38 | options.injest = injest === "true"; |
| 39 | } |
| 40 | |
| 41 | if (jsonUrl) { |
| 42 | const jsonURL = new URL(jsonUrl); |
| 43 | |
| 44 | invariant(jsonURL, "url must be a valid URL"); |
| 45 | |
| 46 | const doc = await createFromUrl(jsonURL, title ?? jsonURL.href, options); |
| 47 | |
| 48 | context.waitUntil( |
| 49 | sendEvent({ |
| 50 | type: "create", |
| 51 | from: "url", |
| 52 | hostname: jsonURL.hostname, |
| 53 | id: doc.id, |
| 54 | source: url.searchParams.get("utm_source") ?? url.hostname, |
| 55 | }) |
| 56 | ); |
| 57 | |
| 58 | return redirect(`/j/${doc.id}`); |
| 59 | } |
| 60 | |
| 61 | if (base64EncodedJson) { |
| 62 | const doc = await createFromRawJson( |
| 63 | title ?? "Untitled", |
| 64 | atob(base64EncodedJson), |
| 65 | options |
| 66 | ); |
| 67 |
nothing calls this directly
no test coverage detected