Skip to content

Instantly share code, notes, and snippets.

@taktran
Created October 17, 2018 10:56
Show Gist options
  • Save taktran/fe910e93f1cf3b254c4b43a709dc057e to your computer and use it in GitHub Desktop.
Save taktran/fe910e93f1cf3b254c4b43a709dc057e to your computer and use it in GitHub Desktop.
const GITHUB_TEAM_API_URL_PREFIX = 'https://api.github.com/repos/SOME_TEAM';
const githubArchiveUrl = ({ repoName, branch }) =>
`${GITHUB_TEAM_API_URL_PREFIX}/${repoName}/tarball/${branch}`;
const getGithubArchiveRedirectUrl = ({ repoName, branch, githubToken }) => {
const url = githubArchiveUrl({ repoName, branch });
return fetch(url, {
headers: {
Authorization: `token ${githubToken}`
},
redirect: 'manual' // Don't follow redirect, just want the URL
}).then(res => {
if (res.status !== 302) {
throw new Error(`Unexpected response for ${url} (${status})`);
}
const { headers: { _headers: { location } } } = res;
const [ redirectUrl ] = location || [];
return redirectUrl;
});
};
// To call
await getGithubArchiveRedirectUrl({ repoName, branch, githubToken });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment