Skip to content

Instantly share code, notes, and snippets.

@davidbgk
Forked from DavidWells/github-proxy-client.js
Created March 17, 2022 02:39
Show Gist options
  • Save davidbgk/0c33feb19d301d5d58bc0eabc55b9de6 to your computer and use it in GitHub Desktop.
Save davidbgk/0c33feb19d301d5d58bc0eabc55b9de6 to your computer and use it in GitHub Desktop.

Revisions

  1. @DavidWells DavidWells revised this gist Mar 11, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions github-proxy-client.js
    Original 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: {
  2. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-proxy-client.js
    Original 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, ...overrides }
    const payload = { method, ...defaults, ...overrides }
    switch (method) {
    case 'GET': {
    if (data) url = `${url}?${new URLSearchParams(data)}`
  3. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions github-proxy-client.js
    Original 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.davidwells[`${template}`].post({ name: repoName })
    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}${path.slice(0, -1).join('/')}`)
    console.log(`Calling: ${url}`)
    console.log('payload', payload)
    return fetch(url, payload).then((d) => d.json())
    }
  4. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-proxy-client.js
    Original 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)
    console.log('repo', repoInfo)
    })

    function generateAPI(baseUrl, defaults = {}, scope = []) {
  5. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 24 additions and 26 deletions.
    50 changes: 24 additions & 26 deletions github-proxy-client.js
    Original 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)
    }
    })
    }

    /* Usage */
    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}
    // 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 })
    }

    getRepo().then((r) => {
    console.log('repo, r)
    })
    }
  6. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions github-proxy-client.js
    Original 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
    // 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
    // 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 })
    }

  7. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 6 additions and 9 deletions.
    15 changes: 6 additions & 9 deletions github-proxy-client.js
    Original file line number Diff line number Diff line change
    @@ -33,15 +33,7 @@ function generateAPI(baseUrl, defaults = {}, scope = []) {
    })
    }

    function githubClient(token) {
    return generateAPI('https://api.github.com', {
    headers: {
    'User-Agent': 'required field',
    'Authorization': `bearer ${token}`
    }
    })
    }

    /* 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)
    })
  8. @DavidWells DavidWells revised this gist Mar 10, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-proxy-client.js
    Original 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}
    // GET /repos/{owner}/{repo} https://docs.github.com/en/rest/reference/repos#get-a-repository
    return client.repos.davidwells.analytics.get()
    }

  9. @DavidWells DavidWells created this gist Mar 10, 2022.
    60 changes: 60 additions & 0 deletions github-proxy-client.js
    Original 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)
    })