Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add short links middleware and list endpoint
  • Loading branch information
regalijan committed Oct 26, 2024
1 parent bc17b40 commit 822a6c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions functions/api/short-links/_middleware.ts
@@ -0,0 +1,14 @@
import { jsonError } from "../../common.js";

export async function onRequest(context: RequestContext) {
const { current_user: user } = context.data;

if (!user) return jsonError("Unauthorized", 401);

if (
![0, 2, 4, 5, 6, 7, 9, 10, 11, 12].find((i) => user.permissions & (1 << i))
)
return jsonError("Forbidden", 403);

return await context.next();
}
11 changes: 11 additions & 0 deletions functions/api/short-links/list.ts
@@ -0,0 +1,11 @@
import { jsonResponse } from "../../common.js";

export async function onRequestGet(context: RequestContext) {
const { results } = await context.env.D1.prepare(
"SELECT created_at, destination, path FROM short_links WHERE user = ?;",
)
.bind(context.data.current_user.id)
.all();

return jsonResponse(JSON.stringify(results));
}

0 comments on commit 822a6c0

Please sign in to comment.