Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions functions/create-room/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AppwriteService from "./appwrite.js";
import LivekitService from "./livekit.js";
import { throwIfMissing } from "./utils.js";
import { throwIfMissing, validateCreateRoomInput } from "./utils.js";

export default async ({ req, res, log, error }) => {
throwIfMissing(process.env, [
Expand All @@ -15,17 +15,22 @@ export default async ({ req, res, log, error }) => {

const appwrite = new AppwriteService();
const livekit = new LivekitService();
let body;
let validatedData;

try {
throwIfMissing(JSON.parse(req.body), ["name", "adminUid", "tags"]);
body = JSON.parse(req.body);
throwIfMissing(body, ["name", "adminUid", "tags"]);
validatedData = validateCreateRoomInput(body);
} catch (err) {
error(err.message);
return res.json({ msg: err.message }, 400);
error(err.message);
return res.json({ msg: err.message }, 400);
}

const { name, description, adminUid, tags } = validatedData;

try {
log(req);
const { name, description, adminUid, tags } = JSON.parse(req.body);

// create a new room on appwrite
const newRoomdata = {
Expand Down
23 changes: 23 additions & 0 deletions functions/create-room/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,26 @@ export const throwIfMissing = (obj, keys) => {
throw new Error(`Missing required fields: ${missing.join(", ")}`);
}
};

export function validateCreateRoomInput(body) {
const { name, description, adminUid, tags } = body;
if (typeof name !== "string" || name.trim().length === 0) {
throw new Error("Invalid room name");
}
if (typeof adminUid !== "string" || adminUid.trim().length === 0) {
throw new Error("Invalid adminUid");
}
if (!Array.isArray(tags) || tags.some(tag => typeof tag !== "string")) {
throw new Error("Tags must be an array of strings");
}
if (description !== undefined && typeof description !== "string") {
throw new Error("Description must be a string");
}
return {
name: name.trim(),
description: typeof description === "string" ? description.trim() : "",
adminUid: adminUid.trim(),
tags,
};
}