Skip to content

Commit

Permalink
Test CF build
Browse files Browse the repository at this point in the history
  • Loading branch information
sztupy committed Dec 12, 2023
1 parent c8b0a92 commit 1fb517a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
5 changes: 5 additions & 0 deletions _routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": 1,
"include": ["/api/*"],
"exclude": []
}
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -56,7 +56,3 @@ export default async (req, ctx) => {
disconnectDb();
}
};

export const config = {
path: ["/api/videos/:id/:action"],
};
Original file line number Diff line number Diff line change
@@ -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,
]);
Expand All @@ -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,
]);
Expand Down Expand Up @@ -47,7 +47,3 @@ export default async (req, ctx) => {
disconnectDb();
}
};

export const config = {
path: ["/api/videos/:id"],
};
16 changes: 6 additions & 10 deletions server/functions/videos.mjs → functions/api/videos/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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":
Expand All @@ -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({
Expand Down Expand Up @@ -92,7 +92,3 @@ export default async (req, _) => {
disconnectDb();
}
};

export const config = {
path: ["/api/videos"],
};
1 change: 1 addition & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compatibility_flags = [ "nodejs_compat" ]

0 comments on commit 1fb517a

Please sign in to comment.