A few more api call basics
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import auth0 from "./auth0";
|
||||||
|
|
||||||
function withErrorHandling(handler) {
|
function withErrorHandling(handler) {
|
||||||
return async (req, res) => {
|
return async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -9,4 +11,17 @@ function withErrorHandling(handler) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export { withErrorHandling };
|
function withSession(handler) {
|
||||||
|
return async (req, res) => {
|
||||||
|
const session = await auth0.getSession(req, res);
|
||||||
|
|
||||||
|
if (!session || !session.user) {
|
||||||
|
res.status(401).end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await handler(req, res, session);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { withErrorHandling, withSession };
|
||||||
|
|||||||
@@ -1,27 +1,45 @@
|
|||||||
import db from "../../../lib/db";
|
|
||||||
import Ajv from "ajv";
|
import Ajv from "ajv";
|
||||||
import addFormats from "ajv-formats";
|
import addFormats from "ajv-formats";
|
||||||
import { withErrorHandling } from "../../../lib/apiHelpers";
|
|
||||||
|
|
||||||
const handler = withErrorHandling(async (req, res) => {
|
import db from "../../../lib/db";
|
||||||
const { method } = req;
|
import { withErrorHandling, withSession } from "../../../lib/apiHelpers";
|
||||||
|
|
||||||
switch (method) {
|
const handler = withErrorHandling(
|
||||||
case "GET":
|
withSession(async (req, res, session) => {
|
||||||
await getBookmark(req, res);
|
const { method } = req;
|
||||||
break;
|
const { user } = session;
|
||||||
case "POST":
|
|
||||||
await createBookmark(req, res);
|
switch (method) {
|
||||||
break;
|
case "GET":
|
||||||
default:
|
await getBookmarks(req, res, user);
|
||||||
res.setHeader("Allow", ["GET", "POST"]);
|
break;
|
||||||
res.status(405).end(`Method ${method} Not Allowed`);
|
case "POST":
|
||||||
}
|
await createBookmark(req, res, user);
|
||||||
});
|
break;
|
||||||
|
default:
|
||||||
|
res.setHeader("Allow", ["GET", "POST"]);
|
||||||
|
res.status(405).end(`Method ${method} Not Allowed`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
const ajv = new Ajv();
|
const ajv = new Ajv();
|
||||||
addFormats(ajv);
|
addFormats(ajv);
|
||||||
|
|
||||||
|
const getBookmarks = async (req, res, user) => {
|
||||||
|
const params = {
|
||||||
|
TableName: process.env.FMN_DYNAMODB_TABLENAME,
|
||||||
|
KeyConditionExpression: "hk = :hk AND begins_with(sk, :sk)",
|
||||||
|
ExpressionAttributeValues: {
|
||||||
|
":hk": `user#${user.sub}`,
|
||||||
|
":sk": "bookmark#",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let bookmarks = await db.query(params).promise();
|
||||||
|
res.status(200).json(bookmarks.Items);
|
||||||
|
};
|
||||||
|
|
||||||
const createBookmarkSchema = {
|
const createBookmarkSchema = {
|
||||||
type: "object",
|
type: "object",
|
||||||
properties: {
|
properties: {
|
||||||
@@ -33,18 +51,7 @@ const createBookmarkSchema = {
|
|||||||
|
|
||||||
const validateCreateBookmarkSchema = ajv.compile(createBookmarkSchema);
|
const validateCreateBookmarkSchema = ajv.compile(createBookmarkSchema);
|
||||||
|
|
||||||
const getBookmark = async (req, res) => {
|
const createBookmark = async (req, res, user) => {
|
||||||
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)) {
|
if (!validateCreateBookmarkSchema(req.body)) {
|
||||||
console.warn(validateCreateBookmarkSchema.errors);
|
console.warn(validateCreateBookmarkSchema.errors);
|
||||||
res.status(400).json(validateCreateBookmarkSchema.errors);
|
res.status(400).json(validateCreateBookmarkSchema.errors);
|
||||||
@@ -54,7 +61,7 @@ const createBookmark = async (req, res) => {
|
|||||||
const params = {
|
const params = {
|
||||||
TableName: process.env.FMN_DYNAMODB_TABLENAME,
|
TableName: process.env.FMN_DYNAMODB_TABLENAME,
|
||||||
Item: {
|
Item: {
|
||||||
hk: `user#${123}`,
|
hk: `user#${user.sub}`,
|
||||||
sk: `bookmark#${"abc"}`,
|
sk: `bookmark#${"abc"}`,
|
||||||
title: req.body.title,
|
title: req.body.title,
|
||||||
url: req.body.url,
|
url: req.body.url,
|
||||||
@@ -62,6 +69,7 @@ const createBookmark = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
await db.put(params).promise();
|
await db.put(params).promise();
|
||||||
|
res.status(200).json(params.Item);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default handler;
|
export default handler;
|
||||||
|
|||||||
46
web/pages/api/v1/bookmarks/[bookmarkId].js
Normal file
46
web/pages/api/v1/bookmarks/[bookmarkId].js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import db from "../../../../lib/db";
|
||||||
|
import { withErrorHandling, withSession } from "../../../../lib/apiHelpers";
|
||||||
|
|
||||||
|
const handler = withErrorHandling(
|
||||||
|
withSession(async (req, res, session) => {
|
||||||
|
const { method } = req;
|
||||||
|
const { user } = session;
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case "GET":
|
||||||
|
await getBookmark(req, res, user);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.setHeader("Allow", ["GET", "POST"]);
|
||||||
|
res.status(405).end(`Method ${method} Not Allowed`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const getBookmark = async (req, res, user) => {
|
||||||
|
const { bookmarkId } = req.query;
|
||||||
|
|
||||||
|
if (!bookmarkId) {
|
||||||
|
res.status(404).end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
TableName: process.env.FMN_DYNAMODB_TABLENAME,
|
||||||
|
KeyConditionExpression: "hk = :hk AND sk = :sk",
|
||||||
|
ExpressionAttributeValues: {
|
||||||
|
":hk": `user#${user.sub}`,
|
||||||
|
":sk": `bookmark#${bookmarkId}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let bookmarks = await db.query(params).promise();
|
||||||
|
|
||||||
|
if (bookmarks.Items.length == 1) {
|
||||||
|
res.status(200).json(bookmarks.Items[0]);
|
||||||
|
} else {
|
||||||
|
res.status(404).end();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default handler;
|
||||||
@@ -19,6 +19,15 @@ function Home() {
|
|||||||
Once you have logged in you should be able to click in{" "}
|
Once you have logged in you should be able to click in{" "}
|
||||||
<i>Profile</i> and <i>Logout</i>
|
<i>Profile</i> and <i>Logout</i>
|
||||||
</p>
|
</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{user && (
|
||||||
|
<>
|
||||||
|
<h4>Rendered user info on the client</h4>
|
||||||
|
<img src={user.picture} alt="user picture" />
|
||||||
|
<p>nickname: {user.nickname}</p>
|
||||||
|
<p>name: {user.name}</p>
|
||||||
<p>
|
<p>
|
||||||
<form method="POST" action="/api/v1/bookmarks">
|
<form method="POST" action="/api/v1/bookmarks">
|
||||||
<label for="title">title: </label>
|
<label for="title">title: </label>
|
||||||
@@ -31,15 +40,6 @@ function Home() {
|
|||||||
</p>
|
</p>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{user && (
|
|
||||||
<>
|
|
||||||
<h4>Rendered user info on the client</h4>
|
|
||||||
<img src={user.picture} alt="user picture" />
|
|
||||||
<p>nickname: {user.nickname}</p>
|
|
||||||
<p>name: {user.name}</p>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user