repl-talk.js
Version:
An easy to use API to access data relating to users and posts on https://repl.it.
216 lines (184 loc) • 7.39 kB
JavaScript
/*
Copyright 2019 Leon Davis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const req = require("request");
/* Client */
module.exports.Client = class {
// Authenticate using repl login
login(username, password, cback) {
// Login request
req.post("https://repl.it/login", {
headers: {
"X-Requested-With": "XMLHttpRequest",
"Referrer": "https://repl.it"
},
json: {
"username":username,
"password":password
}
},
(err, res, body) => {
// If there was an error authenticating
if(body.message) return console.log(body.message);
// There was no error authenticating
/*------ Variables ------*/
/* Constants */
this.ALL_FROM_USER = ["id", "username", "firstName", "lastName", "bio", "isVerified", "displayName", "fullName", "url", "isLoggedIn", "isSubscribed", "timeCreated", "karma"];
this.ALL_FROM_ROLE = ["id", "key", "name", "tagline"];
this.ALL_FROM_LANGUAGE = ["id", "displayName", "key", "category", "tagline", "icon", "isNew"];
this.ALL_FROM_REPL = ["id", "language", "isProject", "isPrivate", "isStarred", "title", "slug", "description", "folderId", "isRenamed", "languageDisplayName", "url", "timeCreated", "timeUpdated", "isOwner", "pinnedToProfile", "canWrite", "screenshot", "files", "hostedUrl", "terminalUrl"];
this.ALL_FROM_ENTERPRISE = ["id", "organizationId", "startDate", "endDate", "teacherSeats", "studentSeats"];
this.ALL_FROM_ORGANIZATION = ["id", "name", "country", "postalCode", "state", "city", "googlePlaceId", "timeCreated", "timeUpdated", "timeDeleted", "time_created"];
this.ALL_FROM_POST = ["id", "title", "body", "showHosted", "voteCount", "commentCount", "isPinned", "isLocked", "timeCreated", "timeUpdated", "url", "isAnnouncement", "isAuthor", "canEdit", "canComment", "canVote", "canPin", "canSetType", "canChangeBoard", "canLock", "hasVoted", "canReport", "hasReported", "isAnswered", "isAnswerable", "tutorialPages", "preview"];
this.ALL_FROM_BOARD = ["id", "name", "description", "slug", "cta", "titleCta", "bodyCta", "template", "buttonCta", "color", "replRequired", "isLocked", "isAnswerable", "isPrivate", "timeCreated", "timeUpdated", "url", "canPost"];
this.ALL_FROM_COMMENT = ["id", "body", "voteCount", "timeCreated", "timeUpdated", "url", "isAuthor", "canEdit", "canVote", "canComment", "hasVoted", "canReport", "hasReported", "isAnswer", "canSelectAsAnswer", "canUnselectAsAnswer", "preview"];
/* Board Ids */
this.ASK = 6;
this.SHARE = 6;
this.ANNOUNCEMENTS = 14;
this.CHALLENGE = 16;
this.LEARN = 17;
/*------ Items ------*/
/* From */
this.from = function(use) {
/* use
{
"from": "thing",
"get": ["types"]
}
*/
return `${use.from}{${use.get.map(item => {return item})}}`;
}
/* Roles */
this.roles = function(items=["id", "name", "tagline"]) {
return this.from({
from: "roles",
get: items
});
}
/* Languages */
this.languages = function(items=["id", "key", "category", "tagline", "isNew"]) {
return this.from({
from: "languages",
get: items
});
}
/* Posts */
this.posts = function(items=["id", "title", "body", "voteCount", "timeCreated", "isAnswered", "preview"]) {
return `posts{${this.from({
from: "items",
get: items
})}}`;
}
/* Comments */
this.comments = function(items=["id", "body", "voteCount", "timeCreated", "isAnswer", "preview"]) {
return `comments{${this.from({
from: "items",
get: items
})}}`;
}
/*------ Queries ------*/
/* Query */
this.query = function(use, cback) {
/* use
{
"from": "thing",
"using": {"type": "value"},
"get": ["types"]
}
*/
req.post("https://repl.it/graphql", {
headers: {
"X-Requested-With": "XMLHttpRequest",
"Referrer": "https://repl.it"
},
json: {
query: `query{${use.from}(${Object.keys(use.using).map(type => {return `${type}: ${use.using[type]}`}).toString()}){${use.get.map(type => {return type})}}}`
}
},
(_, __, body) => {
// There was an error
if(body.errors) return console.error(`repl-talk.js error: ${body.errors[0].message}`);
// The data was received
cback(body.data[use.from])
});
}
/* Users */
this.user = function(id, cback, items=["id", "username", "karma", "bio", "timeCreated", "isSubscribed"]) {
if(!isNaN(id)) {
this.query({
"from": "user",
"using": {"id": id},
"get": items
}, data => cback(data));
}
else {
this.query({
"from": "userByUsername",
"using": {"username": `"${id}"`},
"get": items
}, data => cback(data));
}
}
/* Repl */
this.repl = function(id, cback, items=["id", "language", "title", "description", "timeCreated", "timeUpdated"]) {
this.query({
"from": "repl",
"using": {"id": `"${id}"`},
"get": items
}, data => cback(data));
}
/* Enterprise */
this.enterprise = function(id, cback, items=["id", "organizationId", "startDate", "endDate"]) {
this.query({
"from": "enterprise",
"using": {"id": id},
"get": items
}, data => cback(data));
}
/* Organization */
this.organization = function(id, cback, items=["id", "name", "country", "city", "timeCreated"]) {
this.query({
"from": "organization",
"using": {"id": id},
"get": items
}, data => cback(data));
}
/* Post */
this.post = function(id, cback, items=["id", "title", "body", "voteCount", "timeCreated", "isAnswered", "preview"]) {
this.query({
"from": "post",
"using": {"id": id},
"get": items
}, data => cback(data));
}
/* Board */
this.board = function(id, cback, items=["id", "name", "description", "timeCreated"]) {
this.query({
"from": "board",
"using": {"id": id},
"get": items
}, data => cback(data));
}
/* Comment */
this.comment = function(id, cback, items=["id", "body", "voteCount", "timeCreated", "isAnswer", "preview"]) {
this.query({
"from": "comment",
"using": {"id": id},
"get": items
}, data => cback(data));
}
// Run the cback function
cback(body);
});
}
}