Library based on the Star Wars API by Paul Hallett
Using this library should be pretty simple as long as the API you want to use exposes itself somewhere. The description should expose something like that:
{
"people": "http://swapi.co/api/people/",
"planets": "http://swapi.co/api/planets/",
"films": "http://swapi.co/api/films/",
"species": "http://swapi.co/api/species/",
"vehicles": "http://swapi.co/api/vehicles/",
"starships": "http://swapi.co/api/starships/"
}
- Initialize your instance using only the URL where the API is described:
var myApi = new Swapi('http://swapi.co/api/');
- You can now use your API almost as you wish. Since that the instance is initialized using the description given when asked directly to the API, we need to compose with AJAX's asynchronous ways. That's why when you actually use your instance, you should wrap the calls in its
.ready()
method.
myApi.ready(function () { // Here, i can be sure that my API instance is ready to be used // Now, I can do whatever i want ! });
It works in the same way as JQuery's .ready()
method.
For each key word exposed in the API's description, your intance will have 3 methods available:
get[KeyWord](id, success, error)
: retrieves a single result from the givenid
and passes it to thesuccess
callbackgetAll[KeyWord](success, error)
: retrieves all results and passes them to thesuccess
callbackgetAll[KeyWord](page, success, error)
: retrieves a single page of results and passes it to thesuccess
callback
All error
arguments are error callbacks and are optional.
It is possible to handle several APIs at once, they will be completely independant of one another.
Using ES6's promises could be a blast