MCPcopy Create free account
hub / github.com/chronark/envshare / handler

Function handler

pages/api/v1/load.ts:5–32  ·  view source on GitHub ↗
(req: NextRequest)

Source from the content-addressed store, hash-verified

3
4const redis = Redis.fromEnv();
5export default async function handler(req: NextRequest) {
6 const url = new URL(req.url);
7 const id = url.searchParams.get("id");
8 if (!id) {
9 return new NextResponse("id param is missing", { status: 400 });
10 }
11 const key = ["envshare", id].join(":");
12
13 const [data, _] = await Promise.all([
14 await redis.hgetall<{ encrypted: string; remainingReads: number | null; iv: string }>(key),
15 await redis.incr("envshare:metrics:reads"),
16 ]);
17 if (!data) {
18 return new NextResponse("Not Found", { status: 404 });
19 }
20 if (data.remainingReads !== null && data.remainingReads < 1) {
21 await redis.del(key);
22 return new NextResponse("Not Found", { status: 404 });
23 }
24
25 let remainingReads: number | null = null;
26 if (data.remainingReads !== null) {
27 // Decrement the number of reads and return the remaining reads
28 remainingReads = await redis.hincrby(key, "remainingReads", -1);
29 }
30
31 return NextResponse.json({ iv: data.iv, encrypted: data.encrypted, remainingReads });
32}
33
34export const config = {
35 runtime: "edge",

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected