twitter-lite
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -1,2 +0,2 @@ | ||
var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),o=require("./stream"),s=function(e,t){return void 0===t&&(t="1.1"),"https://"+e+".twitter.com/"+t},i={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},a=function(r){var n,o=Object.assign({},i,r);this.authType=o.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:o.consumer_key,secret:o.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:o.access_token_key,secret:o.access_token_secret},this.url=s(o.subdomain),this.oauth=s(o.subdomain,"oauth"),this.config=o};a._handleResponse=function(e){var t=e.headers.raw();return e.json().then(function(e){return e._headers=t,e})},a.prototype.getBearerToken=function(){return new Promise(function(e,t){var n;return n={Authorization:"Basic "+Buffer.from(this.config.consumer_key+":"+this.config.consumer_secret).toString("base64"),"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},r("https://api.twitter.com/oauth2/token",{method:"POST",body:"grant_type=client_credentials",headers:n}).then(a._handleResponse).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},a.prototype.getRequestToken=function(e){return new Promise(function(t,o){var s,i,a;return s={url:this.oauth+"/request_token",method:"POST"},i={},e&&(i={oauth_callback:e}),i&&(s.url+="?"+n.stringify(i)),a=this.client.toHeader(this.client.authorize(s,{})),r(s.url,{method:"POST",headers:Object.assign({},u,a)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},a.prototype.getAccessToken=function(e){return new Promise(function(t,o){var s,i,a;return s={url:this.oauth+"/access_token",method:"POST"},(i={oauth_verifier:e.verifier})&&(s.url+="?"+n.stringify(i)),a=this.client.toHeader(this.client.authorize(s,{key:e.key,secret:e.secret})),r(s.url,{method:"POST",headers:Object.assign({},u,a)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},a.prototype._makeRequest=function(e,t,r){var o={url:this.url+"/"+t+".json",method:e};r&&(o.url+="?"+n.stringify(r));return{requestData:o,headers:"User"===this.authType?this.client.toHeader(this.client.authorize(o,this.token)):{Authorization:"Bearer "+this.config.bearer_token}}},a.prototype.get=function(e,t){var n=this._makeRequest("GET",e,t);return r(n.requestData.url,{headers:n.headers}).then(a._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},a.prototype.post=function(e,t,n){var o=this._makeRequest("POST",e,n);return r(o.requestData.url,{method:"POST",headers:Object.assign({},u,o.headers),body:JSON.stringify(t)}).then(a._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},a.prototype.stream=function(e,t){var i=this;if("User"!==this.authType)throw new Error("Streams require user context authentication");var u=new o,a={url:s("stream")+"/"+e+".json",method:"GET"};t&&(a.url+="?"+n.stringify(t));var c=this.client.toHeader(this.client.authorize(a,this.token));return r(a.url,{headers:c}).then(function(e){i.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=a; | ||
var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),s=require("./stream"),o=function(e,t){return void 0===t&&(t="1.1"),"https://"+e+".twitter.com/"+t},i={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u=["direct_messages/events/new","direct_messages/welcome_messages/new","direct_messages/welcome_messages/rules/new"],a={"Content-Type":"application/json",Accept:"application/json"};function c(e){return e.replace(/!/g,"%21").replace(/\*/g,"%2A").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29")}var h=function(r){var n,s=Object.assign({},i,r);this.authType=s.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:s.consumer_key,secret:s.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:s.access_token_key,secret:s.access_token_secret},this.url=o(s.subdomain),this.oauth=o(s.subdomain,"oauth"),this.config=s};h._handleResponse=function(e){var t=e.headers.raw();return e.json().then(function(e){return e._headers=t,e})},h.prototype.getBearerToken=function(){return new Promise(function(e,t){var n;return n={Authorization:"Basic "+Buffer.from(this.config.consumer_key+":"+this.config.consumer_secret).toString("base64"),"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},r("https://api.twitter.com/oauth2/token",{method:"POST",body:"grant_type=client_credentials",headers:n}).then(h._handleResponse).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},h.prototype.getRequestToken=function(e){return new Promise(function(t,s){var o,i,u;return o={url:this.oauth+"/request_token",method:"POST"},i={},e&&(i={oauth_callback:e}),i&&(o.url+="?"+n.stringify(i)),u=this.client.toHeader(this.client.authorize(o,{})),r(o.url,{method:"POST",headers:Object.assign({},a,u)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return s(e)}},s)}.bind(this))},h.prototype.getAccessToken=function(e){return new Promise(function(t,s){var o,i,u;return o={url:this.oauth+"/access_token",method:"POST"},(i={oauth_verifier:e.verifier})&&(o.url+="?"+n.stringify(i)),u=this.client.toHeader(this.client.authorize(o,{key:e.key,secret:e.secret})),r(o.url,{method:"POST",headers:Object.assign({},a,u)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return s(e)}},s)}.bind(this))},h.prototype._makeRequest=function(e,t,r){var s={url:this.url+"/"+t+".json",method:e};r&&("POST"===e?s.data=r:s.url+="?"+n.stringify(r));return{requestData:s,headers:"User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token}}},h.prototype.get=function(e,t){var n=this._makeRequest("GET",e,t);return r(n.requestData.url,{headers:n.headers}).then(h._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},h.prototype.post=function(e,t,s){var o=this._makeRequest("POST",e,s),i=o.requestData,d=Object.assign({},a,o.headers);return u.includes(e)?t=JSON.stringify(t):(t=n.stringify(s),d["Content-Type"]="application/x-www-form-urlencoded"),r(i.url,{method:"POST",headers:d,body:c(t)}).then(h._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},h.prototype.stream=function(e,t){var i=this;if("User"!==this.authType)throw new Error("Streams require user context authentication");var u=new s,a={url:o("stream")+"/"+e+".json",method:"POST"};t&&(a.data=t);var h=this.client.toHeader(this.client.authorize(a,this.token));return r(a.url,{method:"POST",headers:Object.assign({},h,{"Content-Type":"application/x-www-form-urlencoded"}),body:c(n.stringify(t))}).then(function(e){i.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=h; | ||
//# sourceMappingURL=twitter.js.map |
@@ -1,2 +0,2 @@ | ||
var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),o=require("./stream"),s=function(e,t){return void 0===t&&(t="1.1"),"https://"+e+".twitter.com/"+t},i={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},a=function(r){var n,o=Object.assign({},i,r);this.authType=o.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:o.consumer_key,secret:o.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:o.access_token_key,secret:o.access_token_secret},this.url=s(o.subdomain),this.oauth=s(o.subdomain,"oauth"),this.config=o};a._handleResponse=function(e){var t=e.headers.raw();return e.json().then(function(e){return e._headers=t,e})},a.prototype.getBearerToken=function(){return new Promise(function(e,t){var n;return n={Authorization:"Basic "+Buffer.from(this.config.consumer_key+":"+this.config.consumer_secret).toString("base64"),"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},r("https://api.twitter.com/oauth2/token",{method:"POST",body:"grant_type=client_credentials",headers:n}).then(a._handleResponse).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},a.prototype.getRequestToken=function(e){return new Promise(function(t,o){var s,i,a;return s={url:this.oauth+"/request_token",method:"POST"},i={},e&&(i={oauth_callback:e}),i&&(s.url+="?"+n.stringify(i)),a=this.client.toHeader(this.client.authorize(s,{})),r(s.url,{method:"POST",headers:Object.assign({},u,a)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},a.prototype.getAccessToken=function(e){return new Promise(function(t,o){var s,i,a;return s={url:this.oauth+"/access_token",method:"POST"},(i={oauth_verifier:e.verifier})&&(s.url+="?"+n.stringify(i)),a=this.client.toHeader(this.client.authorize(s,{key:e.key,secret:e.secret})),r(s.url,{method:"POST",headers:Object.assign({},u,a)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},a.prototype._makeRequest=function(e,t,r){var o={url:this.url+"/"+t+".json",method:e};r&&(o.url+="?"+n.stringify(r));return{requestData:o,headers:"User"===this.authType?this.client.toHeader(this.client.authorize(o,this.token)):{Authorization:"Bearer "+this.config.bearer_token}}},a.prototype.get=function(e,t){var n=this._makeRequest("GET",e,t);return r(n.requestData.url,{headers:n.headers}).then(a._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},a.prototype.post=function(e,t,n){var o=this._makeRequest("POST",e,n);return r(o.requestData.url,{method:"POST",headers:Object.assign({},u,o.headers),body:JSON.stringify(t)}).then(a._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},a.prototype.stream=function(e,t){var i=this;if("User"!==this.authType)throw new Error("Streams require user context authentication");var u=new o,a={url:s("stream")+"/"+e+".json",method:"GET"};t&&(a.url+="?"+n.stringify(t));var c=this.client.toHeader(this.client.authorize(a,this.token));return r(a.url,{headers:c}).then(function(e){i.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=a; | ||
var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),s=require("./stream"),o=function(e,t){return void 0===t&&(t="1.1"),"https://"+e+".twitter.com/"+t},i={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u=["direct_messages/events/new","direct_messages/welcome_messages/new","direct_messages/welcome_messages/rules/new"],a={"Content-Type":"application/json",Accept:"application/json"};function c(e){return e.replace(/!/g,"%21").replace(/\*/g,"%2A").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29")}var h=function(r){var n,s=Object.assign({},i,r);this.authType=s.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:s.consumer_key,secret:s.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:s.access_token_key,secret:s.access_token_secret},this.url=o(s.subdomain),this.oauth=o(s.subdomain,"oauth"),this.config=s};h._handleResponse=function(e){var t=e.headers.raw();return e.json().then(function(e){return e._headers=t,e})},h.prototype.getBearerToken=function(){return new Promise(function(e,t){var n;return n={Authorization:"Basic "+Buffer.from(this.config.consumer_key+":"+this.config.consumer_secret).toString("base64"),"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},r("https://api.twitter.com/oauth2/token",{method:"POST",body:"grant_type=client_credentials",headers:n}).then(h._handleResponse).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},h.prototype.getRequestToken=function(e){return new Promise(function(t,s){var o,i,u;return o={url:this.oauth+"/request_token",method:"POST"},i={},e&&(i={oauth_callback:e}),i&&(o.url+="?"+n.stringify(i)),u=this.client.toHeader(this.client.authorize(o,{})),r(o.url,{method:"POST",headers:Object.assign({},a,u)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return s(e)}},s)}.bind(this))},h.prototype.getAccessToken=function(e){return new Promise(function(t,s){var o,i,u;return o={url:this.oauth+"/access_token",method:"POST"},(i={oauth_verifier:e.verifier})&&(o.url+="?"+n.stringify(i)),u=this.client.toHeader(this.client.authorize(o,{key:e.key,secret:e.secret})),r(o.url,{method:"POST",headers:Object.assign({},a,u)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return s(e)}},s)}.bind(this))},h.prototype._makeRequest=function(e,t,r){var s={url:this.url+"/"+t+".json",method:e};r&&("POST"===e?s.data=r:s.url+="?"+n.stringify(r));return{requestData:s,headers:"User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token}}},h.prototype.get=function(e,t){var n=this._makeRequest("GET",e,t);return r(n.requestData.url,{headers:n.headers}).then(h._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},h.prototype.post=function(e,t,s){var o=this._makeRequest("POST",e,s),i=o.requestData,d=Object.assign({},a,o.headers);return u.includes(e)?t=JSON.stringify(t):(t=n.stringify(s),d["Content-Type"]="application/x-www-form-urlencoded"),r(i.url,{method:"POST",headers:d,body:c(t)}).then(h._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},h.prototype.stream=function(e,t){var i=this;if("User"!==this.authType)throw new Error("Streams require user context authentication");var u=new s,a={url:o("stream")+"/"+e+".json",method:"POST"};t&&(a.data=t);var h=this.client.toHeader(this.client.authorize(a,this.token));return r(a.url,{method:"POST",headers:Object.assign({},h,{"Content-Type":"application/x-www-form-urlencoded"}),body:c(n.stringify(t))}).then(function(e){i.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=h; | ||
//# sourceMappingURL=twitter.m.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t():"function"==typeof define&&define.amd?define(t):t()}(0,function(){var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),o=require("./stream"),i=function(e,t){return void 0===t&&(t="1.1"),"https://"+e+".twitter.com/"+t},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},a=function(r){var n,o=Object.assign({},s,r);this.authType=o.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:o.consumer_key,secret:o.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:o.access_token_key,secret:o.access_token_secret},this.url=i(o.subdomain),this.oauth=i(o.subdomain,"oauth"),this.config=o};a._handleResponse=function(e){var t=e.headers.raw();return e.json().then(function(e){return e._headers=t,e})},a.prototype.getBearerToken=function(){return new Promise(function(e,t){var n;return n={Authorization:"Basic "+Buffer.from(this.config.consumer_key+":"+this.config.consumer_secret).toString("base64"),"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},r("https://api.twitter.com/oauth2/token",{method:"POST",body:"grant_type=client_credentials",headers:n}).then(a._handleResponse).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},a.prototype.getRequestToken=function(e){return new Promise(function(t,o){var i,s,a;return i={url:this.oauth+"/request_token",method:"POST"},s={},e&&(s={oauth_callback:e}),s&&(i.url+="?"+n.stringify(s)),a=this.client.toHeader(this.client.authorize(i,{})),r(i.url,{method:"POST",headers:Object.assign({},u,a)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},a.prototype.getAccessToken=function(e){return new Promise(function(t,o){var i,s,a;return i={url:this.oauth+"/access_token",method:"POST"},(s={oauth_verifier:e.verifier})&&(i.url+="?"+n.stringify(s)),a=this.client.toHeader(this.client.authorize(i,{key:e.key,secret:e.secret})),r(i.url,{method:"POST",headers:Object.assign({},u,a)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},a.prototype._makeRequest=function(e,t,r){var o={url:this.url+"/"+t+".json",method:e};r&&(o.url+="?"+n.stringify(r));return{requestData:o,headers:"User"===this.authType?this.client.toHeader(this.client.authorize(o,this.token)):{Authorization:"Bearer "+this.config.bearer_token}}},a.prototype.get=function(e,t){var n=this._makeRequest("GET",e,t);return r(n.requestData.url,{headers:n.headers}).then(a._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},a.prototype.post=function(e,t,n){var o=this._makeRequest("POST",e,n);return r(o.requestData.url,{method:"POST",headers:Object.assign({},u,o.headers),body:JSON.stringify(t)}).then(a._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},a.prototype.stream=function(e,t){var s=this;if("User"!==this.authType)throw new Error("Streams require user context authentication");var u=new o,a={url:i("stream")+"/"+e+".json",method:"GET"};t&&(a.url+="?"+n.stringify(t));var c=this.client.toHeader(this.client.authorize(a,this.token));return r(a.url,{headers:c}).then(function(e){s.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=a}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t():"function"==typeof define&&define.amd?define(t):t()}(0,function(){var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),o=require("./stream"),s=function(e,t){return void 0===t&&(t="1.1"),"https://"+e+".twitter.com/"+t},i={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u=["direct_messages/events/new","direct_messages/welcome_messages/new","direct_messages/welcome_messages/rules/new"],a={"Content-Type":"application/json",Accept:"application/json"};function c(e){return e.replace(/!/g,"%21").replace(/\*/g,"%2A").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29")}var h=function(r){var n,o=Object.assign({},i,r);this.authType=o.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:o.consumer_key,secret:o.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:o.access_token_key,secret:o.access_token_secret},this.url=s(o.subdomain),this.oauth=s(o.subdomain,"oauth"),this.config=o};h._handleResponse=function(e){var t=e.headers.raw();return e.json().then(function(e){return e._headers=t,e})},h.prototype.getBearerToken=function(){return new Promise(function(e,t){var n;return n={Authorization:"Basic "+Buffer.from(this.config.consumer_key+":"+this.config.consumer_secret).toString("base64"),"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},r("https://api.twitter.com/oauth2/token",{method:"POST",body:"grant_type=client_credentials",headers:n}).then(h._handleResponse).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},h.prototype.getRequestToken=function(e){return new Promise(function(t,o){var s,i,u;return s={url:this.oauth+"/request_token",method:"POST"},i={},e&&(i={oauth_callback:e}),i&&(s.url+="?"+n.stringify(i)),u=this.client.toHeader(this.client.authorize(s,{})),r(s.url,{method:"POST",headers:Object.assign({},a,u)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},h.prototype.getAccessToken=function(e){return new Promise(function(t,o){var s,i,u;return s={url:this.oauth+"/access_token",method:"POST"},(i={oauth_verifier:e.verifier})&&(s.url+="?"+n.stringify(i)),u=this.client.toHeader(this.client.authorize(s,{key:e.key,secret:e.secret})),r(s.url,{method:"POST",headers:Object.assign({},a,u)}).then(function(e){return e.text()}).then(function(e){return n.parse(e)}).then(function(e){try{return t(e)}catch(e){return o(e)}},o)}.bind(this))},h.prototype._makeRequest=function(e,t,r){var o={url:this.url+"/"+t+".json",method:e};r&&("POST"===e?o.data=r:o.url+="?"+n.stringify(r));return{requestData:o,headers:"User"===this.authType?this.client.toHeader(this.client.authorize(o,this.token)):{Authorization:"Bearer "+this.config.bearer_token}}},h.prototype.get=function(e,t){var n=this._makeRequest("GET",e,t);return r(n.requestData.url,{headers:n.headers}).then(h._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},h.prototype.post=function(e,t,o){var s=this._makeRequest("POST",e,o),i=s.requestData,d=Object.assign({},a,s.headers);return u.includes(e)?t=JSON.stringify(t):(t=n.stringify(o),d["Content-Type"]="application/x-www-form-urlencoded"),r(i.url,{method:"POST",headers:d,body:c(t)}).then(h._handleResponse).then(function(e){return"errors"in e?Promise.reject(e):e})},h.prototype.stream=function(e,t){var i=this;if("User"!==this.authType)throw new Error("Streams require user context authentication");var u=new o,a={url:s("stream")+"/"+e+".json",method:"POST"};t&&(a.data=t);var h=this.client.toHeader(this.client.authorize(a,this.token));return r(a.url,{method:"POST",headers:Object.assign({},h,{"Content-Type":"application/x-www-form-urlencoded"}),body:c(n.stringify(t))}).then(function(e){i.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=h}); | ||
//# sourceMappingURL=twitter.umd.js.map |
{ | ||
"name": "twitter-lite", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "A tiny, full-featured client / server library for the Twitter API", | ||
@@ -54,3 +54,6 @@ "source": [ | ||
] | ||
}, | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
@@ -183,3 +183,3 @@ # Twitter Lite | ||
Returns a Promise resolving to the API response object, or rejecting on error. The response object also contains the HTTP response code and [headers](https://developer.twitter.com/en/docs/basics/rate-limiting.html), under the `_header` key. These are useful to check for [rate limit](#rate-limiting) information. | ||
Returns a Promise resolving to the API response object, or rejecting on error. The response and error objects also contain the HTTP response code and [headers](https://developer.twitter.com/en/docs/basics/rate-limiting.html), under the `_headers` key. These are useful to check for [rate limit](#rate-limiting) information. | ||
@@ -238,3 +238,3 @@ ```es6 | ||
Given that [developers expect promises to reject when they don't return the requested data](https://github.com/ttezel/twit/issues/256), `.get` and `.post` now reject instead of silently returning API errors as an array under the `errors` key of the response object. You can use try/catch to handle errors: | ||
Given that [developers expect promises to reject when they don't return the requested data](https://github.com/ttezel/twit/issues/256), `.get` and `.post` now reject instead of silently returning API errors as an array under the `errors` key of the response object. You can use try/catch to handle errors. The error object contains an `errors` property with the error `code` and `message`, and a `_headers` property with the the HTTP response code and [Headers](https://developer.twitter.com/en/docs/basics/rate-limiting.html) object returned by the Twitter API. Note that each `_headers` property is an array, usually of length 1. | ||
@@ -241,0 +241,0 @@ ```es6 |
@@ -34,2 +34,12 @@ const crypto = require("crypto"); | ||
// Twitter expects POST body parameters to be URL-encoded: https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature | ||
// However, some endpoints expect a JSON payload - https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event | ||
// It appears that JSON payloads don't need to be included in the signature, | ||
// because sending DMs works without signing the POST body | ||
const JSON_ENDPOINTS = [ | ||
"direct_messages/events/new", | ||
"direct_messages/welcome_messages/new", | ||
"direct_messages/welcome_messages/rules/new" | ||
]; | ||
const baseHeaders = { | ||
@@ -40,2 +50,12 @@ "Content-Type": "application/json", | ||
function percentEncode(string) { | ||
// From OAuth.prototype.percentEncode | ||
return string | ||
.replace(/!/g, "%21") | ||
.replace(/\*/g, "%2A") | ||
.replace(/'/g, "%27") | ||
.replace(/\(/g, "%28") | ||
.replace(/\)/g, "%29"); | ||
} | ||
class Twitter { | ||
@@ -69,3 +89,3 @@ constructor(options) { | ||
return response.json().then(res => { | ||
res._headers = headers; | ||
res._headers = headers; // TODO: this creates an array-like object when it adds _headers to an array response | ||
return res; | ||
@@ -157,3 +177,5 @@ }); | ||
}; | ||
if (parameters) requestData.url += "?" + querystring.stringify(parameters); | ||
if (parameters) | ||
if (method === "POST") requestData.data = parameters; | ||
else requestData.url += "?" + querystring.stringify(parameters); | ||
@@ -185,4 +207,4 @@ let headers = {}; | ||
.then(Twitter._handleResponse) | ||
.then( | ||
results => ("errors" in results ? Promise.reject(results) : results) | ||
.then(results => | ||
"errors" in results ? Promise.reject(results) : results | ||
); | ||
@@ -198,10 +220,18 @@ } | ||
const postHeaders = Object.assign({}, baseHeaders, headers); | ||
if (JSON_ENDPOINTS.includes(resource)) { | ||
body = JSON.stringify(body); | ||
} else { | ||
body = querystring.stringify(parameters); | ||
postHeaders["Content-Type"] = "application/x-www-form-urlencoded"; | ||
} | ||
return Fetch(requestData.url, { | ||
method: "POST", | ||
headers: Object.assign({}, baseHeaders, headers), | ||
body: JSON.stringify(body) | ||
headers: postHeaders, | ||
body: percentEncode(body) | ||
}) | ||
.then(Twitter._handleResponse) | ||
.then( | ||
results => ("errors" in results ? Promise.reject(results) : results) | ||
.then(results => | ||
"errors" in results ? Promise.reject(results) : results | ||
); | ||
@@ -216,7 +246,9 @@ } | ||
// POST the request, in order to accommodate long parameter lists, e.g. | ||
// up to 5000 ids for statuses/filter - https://developer.twitter.com/en/docs/tweets/filter-realtime/api-reference/post-statuses-filter | ||
const requestData = { | ||
url: `${getUrl("stream")}/${resource}.json`, | ||
method: "GET" | ||
method: "POST" | ||
}; | ||
if (parameters) requestData.url += "?" + querystring.stringify(parameters); | ||
if (parameters) requestData.data = parameters; | ||
@@ -227,3 +259,10 @@ const headers = this.client.toHeader( | ||
const request = Fetch(requestData.url, { headers }); | ||
const request = Fetch(requestData.url, { | ||
method: "POST", | ||
headers: { | ||
...headers, | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
}, | ||
body: percentEncode(querystring.stringify(parameters)) | ||
}); | ||
@@ -230,0 +269,0 @@ request |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
77698
333