UNPKG

authbase

Version:

AuthBase client library

116 lines (97 loc) 3.37 kB
var request = require('superagent'), q = require('q'), errors = require('./errors'), _ = require('lodash'); function AuthBase(options) { options = options || {}; options.url = options.url || 'https://api.authbase.io'; if (typeof options.appId !== 'string') throw errors.missingAppId; if (typeof options.appSecret !== 'string') throw errors.missingAppSecret; function exceptionToError(ex) { return { message: 'There was an exception while trying to complete request', code: 'exception' }; } function promisify(callback) { var deferred = q.defer(); callback(function(err, value) { if (err) deferred.reject(err); else deferred.resolve(value); }); return deferred.promise; } function completable(callback, promised) { return function(error, user) { // call the callback if it was provided if (typeof callback === 'function') { if (error) callback(error); else callback(undefined, user); } // fulfill the promise if (error) promised(error); else promised(undefined, user); }; } function responseHandler(complete) { return function(err, res) { try { if (err) complete(exceptionToError(err)); else if (res.error) complete(res.body); else complete(undefined, res.body.data); } catch(ex) { complete(exceptionToError(ex)); } }; } function makeRequest(method, endpoint, data, callback) { return promisify(function(promised) { method(options.url + endpoint) .set('X-App-Id', options.appId) .set('X-App-Secret', options.appSecret) .send(data) .end(responseHandler(completable(callback, promised))); }); } function api(method, endpoint){ return makeRequest.bind(this, method, endpoint); } function setProfile(userid){ // arguments: (userid, [path], value, [callback]) var callback; var path = ''; var value; if (arguments.length === 3 && arguments[2] instanceof Function || arguments.length === 2){ // (userid, value, callback) or (userid, value) value = arguments[1]; callback = arguments[2]; } else { // (userid, path, value) or (userid, path, value, callback) path = arguments[1]; value = arguments[2]; callback = arguments[3]; } var endpoint = '/user/' + userid + '/profile/' + _.reject(path.split('.'), _.isEmpty()).join('/'); return makeRequest(request.post, endpoint, {data: value}, callback); } function getProfile(userid){ // arguments: (userid, [path], [callback]) var callback; var path = ''; if (arguments.length === 2 && arguments[1] instanceof Function || arguments.length === 1){ // (userid, callback) or (userid) callback = arguments[1]; } else { // (userid, path) or (userid, path, callback) path = arguments[1]; callback = arguments[2]; } var endpoint = '/user/' + userid + '/profile/' + _.reject(path.split('.'), _.isEmpty()).join('/'); return makeRequest(request.get, endpoint, undefined, callback); } // public API return { register: api(request.post, '/register'), unregister: api(request.post, '/unregister'), verify: api(request.get, '/verify'), setProfile: setProfile, getProfile: getProfile }; } module.exports = AuthBase;