twitter-lite
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -1,2 +0,2 @@ | ||
var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),o=require("./stream"),i=function(t,e){return void 0===e&&(e="1.1"),"https://"+t+".twitter.com/"+e},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"},c=function(r){var n,o=Object.assign({},s,r);this.authType=o.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:o.consumer_key,secret:o.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).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};c.prototype.getBearerToken=function(){return new Promise(function(t,e){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(function(t){return t.json()}).then(function(r){try{return t(r)}catch(t){return e(t)}},e)}.bind(this))},c.prototype.getRequestToken=function(t){return new Promise(function(e,o){var i,s,c;return i={url:this.oauth+"/request_token",method:"POST"},s={},t&&(s={oauth_callback:t}),s&&(i.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(i,{})),r(i.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return o(t)}},o)}.bind(this))},c.prototype.getAccessToken=function(t){return new Promise(function(e,o){var i,s,c;return i={url:this.oauth+"/access_token",method:"POST"},(s={oauth_verifier:t.verifier})&&(i.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(i,{key:t.key,secret:t.secret})),r(i.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return o(t)}},o)}.bind(this))},c.prototype._makeRequest=function(t,e,r){var o={url:this.url+"/"+e+".json",method:t};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}}},c.prototype.get=function(t,e){return new Promise(function(n,o){var i;return i=this._makeRequest("GET",t,e),r(i.requestData.url,{headers:i.headers}).then(function(t){return t.json()}).then(function(t){try{return n(t)}catch(t){return o(t)}},o)}.bind(this))},c.prototype.post=function(t,e,n){return new Promise(function(o,i){var s;return s=this._makeRequest("POST",t,n),r(s.requestData.url,{method:"POST",headers:Object.assign({},u,s.headers),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return i(t)}},i)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new o,c={url:i("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c; | ||
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; | ||
//# sourceMappingURL=twitter.js.map |
@@ -1,2 +0,2 @@ | ||
var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),o=require("./stream"),i=function(t,e){return void 0===e&&(e="1.1"),"https://"+t+".twitter.com/"+e},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"},c=function(r){var n,o=Object.assign({},s,r);this.authType=o.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:o.consumer_key,secret:o.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).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};c.prototype.getBearerToken=function(){return new Promise(function(t,e){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(function(t){return t.json()}).then(function(r){try{return t(r)}catch(t){return e(t)}},e)}.bind(this))},c.prototype.getRequestToken=function(t){return new Promise(function(e,o){var i,s,c;return i={url:this.oauth+"/request_token",method:"POST"},s={},t&&(s={oauth_callback:t}),s&&(i.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(i,{})),r(i.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return o(t)}},o)}.bind(this))},c.prototype.getAccessToken=function(t){return new Promise(function(e,o){var i,s,c;return i={url:this.oauth+"/access_token",method:"POST"},(s={oauth_verifier:t.verifier})&&(i.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(i,{key:t.key,secret:t.secret})),r(i.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return o(t)}},o)}.bind(this))},c.prototype._makeRequest=function(t,e,r){var o={url:this.url+"/"+e+".json",method:t};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}}},c.prototype.get=function(t,e){return new Promise(function(n,o){var i;return i=this._makeRequest("GET",t,e),r(i.requestData.url,{headers:i.headers}).then(function(t){return t.json()}).then(function(t){try{return n(t)}catch(t){return o(t)}},o)}.bind(this))},c.prototype.post=function(t,e,n){return new Promise(function(o,i){var s;return s=this._makeRequest("POST",t,n),r(s.requestData.url,{method:"POST",headers:Object.assign({},u,s.headers),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return i(t)}},i)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new o,c={url:i("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c; | ||
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; | ||
//# 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},u={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},s={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,o=Object.assign({},u,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};c.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(function(e){return e.json()}).then(function(r){try{return e(r)}catch(e){return t(e)}},t)}.bind(this))},c.prototype.getRequestToken=function(e){return new Promise(function(t,o){var i,u,c;return i={url:this.oauth+"/request_token",method:"POST"},u={},e&&(u={oauth_callback:e}),u&&(i.url+="?"+n.stringify(u)),{},c=this.client.toHeader(this.client.authorize(i,{})),r(i.url,{method:"POST",headers:Object.assign({},s,c)}).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))},c.prototype.getAccessToken=function(e){return new Promise(function(t,o){var i,u,c;return i={url:this.oauth+"/access_token",method:"POST"},(u={oauth_verifier:e.verifier})&&(i.url+="?"+n.stringify(u)),{},c=this.client.toHeader(this.client.authorize(i,{key:e.key,secret:e.secret})),r(i.url,{method:"POST",headers:Object.assign({},s,c)}).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))},c.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}}},c.prototype.get=function(e,t){return new Promise(function(n,o){var i;return i=this._makeRequest("GET",e,t),r(i.requestData.url,{headers:i.headers}).then(function(e){return e.json()}).then(function(e){try{return n(e)}catch(e){return o(e)}},o)}.bind(this))},c.prototype.post=function(e,t,n){return new Promise(function(o,i){var u;return u=this._makeRequest("POST",e,n),r(u.requestData.url,{method:"POST",headers:Object.assign({},s,u.headers),body:JSON.stringify(t)}).then(function(e){return e.json()}).then(function(e){try{return o(e)}catch(e){return i(e)}},i)}.bind(this))},c.prototype.stream=function(e,t){var u=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var s=new o,c={url:i("stream")+"/"+e+".json",method:"GET"};t&&(c.url+="?"+n.stringify(t));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(e){u.stream.destroy=function(){return e.body.destroy()},200===e.status?s.emit("start",e):s.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return s.parse(e)}).on("error",function(e){return s.emit("error",e)}).on("end",function(){return s.emit("end",e)})}).catch(function(e){return s.emit("error",e)}),s},module.exports=c}); | ||
!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}); | ||
//# sourceMappingURL=twitter.umd.js.map |
{ | ||
"name": "twitter-lite", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "A tiny, full-featured client / server library for the Twitter API", | ||
@@ -5,0 +5,0 @@ "source": [ |
@@ -10,5 +10,5 @@ # Twitter Lite | ||
- Promise driven via Async / Await | ||
- Both REST and Stream support | ||
- REST and Stream support | ||
- Works both in Node and in browsers | ||
- Up-to-date APIs | ||
- Rate limiting support | ||
- Under 1kb | ||
@@ -184,2 +184,4 @@ - Minimal dependencies | ||
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. | ||
```es6 | ||
@@ -200,2 +202,4 @@ const client = new Twitter({ | ||
Same return as `.get()`. | ||
Use the `.post` method for actions that change state, as documented in the Twitter API. For [example](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-create.html), to follow a user: | ||
@@ -220,3 +224,3 @@ | ||
See the [app authenticatio example](#app-authentication-example). | ||
See the [app authentication example](#app-authentication-example). | ||
@@ -233,6 +237,30 @@ ### .getRequestToken(twitterCallbackUrl) | ||
### API Errors | ||
### API errors | ||
API errors are returned as an array of errors under the `errors` key of the response object. Make sure to check for the presence of this field, in addition to any try/catch blocks you may have. | ||
**Breaking change in v0.7** | ||
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: | ||
```es6 | ||
try { | ||
const response = await client.get("some/endpoint"); | ||
// ... use response here ... | ||
} catch (e) { | ||
if ('errors' in e) { | ||
// Twitter API error | ||
if (e.errors[0].code === 88) | ||
// rate limit exceeded | ||
console.log("Rate limit will reset on", new Date(e._headers["x-rate-limit-reset"] * 1000)); | ||
else | ||
// some other kind of error, e.g. read-only API trying to POST | ||
} else { | ||
// non-API error, e.g. network problem or invalid JSON in response | ||
} | ||
} | ||
``` | ||
#### Rate limiting | ||
A particular case of errors is exceeding the [rate limits](https://developer.twitter.com/en/docs/basics/rate-limits.html). See the example immediately above for detecting rate limit errors, and read [Twitter's documentation on rate limiting](https://developer.twitter.com/en/docs/basics/rate-limiting.html). | ||
### Numeric vs. string IDs | ||
@@ -250,3 +278,3 @@ | ||
2. `yarn/npm install` | ||
3. Go to https://apps.twitter.com and create an app for testing this module. Make sure it has read/write permissions. | ||
3. Go to <https://apps.twitter.com> and create an app for testing this module. Make sure it has read/write permissions. | ||
4. Grab the consumer key/secret, and the access token/secret and place them in a [.env](https://www.npmjs.com/package/dotenv) file in the project's root directory, under the following variables: | ||
@@ -261,5 +289,7 @@ ``` | ||
6. Add your contribution, along with test case(s). Note: feel free to skip the ["should DM user"](https://github.com/Preposterous/twitter-lite/blob/34e8dbb3efb9a45564275f16473af59dbc4409e5/twitter.test.js#L167) test during development by changing that `it()` call to `it.skip()`, but remember to revert that change before committing. This will prevent your account from being flagged as [abusing the API to send too many DMs](https://github.com/Preposterous/twitter-lite/commit/5ee2ce4232faa07453ea2f0b4d63ee7a6d119ce7). | ||
7. When all tests pass, run `npm run precommit` for linting with [prettier](https://www.npmjs.com/package/prettier) | ||
8. Commit using a [descriptive message](https://chris.beams.io/posts/git-commit/) (please squash all your commits into one!) | ||
9. `git push` and submit your PR! | ||
7. Make sure all tests pass. | ||
8. `git add` the changed files | ||
9. `npm run precommit` to lint with [prettier](https://www.npmjs.com/package/prettier) | ||
10. Commit using a [descriptive message](https://chris.beams.io/posts/git-commit/) (please squash all your commits into one!) | ||
11. `git push` and submit your PR! | ||
@@ -266,0 +296,0 @@ ## Credits |
@@ -58,2 +58,16 @@ const crypto = require("crypto"); | ||
/** | ||
* Parse the JSON from a Response object and add the Headers under _headers | ||
* @param {Response} response - the Response object returned by Fetch | ||
* @return {Promise<Object>} | ||
* @private | ||
*/ | ||
static _handleResponse(response) { | ||
const headers = response.headers.raw(); // https://github.com/bitinn/node-fetch/issues/495 | ||
return response.json().then(res => { | ||
res._headers = headers; | ||
return res; | ||
}); | ||
} | ||
async getBearerToken() { | ||
@@ -73,3 +87,3 @@ const headers = { | ||
headers | ||
}).then(res => res.json()); | ||
}).then(Twitter._handleResponse); | ||
@@ -89,4 +103,5 @@ return results; | ||
let headers = {}; | ||
headers = this.client.toHeader(this.client.authorize(requestData, {})); | ||
const headers = this.client.toHeader( | ||
this.client.authorize(requestData, {}) | ||
); | ||
@@ -112,4 +127,3 @@ const results = await Fetch(requestData.url, { | ||
let headers = {}; | ||
headers = this.client.toHeader( | ||
const headers = this.client.toHeader( | ||
this.client.authorize(requestData, { | ||
@@ -131,2 +145,10 @@ key: options.key, | ||
/** | ||
* Construct the data and headers for an authenticated HTTP request to the Twitter API | ||
* @param {string} method - "GET" or "POST" | ||
* @param {string} resource - the API endpoint | ||
* @param {object} parameters | ||
* @return {{requestData: {url: string, method: string}, headers: ({Authorization: string}|OAuth.Header)}} | ||
* @private | ||
*/ | ||
_makeRequest(method, resource, parameters) { | ||
@@ -155,3 +177,3 @@ const requestData = { | ||
async get(resource, parameters) { | ||
get(resource, parameters) { | ||
const { requestData, headers } = this._makeRequest( | ||
@@ -163,9 +185,10 @@ "GET", | ||
const results = await Fetch(requestData.url, { headers }).then(res => | ||
res.json() | ||
); | ||
return results; | ||
return Fetch(requestData.url, { headers }) | ||
.then(Twitter._handleResponse) | ||
.then( | ||
results => ("errors" in results ? Promise.reject(results) : results) | ||
); | ||
} | ||
async post(resource, body, parameters) { | ||
post(resource, body, parameters) { | ||
const { requestData, headers } = this._makeRequest( | ||
@@ -177,8 +200,11 @@ "POST", | ||
const results = await Fetch(requestData.url, { | ||
return Fetch(requestData.url, { | ||
method: "POST", | ||
headers: Object.assign({}, baseHeaders, headers), | ||
body: JSON.stringify(body) | ||
}).then(res => res.json()); | ||
return results; | ||
}) | ||
.then(Twitter._handleResponse) | ||
.then( | ||
results => ("errors" in results ? Promise.reject(results) : results) | ||
); | ||
} | ||
@@ -188,3 +214,3 @@ | ||
if (this.authType !== "User") | ||
throw Error("Streams require user context authentication"); | ||
throw new Error("Streams require user context authentication"); | ||
@@ -191,0 +217,0 @@ const stream = new Stream(); |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
68193
291
303
0