|
| 1 | +let headers = require('../utils/headers.js'); |
| 2 | +let variables = require('../utils/variables.js'); |
| 3 | + |
| 4 | +class Board { |
| 5 | + constructor(slug) { |
| 6 | + this.slug = slug; |
| 7 | + } |
| 8 | + |
| 9 | + async boardData() { |
| 10 | + let slug = this.slug; |
| 11 | + let info = await variables |
| 12 | + .fetch('https://staging.repl.it/graphql', { |
| 13 | + method: 'POST', |
| 14 | + headers, |
| 15 | + body: JSON.stringify({ |
| 16 | + query: ` |
| 17 | + query Board($slug: String!) { |
| 18 | + boardBySlug(slug: $slug) { |
| 19 | + ${variables.boardAttributes} |
| 20 | + } |
| 21 | + }`, |
| 22 | + variables: { |
| 23 | + slug |
| 24 | + } |
| 25 | + }) |
| 26 | + }) |
| 27 | + .then(res => res.json()); |
| 28 | + |
| 29 | + if (!info.data.boardBySlug) { |
| 30 | + throw new Error(`${slug} is not a board. Please query boards on Repl.it.`); |
| 31 | + } else { |
| 32 | + return info.data.boardBySlug; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + async boardPosts(after, count, order) { |
| 37 | + if (!after) after = ''; |
| 38 | + if (!count) count = 5; |
| 39 | + if (!order) order = ''; |
| 40 | + |
| 41 | + let slug = this.slug; |
| 42 | + let output = []; |
| 43 | + |
| 44 | + async function recurse(after) { |
| 45 | + if (after === null) return; |
| 46 | + |
| 47 | + let info = await variables |
| 48 | + .fetch('https://staging.repl.it/graphql', { |
| 49 | + method: 'POST', |
| 50 | + headers, |
| 51 | + body: JSON.stringify({ |
| 52 | + query: ` |
| 53 | + query BoardPosts($slug: String!, $after: String!, $count: Int!, $order: String!) { |
| 54 | + boardBySlug(slug: $slug) { |
| 55 | + posts(count: $count, after: $after, order: $order) { |
| 56 | + items { ${variables.postAttributes} } |
| 57 | + pageInfo { |
| 58 | + nextCursor |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }`, |
| 63 | + variables: { |
| 64 | + slug, |
| 65 | + after, |
| 66 | + count, |
| 67 | + order |
| 68 | + } |
| 69 | + }) |
| 70 | + }).then(res => res.json()); |
| 71 | + |
| 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 | + } |
| 85 | + |
| 86 | + await recurse(after); |
| 87 | + return output; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +module.exports = { |
| 92 | + Board: Board |
| 93 | +}; |
0 commit comments