Add short links middleware and list endpoint
This commit is contained in:
parent
bc17b40c27
commit
822a6c0e04
14
functions/api/short-links/_middleware.ts
Normal file
14
functions/api/short-links/_middleware.ts
Normal file
@ -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
functions/api/short-links/list.ts
Normal file
11
functions/api/short-links/list.ts
Normal file
@ -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));
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user