A simple js client for gapi based on SuperAgent
This project is still in alpha phase and not ready for actual use
First, create a new Gapi instance by passing your Gapi key to the constructor. By default the url will be set to https://rest.gadventures.com
.
import Gapi from 'gapi-js';
const g = new Gapi({ key: yourGapiKey [, url: gapiUrl, proxy: yourProxy] });
These commands are all chainable, but must always start with a resource name.
Keep in mind, when chaining get()
, list()
, post()
, patch()
, and del()
, only the last chained item will take affect on the request.
gapi-js
will wait untill end()
is called, to make the actual server request.
Used to request a single object.
g.countries.get(1090831)
g.end( (error, response) => {
if( error ) {
// do someting w/ the error object
}else{
// do something with the response object
}
} )
Request a list of items from the resource. Based on gapi's pagination, by default, will return the 20 items from the first page. To change the requested page and/or the page size, look at page()
g.places.list(); // page = 1, pageSize = 20
g.places.list().page(2) // page = 2 , pageSize = 20
g.places.list().page(2, 15) // page = 2 , pageSize = 15
g.end(callback);
Querystring parameters to pass to Gapi
g.places.list().query({name: 'Station'}) // search for all places that include 'Station' in their name
Request a certain page. By default will request the first page with a page size of 20;
g.places.list(); // page = 1, pageSize = 20
g.places.list().page(2) // page = 2, pageSize = 20
g.places.list().page(2, 15) // page = 2, pageSize = 15
Post and patch requests to gapi. To pass data, you must also call send()
in your chain.
g.countries.post().send({name: 'Canada', id: 'CA'}).end() // will add a new country to the `countries` resource
...
g.places.patch('1090831').send({name: 'Toronto'}); // will update the name of a resource.
g.places.end()
Allows for passing parameters to post()
or patch()
. send()
accepts many formats
g.countries.post().send('{"name":"tj"}') // JSON String
...
g.places.patch('1090831').send({name: 'Toronto'}) // Object
...
g.places.patch('1090831')
.send('name=Toronto')
.send('population=3000000') // Chaining query strings
.end()
...
g.places.post()
.send({name='Toronto'})
.send({population=3000000}) // Chaining Objects
.end()
Remove a resource from the server.
g.places.del('8317609').end()
Your callback function will always be passed two arguments: error
and response
. If no error occurred, the first argument will be null
g.countries
.get('123')
.end( (err, res) => {
if( err ) {
// do someting w/ the error object
}else{
// do something with the response object
}
})