contentstack
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -22,3 +22,3 @@ "use strict"; | ||
* @example | ||
* var site = Contentstack.Stack('api_key', 'access_token', 'environment'); | ||
* var Stack = Contentstack.Stack('api_key', 'access_token', 'environment'); | ||
* OR | ||
@@ -25,0 +25,0 @@ * var Stack = Contentstack.Stack({ |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var Utils = require('./utils'); | ||
var when = require('when'); | ||
@@ -17,3 +18,4 @@ // if script is running under node.js then use node-XMLHttpRequest node modules for the built-in http client to emulate the browser XMLHttpRequest object. | ||
function Request(options, callback) { | ||
function Request(options) { | ||
var deferred = when.defer(); | ||
var xhr = new HTTPRequest(), | ||
@@ -55,8 +57,9 @@ method = options.method || "GET", | ||
if (xhr.status >= 200 && xhr.status < 300) { | ||
callback(null, data); | ||
deferred.resolve(data); | ||
} else { | ||
callback(data || error, null); | ||
deferred.reject(data || error); | ||
} | ||
} | ||
}; | ||
return deferred.promise; | ||
} | ||
@@ -63,0 +66,0 @@ |
172
lib/utils.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
*/ | ||
var when = require('when'); | ||
var utils = {}; | ||
@@ -164,2 +165,173 @@ module.exports = exports = utils; | ||
return _results; | ||
}; | ||
exports.sendRequest = function (queryObject) { | ||
var Request = require('./request'); | ||
var env_uid = queryObject.environment_uid; | ||
if (env_uid) { | ||
queryObject._query.environment_uid = env_uid; | ||
} else { | ||
queryObject._query.environment = queryObject.environment; | ||
} | ||
var deferred = when.defer(); | ||
var self = queryObject; | ||
var continueFlag = false; | ||
var cachePolicy = (typeof self.queryCachePolicy !== 'undefined') ? self.queryCachePolicy : self.cachePolicy; | ||
var tojson = (typeof self.tojson !== 'undefined') ? self.tojson : false; | ||
var isSingle = (self.entry_uid || self.singleEntry) ? true : false; | ||
var hashQuery = utils.getHash(utils.parseQueryFromParams(self, isSingle, tojson)); | ||
var spreadResult; | ||
var getCacheCallback = function () { | ||
return function (err, entries) { | ||
try { | ||
if (err) throw err; | ||
if (!tojson) entries = utils.resultWrapper(entries); | ||
spreadResult = utils.spreadResult(entries); | ||
return deferred.resolve(spreadResult); | ||
} catch (e) { | ||
return deferred.reject(e); | ||
} | ||
} | ||
}; | ||
var callback = function (continueFlag) { | ||
if(continueFlag) { | ||
Request(queryObject.requestParams) | ||
.then(function (data) { | ||
try { | ||
self.entry_uid = self.tojson = self.queryCachePolicy = undefined; | ||
var entries = {}; | ||
if (queryObject.singleEntry) { | ||
queryObject.singleEntry = false; | ||
if(data.schema) entries.schema = data.schema; | ||
if (data.entries && data.entries.length) { | ||
entries.entry = utils.getData(data, queryObject.headers.access_token).entries[0]; | ||
} else { | ||
if(cachePolicy === 2) { | ||
self.provider.get(hashQuery, getCacheCallback()); | ||
} else { | ||
return deferred.reject({ error_code: 141, error_message: 'The requested entry doesn\'t exist.' }); | ||
} | ||
return; | ||
} | ||
} else { | ||
entries = utils.getData(data, queryObject.headers.access_token); | ||
} | ||
if(cachePolicy !== -1) { | ||
self.provider.set(hashQuery, entries, function (err) { | ||
try { | ||
if (err) throw err; | ||
if(!tojson) entries = utils.resultWrapper(entries); | ||
spreadResult = utils.spreadResult(entries); | ||
return deferred.resolve(spreadResult); | ||
} catch(e) { | ||
return deferred.reject(e); | ||
} | ||
}); | ||
} else { | ||
if(!tojson) entries = utils.resultWrapper(entries); | ||
spreadResult = utils.spreadResult(entries); | ||
return deferred.resolve(spreadResult); | ||
} | ||
} catch (e) { | ||
return deferred.reject({ | ||
message: e.message | ||
}); | ||
} | ||
}.bind(self)) | ||
.catch(function (error) { | ||
if(cachePolicy === 2) { | ||
self.provider.get(hashQuery, getCacheCallback()); | ||
} else { | ||
return deferred.reject(error); | ||
} | ||
}); | ||
} | ||
}; | ||
switch (cachePolicy) { | ||
case 1: | ||
self.provider.get(hashQuery, function (err, _data) { | ||
try { | ||
if(err || !_data) { | ||
callback(true); | ||
} else { | ||
if (!tojson) _data = utils.resultWrapper(_data); | ||
spreadResult = utils.spreadResult(_data); | ||
return deferred.resolve(spreadResult); | ||
} | ||
} catch(e) { | ||
return deferred.reject(e); | ||
} | ||
}); | ||
break; | ||
case 2: | ||
case 0: | ||
case -1: | ||
callback(true); | ||
}; | ||
if(cachePolicy !== 3) { | ||
return deferred.promise; | ||
} else { | ||
return { | ||
cache: (function () { | ||
var Defer = when.defer(); | ||
self.provider.get(hashQuery, function (err, _data) { | ||
try { | ||
if(err) { | ||
return Defer.reject(err); | ||
} else { | ||
if (!tojson) _data = utils.resultWrapper(_data); | ||
spreadResult = utils.spreadResult(_data); | ||
return Defer.resolve(spreadResult); | ||
} | ||
} catch(e) { | ||
return Defer.reject(e); | ||
} | ||
}); | ||
return Defer.promise; | ||
}()), | ||
network: (function () { | ||
callback(true); | ||
return deferred.promise; | ||
}()), | ||
both: function (_callback_) { | ||
self.provider.get(hashQuery, function (err, entries) { | ||
if (!tojson) entries = utils.resultWrapper(entries); | ||
spreadResult = utils.spreadResult(entries); | ||
_callback_(err, spreadResult) | ||
}); | ||
Request(queryObject.requestParams) | ||
.then(function (data) { | ||
try { | ||
self.entry_uid = self.tojson = self.queryCachePolicy = undefined; | ||
var entries = {}, error = null; | ||
if (queryObject.singleEntry) { | ||
queryObject.singleEntry = false; | ||
if(data.schema) entries.schema = data.schema; | ||
if (data.entries && data.entries.length) { | ||
entries.entry = utils.getData(data, queryObject.headers.access_token).entries[0]; | ||
} else { | ||
error = { error_code: 141, error_message: 'The requested entry doesn\'t exist.' }; | ||
} | ||
} else { | ||
entries = utils.getData(data, queryObject.headers.access_token); | ||
} | ||
if(!tojson) entries = utils.resultWrapper(entries); | ||
spreadResult = utils.spreadResult(entries); | ||
_callback_(error, spreadResult); | ||
} catch (e) { | ||
_callback_(e); | ||
} | ||
}.bind(self)) | ||
.catch(function (error) { | ||
_callback_(error); | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
{ | ||
"name": "contentstack", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "The Built.io Contentstack Javascript SDK", | ||
@@ -13,2 +13,3 @@ "homepage": "https://www.built.io/products/contentstack/overview", | ||
"localStorage": "1.0.3", | ||
"when": "3.7.7", | ||
"xmlhttprequest": "1.8.0" | ||
@@ -15,0 +16,0 @@ }, |
@@ -183,18 +183,2 @@ // 'use strict'; | ||
/** | ||
* @method spread | ||
* @description This method is used to spread the result to the success callback with arguments. | ||
* @example | ||
* blogEntry.spread(function (entry, schema, count) { | ||
* // success function | ||
* },function (error) { | ||
* // error function | ||
* }) | ||
* @returns {Entry} | ||
*/ | ||
Entry.prototype.spread = function () { | ||
this._spread = true; | ||
if(this && this.then) this.then.apply(this, arguments); | ||
}; | ||
/** | ||
* @method toJSON | ||
@@ -228,3 +212,3 @@ * @description This method is used to convert the result in to plain javascript object. | ||
headers: this.headers, | ||
url: this.config.protocol + "://" + this.config.host + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries + this.entry_uid, | ||
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries + this.entry_uid, | ||
body: { | ||
@@ -235,3 +219,3 @@ _method: 'GET', | ||
}; | ||
return this; | ||
return Utils.sendRequest(this); | ||
} else { | ||
@@ -242,157 +226,2 @@ console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')"); | ||
/** | ||
* @method then | ||
* @description Execute query and get data from Built.io Contentstack | ||
* @example | ||
* blogEntry.then(function () { | ||
* // sucess function | ||
* },function () { | ||
* // error function | ||
* }) | ||
*/ | ||
Entry.prototype.then = function (success, fail) { | ||
var env_uid = this.environment_uid, | ||
params = this.requestParams; | ||
if (env_uid) { | ||
this._query.environment_uid = env_uid; | ||
} else { | ||
this._query.environment = this.environment; | ||
} | ||
var self = this; | ||
var continueFlag = false; | ||
var cachePolicy = (typeof self.queryCachePolicy !== 'undefined') ? self.queryCachePolicy : self.cachePolicy; | ||
var tojson = (typeof self.tojson !== 'undefined') ? self.tojson : false; | ||
var spread = (typeof self._spread !== 'undefined') ? self._spread : false; | ||
var isSingle = (self.entry_uid || self.singleEntry) ? true : false; | ||
var hashQuery = Utils.getHash(Utils.parseQueryFromParams(self, isSingle, tojson)); | ||
var spreadResult; | ||
var getCacheCallback = function () { | ||
return function (err, entries) { | ||
try { | ||
if (err) throw err; | ||
if (!tojson) entries = Utils.resultWrapper(entries); | ||
spreadResult = Utils.spreadResult(entries); | ||
if(isSingle) { | ||
success(spreadResult); | ||
} else if (spread) { | ||
success.apply(null, spreadResult); | ||
} else { | ||
success(spreadResult); | ||
} | ||
} catch (e) { | ||
fail(e); | ||
} | ||
} | ||
}; | ||
var callback = function (continueFlag) { | ||
if(continueFlag) { | ||
Request(params, function (err, data) { | ||
try { | ||
self.queryCachePolicy = undefined; | ||
self.tojson = undefined; | ||
self._spread = undefined; | ||
self.entry_uid = undefined; | ||
if (!err) { | ||
var entries = {}; | ||
if (this.singleEntry) { | ||
if (data.entries && data.entries.length) { | ||
entries.entry = Utils.getData(data, this.headers.access_token).entries[0]; | ||
if(data.schema) entries.schema = data.schema; | ||
} else { | ||
if(cachePolicy === 2) { | ||
self.provider.get(hashQuery, getCacheCallback()); | ||
} else { | ||
fail({ error_code: 141, error_message: 'The requested entry doesn\'t exist.' }); | ||
} | ||
return; | ||
} | ||
this.singleEntry = false; | ||
} else { | ||
entries = Utils.getData(data, this.headers.access_token); | ||
} | ||
if(cachePolicy !== -1) { | ||
self.provider.set(hashQuery, entries, function (err) { | ||
try { | ||
if (err) throw err; | ||
if(!tojson) entries = Utils.resultWrapper(entries); | ||
spreadResult = Utils.spreadResult(entries); | ||
if(isSingle) { | ||
success(spreadResult); | ||
} else if (spread) { | ||
success.apply(null, spreadResult); | ||
} else { | ||
success(spreadResult); | ||
} | ||
} catch(e) { | ||
fail(e); | ||
} | ||
}); | ||
} else { | ||
if(!tojson) entries = Utils.resultWrapper(entries); | ||
spreadResult = Utils.spreadResult(entries); | ||
if(isSingle) { | ||
success(spreadResult); | ||
} else if (spread) { | ||
success.apply(null, spreadResult); | ||
} else { | ||
success(spreadResult); | ||
} | ||
} | ||
} else { | ||
if(cachePolicy === 2) { | ||
self.provider.get(hashQuery, getCacheCallback()); | ||
} else { | ||
fail(err); | ||
} | ||
} | ||
} catch (e) { | ||
fail({ | ||
message: e.message | ||
}); | ||
} | ||
}.bind(self)); | ||
} | ||
}; | ||
switch (cachePolicy) { | ||
case 1: | ||
case 3: | ||
self.provider.get(hashQuery, function (err, _data) { | ||
try { | ||
var flag = false; | ||
if(err) { | ||
flag = true; | ||
fail(err); | ||
} else { | ||
if(cachePolicy === 3 || !_data) flag = true; | ||
if(_data) { | ||
if (!tojson) _data = Utils.resultWrapper(_data); | ||
spreadResult = Utils.spreadResult(_data); | ||
if(isSingle) { | ||
success(spreadResult); | ||
} else if (spread) { | ||
success.apply(null, spreadResult); | ||
} else { | ||
success(spreadResult); | ||
} | ||
} else if(cachePolicy === 3) { | ||
success(_data); | ||
} | ||
} | ||
callback(flag); | ||
} catch(e) { | ||
fail(e); | ||
} | ||
}); | ||
break; | ||
case 2: | ||
case 0: | ||
case -1: | ||
callback(true); | ||
}; | ||
}; | ||
module.exports = Entry; |
@@ -434,3 +434,3 @@ 'use strict'; | ||
headers: this.headers, | ||
url: this.config.protocol + "://" + this.config.host + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries, | ||
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries, | ||
body: { | ||
@@ -441,3 +441,3 @@ _method: 'GET', | ||
}; | ||
return this; | ||
return Utils.sendRequest(this); | ||
}; | ||
@@ -457,3 +457,3 @@ | ||
headers: this.headers, | ||
url: this.config.protocol + "://" + this.config.host + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries, | ||
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries, | ||
body: { | ||
@@ -464,5 +464,5 @@ _method: 'GET', | ||
}; | ||
return this; | ||
return Utils.sendRequest(this); | ||
}; | ||
module.exports = Query; |
@@ -9,3 +9,3 @@ 'use strict'; | ||
var cache = require('./cache'); | ||
var config = require('./../config'); | ||
var Config = require('./../config.js'); | ||
var Entry = require('./entry/entry'); | ||
@@ -22,6 +22,42 @@ var Query = require('./entry/query'); | ||
function Stack() { | ||
this.config = config; | ||
this.config = Config; | ||
} | ||
/** | ||
* @method setPort | ||
* @description Sets the port of the host. | ||
* @param {Number} port - Port Number | ||
* @return Stack | ||
* */ | ||
Stack.prototype.setPort = function (port) { | ||
if(typeof port === "number") this.config.port = port; | ||
this._environment(this.environment, this.headers.api_key, true); | ||
return this; | ||
} | ||
/** | ||
* @method setProtocol | ||
* @description Sets the protocol of the host. | ||
* @param {String} protocol - http/https protocol | ||
* @return Stack | ||
* */ | ||
Stack.prototype.setProtocol = function (protocol) { | ||
if(typeof protocol === "string" && ~["https", "http"].indexOf(protocol)) this.config.protocol = protocol; | ||
this._environment(this.environment, this.headers.api_key, true); | ||
return this; | ||
} | ||
/** | ||
* @method setHost | ||
* @description Sets the host of the API server. | ||
* @param {String} host - valid ip or host | ||
* @return Stack | ||
* */ | ||
Stack.prototype.setHost = function (host) { | ||
if(typeof host === "string" && host) this.config.host = host; | ||
this._environment(this.environment, this.headers.api_key, true); | ||
return this; | ||
} | ||
/** | ||
* Initialize headers and environment. | ||
@@ -33,2 +69,4 @@ * @api protected | ||
var self = this; | ||
self.cachePolicy = CacheProvider.policies.IGNORE_CACHE; | ||
self.provider = CacheProvider.providers('localstorage'); | ||
return function () { | ||
@@ -43,5 +81,3 @@ switch (arguments.length) { | ||
self.environment = arguments[0].environment; | ||
self._environment(arguments[0].environment, arguments[0].api_key); | ||
self.setCachePolicy(CacheProvider.policies.ONLY_NETWORK); | ||
self.setCacheProvider(CacheProvider.providers('localstorage')); | ||
self._environment(self.environment, self.headers.api_key); | ||
return self; | ||
@@ -57,6 +93,4 @@ } else { | ||
}; | ||
self.cachePolicy = CacheProvider.policies.IGNORE_CACHE; | ||
self.provider = CacheProvider.providers('localstorage'); | ||
self.environment = arguments[2]; | ||
self._environment(arguments[2], arguments[0]); | ||
self._environment(self.environment, self.headers.api_key); | ||
return self; | ||
@@ -80,15 +114,14 @@ } else { | ||
*/ | ||
Stack.prototype._environment = function (env, api_key) { | ||
Stack.prototype._environment = function (env, api_key, force) { | ||
var self = this, | ||
key = api_key + '.environment.' + env; | ||
var environmentUid = cache.get(key); | ||
if (environmentUid) { | ||
var environmentUid = self.environment_uid || cache.get(key); | ||
if (environmentUid && !force) { | ||
self.environment_uid = environmentUid; | ||
} else { | ||
Request({ | ||
url: self.config.protocol + "://" + self.config.host + '/' + self.config.version + self.config.urls.environments + env, | ||
url: self.config.protocol + "://" + self.config.host + ':' + self.config.port + '/' + self.config.version + self.config.urls.environments + env, | ||
headers: self.headers | ||
}, function (err, data) { | ||
}).then(function (data) { | ||
try { | ||
if (err) throw err; | ||
if (data && data.environment && data.environment.uid) { | ||
@@ -101,3 +134,6 @@ cache.set(key, data.environment.uid); | ||
} | ||
}).catch(function (err) { | ||
console.error('Could not retrieve the environment due to %s error: ', err.message); | ||
}); | ||
} | ||
@@ -253,3 +289,3 @@ }; | ||
headers: this.headers, | ||
url: this.config.protocol + "://" + this.config.host + '/' + this.config.version + this.config.urls.content_types, | ||
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types, | ||
body: { | ||
@@ -267,13 +303,3 @@ _method: 'GET', | ||
} | ||
return { | ||
then: function (success, fail) { | ||
Request(query, function (err, result) { | ||
if(!err) { | ||
success(result); | ||
} else { | ||
fail(err); | ||
} | ||
}); | ||
} | ||
}; | ||
return Request(query); | ||
}; | ||
@@ -280,0 +306,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
436321
24
9241
3
19
+ Addedwhen@3.7.7
+ Addedwhen@3.7.7(transitive)