Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/functions/api/short-links/[[id]].ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (41 sloc)
1.32 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { jsonError } from "../../common.js"; | |
export async function onRequestDelete(context: RequestContext) { | |
const path = decodeURIComponent(context.params.id as string); | |
if (typeof path !== "string") return jsonError("Invalid path", 400); | |
await context.env.D1.prepare( | |
"DELETE FROM short_links WHERE path = ? AND user = ?;", | |
) | |
.bind(path, context.data.current_user.id) | |
.run(); | |
return new Response(null, { | |
status: 204, | |
}); | |
} | |
export async function onRequestPatch(context: RequestContext) { | |
const { body } = context.data; | |
if (!body) return jsonError("Request body missing", 400); | |
let data: { path: any }; | |
try { | |
data = JSON.parse(body); | |
} catch { | |
return jsonError("Invalid JSON", 400); | |
} | |
if (Object.keys(data).find((k) => k !== "path")) | |
return jsonError("Only the `path` property can be modified.", 400); | |
const { path } = data; | |
if (typeof path !== "string") | |
return jsonError("The `path` property must be a string", 400); | |
if (path.length > 256) return jsonError("Path is too long", 400); | |
await context.env.D1.prepare( | |
"UPDATE short_links SET path = ? WHERE path = ? AND user = ?;", | |
) | |
.bind( | |
path, | |
decodeURIComponent(context.params.id as string), | |
context.data.current_user.id, | |
) | |
.run(); | |
return new Response(null, { | |
status: 204, | |
}); | |
} |