Created
June 20, 2021 17:38
-
-
Save steveruizok/94f660c3b18dafdc5c4ea811d852ea23 to your computer and use it in GitHub Desktop.
Use the GitHub API to check whether a given user is sponsoring you.
This file contains hidden or 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
/** | |
* A function that will return whether the given user is sponsoring you. | |
* Requires a personal access token from GitHub. | |
* See https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token | |
* | |
* @param userA The sponsoring user. | |
*/ | |
async function isUserSponsoringMe(username: string) { | |
const res = await fetch('https://api.github.com/graphql', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `bearer ${process.env.YOUR_GITHUB_PERSONAL_ACCESS_TOKEN}`, | |
}, | |
body: JSON.stringify({ | |
query: ` | |
query { | |
user(login: "${process.env.YOUR_GITHUB_ID}") { | |
isSponsoredBy(accountLogin: "${username}") | |
} | |
} | |
`, | |
}), | |
}).then((res) => res.json()) | |
return res?.data?.user?.isSponsoredBy || false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment