({ request, context })
| 18 | }; |
| 19 | |
| 20 | export const action: ActionFunction = async ({ request, context }) => { |
| 21 | const url = new URL(request.url); |
| 22 | |
| 23 | const { title, content, ttl, readOnly } = await request.json(); |
| 24 | |
| 25 | if (!title || !content) { |
| 26 | return json({ message: "Missing title or content" }, 400); |
| 27 | } |
| 28 | |
| 29 | invariant(typeof title === "string", "title must be a string"); |
| 30 | invariant(content !== null, "content cannot be null"); |
| 31 | |
| 32 | const source = url.searchParams.get("utm_source"); |
| 33 | |
| 34 | const options: CreateJsonOptions = {}; |
| 35 | |
| 36 | if (typeof ttl === "number") { |
| 37 | if (ttl < 60) { |
| 38 | return json({ message: "ttl must be at least 60 seconds" }, 400); |
| 39 | } |
| 40 | |
| 41 | options.ttl = ttl; |
| 42 | } |
| 43 | |
| 44 | if (typeof readOnly === "boolean") { |
| 45 | options.readOnly = readOnly; |
| 46 | } |
| 47 | |
| 48 | const doc = await createFromRawJson(title, JSON.stringify(content), options); |
| 49 | url.pathname = `/j/${doc.id}`; |
| 50 | |
| 51 | url.searchParams.delete("utm_source"); |
| 52 | |
| 53 | context.waitUntil( |
| 54 | sendEvent({ |
| 55 | type: "create", |
| 56 | from: "url", |
| 57 | hostname: url.hostname, |
| 58 | id: doc.id, |
| 59 | source, |
| 60 | }) |
| 61 | ); |
| 62 | |
| 63 | return json( |
| 64 | { id: doc.id, title, location: url.toString() }, |
| 65 | { |
| 66 | headers: { |
| 67 | "Access-Control-Allow-Origin": "*", |
| 68 | }, |
| 69 | } |
| 70 | ); |
| 71 | }; |
nothing calls this directly
no test coverage detected