How to create an API wrapper using a JavaScript proxy (#snippet)

I just found a super smart gist from David Wells that’s worth bookmarking.
David uses a JavaScript Proxy to create wrapper around fetch. And thanks to the proxy, object properties are automatically mapped to resources of a RESTful A…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I just found a super smart gist from David Wells that's worth bookmarking.

David uses a JavaScript Proxy to create wrapper around fetch. And thanks to the proxy, object properties are automatically mapped to resources of a RESTful API.

apiObject.cars() maps to /cars/ and apiObject.cars('123') maps to /cars/123/. đź‘Ź

// Found at https://gist.github.com/DavidWells/53518b3c12344952641dc81cc7599939
const createApi = (url) => {
  return new Proxy({}, {
    get(target, key) {
      return async function(id = "") {
        const response = await fetch(`${url}/${key}/${id}`)
        if (response.ok) {
          return response.json();
        }
        return Promise.resolve({ error: "Malformed Request" })
      }
    }
  })
}

let api = createApi("https://swapi.co/api")

// 'get' request to https://swapi.co/api/people
let people = await api.people()

// 'get' request to https://swapi.co/api/people/1
let person = await api.people(1)

What a great little trick! đź’Ż


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-06-25T22:00:00+00:00) How to create an API wrapper using a JavaScript proxy (#snippet). Retrieved from https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/

MLA
" » How to create an API wrapper using a JavaScript proxy (#snippet)." Stefan Judis | Sciencx - Saturday June 25, 2022, https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/
HARVARD
Stefan Judis | Sciencx Saturday June 25, 2022 » How to create an API wrapper using a JavaScript proxy (#snippet)., viewed ,<https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » How to create an API wrapper using a JavaScript proxy (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/
CHICAGO
" » How to create an API wrapper using a JavaScript proxy (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/
IEEE
" » How to create an API wrapper using a JavaScript proxy (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/. [Accessed: ]
rf:citation
» How to create an API wrapper using a JavaScript proxy (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2022/06/25/how-to-create-an-api-wrapper-using-a-javascript-proxy-snippet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.