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

Commit 739ab43

Browse files
author
Rayhan Arayilakath
committed
Refactor User Class
1 parent 793559b commit 739ab43

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ let _defaultInitVariables = {
99
length: undefined,
1010
removeMarkdown: undefined
1111
}
12+
previewCount: {
13+
comments: undefined
14+
}
1215
}
1316

1417
module.exports = function(initVariables) {
@@ -17,12 +20,11 @@ module.exports = function(initVariables) {
1720
if(typeof value == 'object') {
1821
for([nestedKey, nestedValue] of Object.entries(value)) {
1922
if(typeof nestedValue != 'string' && typeof nestedValue != 'number') throw new Error(`Invalid type for value of ${nestedKey}.`);
20-
else _defaultInitVariables[key][nestedKey] = nestedValue;
23+
else if(_defaultInitVariables[key][nestedKey]) _defaultInitVariables[key][nestedKey] = nestedValue;
2124
}
2225
} else {
23-
console.log(typeof value)
2426
if(typeof value != 'string' && typeof value != 'number') throw new Error(`Invalid type for value of ${key}.`);
25-
else _defaultInitVariables[key] = value;
27+
else if(_defaultInitVariables[key]) _defaultInitVariables[key] = value;
2628
}
2729
}
2830
global.initVariables = _defaultInitVariables

src/classes/User.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class User {
3333
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
3434

3535
if (!info.data.userByUsername) {
36-
throw new Error(`${user} is not a user. Please query users on Repl.it.`);
36+
throw new Error(`${user} is not a user. Please query users on Replit.`);
3737
} else {
3838
return info.data.userByUsername;
3939
}
@@ -64,10 +64,10 @@ class User {
6464
user { ${constants.userAttributes} },
6565
board { ${constants.boardAttributes} },
6666
repl { ${constants.replAttributes} },
67-
comments(count: 10) { ${constants.commentAttributes} },
67+
comments(count: ${global.initVariables.previewCount.comments || 10}) { ${constants.commentAttributes} },
6868
votes { id, user { ${constants.userAttributes} } },
6969
answeredBy { ${constants.userAttributes} },
70-
answer { ${constants.commentAttributes} }
70+
answer { ${ constants.commentAttributes} }
7171
}
7272
pageInfo {
7373
nextCursor
@@ -85,9 +85,11 @@ class User {
8585
})
8686
.then(res => res.json());
8787

88+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
89+
8890
if (!info.data.userByUsername) {
8991
throw new Error(
90-
`${user} is not a user. Please query users on Repl.it.`
92+
`${user} is not a user. Please query users on Replit.`
9193
);
9294
} else {
9395
info.data.userByUsername.posts.items.forEach(post => {
@@ -123,7 +125,22 @@ class User {
123125
query UserComment($user: String!, $after: String!, $count: Int!, $order: String!) {
124126
userByUsername(username: $user) {
125127
comments(count: $count, after: $after, order: $order) {
126-
items { ${variables.commentAttributes} }
128+
items {
129+
${constants.commentAttributes},
130+
parentComment { ${constants.commentAttributes} },
131+
comments(count: ${global.initVariables.previewCount.comments || 10}) { ${constants.commentAttributes} },
132+
user { ${constants.userAttributes} },
133+
post {
134+
${constants.postAttributes},
135+
user { ${constants.userAttributes} },
136+
board { ${constants.boardAttributes} },
137+
repl { ${constants.replAttributes} },
138+
comments(count: ${global.initVariables.previewCount.comments || 10}) { ${constants.commentAttributes} },
139+
votes { id, user { ${constants.userAttributes} } },
140+
answeredBy { ${constants.userAttributes} },
141+
answer { ${ constants.commentAttributes} }
142+
}
143+
}
127144
pageInfo {
128145
nextCursor
129146
}
@@ -140,9 +157,11 @@ class User {
140157
})
141158
.then(res => res.json());
142159

160+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
161+
143162
if (!info.data.userByUsername) {
144163
throw new Error(
145-
`${user} is not a user. Please query users on Repl.it.`
164+
`${user} is not a user. Please query users on Replit.`
146165
);
147166
} else {
148167
info.data.userByUsername.comments.items.forEach(comment => {

0 commit comments

Comments
 (0)