From 6cbffba028588699ad8e2b1a5e326f79a140ed2f Mon Sep 17 00:00:00 2001 From: Hector Virgen Date: Tue, 23 Dec 2014 14:23:47 -0800 Subject: [PATCH] Bump version number and rebuild for release --- api-angular-min.js | 2 +- api-angular.js | 52 +++++++++++++++++++++++++++++++++++++++------- bower.json | 2 +- package.json | 2 +- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/api-angular-min.js b/api-angular-min.js index 29384ce..6c6290f 100644 --- a/api-angular-min.js +++ b/api-angular-min.js @@ -1 +1 @@ -!function(){"use strict";function a(b,c){for(var d in c)if(c.hasOwnProperty(d))try{b[d]=c[d].constructor===Object?a(b[d],c[d]):c[d]}catch(e){b[d]=c[d]}return b}var b="undefined"!=typeof exports?exports:window,c=b.btoa||require("btoa"),d=b.Promise||require("bluebird"),e=function(b,c,d){this._endpoint=b,this._http=d,this._queue=[],this._batchTimeout=null,this._options=a({query:{},params:{track:this._generateTrackId()},cookies:""},c||{}),this._events={}};e.prototype._generateTrackId=function(){return c(1e8*Math.random()).substr(0,10)},e.prototype.execute=function(b,c){if(!b||"string"!=typeof b)throw new Error("Method is required to execute API calls");var e=new d(function(d,e){var f=a({},this._options.params);this._queue.push({method:b,params:a(f,c||{}),deferred:{resolve:d,reject:e}}),null===this._batchTimeout&&(this._batchTimeout=setTimeout(this._postToApi.bind(this),1))}.bind(this));return e},e.prototype._postToApi=function(){var a,b=i(this._queue),c=[];for(var d in this._options.query)c.push(d+"="+this._options.query[d]);a=c.join("&"),this._http.post({body:b,url:this._endpoint+"?"+a,cookies:this._options.cookies}).then(f).then(g.bind(this,this._queue)).catch(h.bind(this,this._queue)),this.resetQueue()};var f=function(a){var b=[],c=JSON.parse(a.body);for(var d in c)b.push(JSON.parse(c[d]));return b},g=function(a,b){for(var c in a){var d=b[c];if(d.stat&&this._events.hasOwnProperty(d.stat))for(var e in this._events[d.stat])this._events[d.stat][e](a[c],d);d.stat&&"ok"!==d.stat?a[c].deferred.reject(d):a[c].deferred.resolve(d)}return b},h=function(a,b){for(var c in a)a[c].deferred.reject(b);return b};e.prototype.resetQueue=function(){null!==this._batchTimeout&&(clearTimeout(this._batchTimeout),this._batchTimeout=null),this._queue=[]},e.prototype.on=function(a,b){this._events.hasOwnProperty(a)||(this._events[a]=[]),this._events[a].push(b)},e.middleware=function(b,c){var d=require("./http_adapter/node"),f=new d;return function(d,g,h){d.api=new e(b,a({query:{application_id:"user",format:"JSON",session_token:d.cookies&&d.cookies.S},params:{api_signature:""},cookies:d.headers&&d.headers.cookie},c||{}),f),h()}};var i=function(a){var b=[];for(var c in a){var d=j(a[c]);b.push(d)}return"\n"+b.join("\n")+"\n"},j=function(a){var b=["method="+encodeURIComponent(a.method)];for(var c in a.params)null!==a.params[c]&&a.params.hasOwnProperty(c)&&b.push(encodeURIComponent(c)+"="+encodeURIComponent(a.params[c]));return b.join("&")};"undefined"!=typeof exports?module.exports=e:b.TaggedApi=e}(),function(){"use strict";function a(a){this._$http=a}{var b="undefined"!=typeof exports?exports:window;b.Promise||require("bluebird")}a.$inject=["$http"],a.prototype.post=function(a){return this._$http.post(a.url,a.body,{timeout:1e4,transformResponse:c}).then(d)};var c=function(a){return a},d=function(a){return{body:a.data}};if("undefined"!=typeof exports)module.exports=a;else{var e=b.TaggedApi||{};e.AngularAdapter=a}}(),function(){"use strict";var a=function(a,b){function c(a,c,d){var e=new b.AngularAdapter(a,c),f=new b("/api/",{query:{application_id:"user",format:"json",session_token:c.S}},e);return f.execute=function(a,c){return d.when(b.prototype.execute.call(this,a,c))},f}var d=a.module("tagged.service.api",["ngCookies"]);d.factory("taggedApi",c),c.$inject=["$http","$cookies","$q"]};"undefined"!=typeof exports?module.exports=a:TaggedApi.angularWrapper=a}(),TaggedApi.angularWrapper(angular,TaggedApi); \ No newline at end of file +!function(){"use strict";function a(b,c){for(var d in c)if(c.hasOwnProperty(d))try{b[d]=c[d].constructor===Object?a(b[d],c[d]):c[d]}catch(e){b[d]=c[d]}return b}var b="undefined"!=typeof exports?exports:window,c=b.btoa||require("btoa"),d=b.Promise||require("bluebird"),e=function(b,c,d){this._endpoint=b,this._http=d,this._queue=[],this._batchTimeout=null,this._options=a({query:{},params:{track:this._generateTrackId()},cookies:""},c||{}),this._events={}};e.prototype._generateTrackId=function(){return c(1e8*Math.random()).substr(0,10)},e.prototype.execute=function(b,c){if(!b||"string"!=typeof b)throw new Error("Method is required to execute API calls");var e=new d(function(d,e){var f=a({},this._options.params);this._queue.push({method:b,params:a(f,c||{}),deferred:{resolve:d,reject:e}}),null===this._batchTimeout&&(this._batchTimeout=setTimeout(this._postToApi.bind(this),1))}.bind(this));return e},e.prototype._postToApi=function(){var a,b=i(this._queue),c=[];for(var d in this._options.query)c.push(d+"="+this._options.query[d]);a=c.join("&"),this._http.post({body:b,url:this._endpoint+"?"+a,cookies:this._options.cookies}).then(f).then(g.bind(this,this._queue)).catch(h.bind(this,this._queue)),this.resetQueue()};var f=function(a){var b=[],c=JSON.parse(a.body);for(var d in c)b.push(JSON.parse(c[d]));return b},g=function(a,b){for(var c in a){var d=b[c];if(d.stat&&this._events.hasOwnProperty(d.stat))for(var e in this._events[d.stat])this._events[d.stat][e](a[c],d);d.stat&&"ok"!==d.stat?a[c].deferred.reject(d):a[c].deferred.resolve(d)}return b},h=function(a,b){for(var c in a)a[c].deferred.reject(b);return b};e.prototype.resetQueue=function(){null!==this._batchTimeout&&(clearTimeout(this._batchTimeout),this._batchTimeout=null),this._queue=[]},e.prototype.on=function(a,b){this._events.hasOwnProperty(a)||(this._events[a]=[]),this._events[a].push(b)},e.middleware=function(b,c){var d=require("./http_adapter/node"),f=new d;return function(d,g,h){d.api=new e(b,a({query:{application_id:"user",format:"JSON",session_token:d.cookies&&d.cookies.S},params:{api_signature:""},cookies:d.headers&&d.headers.cookie},c||{}),f),h()}};var i=function(a){var b=[];for(var c in a){var d=j(a[c]);b.push(d)}return"\n"+b.join("\n")+"\n"},j=function(a){var b=["method="+encodeURIComponent(a.method)];for(var c in a.params)null!==a.params[c]&&a.params.hasOwnProperty(c)&&b.push(k(c,a.params[c]));return b.join("&")},k=function(a,b){var c=typeof b;switch(c){case"string":case"number":case"boolean":return l(a,b);case"object":return null===b?l(a,b):m(a,b);default:throw new Error("Unable to parameterize key "+a+" with type "+c)}},l=function(a,b){return encodeURIComponent(a)+"="+encodeURIComponent(b)},m=function(a,b){var c=[];if(Array.isArray(b))for(var d=0,e=b.length;e>d;d++)c.push(encodeURIComponent(a)+"[]="+encodeURIComponent(b[d]));else for(var f in b)b.hasOwnProperty(f)&&c.push(encodeURIComponent(a)+"["+encodeURIComponent(f)+"]="+encodeURIComponent(b[f]));return c.join("&")};"undefined"!=typeof exports?module.exports=e:b.TaggedApi=e}(),function(){"use strict";function a(a){this._$http=a}{var b="undefined"!=typeof exports?exports:window;b.Promise||require("bluebird")}a.$inject=["$http"],a.prototype.post=function(a){return this._$http.post(a.url,a.body,{timeout:1e4,transformResponse:c}).then(d)};var c=function(a){return a},d=function(a){return{body:a.data}};if("undefined"!=typeof exports)module.exports=a;else{var e=b.TaggedApi||{};e.AngularAdapter=a}}(),function(){"use strict";var a=function(a,b){function c(a,c,d){var e=new b.AngularAdapter(a,c),f=new b("/api/",{query:{application_id:"user",format:"json",session_token:c.S}},e);return f.execute=function(a,c){return d.when(b.prototype.execute.call(this,a,c))},f}var d=a.module("tagged.service.api",["ngCookies"]);d.factory("taggedApi",c),c.$inject=["$http","$cookies","$q"]};"undefined"!=typeof exports?module.exports=a:TaggedApi.angularWrapper=a}(),TaggedApi.angularWrapper(angular,TaggedApi); \ No newline at end of file diff --git a/api-angular.js b/api-angular.js index 53f3a20..e194407 100644 --- a/api-angular.js +++ b/api-angular.js @@ -211,14 +211,7 @@ //TODO: support arrays as values if (null !== call.params[key] && call.params.hasOwnProperty(key)) { - params.push( - // Keys and values must be encoded to - // prevent accidental breakage of string - // splits by `=` and `&`. - encodeURIComponent(key) + - "=" + - encodeURIComponent(call.params[key]) - ); + params.push(parameterize(key, call.params[key])); } } @@ -227,6 +220,49 @@ return params.join('&'); }; + var parameterize = function(key, value) { + var type = typeof value; + switch (type) { + case 'string': + case 'number': + case 'boolean': + return parameterizePrimitive(key, value); + break; + + case 'object': + // `null` is considered an "object" + return (null === value) ? parameterizePrimitive(key, value) : parameterizeObject(key, value); + break; + + default: + throw new Error("Unable to parameterize key " + key + " with type " + type); + } + }; + + var parameterizePrimitive = function(key, value) { + // Keys and values must be encoded to + // prevent accidental breakage of string + // splits by `=` and `&`. + return encodeURIComponent(key) + "=" + encodeURIComponent(value); + }; + + var parameterizeObject = function(key, value) { + var params = []; + if (Array.isArray(value)) { + for (var i = 0, len = value.length; i < len; i++) { + params.push(encodeURIComponent(key) + "[]=" + encodeURIComponent(value[i])); + } + } else { + // assume object + for (var subkey in value) { + if (!value.hasOwnProperty(subkey)) continue; + params.push(encodeURIComponent(key) + "[" + encodeURIComponent(subkey) + "]=" + encodeURIComponent(value[subkey])); + } + } + + return params.join('&'); + }; + // Recursively merge properties of two objects // Adapted from http://stackoverflow.com/a/383245/249394 function mergeRecursive(obj1, obj2) { diff --git a/bower.json b/bower.json index 49d69ee..8ef2280 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "tagged-api-client", - "version": "0.0.12", + "version": "0.1.1", "authors": [ "Hector Virgen " ], diff --git a/package.json b/package.json index f9dd684..18bb33b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tagged-api", - "version": "0.1.0", + "version": "0.1.1", "description": "API client for Tagged with support for nodejs and angularjs", "main": "lib/index.js", "scripts": {