-
-
Save davidbgk/0c33feb19d301d5d58bc0eabc55b9de6 to your computer and use it in GitHub Desktop.
Revisions
-
DavidWells revised this gist
Mar 11, 2022 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ /* 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: { -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ function generateAPI(baseUrl, defaults = {}, scope = []) { 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)}` -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ async function getRepo() { async function generateRepoFromTemplate({ template, repoName }) { /* POST /repos/{template_owner}/{template_repo}/generate */ return githubClient.repos[`${template}`].generate.post({ name: repoName }) } getRepo().then((repoInfo) => { @@ -42,7 +42,7 @@ function generateAPI(baseUrl, defaults = {}, scope = []) { payload.body = JSON.stringify(data) } } console.log(`Calling: ${url}`) console.log('payload', payload) return fetch(url, payload).then((d) => d.json()) } -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ async function generateRepoFromTemplate({ template, repoName }) { } getRepo().then((repoInfo) => { console.log('repo', repoInfo) }) function generateAPI(baseUrl, defaults = {}, scope = []) { -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 24 additions and 26 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,26 @@ /* Ultra lightweight Github REST Client */ 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.davidwells[`${template}`].post({ name: repoName }) } getRepo().then((repoInfo) => { console.log('repo, repoInfo) }) function generateAPI(baseUrl, defaults = {}, scope = []) { const callable = () => {} callable.url = baseUrl @@ -31,29 +54,4 @@ function generateAPI(baseUrl, defaults = {}, scope = []) { return generateAPI(arg ? `${url}/${arg}` : url, defaults, path) } }) } -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -43,12 +43,14 @@ const client = generateAPI('https://api.github.com', { }) async function getRepo() { // GET /repos/{owner}/{repo} // https://docs.github.com/en/rest/reference/repos#get-a-repository return client.repos.davidwells.analytics.get() } async function generateRepoFromTemplate({ template, repoName }) { // POST /repos/{template_owner}/{template_repo}/generate // https://docs.github.com/en/rest/reference/repos#create-a-repository-using-a-template return client.repos.davidwells[`${template}`].post({ name: repoName }) } -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 6 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -33,15 +33,7 @@ function generateAPI(baseUrl, defaults = {}, scope = []) { }) } /* Usage */ const token = 'tokenxyz' const client = generateAPI('https://api.github.com', { headers: { @@ -55,6 +47,11 @@ async function getRepo() { return client.repos.davidwells.analytics.get() } async function generateRepoFromTemplate({ template, repoName }) { // POST /repos/{template_owner}/{template_repo}/generate https://docs.github.com/en/rest/reference/repos#create-a-repository-using-a-template return client.repos.davidwells[`${template}`].post({ name: repoName }) } getRepo().then((r) => { console.log('repo, r) }) -
DavidWells revised this gist
Mar 10, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ const client = generateAPI('https://api.github.com', { }) async function getRepo() { // GET /repos/{owner}/{repo} https://docs.github.com/en/rest/reference/repos#get-a-repository return client.repos.davidwells.analytics.get() } -
DavidWells created this gist
Mar 10, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ 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, ...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}${path.slice(0, -1).join('/')}`) 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) } }) } function githubClient(token) { return generateAPI('https://api.github.com', { headers: { 'User-Agent': 'required field', 'Authorization': `bearer ${token}` } }) } const token = 'tokenxyz' const client = generateAPI('https://api.github.com', { headers: { 'User-Agent': 'required field', 'Authorization': `bearer ${token}` } }) async function getRepo() { // GET /repos/{owner}/{repo} return client.repos.davidwells.analytics.get() } getRepo().then((r) => { console.log('repo, r) })