Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
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
59 changes: 59 additions & 0 deletions src/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const fetch = require('node-fetch');

const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'keep-alive',
'X-Requested-With': 'REPLAPIit',
Referrer: 'https://repl.it',
Origin: 'https://repl.it'
};

let roleAttributes = `id, name, key, tagline`;
let languageAttributes = `id, displayName, key, category, tagline, icon, isNew`;

let userAttributes = `id, username, url, image, karma, firstName, lastName, fullName, displayName, isLoggedIn, bio, timeCreated, organization { name }, subscription { planId }, languages { ${languageAttributes} }, roles { ${roleAttributes} }`;
let boardAttributes = `id, url, slug, cta, titleCta, bodyCta, buttonCta, description, name, replRequired, isLocked, isPrivate`;
let replAttributes = `id, hostedUrl, title, lang { ${languageAttributes} }, language, timeCreated`;
let commentAttributes = `id, body, voteCount, timeCreated, timeUpdated, user { ${userAttributes} }, url, post { id }, parentComment { id }, comments { id }, isAuthor, canEdit, canVote, canComment, hasVoted, canReport, hasReported, isAnswer, canSelectAsAnswer, canUnselectAsAnswer, preview(removeMarkdown: true, length: 150)`;
let postAttributes = `id, title, body, showHosted, voteCount, commentCount, isPinned, isLocked, timeCreated, timeUpdated, url, user { ${userAttributes} }, board { ${boardAttributes} }, repl { ${replAttributes} }, isAnnouncement, isAuthor, canEdit, canComment, canVote, canPin, canSetType, canChangeBoard, canLock, hasVoted, canReport, hasReported, isAnswered, isAnswerable, answeredBy { ${userAttributes} }, answer { ${commentAttributes} }, tutorialPages, preview(removeMarkdown: true, length: 150)`;

class Post {
constructor(id, filter) {
this.id = id;
}

async postData() {
let id = this.id;
if (typeof id != 'number') {
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
}

let info = await fetch('https://repl.it/graphql', {
method: 'POST',
headers,
body: JSON.stringify({
query: `
query Post($id: Int!) {
post(id: $id) {
${postAttributes}
}
}`,
variables: {
id
}
})
}).then(res => res.json());

if (!info.data.post) {
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
} else {
return info.data.post;
}
}
}

module.exports = {
Post: Post
};