appbase-client
v0.1.0
Published
*appbase-client* exposes a set of basic functions that can help in rapid frontend development @ [Scroll.in](http://scroll.in).
Downloads
4
Readme
A client to be used in conjunction with appbase.
appbase-client exposes a set of basic functions that can help in rapid frontend development @ Scroll.in.
It can be used directly in browser by loading the file in a script
tag or can be used in browserify
and webpack
environments too.
- In browser, it is available in the
window
global with the nameAppBase
. - In
browserify
orwebpack
, it can be used as:var AppBase = require('appbase-client')
- in ES6,
import AppBase from 'appbase-client'
User API
checkSession(errorCallback, showModal, options)
errorCallback
is a function that is called if a valid session is not found.showModal
's default value istrue
. It shows a modal dialog if the session is not valid anderrorCallback
is called when the modal's button is clicked.options
is an object that should be passes if you want to customise the modal. Its values are documented here.- This function should be the first to get called before you proceed with your own functions.
isAdmin()
- Checks session and returns
true
is the current user is an admin, otherwise returns false.
getUser()
- Returns current user's information as an object that is set be server in
userinfo
cookie. Returnsnull
if cookie is invalid.
sidToUidGroups()
- Returns and object like
{uid: 1, groups: ['admin', 'editor']}
from the session cookie. Returnsnull
if invalid session.
Network API
setBaseURL(base)
- Sets the base url for api calls to
base
. For ex: if a resource exists athttp://api.example.com/1.0/user/1
, you can set the base url tohttp://api.example.com/1.0
and then callFetch
methods using the endpoints only.
setCredentials(value)
- This is used by the ajax requests to decide whether to send current cookies or not. Valid values are
same-origin
andinclude
. If not set,same-origin
is used. - Set to
same-origin
if your client lives on the same domain as the api server. - Set to
include
if client and server domains are different.
Fetch
API
- Fetch has 5 methods corresponding to each http method:
get
,post
,put
,patch
anddel
(instead ofdelete
). - Fetch abstracts away reponse caching so that subsequest
GET
requests for the same resource are returned from memory instead of querying server again.
Each method has this signature:
methodname(path, options={}, success=null, error=null)
path
is the url endpoint to make the request. For ex: first set the base url usingsetBaseURL(base)
and then you can callFetch.get('/user/1')
options
: you have to passoptions
object for each function call even if its value is empty({}
).In
options
you can pass values like:data
:- If the called method is
get
, the key-value pair indata
will be appended to the called url as a querystring. - Otherwise it will be posted as JSON body unless
options.form
istrue
. In that case, the data will be posted asmultipart/form-data
.
- If the called method is
form
- If this value is true,
data
will be posted asForm
, otherwise asJSON
. Useform: true
if you are uploading files.
- If this value is true,
headers
- Pass additional headers as an object.
success
is a function that is passed the success response of aFetch
call likesuccess(data)
.error
is a function that is passed the error response of aFetch
call likeerror(err)
. Here,err
hascode
that is the http error code andresponse
that is the error data that was sent by the server.You can also call
Fetch.clear(prefix)
to manually clear the cache. All cached urls starting withprefix
will be cleared.If you call
Fetch.clear()
without any arguments, the full cache will be cleared.
Setting up for development:
git clone https://github.com/brijeshb42/appbase-client.git
cd appbase-client
npm install
- Run
npm run dev
. It will start a development server @http://localhost:8080
where you can test the API on the window globalAppBase
. npm run build
will build the library for use in production.