Starting on working bookmarks api

This commit is contained in:
2022-06-27 09:01:36 -05:00
parent 4c37658cd8
commit 6fe79a1892
13 changed files with 652 additions and 58 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

@@ -1,10 +1,6 @@
import auth0 from "../../lib/auth0";
import { withErrorHandling } from "../../lib/apiHelpers";
export default async function callback(req, res) {
try {
await auth0.handleCallback(req, res);
} catch (error) {
console.error(error);
res.status(error.status || 500).end(error.message);
}
}
export default callback = withErrorHandling(async (req, res) => {
await auth0.handleCallback(req, res);
});

View File

@@ -1,23 +1,26 @@
const AWS = require("aws-sdk");
import db from "../../lib/db";
import { withErrorHandling } from "../../lib/apiHelpers";
AWS.config.update({
accessKeyId: process.env.FMN_AWS_ACCESS_KEY_ID, // Do NOT HARD-CODE your secret credentials here
secretAccessKey: process.env.FMN_AWS_SECRET_ACCESS_KEY, // Do NOT HARD-CODE your secret credentials here
region: process.env.FMN_AWS_REGION,
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 dynamodb = new AWS.DynamoDB.DocumentClient({
region: process.env.FMN_AWS_REGION,
});
const handler = async (req, res) => {
const get = async (req, res) => {
const params = {
TableName: process.env.FMN_DYNAMODB_TABLENAME,
KeyConditionExpression: "hk = :hk",
ExpressionAttributeValues: { ":hk": "benramey@fastmail.com" },
};
let user = await dynamodb.query(params).promise();
let user = await db.query(params).promise();
res.status(200).json(user);
};

View File

@@ -1,10 +1,6 @@
import auth0 from "../../lib/auth0";
import { withErrorHandling } from "../../lib/apiHelpers";
export default async function login(req, res) {
try {
await auth0.handleLogin(req, res);
} catch (error) {
console.error(error);
res.status(error.status || 500).end(error.message);
}
}
export default login = withErrorHandling(async (req, res) => {
await auth0.handleLogin(req, res);
});

View File

@@ -1,10 +1,6 @@
import auth0 from "../../lib/auth0";
import { withErrorHandling } from "../../lib/apiHelpers";
export default async function logout(req, res) {
try {
await auth0.handleLogout(req, res);
} catch (error) {
console.error(error);
res.status(error.status || 500).end(error.message);
}
}
export default logout = withErrorHandling(async (req, res) => {
await auth0.handleLogout(req, res);
});

View File

@@ -1,10 +1,6 @@
import auth0 from "../../lib/auth0";
import { withErrorHandling } from "../../lib/apiHelpers";
export default async function me(req, res) {
try {
await auth0.handleProfile(req, res);
} catch (error) {
console.error(error);
res.status(error.status || 500).end(error.message);
}
}
export default me = withErrorHandling(async (req, res) => {
await auth0.handleProfile(req, res);
});

View File

@@ -19,6 +19,16 @@ function Home() {
Once you have logged in you should be able to click in{" "}
<i>Profile</i> and <i>Logout</i>
</p>
<p>
<form method="POST" action="/api/bookmarks">
<label for="title">title: </label>
<input type="text" name="title"/>
<br />
<label for="url">url:</label>
<input type="text" name="url"/>
<button type="submit">Add bookmark</button>
</form>
</p>
</>
)}