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
66export default class Board {
7- constructor ( slug ) {
8- this . slug = slug ;
9- }
7+ constructor ( slug ) {
8+ this . slug = slug ;
9+ }
1010
11- async boardData ( ) {
12- let slug = this . slug ;
13- let info = await fetch ( variables . graphql , {
14- method : 'POST' ,
15- headers,
16- body : JSON . stringify ( {
17- query : `
18- query Board($slug: String!) {
19- boardBySlug(slug: $slug) {
20- ${ variables . boardAttributes }
21- }
22- }` ,
23- variables : JSON . stringify ( {
24- slug : slug
25- } )
26- } )
27- } )
28- . then ( res => res . json ( ) ) ;
11+ async boardData ( ) {
12+ const { slug } = this ;
13+ const info = await fetch ( constants . graphql , {
14+ method : 'POST' ,
15+ headers,
16+ body : JSON . stringify ( {
17+ query : `
18+ query Board($slug: String!) {
19+ boardBySlug(slug: $slug) {
20+ ${ constants . boardAttributes }
21+ }
22+ }` ,
23+ variables : JSON . stringify ( {
24+ slug,
25+ } ) ,
26+ } ) ,
27+ } ) . then ( ( res ) => res . json ( ) ) ;
2928
30- if ( ! info . data . boardBySlug ) {
31- throw new Error ( `${ slug } is not a board. Please query boards on Repl.it.` ) ;
32- } else {
33- return info . data . boardBySlug ;
34- }
35- }
36-
37- async boardPosts ( after , count , order ) {
38- if ( ! after ) after = '' ;
39- if ( ! count ) count = 5 ;
40- if ( ! order ) order = '' ;
29+ if ( info . errors ) throw new Error ( `Replit GraphQL Error(s): ${ JSON . stringify ( info . errors ) } ` ) ;
4130
42- let slug = this . slug ;
43- let output = [ ] ;
31+ if ( ! info . data . boardBySlug ) {
32+ throw new Error ( `${ slug } is not a board. Please query boards on Replit.` ) ;
33+ } else {
34+ return info . data . boardBySlug ;
35+ }
36+ }
4437
45- async function recurse ( after ) {
46- if ( after === null ) return ;
38+ async boardPosts ( after = '' , count = 5 , order = '' ) {
39+ const { slug } = this ;
40+ const output = [ ] ;
4741
48- let info = await fetch ( variables . graphql , {
49- method : 'POST' ,
50- headers,
51- body : JSON . stringify ( {
52- query : `
42+ async function recurse ( recurseAfter ) {
43+ if ( recurseAfter === null ) return ;
44+
45+ const info = await fetch ( constants . graphql , {
46+ method : 'POST' ,
47+ headers,
48+ body : JSON . stringify ( {
49+ query : `
5350 query BoardPosts($slug: String!, $after: String!, $count: Int!, $order: String!) {
5451 boardBySlug(slug: $slug) {
5552 posts(count: $count, after: $after, order: $order) {
56- items { ${ variables . postAttributes } }
53+ items { ${ constants . postAttributes } }
5754 pageInfo {
5855 nextCursor
5956 }
6057 }
6158 }
6259 }` ,
63- variables : JSON . stringify ( {
64- slug : slug ,
65- after : after ,
66- count : count ,
67- order : order
68- } )
69- } )
70- } ) . then ( res => res . json ( ) ) ;
60+ variables : JSON . stringify ( {
61+ slug,
62+ recurseAfter,
63+ count,
64+ order,
65+ } ) ,
66+ } ) ,
67+ } ) . then ( ( res ) => res . json ( ) ) ;
68+
69+ if ( info . errors ) throw new Error ( `Replit GraphQL Error(s): ${ JSON . stringify ( info . errors ) } ` ) ;
7170
72- if ( ! info . data . boardBySlug ) {
73- throw new Error (
74- `${ slug } is not a board. Please query boards on Repl.it.`
75- ) ;
76- } else {
77- info . data . boardBySlug . posts . items . forEach ( post => {
78- output . push ( post ) ;
79- } ) ;
80- if ( output . length != count ) {
81- await recurse ( info . data . boardBySlug . posts . pageInfo . nextCursor ) ;
82- }
83- }
84- }
71+ if ( ! info . data . boardBySlug ) {
72+ throw new Error (
73+ `${ slug } is not a board. Please query boards on Replit.` ,
74+ ) ;
75+ } else {
76+ info . data . boardBySlug . posts . items . forEach ( ( post ) => {
77+ output . push ( post ) ;
78+ } ) ;
79+ if ( output . length != = count ) {
80+ await recurse ( info . data . boardBySlug . posts . pageInfo . nextCursor ) ;
81+ }
82+ }
83+ }
8584
86- await recurse ( after ) ;
87- return output ;
88- }
89- }
85+ await recurse ( after ) ;
86+ return output ;
87+ }
88+ }
0 commit comments