Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

floca-rest-client

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

floca-rest-client - npm Package Compare versions

Comparing version 0.1.0 to 0.8.0

.eslintrc.json

94

lib/Connector.js

@@ -1,64 +0,62 @@

var Vanilla = require('./Vanilla');
var UUID = require('./UUID');
var Vanilla = require('./Vanilla')
var UUID = require('./UUID')
var _ = require('isa.js');
var _ = require('isa.js')
module.exports = {
API_KEY: '',
init: function( options, callback ){
var self = this;
init: function ( options, callback ) {
this.options = options || {}
this.API_KEY = this.options.API_KEY
this.server = this.options.server || ''
this.harconName = this.options.harconName || 'Unknown'
this.options = options || {};
this.API_KEY = this.options.API_KEY;
this.server = this.options.server || '';
this.harconName = this.options.harconName || 'Unknown';
if( this.options.jwtRequestPath )
this.getToken( function(){
if( callback )
callback();
});
if ( this.options.jwtRequestPath )
this.getToken( function () {
if ( callback )
callback()
})
},
getToken: function( opts, callback ){
if( _.isFunction( opts ) )
callback = opts;
opts = opts || {};
getToken: function ( opts, callback ) {
if ( _.isFunction( opts ) )
callback = opts
opts = opts || {}
var self = this;
function cb( err, res ){
if( !err && !Vanilla.hasToken() ) err = new Error('App is not initiated properly');
callback( err, res );
var self = this
function cb ( err, res ) {
if ( !err && !Vanilla.hasToken() ) err = new Error('App is not initiated properly')
callback( err, res )
}
if( !Vanilla.hasToken() ){
var apiKey = opts.apiKey || self.API_KEY;
Vanilla.get( self.options.jwtRequestPath, (apiKey) ? { 'api-key': apiKey } : { }, cb );
if ( !Vanilla.hasToken() ) {
var apiKey = opts.apiKey || self.API_KEY
Vanilla.get( self.options.jwtRequestPath, (apiKey) ? { 'api-key': apiKey } : { }, cb )
} else
cb();
cb()
},
harconREST: function( division, context, service, params, opts, callback ){
if( _.isFunction( opts ) )
callback = opts;
opts = opts || {};
harconREST: function ( division, context, service, params, opts, callback ) {
if ( _.isFunction( opts ) )
callback = opts
opts = opts || {}
var self = this;
var path = (division ? '/' + division : '') + (context ? '/' + context : '') + '/' + service;
var apiKey = opts.apiKey || self.API_KEY;
Vanilla.post( self.server + path, apiKey ? { 'api-key': apiKey } : { }, { params: params }, function(err, res, options){
callback(err, res);
} );
var self = this
var path = (division ? '/' + division : '') + (context ? '/' + context : '') + '/' + service
var apiKey = opts.apiKey || self.API_KEY
Vanilla.post( self.server + path, apiKey ? { 'api-key': apiKey } : { }, { params: params }, function (err, res, options) {
callback(err, res)
} )
},
harcomm: function( division, context, service, params, opts, callback ){
if( _.isFunction( opts ) )
callback = opts;
opts = opts || {};
harcomm: function ( division, context, service, params, opts, callback ) {
if ( _.isFunction( opts ) )
callback = opts
opts = opts || {}
var self = this;
var event = (context ? context + '.' : '') + service;
var apiKey = opts.apiKey || self.API_KEY;
var self = this
var event = (context ? context + '.' : '') + service
var apiKey = opts.apiKey || self.API_KEY
Vanilla.post( self.server + self.harconName, apiKey ? { 'api-key': apiKey } : { }, {
id: UUID.generateUUID(), division: division, event: event, params: params
}, function(err, res, options){
callback(err, res);
} );
}, function (err, res, options) {
callback(err, res)
} )
}
};
}
// Prefix for all lscache keys
var CACHE_PREFIX = 'lscache-';
var CACHE_PREFIX = 'lscache-'
// Suffix for the key name on the expiration items in localStorage
var CACHE_SUFFIX = '-cacheexpiration';
var CACHE_SUFFIX = '-cacheexpiration'
// expiration date radix (set to Base-36 for most space savings)
var EXPIRY_RADIX = 10;
var EXPIRY_RADIX = 10
// time resolution in minutes
var EXPIRY_UNITS = 60 * 1000;
var EXPIRY_UNITS = 60 * 1000
// ECMAScript max Date (epoch + 1e8 days)
var MAX_DATE = Math.floor(8.64e15 / EXPIRY_UNITS);
var MAX_DATE = Math.floor(8.64e15 / EXPIRY_UNITS)
var cachedStorage;
var cachedJSON;
var cacheBucket = '';
var warnings = false;
var cachedStorage
var cachedJSON
var cacheBucket = ''
var warnings = false
function supportsStorage() {
var key = '__lscachetest__';
var value = key;
function supportsStorage () {
var key = '__lscachetest__'
var value = key
if (cachedStorage !== undefined) {
return cachedStorage;
return cachedStorage
}
try {
setItem(key, value);
removeItem(key);
cachedStorage = true;
setItem(key, value)
removeItem(key)
cachedStorage = true
} catch (e) {
if (isOutOfSpace(e)) { // If we hit the limit, then it means we have support,
cachedStorage = true; // just maxed it out and even the set test failed.
cachedStorage = true // just maxed it out and even the set test failed.
} else {
cachedStorage = false;
cachedStorage = false
}
}
return cachedStorage;
return cachedStorage
}
// Check to set if the error is us dealing with being out of space
function isOutOfSpace(e) {
function isOutOfSpace (e) {
if (e && e.name === 'QUOTA_EXCEEDED_ERR' ||
e.name === 'NS_ERROR_DOM_QUOTA_REACHED' ||
e.name === 'QuotaExceededError') {
return true;
return true
}
return false;
return false
}
// Determines if native JSON (de-)serialization is supported in the browser.
function supportsJSON() {
/*jshint eqnull:true */
function supportsJSON () {
if (cachedJSON === undefined) {
cachedJSON = (window.JSON != null);
cachedJSON = (window.JSON != null)
}
return cachedJSON;
return cachedJSON
}

@@ -67,4 +66,4 @@

*/
function escapeRegExpSpecialCharacters(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
function escapeRegExpSpecialCharacters (text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}

@@ -77,4 +76,4 @@

*/
function expirationKey(key) {
return key + CACHE_SUFFIX;
function expirationKey (key) {
return key + CACHE_SUFFIX
}

@@ -86,4 +85,4 @@

*/
function currentTime() {
return Math.floor((new Date().getTime()) / EXPIRY_UNITS);
function currentTime () {
return Math.floor((new Date().getTime()) / EXPIRY_UNITS)
}

@@ -95,25 +94,25 @@

function getItem(key) {
return localStorage.getItem(CACHE_PREFIX + cacheBucket + key);
function getItem (key) {
return localStorage.getItem(CACHE_PREFIX + cacheBucket + key)
}
function setItem(key, value) {
function setItem (key, value) {
// Fix for iPad issue - sometimes throws QUOTA_EXCEEDED_ERR on setItem.
localStorage.removeItem(CACHE_PREFIX + cacheBucket + key);
localStorage.setItem(CACHE_PREFIX + cacheBucket + key, value);
localStorage.removeItem(CACHE_PREFIX + cacheBucket + key)
localStorage.setItem(CACHE_PREFIX + cacheBucket + key, value)
}
function removeItem(key) {
localStorage.removeItem(CACHE_PREFIX + cacheBucket + key);
function removeItem (key) {
localStorage.removeItem(CACHE_PREFIX + cacheBucket + key)
}
function eachKey(fn) {
var prefixRegExp = new RegExp('^' + CACHE_PREFIX + escapeRegExpSpecialCharacters(cacheBucket) + '(.*)');
function eachKey (fn) {
var prefixRegExp = new RegExp('^' + CACHE_PREFIX + escapeRegExpSpecialCharacters(cacheBucket) + '(.*)')
// Loop in reverse as removing items will change indices of tail
for (var i = localStorage.length - 1; i >= 0; --i) {
var key = localStorage.key(i);
key = key && key.match(prefixRegExp);
key = key && key[1];
var key = localStorage.key(i)
key = key && key.match(prefixRegExp)
key = key && key[1]
if (key && key.indexOf(CACHE_SUFFIX) < 0) {
fn(key, expirationKey(key));
fn(key, expirationKey(key))
}

@@ -123,21 +122,21 @@ }

function flushItem(key) {
var exprKey = expirationKey(key);
function flushItem (key) {
var exprKey = expirationKey(key)
removeItem(key);
removeItem(exprKey);
removeItem(key)
removeItem(exprKey)
}
function flushExpiredItem(key) {
var exprKey = expirationKey(key);
var expr = getItem(exprKey);
function flushExpiredItem (key) {
var exprKey = expirationKey(key)
var expr = getItem(exprKey)
if (expr) {
var expirationTime = parseInt(expr, EXPIRY_RADIX);
var expirationTime = parseInt(expr, EXPIRY_RADIX)
// Check if we should actually kick item out of storage
if (currentTime() >= expirationTime) {
removeItem(key);
removeItem(exprKey);
return true;
removeItem(key)
removeItem(exprKey)
return true
}

@@ -147,7 +146,7 @@ }

function warn(message, err) {
if (!warnings) return;
if (!('console' in window) || typeof window.console.warn !== 'function') return;
window.console.warn("lscache - " + message);
if (err) window.console.warn("lscache - The error was: " + err.message);
function warn (message, err) {
if (!warnings) return
if (!('console' in window) || typeof window.console.warn !== 'function') return
window.console.warn('lscache - ' + message)
if (err) window.console.warn('lscache - The error was: ' + err.message)
}

@@ -157,12 +156,12 @@

set: function (key, value, time) {
if (!supportsStorage()) return;
if (!supportsStorage()) return
if (typeof value !== 'string') {
if (!supportsJSON()) return;
if (!supportsJSON()) return
try {
value = JSON.stringify(value);
value = JSON.stringify(value)
} catch (e) {
// Sometimes we can't stringify due to circular refs
// in complex objects, so we won't bother storing then.
return;
return
}

@@ -172,14 +171,14 @@ }

try {
setItem(key, value);
setItem(key, value)
} catch (e) {
if (isOutOfSpace(e)) {
var storedKeys = [];
var storedKey;
var storedKeys = []
var storedKey
eachKey(function (key, exprKey) {
var expiration = getItem(exprKey);
var expiration = getItem(exprKey)
if (expiration) {
expiration = parseInt(expiration, EXPIRY_RADIX);
expiration = parseInt(expiration, EXPIRY_RADIX)
} else {
// TODO: Store date added for non-expiring items for smarter removal
expiration = MAX_DATE;
expiration = MAX_DATE
}

@@ -190,27 +189,27 @@ storedKeys.push({

expiration: expiration
});
});
})
})
storedKeys.sort(function (a, b) {
return (b.expiration - a.expiration);
});
return (b.expiration - a.expiration)
})
var targetSize = (value || '').length;
var targetSize = (value || '').length
while (storedKeys.length && targetSize > 0) {
storedKey = storedKeys.pop();
warn("Cache is full, removing item with key '" + key + "'");
flushItem(storedKey.key);
targetSize -= storedKey.size;
storedKey = storedKeys.pop()
warn("Cache is full, removing item with key '" + key + "'")
flushItem(storedKey.key)
targetSize -= storedKey.size
}
try {
setItem(key, value);
setItem(key, value)
} catch (e) {
// value may be larger than total quota
warn("Could not add item with key '" + key + "', perhaps it's too big?", e);
return;
warn("Could not add item with key '" + key + "', perhaps it's too big?", e)
return
}
} else {
// If it was some other error, just give up.
warn("Could not add item with key '" + key + "'", e);
return;
warn("Could not add item with key '" + key + "'", e)
return
}

@@ -220,5 +219,5 @@ }

if (time) {
setItem(expirationKey(key), (currentTime() + time).toString(EXPIRY_RADIX));
setItem(expirationKey(key), (currentTime() + time).toString(EXPIRY_RADIX))
} else {
removeItem(expirationKey(key));
removeItem(expirationKey(key))
}

@@ -228,17 +227,17 @@ },

get: function (key) {
if (!supportsStorage()) return null;
if (!supportsStorage()) return null
if (flushExpiredItem(key)) {
return null;
return null
}
var value = getItem(key);
var value = getItem(key)
if (!value || !supportsJSON()) {
return value;
return value
}
try {
return JSON.parse(value);
return JSON.parse(value)
} catch (e) {
return value;
return value
}

@@ -248,39 +247,39 @@ },

remove: function (key) {
if (!supportsStorage()) return;
if (!supportsStorage()) return
flushItem(key);
flushItem(key)
},
supported: function () {
return supportsStorage();
return supportsStorage()
},
flush: function () {
if (!supportsStorage()) return;
if (!supportsStorage()) return
eachKey(function (key) {
flushItem(key);
});
flushItem(key)
})
},
flushExpired: function () {
if (!supportsStorage()) return;
if (!supportsStorage()) return
eachKey(function (key) {
flushExpiredItem(key);
});
flushExpiredItem(key)
})
},
setBucket: function (bucket) {
cacheBucket = bucket;
cacheBucket = bucket
},
resetBucket: function () {
cacheBucket = '';
cacheBucket = ''
},
enableWarnings: function (enabled) {
warnings = enabled;
warnings = enabled
}
};
}
module.exports = {
generateUUID: function(){
var d = new Date().getTime();
if(window.performance && typeof window.performance.now === "function"){
d += window.performance.now(); //use high-precision timer if available
generateUUID: function () {
var d = new Date().getTime()
if (window.performance && typeof window.performance.now === 'function') {
d += window.performance.now()
// use high-precision timer if available
}
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c==='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0
d = Math.floor(d / 16)
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
})
return uuid
}
};
}

@@ -1,147 +0,159 @@

var LSCache = require('./LSCache');
var LSCache = require('./LSCache')
var tokenName = 'x-nodrium-jwt';
var tokenName = 'x-nodrium-jwt'
var DEFAULT_TIMEOUT = 120;
var DEFAULT_TIMEOUT = 120
if( LSCache.get( tokenName ) === 'undefined' )
LSCache.remove( tokenName );
if ( LSCache.get( tokenName ) === 'undefined' )
LSCache.remove( tokenName )
var index = 0;
module.exports = {
token: LSCache.get( tokenName ),
setCache: function(name, object, timeout){
LSCache.set(name, object, timeout || DEFAULT_TIMEOUT);
setCache: function (name, object, timeout) {
LSCache.set(name, object, timeout || DEFAULT_TIMEOUT)
},
getCache: function( name ){
return LSCache.get( name );
getCache: function ( name ) {
return LSCache.get( name )
},
hasToken: function(){
return !!this.token;
hasToken: function () {
return !!this.token
},
removeToken: function(){
this.token = null;
LSCache.remove( tokenName );
removeToken: function () {
this.token = null
LSCache.remove( tokenName )
},
setToken: function(token){
var self = this;
if( token ){
self.token = token;
LSCache.set(tokenName, token, 120);
setToken: function (token) {
var self = this
if ( token ) {
self.token = token
LSCache.set(tokenName, token, 120)
}
},
initiateRequest: function( request, headers ){
var self = this;
initiateRequest: function ( request, headers ) {
var self = this
if( self.token )
request.setRequestHeader(tokenName, self.token);
Object.keys( headers ).forEach( function( header ){
if( headers[header] )
request.setRequestHeader( header, headers[header] );
} );
if ( self.token )
request.setRequestHeader(tokenName, self.token)
Object.keys( headers ).forEach( function ( header ) {
if ( headers[header] )
request.setRequestHeader( header, headers[header] )
} )
},
setRequestToReady: function( request, callback ){
var self = this;
request.onreadystatechange = function() {
if (request.readyState !== 4) return;
setRequestToReady: function ( request, callback ) {
var self = this
request.onreadystatechange = function () {
if (request.readyState !== 4) return
if (request.status >= 200 && request.status < 400) {
self.setToken( request.getResponseHeader( tokenName ) || self.token );
callback( null, JSON.parse(request.responseText), { status: request.status } );
self.setToken( request.getResponseHeader( tokenName ) || self.token )
callback( null, JSON.parse(request.responseText), { status: request.status } )
}
else {
callback( new Error('Error occurred', request.status), request.responseText, { status: request.status } );
callback( new Error('Error occurred', request.status), request.responseText, { status: request.status } )
}
};
}
},
get: function( url, headers, callback ){
var self = this;
get: function ( url, headers, callback ) {
var self = this
window.signalWork();
var callbackFn = function(err, res){ window.signalTermination(); callback(err, res); };
window.signalWork()
var callbackFn = function (err, res) {
window.signalTermination()
callback(err, res)
}
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.setRequestHeader("Content-Type", "application/json");
var request = new XMLHttpRequest()
request.open('GET', url, true)
request.setRequestHeader('Content-Type', 'application/json')
self.initiateRequest( request, headers || {} );
self.initiateRequest( request, headers || {} )
request.onload = function() {
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
self.setToken( request.getResponseHeader( tokenName ) || self.token );
var data = JSON.parse(request.responseText);
callbackFn( null, data, { status: request.status } );
self.setToken( request.getResponseHeader( tokenName ) || self.token )
var data = JSON.parse(request.responseText)
callbackFn( null, data, { status: request.status } )
} else {
callbackFn( new Error('Failed'), request.responseText, { status: request.status } );
callbackFn( new Error('Failed'), request.responseText, { status: request.status } )
}
};
request.onerror = function() {
callbackFn( new Error('Failed'), request.responseText, { status: request.status } );
};
request.send();
}
request.onerror = function () {
callbackFn( new Error('Failed'), request.responseText, { status: request.status } )
}
request.send()
},
del: function( url, headers, data, callback ){
var self = this;
del: function ( url, headers, data, callback ) {
var self = this
window.signalWork();
var callbackFn = function(err, res){ window.signalTermination(); callback(err, res); };
window.signalWork()
var callbackFn = function (err, res) {
window.signalTermination()
callback(err, res)
}
var request = new XMLHttpRequest();
var packet = JSON.stringify(data);
var request = new XMLHttpRequest()
var packet = JSON.stringify(data)
request.open('DELETE', url, true);
request.setRequestHeader("Content-Type", "application/json");
request.open('DELETE', url, true)
request.setRequestHeader('Content-Type', 'application/json')
self.initiateRequest( request, headers || {} );
self.initiateRequest( request, headers || {} )
self.setRequestToReady( request, callbackFn );
request.send( packet );
self.setRequestToReady( request, callbackFn )
request.send( packet )
},
post: function( url, headers, data, callback ){
var self = this;
post: function ( url, headers, data, callback ) {
var self = this
window.signalWork();
var callbackFn = function(err, res){ window.signalTermination(); callback(err, res); };
window.signalWork()
var callbackFn = function (err, res) {
window.signalTermination()
callback(err, res)
}
var request = new XMLHttpRequest();
var packet = JSON.stringify(data);
var request = new XMLHttpRequest()
var packet = JSON.stringify(data)
request.open('POST', url, true);
request.setRequestHeader("Content-Type", "application/json");
request.open('POST', url, true)
request.setRequestHeader('Content-Type', 'application/json')
self.initiateRequest( request, headers || {} );
self.initiateRequest( request, headers || {} )
self.setRequestToReady( request, callbackFn );
request.send( packet );
self.setRequestToReady( request, callbackFn )
request.send( packet )
},
upload: function( url, headers, name, files, options ){
var self = this;
upload: function ( url, headers, name, files, options ) {
var self = this
window.signalWork();
var doneFn = function(err, res){ window.signalTermination(); options.done(err, res); };
window.signalWork()
var doneFn = function (err, res) {
window.signalTermination()
options.done(err, res)
}
var formData = new FormData();
( Array.isArray(files)?files:[files] ).forEach( function(file, i){
formData.append( name, file );
} );
var formData = new FormData()
var fileArray = Array.isArray(files) ? files : [files]
fileArray.forEach( function (file, i) {
formData.append( name, file )
} )
var request = new XMLHttpRequest();
request.open('POST', url, true);
var request = new XMLHttpRequest()
request.open('POST', url, true)
self.initiateRequest( request, headers || {} );
self.initiateRequest( request, headers || {} )
if ( request.upload && options.progress ) {
request.upload.addEventListener('progress', options.progress);
request.upload.addEventListener('progress', options.progress)
}
self.setRequestToReady( request, doneFn );
self.setRequestToReady( request, doneFn )
/*
request.onload = function(){
console.log( arguments );
};
request.onerror = function(){
console.log( arguments );
};*/
request.send( formData );
request.onload = function () {
console.log( arguments )
}
request.onerror = function () {
console.log( arguments )
}*/
request.send( formData )
}
};
}
{
"name": "floca-rest-client",
"version": "0.1.0",
"version": "0.8.0",
"description": "REST client for floca-based projects",

@@ -26,8 +26,8 @@ "keywords": [

"dependencies": {
"isa.js": "^1.0.1"
"isa.js": "^1.7.5"
},
"devDependencies": {},
"engines": {
"node": ">= 4.0.0"
"node": ">= 6.0.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc