Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return post url of created articles #40

Open
wants to merge 1 commit into
base: patch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@

npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn-error.log*
.idea
25 changes: 15 additions & 10 deletions controller/postFromDev.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,36 @@ exports.postFromDev = async (req, res, next) => {

let mediumPost;
let hashPost;
let postURL;

if (medium) {
mediumPost = await postToMedium(article, req.body.medium_userID, req.body.medium_token);
if (!mediumPost) {
logger.error("An Error Occured While Posting from Dev.to to Medium")
logger.error("An Error Occurred While Posting from Dev.to to Medium")
logger.info({ article, medium_userID: req.body.medium_userID, medium_token: req.body.medium_token })
return res.status(400).json({ "Error": "An Error Occured While Posting from Dev.to to Medium" });
return res.status(400).json({ "Error": "An Error Occurred While Posting from Dev.to to Medium" });
}

postURL = mediumPost.data.data.url;
}
if (hash) {
hashPost = await postToHashnode(article, req.body.hash_token, "dev");
if (!hashPost) {
logger.error("An Error Occured While Posting from Dev.to to Hashnode")
logger.error("An Error Occurred While Posting from Dev.to to Hashnode")
logger.info({ article, hash_token: req.body.hash_token, platform: "dev" })
return res.status(400).json({ "Error": "An Error Occured While Posting from Dev.to to Hashnode" });
return res.status(400).json({ "Error": "An Error Occurred While Posting from Dev.to to Hashnode" });
}
postURL = `https://hashnode.com/post/${hashPost.data.data.createStory.post.slug}-${hashPost.data.data.createStory.post.cuid}`;
}

if (hashPost || mediumPost) {
logger.info("Sucessfully Created")
return res.status(201).json({ "Message": "Sucessfully Created" });;
if (hashPost) {
logger.info("Successfully Created")
return res.status(201).json({ "Message": "Successfully Created", "hash_link": postURL });;
} else if (mediumPost) {
logger.info("Successfully Created")
return res.status(201).json({ "Message": "Successfully Created", "medium_link": postURL });;
}
logger.info("None Encountred")
return res.status(400).json({ "Error": "None Encountred" });
logger.info("None Encountered")
return res.status(400).json({ "Error": "None Encountered" });
} catch (error) {
logger.error(error)
return res.send(error);
Expand Down
26 changes: 16 additions & 10 deletions controller/postFromHash.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,40 @@ exports.postFromHash = async (req, res, next) => {
const hashArticle = result.data.data.post;
let devArticle;
let mediumArticle;
let postURL;

if (dev) {
devArticle = await postToDev(hashArticle, dev_api, "hash");
if (!devArticle) {
logger.info({ hashArticle, dev_api, platform: "hash" })
logger.error("An Error Occured While Posting on Dev.to from Hashnode")
return res.status(400).json({ "Message": "An Error Occured While Posting on Dev.to from Hashnode" });
logger.error("An Error Occurred While Posting on Dev.to from Hashnode")
return res.status(400).json({ "Message": "An Error Occurred While Posting on Dev.to from Hashnode" });
}
postURL = `${devArticle.data.url}/edit`;
}

if (medium) {
mediumArticle = await postToMedium(hashArticle, medium_id, medium_api, "hash")
if (!mediumArticle) {

logger.info({ hashArticle, medium_id, medium_api, platform: "medium" })
logger.error("An Error Occured While Posting on Medium from Hashnode")
return res.status(400).json({ "Message": "An Error Occured While Posting on Medium from Hashnode" });
logger.error("An Error Occurred While Posting on Medium from Hashnode")
return res.status(400).json({ "Message": "An Error Occurred While Posting on Medium from Hashnode" });
}
postURL = mediumArticle.data.data.url;
}

if (mediumArticle || devArticle) {
logger.info("Blog Sucessfully Posted")
return res.status(201).json({ "Message": "Blog Sucessfully Posted" });
if (mediumArticle) {
logger.info("Blog Successfully Posted")
return res.status(201).json({ "Message": "Blog Successfully Posted", "medium_link": postURL });
} else if (devArticle) {
logger.info("Blog Successfully Posted")
return res.status(201).json({ "Message": "Blog Successfully Posted", "dev_link": postURL });
}
logger.info("None Encountred")
return res.status(400).json({ "Error": "None Encountred" });
logger.info("None Encountered")
return res.status(400).json({ "Error": "None Encountered" });
} catch (error) {
logger.error(error)
return res.send(error);
}
}
}
35 changes: 20 additions & 15 deletions controller/postFromMedium.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { default: axios } = require("axios");
const {default: axios} = require("axios");
const postToDev = require("../services/postToDev");
const postToHashnode = require("../services/postToHashnode");

Expand All @@ -8,43 +8,48 @@ function mediumURLparser(URL) {
return result;
}

exports.postFromMedium = async(req, res, next) => {
try{
exports.postFromMedium = async (req, res, next) => {
try {
const {url, dev, hash, dev_api, hash_api} = req.body;
const link = mediumURLparser(url);
const {data} = await axios.get(link);
const {data} = await axios.get(link);
const feed = data;

const itemArr = feed.items;
const article = itemArr.filter(item => item.link.split("?")[0] === url);
let Devblog;
let hashBlog;
let postURL;

//TODO: Below empty array check not working
if(!article){
if (!article) {
res.status(400).json({"Error": "Blog Not found"});
}
//TODO: Array is comming in article
if(dev) {
//TODO: Array is coming in article
if (dev) {
Devblog = await postToDev(article[0], dev_api, "medium");
if(!Devblog){
if (!Devblog) {
return res.status(400).json({"Error": "Unable to publish to Dev.to From Medium"});
}
postURL = `${Devblog.data.url}/edit`;
}

if(hash){
if (hash) {
hashBlog = await postToHashnode(article[0], hash_api, "medium");
if(!hashBlog){
if (!hashBlog) {
return res.status(400).json({"Error": "Unable to publish to Hashnode from Medium"});
}
postURL = `https://hashnode.com/post/${hashBlog.data.data.createStory.post.slug}-${hashBlog.data.data.createStory.post.cuid}`;
}
if(hashBlog || Devblog){
return res.status(201).json({"Message": "Blog Sucessfully Posted"});
if (hashBlog) {
return res.status(201).json({"Message": "Blog Successfully Posted", "hash_link": postURL});
} else if (Devblog) {
return res.status(201).json({"Message": "Blog Successfully Posted", "dev_link": postURL});
}
return res.status(400).json({"Error": "None Encountred"});
} catch(error){
return res.status(400).json({"Error": "None Encountered"});
} catch (error) {
console.log(error);
return error;
}

}
}
2 changes: 1 addition & 1 deletion services/postToDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ module.exports = async function postToDev(article, token, platform) {
logger.error(error)
}

}
}
4 changes: 2 additions & 2 deletions services/postToHashnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = async function postToHashnode(articleBody, token, platform) {
"https://api.hashnode.com",
{
query:
"mutation createStory($input: CreateStoryInput!){ createStory(input: $input){ code success message } }",
"mutation createStory($input: CreateStoryInput!){ createStory(input: $input){ code success message post { slug cuid } } }",
variables: {
input: {
title: article.title,
Expand Down Expand Up @@ -67,4 +67,4 @@ module.exports = async function postToHashnode(articleBody, token, platform) {
} catch (error) {
logger.error(error)
}
}
}
2 changes: 1 addition & 1 deletion services/postToMedium.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ module.exports = async function postToMedium(article, userID, token, platform) {
} catch (error) {
logger.error(error)
}
}
}