Add ttl option to the /new endpoint, allows setting a min 60s timeout

This commit is contained in:
Eric Allam
2022-05-02 21:30:19 +01:00
parent 5bcab4fce9
commit 436f01e459
2 changed files with 28 additions and 5 deletions
+6 -2
View File
@@ -37,7 +37,8 @@ export async function createFromUrlOrRawJson(
export async function createFromUrl(
url: URL,
title?: string
title?: string,
options?: CreateJsonOptions
): Promise<JSONDocument> {
const docId = createId();
const doc = {
@@ -47,7 +48,10 @@ export async function createFromUrl(
title: title ?? url.hostname,
};
await DOCUMENTS.put(docId, JSON.stringify(doc));
await DOCUMENTS.put(docId, JSON.stringify(doc), {
expirationTtl: options?.ttl ?? undefined,
metadata: options?.metadata ?? undefined,
});
return doc;
}
+22 -3
View File
@@ -1,23 +1,38 @@
import { LoaderFunction, redirect } from "remix";
import invariant from "tiny-invariant";
import { sendEvent } from "~/graphJSON.server";
import { createFromRawJson, createFromUrl } from "~/jsonDoc.server";
import {
createFromRawJson,
createFromUrl,
CreateJsonOptions,
} from "~/jsonDoc.server";
export let loader: LoaderFunction = async ({ request, context }) => {
const url = new URL(request.url);
const jsonUrl = url.searchParams.get("url");
const base64EncodedJson = url.searchParams.get("j");
const ttl = url.searchParams.get("ttl");
if (!jsonUrl && !base64EncodedJson) {
return redirect("/");
}
const options: CreateJsonOptions = {};
if (typeof ttl === "string") {
invariant(ttl.match(/^\d+$/), "ttl must be a number");
options.ttl = parseInt(ttl, 10);
invariant(options.ttl >= 60, "ttl must be at least 60 seconds");
}
if (jsonUrl) {
const jsonURL = new URL(jsonUrl);
invariant(jsonURL, "url must be a valid URL");
const doc = await createFromUrl(jsonURL, jsonURL.href);
const doc = await createFromUrl(jsonURL, jsonURL.href, options);
context.waitUntil(
sendEvent({
@@ -33,7 +48,11 @@ export let loader: LoaderFunction = async ({ request, context }) => {
}
if (base64EncodedJson) {
const doc = await createFromRawJson("Untitled", atob(base64EncodedJson));
const doc = await createFromRawJson(
"Untitled",
atob(base64EncodedJson),
options
);
context.waitUntil(
sendEvent({