diff --git a/_routes.json b/_routes.json new file mode 100644 index 0000000000..224702d4f0 --- /dev/null +++ b/_routes.json @@ -0,0 +1,5 @@ +{ + "version": 1, + "include": ["/api/*"], + "exclude": [] + } diff --git a/server/functions/videos_id_actions.mjs b/functions/api/videos/[video]/[actions].js similarity index 88% rename from server/functions/videos_id_actions.mjs rename to functions/api/videos/[video]/[actions].js index 0144ea566e..c8eee12f43 100644 --- a/server/functions/videos_id_actions.mjs +++ b/functions/api/videos/[video]/[actions].js @@ -1,7 +1,7 @@ -import db, { disconnectDb } from "../db"; +import db, { disconnectDb } from "../../../../server/db"; -export default async (req, ctx) => { - if (req.method !== "POST") { +export async function onRequest (ctx) { + if (ctx.method !== "POST") { return new Response( JSON.stringify({ success: false, message: "Invalid HTTP Method" }), { status: 405 } @@ -56,7 +56,3 @@ export default async (req, ctx) => { disconnectDb(); } }; - -export const config = { - path: ["/api/videos/:id/:action"], -}; diff --git a/server/functions/videos_ids.mjs b/functions/api/videos/[video]/index.js similarity index 84% rename from server/functions/videos_ids.mjs rename to functions/api/videos/[video]/index.js index fdb6c43d30..e588e92198 100644 --- a/server/functions/videos_ids.mjs +++ b/functions/api/videos/[video]/index.js @@ -1,8 +1,8 @@ -import db, { disconnectDb } from "../db"; +import db, { disconnectDb } from "../../../../server/db"; -export default async (req, ctx) => { +export async function onRequest (ctx) { try { - if (req.method === "GET") { + if (ctx.method === "GET") { const result = await db.query("SELECT * FROM videos WHERE id = $1", [ ctx.params.id, ]); @@ -17,7 +17,7 @@ export default async (req, ctx) => { JSON.stringify({ success: true, data: result.rows[0] }), { status: 200 } ); - } else if (req.method === "DELETE") { + } else if (ctx.method === "DELETE") { const result = await db.query("DELETE FROM videos WHERE id = $1", [ ctx.params.id, ]); @@ -47,7 +47,3 @@ export default async (req, ctx) => { disconnectDb(); } }; - -export const config = { - path: ["/api/videos/:id"], -}; diff --git a/server/functions/videos.mjs b/functions/api/videos/index.js similarity index 89% rename from server/functions/videos.mjs rename to functions/api/videos/index.js index 582298bcbc..440789a670 100644 --- a/server/functions/videos.mjs +++ b/functions/api/videos/index.js @@ -1,4 +1,4 @@ -import db, { disconnectDb } from "../db"; +import db, { disconnectDb } from "../../../server/db"; // source: https://stackoverflow.com/questions/3452546/how-do-i-get-the-youtube-video-id-from-a-url function youtubeLinkParser(url) { @@ -8,10 +8,10 @@ function youtubeLinkParser(url) { return match && match[7].length == 11 ? match[7] : false; } -export default async (req, _) => { +export async function onRequest (ctx) { try { - if (req.method === "GET") { - const query = new URLSearchParams(req.url); + if (ctx.method === "GET") { + const query = new URLSearchParams(ctx.url); let orderString = "ORDER BY id ASC"; switch (query.get("order")) { case "rating_asc": @@ -33,8 +33,8 @@ export default async (req, _) => { }), { status: 200 } ); - } else if (req.method === "POST") { - const body = await req.json(); + } else if (ctx.method === "POST") { + const body = await ctx.json(); if (!body.url || !body.title || !youtubeLinkParser(body.url)) { return new Response( JSON.stringify({ @@ -92,7 +92,3 @@ export default async (req, _) => { disconnectDb(); } }; - -export const config = { - path: ["/api/videos"], -}; diff --git a/wrangler.toml b/wrangler.toml new file mode 100644 index 0000000000..1e79a22a7e --- /dev/null +++ b/wrangler.toml @@ -0,0 +1 @@ +compatibility_flags = [ "nodejs_compat" ]