-
-
Save isao/f0bea41e5d052800a52c22d16aded654 to your computer and use it in GitHub Desktop.
Full Github REST api in 34 lines of code
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
/* Ultra lightweight Github REST Client */ | |
// original inspiration via https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb | |
const token = 'github-token-here' | |
const githubClient = generateAPI('https://api.github.com', { | |
headers: { | |
'User-Agent': 'xyz', | |
'Authorization': `bearer ${token}` | |
} | |
}) | |
async function getRepo() { | |
/* GET /repos/{owner}/{repo} */ | |
return githubClient.repos.davidwells.analytics.get() | |
} | |
async function generateRepoFromTemplate({ template, repoName }) { | |
/* POST /repos/{template_owner}/{template_repo}/generate */ | |
return githubClient.repos[`${template}`].generate.post({ name: repoName }) | |
} | |
getRepo().then((repoInfo) => { | |
console.log('repo', repoInfo) | |
}) | |
function generateAPI(baseUrl, defaults = {}, scope = []) { | |
const callable = () => {} | |
callable.url = baseUrl | |
return new Proxy(callable, { | |
get({ url }, propKey) { | |
const method = propKey.toUpperCase() | |
const path = scope.concat(propKey) | |
if (['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].includes(method)) { | |
return (data, overrides = {}) => { | |
const payload = { method, ...defaults, ...overrides } | |
switch (method) { | |
case 'GET': { | |
if (data) url = `${url}?${new URLSearchParams(data)}` | |
break | |
} | |
case 'POST': | |
case 'PUT': | |
case 'PATCH': { | |
payload.body = JSON.stringify(data) | |
} | |
} | |
console.log(`Calling: ${url}`) | |
console.log('payload', payload) | |
return fetch(url, payload).then((d) => d.json()) | |
} | |
} | |
return generateAPI(`${url}/${propKey}`, defaults, path) | |
}, | |
apply({ url }, thisArg, [arg] = []) { | |
const path = url.split('/') | |
return generateAPI(arg ? `${url}/${arg}` : url, defaults, path) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment