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
25 changes: 25 additions & 0 deletions pages/api/get-categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import manifest from '../../manifest.js';

/**
* @brief Get categories list as an array of strings.
*/
function getCategories() {
let categories = [];

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

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());
}
38 changes: 38 additions & 0 deletions pages/api/get-items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import manifest from '../../manifest.js';

function getCategoryToItemsMap() {
let map = {};

for (let i = 0; i < manifest.length; i++) {
const entry = manifest[i];
map[entry.category] = entry.items;
}

return map;
}

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 categoryName = req.query.category;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing it if you don't need it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i kept it in case anyone wants it later. however if you want to remove it that's okay.


const map = getCategoryToItemsMap();

if (!map.hasOwnProperty(categoryName)) {
res.status(400).end(`400 Bad Request\nInvalid category name "${categoryName}".`);
return;
}

const items = map[categoryName];
res.status(200).json(items);
}