Skip to content

Instantly share code, notes, and snippets.

@roxeteer
Last active November 25, 2018 08:56
Show Gist options
  • Save roxeteer/cde1d1d9bc158b6f8801257815509998 to your computer and use it in GitHub Desktop.
Save roxeteer/cde1d1d9bc158b6f8801257815509998 to your computer and use it in GitHub Desktop.
const got = require('got');
// When using Architect framework, you can share code between functions
// Read the documentation at https://arc.codes/guides/sharing-common-code
const { getGhostToken } = require('@architect/shared/ghost_auth');
async function createPost(postData) {
const { GHOST_NAME } = process.env;
const tokenData = await getGhostToken();
const headers = {
'Authorization': `${tokenData.token_type} ${tokenData.access_token}`
};
const body = {
posts: [
{
title: '', // make sure title exists, it's a required field
...postData
}
]
};
let result;
try {
result = await got.post(
`https://${GHOST_NAME}.ghost.io/ghost/api/v0.1/posts`,
{
headers,
body,
json: true
}
).on('error', (err) => {
console.error(err);
throw new Error(err.message);
});
} catch (e) {
console.error(e);
throw e;
}
return result.body.posts[0];
}
module.exports = {
createPost
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment