|
| 1 | +// This is used to ensure that URLs are always in the correct format |
| 2 | +function fixUrl(url) { |
| 3 | + if (!url) { |
| 4 | + return null; |
| 5 | + } |
| 6 | + |
| 7 | + if (!url.startsWith('http://') && !url.startsWith('https://')) { |
| 8 | + return `https://${url}`; |
| 9 | + } |
| 10 | + |
| 11 | + return url; |
| 12 | +} |
| 13 | + |
| 14 | +async function fetchOpenCollectiveData() { |
| 15 | + const endpoint = 'https://api.opencollective.com/graphql/v2'; |
| 16 | + |
| 17 | + const query = `{ |
| 18 | + account(slug: "nodejs") { |
| 19 | + orders(status: ACTIVE, filter: INCOMING) { |
| 20 | + totalCount |
| 21 | + nodes { |
| 22 | + fromAccount { |
| 23 | + name |
| 24 | + website |
| 25 | + imageUrl |
| 26 | + } |
| 27 | + amount { |
| 28 | + value |
| 29 | + } |
| 30 | + tier { |
| 31 | + slug |
| 32 | + } |
| 33 | + frequency |
| 34 | + totalDonations { |
| 35 | + value |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + donations: orders( |
| 41 | + account: { slug: "nodejs" } |
| 42 | + frequency: ONETIME |
| 43 | + status: PAID |
| 44 | + filter: INCOMING |
| 45 | + ) { |
| 46 | + totalCount |
| 47 | + nodes { |
| 48 | + id |
| 49 | + updatedAt |
| 50 | + frequency |
| 51 | + status |
| 52 | + amount { |
| 53 | + value |
| 54 | + currency |
| 55 | + } |
| 56 | + fromAccount { |
| 57 | + name |
| 58 | + website |
| 59 | + imageUrl |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + }`; |
| 64 | + |
| 65 | + const response = await fetch(endpoint, { |
| 66 | + method: 'POST', |
| 67 | + headers: { 'Content-Type': 'application/json' }, |
| 68 | + body: JSON.stringify({ query }), |
| 69 | + }); |
| 70 | + |
| 71 | + const payload = await response.json(); |
| 72 | + |
| 73 | + const sponsors = payload.data.account.orders.nodes.map(order => ({ |
| 74 | + name: order.fromAccount.name, |
| 75 | + url: fixUrl(order.fromAccount.website), |
| 76 | + image: order.fromAccount.imageUrl, |
| 77 | + source: 'opencollective', |
| 78 | + })); |
| 79 | + |
| 80 | + const donations = payload.data.donations.nodes.map(transaction => ({ |
| 81 | + name: transaction.fromAccount.name, |
| 82 | + url: fixUrl(transaction.fromAccount.website), |
| 83 | + image: transaction.fromAccount.imageUrl, |
| 84 | + source: 'opencollective', |
| 85 | + })); |
| 86 | + |
| 87 | + sponsors.push(...donations); |
| 88 | + |
| 89 | + return sponsors; |
| 90 | +} |
| 91 | + |
| 92 | +// TODO: implement github sponsors data fetching |
| 93 | +// TODO: implement ramdomizing of supporters |
| 94 | + |
| 95 | +export { fetchOpenCollectiveData }; |
0 commit comments