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

Commit d920431

Browse files
author
Ray Arayilakath
committed
Last lint, checked all query functions before release.
1 parent 5030df1 commit d920431

File tree

10 files changed

+156
-105
lines changed

10 files changed

+156
-105
lines changed

index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaultInitVariables = {
1717
previewCount: {
1818
comments: undefined,
1919
},
20+
experimentalFeatures: undefined,
2021
};
2122

2223
export default function ReplAPI(initVariables) {
@@ -43,16 +44,17 @@ export default function ReplAPI(initVariables) {
4344

4445
return {
4546
defaults: global.initVariables,
46-
User: replapi.User,
47-
Post: replapi.Post,
48-
Repl: replapi.Repl,
49-
Comment: replapi.Comment,
50-
Leaderboard: replapi.Leaderboard,
51-
Languages: replapi.Languages,
5247
Board: replapi.Board,
53-
Notifications: replapi.Notifications,
54-
Login: replapi.Login,
48+
Comment: replapi.Comment,
5549
CustomDataQuery: replapi.CustomDataQuery,
5650
CustomRecursiveQuery: replapi.CustomRecursiveQuery,
51+
Database: replapi.Database,
52+
Languages: replapi.Languages,
53+
Leaderboard: replapi.Leaderboard,
54+
Login: replapi.Login,
55+
Notifications: replapi.Notifications,
56+
Post: replapi.Post,
57+
Repl: replapi.Repl,
58+
User: replapi.User,
5759
};
5860
}

src/classes/Board.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export default class Board {
6363
}`,
6464
variables: JSON.stringify({
6565
slug,
66-
recurseAfter,
6766
count,
6867
order,
68+
after: recurseAfter,
6969
}),
7070
}),
7171
}).then((res) => res.json());

src/classes/Database.js

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,66 @@
1-
import fetch from 'node-fetch'
1+
import fetch from 'node-fetch';
22

3-
import headers from '../utils/headers.js'
4-
import constants from '../utils/constants.js'
3+
import headers from '../utils/headers.js';
4+
import constants from '../utils/constants.js';
55

6-
function _hash(value, salt) {
7-
let hash = crypto.createHmac('sha512', salt);
8-
hash.update(value);
9-
let result = hash.digest('hex');
10-
return {
11-
salt: salt,
12-
hashedpassword: result
13-
};
6+
function hash(value, salt) {
7+
const hash = crypto.createHmac('sha512', salt);
8+
hash.update(value);
9+
const result = hash.digest('hex');
10+
return {
11+
salt,
12+
hashedpassword: result,
13+
};
1414
}
1515

16-
function _compare(value, hashData) {
17-
let resultData = _hash(value, hashData.salt);
18-
if (resultData.hashedpassword === hashData.hashedpassword) {
19-
return true;
20-
}
21-
return false;
16+
function compare(value, hashData) {
17+
const resultData = _hash(value, hashData.salt);
18+
if (resultData.hashedpassword === hashData.hashedpassword) {
19+
return true;
20+
}
21+
return false;
2222
}
2323

24-
export default class Database {
25-
constructor(replitdbtoken, salt, options) {
26-
this.replitdbtoken = replitdbtoken || process.env.REPLIT_DB_URL.split('/')[4];
27-
this.salt = salt;
28-
this.options = options;
29-
}
24+
let exportable;
3025

31-
async set(key, value) {
32-
let info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}`, {
33-
method: 'POST',
34-
headers,
35-
body: encodeURIComponent(key) + '=' + encodeURIComponent(JSON.stringify(value))
36-
}).then(res => res.json());
37-
}
26+
if (global.initVariables.experimentalFeatures) {
27+
let exportable = class Database {
28+
constructor(replitdbtoken, salt, options) {
29+
this.replitdbtoken = replitdbtoken || process.env.REPLIT_DB_URL.split('/')[4];
30+
this.salt = salt;
31+
this.options = options;
32+
}
3833

39-
async get(key, ) {
40-
let info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}/${key}`, {
41-
method: 'GET',
42-
headers
43-
}).then(res => {
44-
res.json()
45-
});
46-
}
47-
48-
async delete(key) {
49-
let info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}/${key}`, {
50-
method: 'GET',
51-
headers
52-
}).then(res => {
53-
res.json()
54-
});
55-
}
56-
}
34+
async set(key, value) {
35+
const info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}`, {
36+
method: 'POST',
37+
headers,
38+
body: `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`,
39+
}).then((res) => res.json());
40+
}
41+
42+
async get(key) {
43+
const info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}/${key}`, {
44+
method: 'GET',
45+
headers,
46+
}).then((res) => {
47+
res.json();
48+
});
49+
}
50+
51+
async delete(key) {
52+
const info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}/${key}`, {
53+
method: 'GET',
54+
headers,
55+
}).then((res) => {
56+
res.json();
57+
});
58+
}
59+
}
60+
} else {
61+
exportable = function noExperimentalFeatures() {
62+
console.log('Experimental Features are not enabled. To use learn more about experimental features please visit the documentation.');
63+
};
64+
}
65+
66+
export default exportable;

src/classes/Languages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export default class Languages {
3030
this.lang = lang;
3131
}
3232

33-
async getLang() {
33+
async langData() {
3434
const langs = await fetchVariable();
3535
return langs[this.lang];
3636
}
3737

38-
async getAllLangs() {
38+
async langDataAll() {
3939
const langs = await fetchVariable();
4040
return langs;
4141
}

src/classes/Leaderboard.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default class Leaderboard {
1818
}
1919
}`;
2020
variables = {
21-
after,
2221
count,
2322
since,
2423
};
@@ -33,7 +32,6 @@ export default class Leaderboard {
3332
}
3433
}`;
3534
variables = {
36-
after,
3735
count,
3836
};
3937
}
@@ -48,10 +46,12 @@ export default class Leaderboard {
4846
headers,
4947
body: JSON.stringify({
5048
query,
51-
variables: JSON.stringify(variables),
49+
variables: JSON.stringify({
50+
...variables,
51+
after: recurseAfter,
52+
}),
5253
}),
53-
})
54-
.then((res) => res.json());
54+
}).then((res) => res.json());
5555

5656
if (!info.data.leaderboard) {
5757
throw new Error('Cannot fetch leaderboard');

src/classes/Notifications.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import constants from '../utils/constants.js';
55

66
let exportable;
77

8-
if (global.initVariables.experimental) {
8+
if (global.initVariables.experimentalFeatures) {
99
exportable = class Notifications {
1010
async postReplyNotification(after, count) {
1111
if (!global.cookies) {

src/classes/Post.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,39 @@ export default class Post {
397397
);
398398
}
399399
}
400+
401+
async posts(after = '', count = 10, order = '') {
402+
const info = await fetch(constants.graphql, {
403+
method: 'POST',
404+
headers,
405+
body: JSON.stringify({
406+
query: `
407+
query Posts($after: String!, $count: Int!, $order: String!) {
408+
posts(after: $after, count: $count, order: $order) {
409+
items {
410+
id
411+
title
412+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
413+
}
414+
pageInfo {
415+
nextCursor
416+
}
417+
}
418+
}`,
419+
variables: JSON.stringify({
420+
after,
421+
count,
422+
order,
423+
}),
424+
}),
425+
}).then((res) => res.json());
426+
427+
if (info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`);
428+
429+
if (!info.data.posts) {
430+
throw new Error('Could not fetch posts.');
431+
} else {
432+
return info.data.posts;
433+
}
434+
}
400435
}

src/classes/Repl.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export default class Repl {
2121
}
2222

2323
async replGraphQLData() {
24-
const { username } = this;
25-
const { slug } = this;
24+
const { username, slug } = this;
2625

2726
const id = await getReplId(username, slug);
2827
const info = await fetch(constants.graphql, {
@@ -53,8 +52,7 @@ export default class Repl {
5352
}
5453

5554
async replRESTData() {
56-
const { username } = this;
57-
const { slug } = this;
55+
const { username, slug } = this;
5856

5957
const info = await fetch(`${constants.restful}/data/repls/@${username}/${slug}`, {
6058
method: 'GET',
@@ -70,8 +68,7 @@ export default class Repl {
7068
}
7169

7270
async replLangs() {
73-
const { username } = this;
74-
const { slug } = this;
71+
const { username, slug } = this;
7572

7673
const info = await fetch(`https://replangs.rayhanadev.repl.co/${username}/${slug}`, {
7774
method: 'GET',
@@ -86,15 +83,20 @@ export default class Repl {
8683
}
8784

8885
async replTitleGen() {
89-
const info = await fetch(constants.graphql, {
90-
method: 'POST',
91-
headers,
92-
body: JSON.stringify({
93-
query: '{ replTitle }',
94-
}),
95-
}).then((res) => res.json());
96-
97-
if (info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`);
98-
else return info.data.replTitle;
86+
if (!global.cookies) {
87+
throw new Error('ReplAPI.it: Not logged in.');
88+
} else {
89+
headers['Set-Cookie'] = global.cookies;
90+
const info = await fetch(constants.graphql, {
91+
method: 'POST',
92+
headers,
93+
body: JSON.stringify({
94+
query: '{ replTitle }',
95+
}),
96+
}).then((res) => res.json());
97+
98+
if (info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`);
99+
else return info.data.replTitle;
100+
}
99101
}
100102
}

0 commit comments

Comments
 (0)