Created
February 27, 2024 01:17
-
-
Save Lissy93/2568a009cf1c9b7ecb801307ebb4ac7b to your computer and use it in GitHub Desktop.
A quick and easy Cloudflare Worker, for fetching all the GitHub Sponsors of a given user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
async function handleRequest(request) { | |
const { pathname } = new URL(request.url); | |
const username = pathname.split('/')[1]; | |
if (!username) { | |
return new Response( | |
'Simple API to fetch GitHub Sponsors of a given user 💖\nUsage: GET /[username]', | |
{ status: 200 } | |
); | |
} | |
const query = ` | |
query { | |
user(login: "${username}") { | |
sponsorshipsAsMaintainer(first: 100) { | |
edges { | |
node { | |
sponsorEntity { | |
... on User { | |
login | |
name | |
avatarUrl | |
} | |
... on Organization { | |
name | |
avatarUrl | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`; | |
try { | |
const apiResponse = await fetch('https://api.github.com/graphql', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `bearer ${GH_API_TOKEN}`, | |
'User-Agent': 'GetUserSponsors' | |
}, | |
body: JSON.stringify({ query }) | |
}); | |
const result = await apiResponse.json(); | |
if (!apiResponse.ok) { | |
throw new Error(result.errors ? result.errors.map(e => e.message).join('; ') : 'Failed to fetch GitHub data'); | |
} | |
// Extracting sponsor details | |
const sponsors = result.data.user.sponsorshipsAsMaintainer.edges.map(edge => edge.node.sponsorEntity); | |
return new Response(JSON.stringify(sponsors), { | |
headers: { 'Content-Type': 'application/json' }, | |
}); | |
} catch (error) { | |
return new Response(error.message, { status: 500 }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About
This is a very simple API, in the form of a Cloudflare Worker, which lets you request a list of all sponsors for a given GitHub user.
There's no CORS, so should work from anywhere, no auth, and just a single easy HTTP GET request.
Why? Because currently, GitHub does not provide a REST endpoint for fetching sponsors. Instead you must use their GraphQL API, which will not work for frontend applications.
For example, I'm using this here to thank the sponsors of my project (using this code).
Usage
Just send a GET request to
http://github-sponsors-api.as93.net/[username]
The response will be an array of users, with each object containing
login
(GitHub username),name
(user's full name) andavatarUrl
(the path to their profile picture). For example:Example: http://github-sponsors-api.as93.net/lissy93
Deployment
npm i -g wrangler
wrangler init sponsors-api
cd sponsors-api
andnpm install
wrangler.toml
add your API key to theGITHUB_TOKEN
env varwrangler dev
wrangler deploy
to deploy your app to the world!Alternatively, just log in to Cloudflare, navigate to "Workers" and then "New" / "Quick Edit".
And then paste the above script in, and then hit "Save & Deploy". Done :)
You can now make requests to
https://[project-name].workers.dev/[user]
🚀License
Licensed under MIT, © Alicia Sykes 2024