Back

Version 5

11/28/2023
import * as uuid from "https://deno.land/std/uuid/mod.ts";
import { blob } from "https://esm.town/v/std/blob";
import { getPolicy } from "https://esm.town/v/xkonti/memoryApiPolicy";
import process from "node:process";
import { Hono } from "npm:hono@3";

export const handleMemoryApiRequest = async (
req: Request,
apiName: string,
contactEmail: string,
lastPolicyUpdate: string,
blobKeyPrefix: string,
apiKeyPrefix: string,
) => {
// ==== HELPERS ====

const getMemoriesKey = (key: string): string => {
return `${blobKeyPrefix}${key}`;
};

const getMemories = async (key: string) => {
return await blob.getJSON(getMemoriesKey(key), []);
};

const setMemories = async (memories: any, key: string) => {
await blob.setJSON(getMemoriesKey(key), memories);
};

const verifyRequest = (c): { memoriesKey: string; error: any } => {
// Verify API key coming as a Bearer header
const authHeader = c.req.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Basic ")) {
console.error("Missing or invalid authorization header");
return { memoriesKey: "", error: c.text("Unauthorized", 401) };
}
// Extract and decode the base64 encoded credentials
Updated: December 21, 2023