Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 4988ee8

Browse files
authored
Create Post.js
1 parent 5ce2ee7 commit 4988ee8

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

src/Post.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const fetch = require('node-fetch');
2+
3+
const headers = {
4+
'Content-Type': 'application/json',
5+
Accept: 'application/json',
6+
'Accept-Encoding': 'gzip, deflate, br',
7+
Connection: 'keep-alive',
8+
'X-Requested-With': 'REPLAPIit',
9+
Referrer: 'https://repl.it',
10+
Origin: 'https://repl.it'
11+
};
12+
13+
let roleAttributes = `id, name, key, tagline`;
14+
let languageAttributes = `id, displayName, key, category, tagline, icon, isNew`;
15+
16+
let userAttributes = `id, username, url, image, karma, firstName, lastName, fullName, displayName, isLoggedIn, bio, timeCreated, organization { name }, subscription { planId }, languages { ${languageAttributes} }, roles { ${roleAttributes} }`;
17+
let boardAttributes = `id, url, slug, cta, titleCta, bodyCta, buttonCta, description, name, replRequired, isLocked, isPrivate`;
18+
let replAttributes = `id, hostedUrl, title, lang { ${languageAttributes} }, language, timeCreated`;
19+
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)`;
20+
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)`;
21+
22+
class Post {
23+
constructor(id, filter) {
24+
this.id = id;
25+
}
26+
27+
async postData() {
28+
let id = this.id;
29+
if (typeof id != 'number') {
30+
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
31+
}
32+
33+
let info = await fetch('https://repl.it/graphql', {
34+
method: 'POST',
35+
headers,
36+
body: JSON.stringify({
37+
query: `
38+
query Post($id: Int!) {
39+
post(id: $id) {
40+
${postAttributes}
41+
}
42+
}`,
43+
variables: {
44+
id
45+
}
46+
})
47+
}).then(res => res.json());
48+
49+
if (!info.data.post) {
50+
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
51+
} else {
52+
return info.data.post;
53+
}
54+
}
55+
}
56+
57+
module.exports = {
58+
Post: Post
59+
};

0 commit comments

Comments
 (0)