(req: NextRequest)
| 3 | |
| 4 | const redis = Redis.fromEnv(); |
| 5 | export 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 | |
| 34 | export const config = { |
| 35 | runtime: "edge", |
nothing calls this directly
no outgoing calls
no test coverage detected