Skip to content
Draft
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
30 changes: 30 additions & 0 deletions pages/api/GetCategories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import manifest from '../../manifest.js';

/**
* @brief Get categories list.
* Category format:
* {
* name: "Category Name",
* id: n
* }
*/
function getCategories() {
let categories = [];

for (let i = 0; i < manifest.length; i++) {
const entry = manifest[i];
categories.push({ name: entry.category, id: i });
}

return categories;
}

export default function handler(req, res) {
if (req.method !== "GET") {
res.setHeader("Allow", ["GET"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
return;
}

res.status(200).json(getCategories());
}
43 changes: 43 additions & 0 deletions pages/api/GetItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import manifest from '../../manifest.js';

/// @brief Check if string can be translated to an integer. Not a float or hexadecimal.
function isValidInteger(inStr) {
return typeof inStr === "string" && /^-?\d+$/.test(inStr);
}

export default function handler(req, res) {
if (req.method !== "GET")
{
res.setHeader("Allow", ["GET"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
return;
}

let categoryID = req.query.category;

// if no category chosen, send all categories
if (categoryID === undefined)
{
res.status(200).json({ categories: manifest });
return;
}

// if categoryID exists but not valid integer
if (!isValidInteger(categoryID))
{
res.status(400).end(`400 Bad Request\nInvalid category ID "${categoryID}"; Non-integer.`);
return;
}

categoryID = Number(categoryID);

// validate categoryID
if (categoryID < 0 || categoryID >= manifest.length)
{
res.status(400).end(`400 Bad Request\nInvalid category ID "${categoryID}"; Out of bounds, category length is ${manifest.length}.`);
return;
}

const items = manifest[categoryID];
res.status(200).json(items);
}
Loading