0.1.0 • Published 10 years ago
appbase-client v0.1.0
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
windowglobal with the nameAppBase. - In
browserifyorwebpack, it can be used as:var AppBase = require('appbase-client')- in ES6,
import AppBase from 'appbase-client'
User API
checkSession(errorCallback, showModal, options)
errorCallbackis 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 anderrorCallbackis called when the modal's button is clicked.optionsis 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
trueis the current user is an admin, otherwise returns false.
getUser()
- Returns current user's information as an object that is set be server in
userinfocookie. Returnsnullif cookie is invalid.
sidToUidGroups()
- Returns and object like
{uid: 1, groups: ['admin', 'editor']}from the session cookie. Returnsnullif 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.0and then callFetchmethods 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-originandinclude. If not set,same-originis used. - Set to
same-originif your client lives on the same domain as the api server. - Set to
includeif client and server domains are different.
Fetch API
- Fetch has 5 methods corresponding to each http method:
get,post,put,patchanddel(instead ofdelete). - Fetch abstracts away reponse caching so that subsequest
GETrequests 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)pathis 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 passoptionsobject for each function call even if its value is empty({}).In
optionsyou can pass values like:data:- If the called method is
get, the key-value pair indatawill be appended to the called url as a querystring. - Otherwise it will be posted as JSON body unless
options.formistrue. In that case, the data will be posted asmultipart/form-data.
- If the called method is
form- If this value is true,
datawill be posted asForm, otherwise asJSON. Useform: trueif you are uploading files.
- If this value is true,
headers- Pass additional headers as an object.
successis a function that is passed the success response of aFetchcall likesuccess(data).erroris a function that is passed the error response of aFetchcall likeerror(err). Here,errhascodethat is the http error code andresponsethat 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 withprefixwill 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.gitcd appbase-clientnpm install- Run
npm run dev. It will start a development server @http://localhost:8080where you can test the API on the window globalAppBase. npm run buildwill build the library for use in production.
0.1.0
10 years ago