diff --git a/api/index.js b/api/index.js index c3e5bec..b325a36 100644 --- a/api/index.js +++ b/api/index.js @@ -1,13 +1,14 @@ const createCard = require('../src/cards/new-log'); const createCardDark = require('../src/cards/new-log-black'); const fetchPost = require('../src/fetchers/post-fetcher'); +const fetchReadPost = require('../src/fetchers/readpost-fetcher'); module.exports = async (req, res) => { - const { name, tag, color } = req.query; + const { name, tag, color, slug } = req.query; res.setHeader('Content-Type', 'image/svg+xml'); try{ - const post = await fetchPost(name, tag); + const post = !slug ? await fetchPost(name, tag) : await fetchReadPost(name, slug); return res.send(color==='dark' ? createCardDark(post) : createCard(post)) } catch(e){ return res.send(e.message) diff --git a/src/fetchers/readpost-fetcher.js b/src/fetchers/readpost-fetcher.js new file mode 100644 index 0000000..a470c39 --- /dev/null +++ b/src/fetchers/readpost-fetcher.js @@ -0,0 +1,43 @@ +const { request } = require("../utils") + +const fetcher = (variables) => { + return request( + { + query: ` + query ReadPost($username: String, $url_slug: String) { + post(username: $username, url_slug: $url_slug) { + id + title + short_description + thumbnail + user { + username + profile { + thumbnail + } + } + url_slug + released_at + updated_at + comments_count + tags + likes + } + } + `, + variables + } + ) +} + +async function fetchReadPost(name, slug) { + try{ + const { data } = await fetcher({"username": name, "url_slug" : slug}); + return data.data.post; + }catch(e){ + throw new Error(e) + } + +} + +module.exports=fetchReadPost; \ No newline at end of file