GithubHelp home page GithubHelp logo

jean-pierregassin / es6-api-wrapper Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 7 KB

A basic ES6 API class that wraps the fetch function and allows for a more dynamic and robust interface to interact with any API - easily extendable and mighty flexible.

License: MIT License

JavaScript 82.52% HTML 17.48%
es6 api-wrapper fetch-api boilerplate

es6-api-wrapper's Introduction

ES6 API Wrapper - About

A basic ES6 API class that wraps the fetch function and allows for a more dynamic and robust interface to interact with any API - easily extendable and mighty flexible.


What issues does this solve?

If you're wanting to interact with a complex API you'll want to make your API calls as structured and flexible as possible, this wrapper aims to solve that issue.

Simply extendable and suited for your API's needs, you can implement more complex functionality or simply use what's currently available.

How do I use it?

A demo implementation can be found in this projects index.html file.

Constructing the wrapper

To construct the API wrapper, you'll need to provide a host url (the API's domain), an object of endpoints and if you desire, a response type (Content-Type).

Endpoints format - Required properties

All endpoints should be listed in the endpoints object.

The endpoints object only requires two properties to function properly: url (string) and methods (array).

The url property must be the trailing endpoint that this endpoint should hit, for example if I wanted to hit a posts endpoint, my url property would be equal to /posts.

The methods property is an array of available HTTP methods that this endpoint has access to, for example my /posts endpoint has access to both POST and GET, so my methods property would be equal to ['POST', 'GET'].

API fetch format

To fetch data from an endpoint, you can call Api.fetch(method (string), endpoint (string), options (object)) - The fetch method returns a promise.

When fetching data you may pass options to each endpoint as you desire, however there are reserved properties which perform special functions, these being id (string), filter (object) and parent (string).

The id property will simply request the current endpoint with a resource identifier, for example http://myapi.com/posts/{id};

The filter property is an object containing anything you'd like to filter against this endpoint, creating a query string that implements your filters, for example http://myapi.com/posts?user=me.

The parent property is unique in that it can be used to retrieve a nested endpoint from a resource identifier. For example, if I have the endpoint /posts and that endpoint accepts a resource identifier such as /posts/{id}, but then also accepts a nested endpoint to retrieve comments like /posts/{id}/comments - the parent property will allow you to do this.

I would still create a new endpoint for my posts comments, however its parent would obviously be the endpoint that it nests under, in this case posts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.