Move api to v1 folder

This commit is contained in:
2022-06-27 09:08:26 -05:00
parent 6fe79a1892
commit 2fa58305cb
15 changed files with 43 additions and 35 deletions

View File

@@ -0,0 +1,67 @@
import db from "../../../lib/db";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import { withErrorHandling } from "../../../lib/apiHelpers";
const handler = withErrorHandling(async (req, res) => {
const { method } = req;
switch (method) {
case "GET":
await getBookmark(req, res);
break;
case "POST":
await createBookmark(req, res);
break;
default:
res.setHeader("Allow", ["GET", "POST"]);
res.status(405).end(`Method ${method} Not Allowed`);
}
});
const ajv = new Ajv();
addFormats(ajv);
const createBookmarkSchema = {
type: "object",
properties: {
title: { type: "string" },
url: { type: "string", format: "uri" },
},
required: ["title", "url"],
};
const validateCreateBookmarkSchema = ajv.compile(createBookmarkSchema);
const getBookmark = async (req, res) => {
const params = {
TableName: process.env.FMN_DYNAMODB_TABLENAME,
KeyConditionExpression: "hk = :hk",
ExpressionAttributeValues: { ":hk": "user#123" },
};
let bookmark = await db.query(params).promise();
res.status(200).json(bookmark);
};
const createBookmark = async (req, res) => {
if (!validateCreateBookmarkSchema(req.body)) {
console.warn(validateCreateBookmarkSchema.errors);
res.status(400).json(validateCreateBookmarkSchema.errors);
return;
}
const params = {
TableName: process.env.FMN_DYNAMODB_TABLENAME,
Item: {
hk: `user#${123}`,
sk: `bookmark#${"abc"}`,
title: req.body.title,
url: req.body.url,
},
};
await db.put(params).promise();
};
export default handler;

View File

@@ -0,0 +1,8 @@
import auth0 from "../../../lib/auth0";
import { withErrorHandling } from "../../../lib/apiHelpers";
const callback = withErrorHandling(async (req, res) => {
await auth0.handleCallback(req, res);
});
export default callback;

28
web/pages/api/v1/hello.js Normal file
View File

@@ -0,0 +1,28 @@
import db from "../../../lib/db";
import { withErrorHandling } from "../../../lib/apiHelpers";
const handler = withErrorHandling(async (req, res) => {
const { method } = req;
switch (method) {
case "GET":
await get(req, res);
default:
res.setHeader("Allow", ["GET"]);
res.status(405).end(`Method ${method} Not Allowed`);
}
});
const get = async (req, res) => {
const params = {
TableName: process.env.FMN_DYNAMODB_TABLENAME,
KeyConditionExpression: "hk = :hk",
ExpressionAttributeValues: { ":hk": "benramey@fastmail.com" },
};
let user = await db.query(params).promise();
res.status(200).json(user);
};
export default handler;

View File

@@ -0,0 +1,8 @@
import auth0 from "../../../lib/auth0";
import { withErrorHandling } from "../../../lib/apiHelpers";
const login = withErrorHandling(async (req, res) => {
await auth0.handleLogin(req, res);
});
export default login;

View File

@@ -0,0 +1,8 @@
import auth0 from "../../../lib/auth0";
import { withErrorHandling } from "../../../lib/apiHelpers";
const logout = withErrorHandling(async (req, res) => {
await auth0.handleLogout(req, res);
});
export default logout;

8
web/pages/api/v1/me.js Normal file
View File

@@ -0,0 +1,8 @@
import auth0 from "../../../lib/auth0";
import { withErrorHandling } from "../../../lib/apiHelpers";
const me = withErrorHandling(async (req, res) => {
await auth0.handleProfile(req, res);
});
export default me;