contentstack
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -138,3 +138,3 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
// merge two objects | ||
exports._merge = function(target, source) { | ||
exports.mergeDeep = function(target, source) { | ||
var self = this; | ||
@@ -156,2 +156,12 @@ var _merge_recursive = function(target, source) { | ||
// merge two objects | ||
exports.merge = function(target, source) { | ||
if (target && source) { | ||
for (var key in source) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
}; | ||
// set access token | ||
@@ -213,7 +223,3 @@ exports.setAccessToken = function(entry) { | ||
var query = this._query[type]['BASE'] || []; | ||
if (typeof arguments[0] === "string") { | ||
query.push(arguments[0]); | ||
} else { | ||
query = query.concat(arguments[0]); | ||
} | ||
query = query.concat(arguments[0]); | ||
this._query[type]['BASE'] = query; | ||
@@ -228,7 +234,3 @@ return this; | ||
var query = this._query[type][arguments[0]] || []; | ||
if (typeof arguments[1] === "string") { | ||
query.push(arguments[1]); | ||
} else { | ||
query = query.concat(arguments[1]); | ||
} | ||
query = query.concat(arguments[1]); | ||
this._query[type][arguments[0]] = query; | ||
@@ -415,8 +417,7 @@ return this; | ||
/** | ||
* Helper for expect and only | ||
* @api private | ||
*/ | ||
var _extend = { | ||
compare: function (type) { | ||
return function (key, value) { | ||
compare: function(type) { | ||
return function(key, value) { | ||
if (key && value && typeof key === 'string' && typeof value === 'string') { | ||
@@ -431,5 +432,5 @@ this._query['query'][key] = this._query['query'][key] || {}; | ||
}, | ||
contained: function (bool) { | ||
contained: function(bool) { | ||
var type = (bool) ? '$in' : '$nin'; | ||
return function (key, value) { | ||
return function(key, value) { | ||
if (key && value && typeof key === 'string' && Array.isArray(value)) { | ||
@@ -445,4 +446,4 @@ this._query['query'][key] = this._query['query'][key] || {}; | ||
}, | ||
exists: function (bool) { | ||
return function (key) { | ||
exists: function(bool) { | ||
return function(key) { | ||
if (key && typeof key === 'string') { | ||
@@ -457,7 +458,9 @@ this._query['query'][key] = this._query['query'][key] || {}; | ||
}, | ||
logical: function (type) { | ||
return function () { | ||
logical: function(type) { | ||
return function() { | ||
var _query = []; | ||
for (var i = 0, _i = arguments.length; i < _i; i++) { | ||
if (typeof arguments[i] === "object") { | ||
if (arguments[i] instanceof Query && arguments[i]._query.query) { | ||
_query.push(arguments[i]._query.query); | ||
} else if (typeof arguments[i] === "object") { | ||
_query.push(arguments[i]); | ||
@@ -474,4 +477,4 @@ } | ||
}, | ||
sort: function (type) { | ||
return function (key) { | ||
sort: function(type) { | ||
return function(key) { | ||
if (key && typeof key === 'string') { | ||
@@ -483,6 +486,6 @@ this._query[type] = key; | ||
} | ||
} | ||
}; | ||
}, | ||
pagination: function (type) { | ||
return function (value) { | ||
pagination: function(type) { | ||
return function(value) { | ||
if (value && typeof value === 'number') { | ||
@@ -516,3 +519,3 @@ this._query[type] = value; | ||
Query.prototype.getQuery = function () { | ||
return JSON.parse(JSON.stringify(this._query.query)); | ||
return this._query.query || {}; | ||
}; | ||
@@ -528,3 +531,3 @@ | ||
*/ | ||
Query.prototype.where = function (key, value) { | ||
Query.prototype.where = function(key, value) { | ||
if (key && value && typeof key === 'string' && typeof value === 'string') { | ||
@@ -547,3 +550,3 @@ this._query['query'][key] = value; | ||
*/ | ||
Query.prototype.regex = function (key, value, options) { | ||
Query.prototype.regex = function(key, value, options) { | ||
if (key && value && typeof key === 'string' && typeof value === 'string') { | ||
@@ -567,3 +570,3 @@ this._query['query'][key] = { | ||
*/ | ||
Query.prototype.search = function (value) { | ||
Query.prototype.search = function(value) { | ||
if (value && typeof value === 'string') { | ||
@@ -687,5 +690,5 @@ this._query['typeahead'] = value; | ||
*/ | ||
Query.prototype.query = function (query) { | ||
Query.prototype.query = function(query) { | ||
if (typeof query === "object") { | ||
this._query['query'] = Utils._merge(this._query['query'], query); | ||
this._query['query'] = Utils.mergeDeep(this._query['query'], query); | ||
return this; | ||
@@ -703,3 +706,3 @@ } else { | ||
*/ | ||
Query.prototype.tags = function (values) { | ||
Query.prototype.tags = function(values) { | ||
if (Array.isArray(values)) { | ||
@@ -737,3 +740,3 @@ this._query['tags'] = values; | ||
*/ | ||
Query.prototype.include_count = function () { | ||
Query.prototype.include_count = function() { | ||
this._query['include_count'] = true; | ||
@@ -784,3 +787,3 @@ return this; | ||
*/ | ||
Query.prototype.count = function () { | ||
Query.prototype.count = function() { | ||
this._query['count'] = true; | ||
@@ -803,3 +806,3 @@ this.requestParams = { | ||
*/ | ||
Query.prototype.find = function () { | ||
Query.prototype.find = function() { | ||
this.requestParams = { | ||
@@ -821,3 +824,3 @@ method: 'POST', | ||
*/ | ||
Query.prototype.findOne = function () { | ||
Query.prototype.findOne = function() { | ||
this.singleEntry = true; | ||
@@ -878,5 +881,5 @@ this._query.limit = 1; | ||
var Request = require('./../lib/request'); | ||
var Utils = require('../lib/utils'); | ||
var Utils = require('./../lib/utils'); | ||
var siteCache = require('./site-cache'); | ||
var config = require('../config'); | ||
var config = require('./../config'); | ||
var Entry = require('./entry/entry'); | ||
@@ -989,4 +992,4 @@ var Query = require('./entry/query'); | ||
} | ||
this.__proto__.__proto__ = new Entry(); | ||
return Object.create(this); | ||
var entry = new Entry(); | ||
return Utils.merge(entry, this); | ||
}; | ||
@@ -1000,4 +1003,4 @@ | ||
Site.prototype.Query = function () { | ||
this.__proto__.__proto__ = new Query(); | ||
return Object.create(this); | ||
var query = new Query(); | ||
return Utils.merge(query, this); | ||
}; | ||
@@ -1010,3 +1013,3 @@ | ||
},{"../config":1,"../lib/utils":4,"./../lib/request":3,"./entry/entry":5,"./entry/query":6,"./site-cache":7}],9:[function(require,module,exports){ | ||
},{"./../config":1,"./../lib/request":3,"./../lib/utils":4,"./entry/entry":5,"./entry/query":6,"./site-cache":7}],9:[function(require,module,exports){ | ||
@@ -1013,0 +1016,0 @@ },{}],10:[function(require,module,exports){ |
@@ -1,1 +0,1 @@ | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){module.exports={protocol:"https",host:"api.contentstack.io",version:"v2"}},{}],2:[function(require,module,exports){"use strict";var Site=require("./src/site");var Utils=require("./lib/utils");var Contentstack=function(){};Contentstack.prototype.Site=Site.initialize();var contentstack=new Contentstack;if(Utils.isBrowser()){window.Contentstack=contentstack}else{module.exports=contentstack}},{"./lib/utils":4,"./src/site":8}],3:[function(require,module,exports){"use strict";var HTTPRequest;var Utils=require("./utils");if(Utils.isBrowser()&&XMLHttpRequest){HTTPRequest=XMLHttpRequest}else{HTTPRequest=require("xmlhttprequest").XMLHttpRequest}function Request(options,callback){var xhr=new HTTPRequest,method=options.method||"GET",url=options.url,headers=options.headers;xhr.open(method,url,true);xhr.setRequestHeader("Content-Type","application/json; charset=UTF-8");for(var header in headers){xhr.setRequestHeader(header,headers[header])}if(options.body&&method=="POST"||method=="PUT"){if(typeof options.body==="object"){xhr.send(JSON.stringify(options.body))}else{xhr.send(options.body)}}else{xhr.send()}xhr.onreadystatechange=function(){if(xhr.readyState===4){var data=xhr.responseText;try{data=JSON.parse(data)}catch(e){console.error("Could not parse the response received from the server.")}if(xhr.status>=200&&xhr.status<300){callback(null,data)}else{callback(data,null)}}}}module.exports=Request},{"./utils":4,xmlhttprequest:9}],4:[function(require,module,exports){(function(process){"use strict";exports._type=function(val){var _typeof,__typeof=typeof val;switch(__typeof){case"object":_typeof=__typeof;if(Array.isArray(val)){__typeof="array"}break;default:_typeof=__typeof}return __typeof};exports._merge=function(target,source){var self=this;var _merge_recursive=function(target,source){for(var key in source){if(self._type(source[key])=="object"&&self._type(target[key])==self._type(source[key])){_merge_recursive(target[key],source[key])}else if(self._type(source[key])=="array"&&self._type(target[key])==self._type(source[key])){target[key]=target[key].concat(source[key])}else{target[key]=source[key]}}};_merge_recursive(target,source);return target};exports.setAccessToken=function(entry){if(typeof entry==="object"){for(var key in entry){if(key==="content_type"&&entry["url"]&&entry["uid"]){entry["url"]+=(~entry["url"].indexOf("?")?"&":"?")+this.token_queryString}else if(typeof entry[key]==="string"){entry[key]=entry[key].replace(/https:[/][/](dev-|stag-|)api.(built|contentstack).io[/](.*?)[/]download(.*?)uid=([a-z0-9]+)/gi,function(val){return val+"&"+this.token_queryString}.bind(this))}else if(typeof entry[key]==="object"){this.setAccessToken(entry[key])}}}return entry};exports.getData=function(data,token_queryString){this.token_queryString="AUTHTOKEN="+token_queryString;if(Array.isArray(data.entries)){data.entries.forEach(function(val){this.setAccessToken(val)}.bind(this))}else if(data.entry){this.setAccessToken(data.entry)}return data};exports.isBrowser=function(){return typeof window!=="undefined"&&typeof process==="object"&&process.title==="browser"}}).call(this,require("_process"))},{_process:10}],5:[function(require,module,exports){"use strict";var Request=require("./../../lib/request");var Utils=require("./../../lib/utils");var _extend=function(type){return function(){this._query[type]=this._query[type]||{};switch(arguments.length){case 1:if(Array.isArray(arguments[0])||typeof arguments[0]==="string"){var query=this._query[type]["BASE"]||[];if(typeof arguments[0]==="string"){query.push(arguments[0])}else{query=query.concat(arguments[0])}this._query[type]["BASE"]=query;return this}else{console.error("Kindly provide valid parameters")}break;case 2:if(typeof arguments[0]==="string"&&(Array.isArray(arguments[1])||typeof arguments[1]==="string")){var query=this._query[type][arguments[0]]||[];if(typeof arguments[1]==="string"){query.push(arguments[1])}else{query=query.concat(arguments[1])}this._query[type][arguments[0]]=query;return this}else{console.error("Kindly provide valid parameters")}break;default:console.error("Kindly provide valid parameters")}}};function Entry(){this._query={};return this}Entry.prototype.only=_extend("only");Entry.prototype.except=_extend("except");Entry.prototype.include=function(val){if(Array.isArray(val)||typeof val==="string"){this._query["include"]=this._query["include"]||[];this._query["include"]=this._query["include"].concat(val);return this}else{console.error("Argument should be a String or an Array.")}};Entry.prototype.locale=function(locale_code){if(locale_code&&typeof locale_code==="string"){this._query["locale"]=locale_code;return this}else{console.error("Argument should be a String.")}};Entry.prototype.addQuery=function(key,value){if(key&&value&&typeof key==="string"){this._query[key]=value;return this}else{console.error("First argument should be a String.")}};Entry.prototype.include_schema=function(){this._query["include_schema"]=true;return this};Entry.prototype.include_owner=function(){this._query["include_owner"]=true;return this};Entry.prototype.fetch=function(){if(this.entry_uid){this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries/"+this.entry_uid,body:{_method:"GET",query:this._query}};return this}else{console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')")}};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}Request(params,function(err,data){try{if(!err){if(this.singleEntry){var entries={};if(data.entries&&data.entries.length){entries.entry=Utils.getData(data,this.headers.access_token).entries[0];success(entries)}else{fail({error_code:141,error_message:"The requested entry doesn't exist."})}this.singleEntry=false}else{success(Utils.getData(data,this.headers.access_token))}}else{fail(err)}}catch(e){fail({message:e.message})}}.bind(this))};module.exports=Entry},{"./../../lib/request":3,"./../../lib/utils":4}],6:[function(require,module,exports){"use strict";var Request=require("./../../lib/request");var Utils=require("./../../lib/utils");var Entry=require("./entry");var _extend={compare:function(type){return function(key,value){if(key&&value&&typeof key==="string"&&typeof value==="string"){this._query["query"][key]=this._query["query"][key]||{};this._query["query"][key][type]=value;return this}else{console.error("Kindly provide valid parameters.")}}},contained:function(bool){var type=bool?"$in":"$nin";return function(key,value){if(key&&value&&typeof key==="string"&&Array.isArray(value)){this._query["query"][key]=this._query["query"][key]||{};this._query["query"][key][type]=this._query["query"][key][type]||[];this._query["query"][key][type]=this._query["query"][key][type].concat(value);return this}else{console.error("Kindly provide valid parameters.")}}},exists:function(bool){return function(key){if(key&&typeof key==="string"){this._query["query"][key]=this._query["query"][key]||{};this._query["query"][key]["$exists"]=bool;return this}else{console.error("Kindly provide valid parameters.")}}},logical:function(type){return function(){var _query=[];for(var i=0,_i=arguments.length;i<_i;i++){if(typeof arguments[i]==="object"){_query.push(arguments[i])}}if(this._query["query"][type]){this._query["query"][type]=this._query["query"][type].concat(_query)}else{this._query["query"][type]=_query}return this}},sort:function(type){return function(key){if(key&&typeof key==="string"){this._query[type]=key;return this}else{console.error("Argument should be a string.")}}},pagination:function(type){return function(value){if(value&&typeof value==="number"){this._query[type]=value;return this}else{console.error("Argument should be a number.")}}}};function Query(){Entry.call(this);this._query=this._query||{};this._query["query"]=this._query["query"]||{}}Query.prototype=Object.create(Entry.prototype);Query.prototype.getQuery=function(){return JSON.parse(JSON.stringify(this._query.query))};Query.prototype.where=function(key,value){if(key&&value&&typeof key==="string"&&typeof value==="string"){this._query["query"][key]=value;return this}else{console.error("Kindly provide valid parameters.")}};Query.prototype.regex=function(key,value,options){if(key&&value&&typeof key==="string"&&typeof value==="string"){this._query["query"][key]={$regex:value};if(options)this._query["query"][key]["$options"]=options;return this}else{console.error("Kindly provide valid parameters.")}};Query.prototype.search=function(value){if(value&&typeof value==="string"){this._query["typeahead"]=value;return this}else{console.error("Kindly provide valid parameters.")}};Query.prototype.lessThan=_extend.compare("$lt");Query.prototype.lessThanOrEqualTo=_extend.compare("$lte");Query.prototype.greaterThan=_extend.compare("$gt");Query.prototype.greaterThanOrEqualTo=_extend.compare("$gte");Query.prototype.notEqualTo=_extend.compare("$ne");Query.prototype.containedIn=_extend.contained(true);Query.prototype.notContainedIn=_extend.contained(false);Query.prototype.exists=_extend.exists(true);Query.prototype.notExists=_extend.exists(false);Query.prototype.or=_extend.logical("$or");Query.prototype.and=_extend.logical("$and");Query.prototype.query=function(query){if(typeof query==="object"){this._query["query"]=Utils._merge(this._query["query"],query);return this}else{console.error("Kindly provide valid parameters")}};Query.prototype.tags=function(values){if(Array.isArray(values)){this._query["tags"]=values;return this}else{console.error("Kindly provide valid parameters")}};Query.prototype.ascending=_extend.sort("asc");Query.prototype.descending=_extend.sort("desc");Query.prototype.include_count=function(){this._query["include_count"]=true;return this};Query.prototype.beforeUid=_extend.sort("before_uid");Query.prototype.afterUid=_extend.sort("after_uid");Query.prototype.skip=_extend.pagination("skip");Query.prototype.limit=_extend.pagination("limit");Query.prototype.count=function(){this._query["count"]=true;this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries",body:{_method:"GET",query:this._query}};return this};Query.prototype.find=function(){this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries",body:{_method:"GET",query:this._query}};return this};Query.prototype.findOne=function(){this.singleEntry=true;this._query.limit=1;this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries",body:{_method:"GET",query:this._query}};return this};module.exports=Query},{"./../../lib/request":3,"./../../lib/utils":4,"./entry":5}],7:[function(require,module,exports){"use strict";var cacheStorage;var Utils=require("./../lib/utils");if(Utils.isBrowser()&&localStorage){cacheStorage=localStorage}else{cacheStorage=require("localStorage")}exports.get=function(key){var data=cacheStorage.getItem(key);try{data=JSON.parse(data)}catch(e){}return data||null};exports.set=function(key,data){if(typeof data==="object"){cacheStorage.setItem(key,JSON.stringify(data))}else{cacheStorage.setItem(key,data)}}},{"./../lib/utils":4,localStorage:9}],8:[function(require,module,exports){"use strict";var Request=require("./../lib/request");var Utils=require("../lib/utils");var siteCache=require("./site-cache");var config=require("../config");var Entry=require("./entry/entry");var Query=require("./entry/query");function Site(){this.config=config}Site.prototype.initialize=function(){var self=this;return function(){switch(arguments.length){case 1:if(typeof arguments[0]==="object"&&typeof arguments[0].site_api_key==="string"&&typeof arguments[0].access_token==="string"&&typeof arguments[0].environment==="string"){self.headers={site_api_key:arguments[0].site_api_key,access_token:arguments[0].access_token};self.environment=arguments[0].environment;self._environment(arguments[0].environment,arguments[0].site_api_key);return self}else{console.error("Kindly provide object parameters.")}case 3:if(typeof arguments[0]==="string"&&typeof arguments[1]==="string"&&typeof arguments[2]==="string"){self.headers={site_api_key:arguments[0],access_token:arguments[1]};self.environment=arguments[2];self._environment(arguments[2],arguments[0]);return self}else{console.error("Kindly provide string parameters.")}default:console.error("Kindly provide valid parameters to initialize the Built.io Contentstack Javascript SDK.")}}};Site.prototype._environment=function(env,site_api_key){var self=this,environmentCacheKey=site_api_key+".environment."+env;var cachedData=siteCache.get(environmentCacheKey);if(cachedData){self.environment_uid=cachedData}else{Request({url:self.config.protocol+"://"+self.config.host+"/"+self.config.version+"/environments/"+env,headers:self.headers},function(err,data){try{if(err)throw err;if(data&&data.environment&&data.environment.uid){siteCache.set(environmentCacheKey,data.environment.uid);self.environment_uid=data.environment.uid}}catch(e){console.error("Could not retrieve the environment due to following error: "+e.message)}})}};Site.prototype.Form=function(uid){if(uid&&typeof uid==="string"){this.form_uid=uid}return this};Site.prototype.Entry=function(uid){if(uid&&typeof uid==="string"){this.entry_uid=uid}this.__proto__.__proto__=new Entry;return Object.create(this)};Site.prototype.Query=function(){this.__proto__.__proto__=new Query;return Object.create(this)};module.exports=new Site},{"../config":1,"../lib/utils":4,"./../lib/request":3,"./entry/entry":5,"./entry/query":6,"./site-cache":7}],9:[function(require,module,exports){},{}],10:[function(require,module,exports){var process=module.exports={};var queue=[];var draining=false;var currentQueue;var queueIndex=-1;function cleanUpNextTick(){draining=false;if(currentQueue.length){queue=currentQueue.concat(queue)}else{queueIndex=-1}if(queue.length){drainQueue()}}function drainQueue(){if(draining){return}var timeout=setTimeout(cleanUpNextTick);draining=true;var len=queue.length;while(len){currentQueue=queue;queue=[];while(++queueIndex<len){currentQueue[queueIndex].run()}queueIndex=-1;len=queue.length}currentQueue=null;draining=false;clearTimeout(timeout)}process.nextTick=function(fun){var args=new Array(arguments.length-1);if(arguments.length>1){for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i]}}queue.push(new Item(fun,args));if(!draining){setTimeout(drainQueue,0)}};function Item(fun,array){this.fun=fun;this.array=array}Item.prototype.run=function(){this.fun.apply(null,this.array)};process.title="browser";process.browser=true;process.env={};process.argv=[];process.version="";process.versions={};function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")};process.umask=function(){return 0}},{}]},{},[2]); | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){module.exports={protocol:"https",host:"api.contentstack.io",version:"v2"}},{}],2:[function(require,module,exports){"use strict";var Site=require("./src/site");var Utils=require("./lib/utils");var Contentstack=function(){};Contentstack.prototype.Site=Site.initialize();var contentstack=new Contentstack;if(Utils.isBrowser()){window.Contentstack=contentstack}else{module.exports=contentstack}},{"./lib/utils":4,"./src/site":8}],3:[function(require,module,exports){"use strict";var HTTPRequest;var Utils=require("./utils");if(Utils.isBrowser()&&XMLHttpRequest){HTTPRequest=XMLHttpRequest}else{HTTPRequest=require("xmlhttprequest").XMLHttpRequest}function Request(options,callback){var xhr=new HTTPRequest,method=options.method||"GET",url=options.url,headers=options.headers;xhr.open(method,url,true);xhr.setRequestHeader("Content-Type","application/json; charset=UTF-8");for(var header in headers){xhr.setRequestHeader(header,headers[header])}if(options.body&&method=="POST"||method=="PUT"){if(typeof options.body==="object"){xhr.send(JSON.stringify(options.body))}else{xhr.send(options.body)}}else{xhr.send()}xhr.onreadystatechange=function(){if(xhr.readyState===4){var data=xhr.responseText;try{data=JSON.parse(data)}catch(e){console.error("Could not parse the response received from the server.")}if(xhr.status>=200&&xhr.status<300){callback(null,data)}else{callback(data,null)}}}}module.exports=Request},{"./utils":4,xmlhttprequest:9}],4:[function(require,module,exports){(function(process){"use strict";exports._type=function(val){var _typeof,__typeof=typeof val;switch(__typeof){case"object":_typeof=__typeof;if(Array.isArray(val)){__typeof="array"}break;default:_typeof=__typeof}return __typeof};exports.mergeDeep=function(target,source){var self=this;var _merge_recursive=function(target,source){for(var key in source){if(self._type(source[key])=="object"&&self._type(target[key])==self._type(source[key])){_merge_recursive(target[key],source[key])}else if(self._type(source[key])=="array"&&self._type(target[key])==self._type(source[key])){target[key]=target[key].concat(source[key])}else{target[key]=source[key]}}};_merge_recursive(target,source);return target};exports.merge=function(target,source){if(target&&source){for(var key in source){target[key]=source[key]}}return target};exports.setAccessToken=function(entry){if(typeof entry==="object"){for(var key in entry){if(key==="content_type"&&entry["url"]&&entry["uid"]){entry["url"]+=(~entry["url"].indexOf("?")?"&":"?")+this.token_queryString}else if(typeof entry[key]==="string"){entry[key]=entry[key].replace(/https:[/][/](dev-|stag-|)api.(built|contentstack).io[/](.*?)[/]download(.*?)uid=([a-z0-9]+)/gi,function(val){return val+"&"+this.token_queryString}.bind(this))}else if(typeof entry[key]==="object"){this.setAccessToken(entry[key])}}}return entry};exports.getData=function(data,token_queryString){this.token_queryString="AUTHTOKEN="+token_queryString;if(Array.isArray(data.entries)){data.entries.forEach(function(val){this.setAccessToken(val)}.bind(this))}else if(data.entry){this.setAccessToken(data.entry)}return data};exports.isBrowser=function(){return typeof window!=="undefined"&&typeof process==="object"&&process.title==="browser"}}).call(this,require("_process"))},{_process:10}],5:[function(require,module,exports){"use strict";var Request=require("./../../lib/request");var Utils=require("./../../lib/utils");var _extend=function(type){return function(){this._query[type]=this._query[type]||{};switch(arguments.length){case 1:if(Array.isArray(arguments[0])||typeof arguments[0]==="string"){var query=this._query[type]["BASE"]||[];query=query.concat(arguments[0]);this._query[type]["BASE"]=query;return this}else{console.error("Kindly provide valid parameters")}break;case 2:if(typeof arguments[0]==="string"&&(Array.isArray(arguments[1])||typeof arguments[1]==="string")){var query=this._query[type][arguments[0]]||[];query=query.concat(arguments[1]);this._query[type][arguments[0]]=query;return this}else{console.error("Kindly provide valid parameters")}break;default:console.error("Kindly provide valid parameters")}}};function Entry(){this._query={};return this}Entry.prototype.only=_extend("only");Entry.prototype.except=_extend("except");Entry.prototype.include=function(val){if(Array.isArray(val)||typeof val==="string"){this._query["include"]=this._query["include"]||[];this._query["include"]=this._query["include"].concat(val);return this}else{console.error("Argument should be a String or an Array.")}};Entry.prototype.locale=function(locale_code){if(locale_code&&typeof locale_code==="string"){this._query["locale"]=locale_code;return this}else{console.error("Argument should be a String.")}};Entry.prototype.addQuery=function(key,value){if(key&&value&&typeof key==="string"){this._query[key]=value;return this}else{console.error("First argument should be a String.")}};Entry.prototype.include_schema=function(){this._query["include_schema"]=true;return this};Entry.prototype.include_owner=function(){this._query["include_owner"]=true;return this};Entry.prototype.fetch=function(){if(this.entry_uid){this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries/"+this.entry_uid,body:{_method:"GET",query:this._query}};return this}else{console.error("Kindly provide an entry uid. e.g. .Entry('bltsomething123')")}};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}Request(params,function(err,data){try{if(!err){if(this.singleEntry){var entries={};if(data.entries&&data.entries.length){entries.entry=Utils.getData(data,this.headers.access_token).entries[0];success(entries)}else{fail({error_code:141,error_message:"The requested entry doesn't exist."})}this.singleEntry=false}else{success(Utils.getData(data,this.headers.access_token))}}else{fail(err)}}catch(e){fail({message:e.message})}}.bind(this))};module.exports=Entry},{"./../../lib/request":3,"./../../lib/utils":4}],6:[function(require,module,exports){"use strict";var Request=require("./../../lib/request");var Utils=require("./../../lib/utils");var Entry=require("./entry");var _extend={compare:function(type){return function(key,value){if(key&&value&&typeof key==="string"&&typeof value==="string"){this._query["query"][key]=this._query["query"][key]||{};this._query["query"][key][type]=value;return this}else{console.error("Kindly provide valid parameters.")}}},contained:function(bool){var type=bool?"$in":"$nin";return function(key,value){if(key&&value&&typeof key==="string"&&Array.isArray(value)){this._query["query"][key]=this._query["query"][key]||{};this._query["query"][key][type]=this._query["query"][key][type]||[];this._query["query"][key][type]=this._query["query"][key][type].concat(value);return this}else{console.error("Kindly provide valid parameters.")}}},exists:function(bool){return function(key){if(key&&typeof key==="string"){this._query["query"][key]=this._query["query"][key]||{};this._query["query"][key]["$exists"]=bool;return this}else{console.error("Kindly provide valid parameters.")}}},logical:function(type){return function(){var _query=[];for(var i=0,_i=arguments.length;i<_i;i++){if(arguments[i]instanceof Query&&arguments[i]._query.query){_query.push(arguments[i]._query.query)}else if(typeof arguments[i]==="object"){_query.push(arguments[i])}}if(this._query["query"][type]){this._query["query"][type]=this._query["query"][type].concat(_query)}else{this._query["query"][type]=_query}return this}},sort:function(type){return function(key){if(key&&typeof key==="string"){this._query[type]=key;return this}else{console.error("Argument should be a string.")}}},pagination:function(type){return function(value){if(value&&typeof value==="number"){this._query[type]=value;return this}else{console.error("Argument should be a number.")}}}};function Query(){Entry.call(this);this._query=this._query||{};this._query["query"]=this._query["query"]||{}}Query.prototype=Object.create(Entry.prototype);Query.prototype.getQuery=function(){return this._query.query||{}};Query.prototype.where=function(key,value){if(key&&value&&typeof key==="string"&&typeof value==="string"){this._query["query"][key]=value;return this}else{console.error("Kindly provide valid parameters.")}};Query.prototype.regex=function(key,value,options){if(key&&value&&typeof key==="string"&&typeof value==="string"){this._query["query"][key]={$regex:value};if(options)this._query["query"][key]["$options"]=options;return this}else{console.error("Kindly provide valid parameters.")}};Query.prototype.search=function(value){if(value&&typeof value==="string"){this._query["typeahead"]=value;return this}else{console.error("Kindly provide valid parameters.")}};Query.prototype.lessThan=_extend.compare("$lt");Query.prototype.lessThanOrEqualTo=_extend.compare("$lte");Query.prototype.greaterThan=_extend.compare("$gt");Query.prototype.greaterThanOrEqualTo=_extend.compare("$gte");Query.prototype.notEqualTo=_extend.compare("$ne");Query.prototype.containedIn=_extend.contained(true);Query.prototype.notContainedIn=_extend.contained(false);Query.prototype.exists=_extend.exists(true);Query.prototype.notExists=_extend.exists(false);Query.prototype.or=_extend.logical("$or");Query.prototype.and=_extend.logical("$and");Query.prototype.query=function(query){if(typeof query==="object"){this._query["query"]=Utils.mergeDeep(this._query["query"],query);return this}else{console.error("Kindly provide valid parameters")}};Query.prototype.tags=function(values){if(Array.isArray(values)){this._query["tags"]=values;return this}else{console.error("Kindly provide valid parameters")}};Query.prototype.ascending=_extend.sort("asc");Query.prototype.descending=_extend.sort("desc");Query.prototype.include_count=function(){this._query["include_count"]=true;return this};Query.prototype.beforeUid=_extend.sort("before_uid");Query.prototype.afterUid=_extend.sort("after_uid");Query.prototype.skip=_extend.pagination("skip");Query.prototype.limit=_extend.pagination("limit");Query.prototype.count=function(){this._query["count"]=true;this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries",body:{_method:"GET",query:this._query}};return this};Query.prototype.find=function(){this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries",body:{_method:"GET",query:this._query}};return this};Query.prototype.findOne=function(){this.singleEntry=true;this._query.limit=1;this.requestParams={method:"POST",headers:this.headers,url:this.config.protocol+"://"+this.config.host+"/"+this.config.version+"/forms/"+this.form_uid+"/entries",body:{_method:"GET",query:this._query}};return this};module.exports=Query},{"./../../lib/request":3,"./../../lib/utils":4,"./entry":5}],7:[function(require,module,exports){"use strict";var cacheStorage;var Utils=require("./../lib/utils");if(Utils.isBrowser()&&localStorage){cacheStorage=localStorage}else{cacheStorage=require("localStorage")}exports.get=function(key){var data=cacheStorage.getItem(key);try{data=JSON.parse(data)}catch(e){}return data||null};exports.set=function(key,data){if(typeof data==="object"){cacheStorage.setItem(key,JSON.stringify(data))}else{cacheStorage.setItem(key,data)}}},{"./../lib/utils":4,localStorage:9}],8:[function(require,module,exports){"use strict";var Request=require("./../lib/request");var Utils=require("./../lib/utils");var siteCache=require("./site-cache");var config=require("./../config");var Entry=require("./entry/entry");var Query=require("./entry/query");function Site(){this.config=config}Site.prototype.initialize=function(){var self=this;return function(){switch(arguments.length){case 1:if(typeof arguments[0]==="object"&&typeof arguments[0].site_api_key==="string"&&typeof arguments[0].access_token==="string"&&typeof arguments[0].environment==="string"){self.headers={site_api_key:arguments[0].site_api_key,access_token:arguments[0].access_token};self.environment=arguments[0].environment;self._environment(arguments[0].environment,arguments[0].site_api_key);return self}else{console.error("Kindly provide object parameters.")}case 3:if(typeof arguments[0]==="string"&&typeof arguments[1]==="string"&&typeof arguments[2]==="string"){self.headers={site_api_key:arguments[0],access_token:arguments[1]};self.environment=arguments[2];self._environment(arguments[2],arguments[0]);return self}else{console.error("Kindly provide string parameters.")}default:console.error("Kindly provide valid parameters to initialize the Built.io Contentstack Javascript SDK.")}}};Site.prototype._environment=function(env,site_api_key){var self=this,environmentCacheKey=site_api_key+".environment."+env;var cachedData=siteCache.get(environmentCacheKey);if(cachedData){self.environment_uid=cachedData}else{Request({url:self.config.protocol+"://"+self.config.host+"/"+self.config.version+"/environments/"+env,headers:self.headers},function(err,data){try{if(err)throw err;if(data&&data.environment&&data.environment.uid){siteCache.set(environmentCacheKey,data.environment.uid);self.environment_uid=data.environment.uid}}catch(e){console.error("Could not retrieve the environment due to following error: "+e.message)}})}};Site.prototype.Form=function(uid){if(uid&&typeof uid==="string"){this.form_uid=uid}return this};Site.prototype.Entry=function(uid){if(uid&&typeof uid==="string"){this.entry_uid=uid}var entry=new Entry;return Utils.merge(entry,this)};Site.prototype.Query=function(){var query=new Query;return Utils.merge(query,this)};module.exports=new Site},{"./../config":1,"./../lib/request":3,"./../lib/utils":4,"./entry/entry":5,"./entry/query":6,"./site-cache":7}],9:[function(require,module,exports){},{}],10:[function(require,module,exports){var process=module.exports={};var queue=[];var draining=false;var currentQueue;var queueIndex=-1;function cleanUpNextTick(){draining=false;if(currentQueue.length){queue=currentQueue.concat(queue)}else{queueIndex=-1}if(queue.length){drainQueue()}}function drainQueue(){if(draining){return}var timeout=setTimeout(cleanUpNextTick);draining=true;var len=queue.length;while(len){currentQueue=queue;queue=[];while(++queueIndex<len){currentQueue[queueIndex].run()}queueIndex=-1;len=queue.length}currentQueue=null;draining=false;clearTimeout(timeout)}process.nextTick=function(fun){var args=new Array(arguments.length-1);if(arguments.length>1){for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i]}}queue.push(new Item(fun,args));if(!draining){setTimeout(drainQueue,0)}};function Item(fun,array){this.fun=fun;this.array=array}Item.prototype.run=function(){this.fun.apply(null,this.array)};process.title="browser";process.browser=true;process.env={};process.argv=[];process.version="";process.versions={};function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")};process.umask=function(){return 0}},{}]},{},[2]); |
@@ -41,2 +41,2 @@ /*! | ||
//displays a detailed error in case of failure; | ||
}); | ||
}); |
@@ -20,3 +20,3 @@ 'use strict'; | ||
// merge two objects | ||
exports._merge = function(target, source) { | ||
exports.mergeDeep = function(target, source) { | ||
var self = this; | ||
@@ -38,2 +38,12 @@ var _merge_recursive = function(target, source) { | ||
// merge two objects | ||
exports.merge = function(target, source) { | ||
if (target && source) { | ||
for (var key in source) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
}; | ||
// set access token | ||
@@ -40,0 +50,0 @@ exports.setAccessToken = function(entry) { |
{ | ||
"name": "contentstack", | ||
"version": "0.0.1", | ||
"description": "The Built.io Contentstack Javascript SDK", | ||
"homepage": "https://www.built.io/products/contentstack/overview", | ||
"author": "Built.io Contentstack", | ||
"main": "contentstack.js", | ||
"dependencies": { | ||
"xmlhttprequest": "^1.7.0", | ||
"localStorage": "1.0.x" | ||
}, | ||
"devDependencies": { | ||
"should": "^6.0.1" | ||
}, | ||
"browser": { | ||
"xmlhttprequest": false, | ||
"localStorage": false | ||
} | ||
"name": "contentstack", | ||
"version": "0.0.2", | ||
"description": "The Built.io Contentstack Javascript SDK", | ||
"homepage": "https://www.built.io/products/contentstack/overview", | ||
"author": { | ||
"name": "Built.io Contentstack" | ||
}, | ||
"main": "contentstack.js", | ||
"dependencies": { | ||
"xmlhttprequest": "^1.7.0", | ||
"localStorage": "1.0.x" | ||
}, | ||
"devDependencies": { | ||
"should": "^6.0.1" | ||
}, | ||
"browser": { | ||
"xmlhttprequest": false, | ||
"localStorage": false | ||
} | ||
} |
@@ -19,7 +19,3 @@ 'use strict'; | ||
var query = this._query[type]['BASE'] || []; | ||
if (typeof arguments[0] === "string") { | ||
query.push(arguments[0]); | ||
} else { | ||
query = query.concat(arguments[0]); | ||
} | ||
query = query.concat(arguments[0]); | ||
this._query[type]['BASE'] = query; | ||
@@ -34,7 +30,3 @@ return this; | ||
var query = this._query[type][arguments[0]] || []; | ||
if (typeof arguments[1] === "string") { | ||
query.push(arguments[1]); | ||
} else { | ||
query = query.concat(arguments[1]); | ||
} | ||
query = query.concat(arguments[1]); | ||
this._query[type][arguments[0]] = query; | ||
@@ -41,0 +33,0 @@ return this; |
@@ -10,8 +10,7 @@ 'use strict'; | ||
/** | ||
* Helper for expect and only | ||
* @api private | ||
*/ | ||
var _extend = { | ||
compare: function (type) { | ||
return function (key, value) { | ||
compare: function(type) { | ||
return function(key, value) { | ||
if (key && value && typeof key === 'string' && typeof value === 'string') { | ||
@@ -26,5 +25,5 @@ this._query['query'][key] = this._query['query'][key] || {}; | ||
}, | ||
contained: function (bool) { | ||
contained: function(bool) { | ||
var type = (bool) ? '$in' : '$nin'; | ||
return function (key, value) { | ||
return function(key, value) { | ||
if (key && value && typeof key === 'string' && Array.isArray(value)) { | ||
@@ -40,4 +39,4 @@ this._query['query'][key] = this._query['query'][key] || {}; | ||
}, | ||
exists: function (bool) { | ||
return function (key) { | ||
exists: function(bool) { | ||
return function(key) { | ||
if (key && typeof key === 'string') { | ||
@@ -52,7 +51,9 @@ this._query['query'][key] = this._query['query'][key] || {}; | ||
}, | ||
logical: function (type) { | ||
return function () { | ||
logical: function(type) { | ||
return function() { | ||
var _query = []; | ||
for (var i = 0, _i = arguments.length; i < _i; i++) { | ||
if (typeof arguments[i] === "object") { | ||
if (arguments[i] instanceof Query && arguments[i]._query.query) { | ||
_query.push(arguments[i]._query.query); | ||
} else if (typeof arguments[i] === "object") { | ||
_query.push(arguments[i]); | ||
@@ -69,4 +70,4 @@ } | ||
}, | ||
sort: function (type) { | ||
return function (key) { | ||
sort: function(type) { | ||
return function(key) { | ||
if (key && typeof key === 'string') { | ||
@@ -78,6 +79,6 @@ this._query[type] = key; | ||
} | ||
} | ||
}; | ||
}, | ||
pagination: function (type) { | ||
return function (value) { | ||
pagination: function(type) { | ||
return function(value) { | ||
if (value && typeof value === 'number') { | ||
@@ -111,3 +112,3 @@ this._query[type] = value; | ||
Query.prototype.getQuery = function () { | ||
return JSON.parse(JSON.stringify(this._query.query)); | ||
return this._query.query || {}; | ||
}; | ||
@@ -123,3 +124,3 @@ | ||
*/ | ||
Query.prototype.where = function (key, value) { | ||
Query.prototype.where = function(key, value) { | ||
if (key && value && typeof key === 'string' && typeof value === 'string') { | ||
@@ -142,3 +143,3 @@ this._query['query'][key] = value; | ||
*/ | ||
Query.prototype.regex = function (key, value, options) { | ||
Query.prototype.regex = function(key, value, options) { | ||
if (key && value && typeof key === 'string' && typeof value === 'string') { | ||
@@ -162,3 +163,3 @@ this._query['query'][key] = { | ||
*/ | ||
Query.prototype.search = function (value) { | ||
Query.prototype.search = function(value) { | ||
if (value && typeof value === 'string') { | ||
@@ -282,5 +283,5 @@ this._query['typeahead'] = value; | ||
*/ | ||
Query.prototype.query = function (query) { | ||
Query.prototype.query = function(query) { | ||
if (typeof query === "object") { | ||
this._query['query'] = Utils._merge(this._query['query'], query); | ||
this._query['query'] = Utils.mergeDeep(this._query['query'], query); | ||
return this; | ||
@@ -298,3 +299,3 @@ } else { | ||
*/ | ||
Query.prototype.tags = function (values) { | ||
Query.prototype.tags = function(values) { | ||
if (Array.isArray(values)) { | ||
@@ -332,3 +333,3 @@ this._query['tags'] = values; | ||
*/ | ||
Query.prototype.include_count = function () { | ||
Query.prototype.include_count = function() { | ||
this._query['include_count'] = true; | ||
@@ -379,3 +380,3 @@ return this; | ||
*/ | ||
Query.prototype.count = function () { | ||
Query.prototype.count = function() { | ||
this._query['count'] = true; | ||
@@ -398,3 +399,3 @@ this.requestParams = { | ||
*/ | ||
Query.prototype.find = function () { | ||
Query.prototype.find = function() { | ||
this.requestParams = { | ||
@@ -416,3 +417,3 @@ method: 'POST', | ||
*/ | ||
Query.prototype.findOne = function () { | ||
Query.prototype.findOne = function() { | ||
this.singleEntry = true; | ||
@@ -419,0 +420,0 @@ this._query.limit = 1; |
@@ -6,5 +6,5 @@ 'use strict'; | ||
var Request = require('./../lib/request'); | ||
var Utils = require('../lib/utils'); | ||
var Utils = require('./../lib/utils'); | ||
var siteCache = require('./site-cache'); | ||
var config = require('../config'); | ||
var config = require('./../config'); | ||
var Entry = require('./entry/entry'); | ||
@@ -117,4 +117,4 @@ var Query = require('./entry/query'); | ||
} | ||
this.__proto__.__proto__ = new Entry(); | ||
return Object.create(this); | ||
var entry = new Entry(); | ||
return Utils.merge(entry, this); | ||
}; | ||
@@ -128,4 +128,4 @@ | ||
Site.prototype.Query = function () { | ||
this.__proto__.__proto__ = new Query(); | ||
return Object.create(this); | ||
var query = new Query(); | ||
return Utils.merge(query, this); | ||
}; | ||
@@ -132,0 +132,0 @@ |
1997
80106