Created
January 13, 2021 17:24
-
-
Save polluterofminds/e6260004ef14ad6c041efd6ae847ca6b to your computer and use it in GitHub Desktop.
Posts API Endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
import { verifyToken } from "./validate"; | |
const config = { | |
headers: { | |
Authorization: | |
"Bearer YOUR PINATA JWT", | |
"Content-Type": "application/json", | |
}, | |
}; | |
const getPosts = async () => { | |
try { | |
const res = await axios.get( | |
`https://api.pinata.cloud/data/pinList?status=pinned&metadata[name]=blogPost`, | |
config | |
); | |
const data = res.data.rows; | |
return data.map(post => { | |
return { | |
name: post.metadata.name.split("blogPost - ")[1], | |
hash: post.ipfs_pin_hash, | |
date: post.date_pinned | |
} | |
}); | |
} catch (error) { | |
throw error; | |
} | |
} | |
export default async (req, res) => { | |
try { | |
const headers = req.headers; | |
const { token } = headers; | |
const verified = await verifyToken(token); | |
if(!verified) { | |
return res.status(404).send("Bad token"); | |
} | |
const posts = await getPosts(); | |
res.status(200).json(posts); | |
} catch (error) { | |
console.log(error); | |
res.status(500).send("Server error"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment