Skip to content

Commit

Permalink
fix node examples (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterLarco authored Apr 10, 2021
1 parent e0e716a commit 2bfd97c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
16 changes: 4 additions & 12 deletions build/Credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const oauth_1_0a_1 = __importDefault(require("oauth-1.0a"));
const crypto_1 = __importDefault(require("crypto"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const TwitterError_1 = __importDefault(require("./TwitterError"));
function removeNullAndUndefined(obj) {
Object.keys(obj).forEach((key) => obj[key] == null && delete obj[key]);
}
function validate(credentials) {
// Ensure all tokens are strings
if ('consumer_key' in credentials &&
typeof credentials.consumer_key != 'string') {
throw new Error('Invalid value for consumer_key. Expected string but got ' +
Expand All @@ -34,18 +36,13 @@ function validate(credentials) {
throw new Error('Invalid value for access_token_secret. Expected string but got ' +
typeof credentials.access_token_secret);
}
// Ensure at least some tokens were provided
if (!('access_token_key' in credentials) &&
!('access_token_secret' in credentials) &&
!('consumer_key' in credentials) &&
!('consumer_secret' in credentials) &&
!('bearer_token' in credentials)) {
throw new Error('Invalid argument: no credentials defined');
}
// Ensure pairwise relationships
//
// consumer_key + consumer_secret
// access_token_key + access_token_secret
if (('consumer_key' in credentials && !('consumer_secret' in credentials)) ||
(!('consumer_key' in credentials) && 'consumer_secret' in credentials)) {
throw new Error('Invalid argument: when using consumer keys, both consumer_key and ' +
Expand All @@ -58,7 +55,6 @@ function validate(credentials) {
throw new Error('Invalid argument: access_token_key and access_token_secret must both ' +
'be defined when using user authorization');
}
// Ensure valid user authentication (if applicable)
if (('access_token_key' in credentials ||
'access_token_secret' in credentials) &&
(!('consumer_key' in credentials) || !('consumer_secret' in credentials))) {
Expand Down Expand Up @@ -94,16 +90,12 @@ async function createBearerToken({ consumer_key, consumer_secret }) {
}
class Credentials {
constructor(args) {
removeNullAndUndefined(args);
validate(args);
if ('consumer_key' in args) {
this._consumer_key = args.consumer_key;
this._consumer_secret = args.consumer_secret;
}
// Reasonably, some clients provide the authorization header as the bearer
// token, in this case we automatically strip the bearer prefix to normalize
// the credentials.
//
// https://github.com/HunterLarco/twitter-v2/issues/32
if ('bearer_token' in args) {
this._bearer_token = args.bearer_token.startsWith('Bearer ')
? args.bearer_token.substr(7)
Expand Down
8 changes: 0 additions & 8 deletions build/TwitterStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ class TwitterStream {
this._events[this._events.length - 1].resolve(promise);
this._events.push(new DeferredPromise());
}
// As per https://developer.twitter.com/en/docs/labs/v1/filtered-stream/faq
//
// When streaming Tweets, the goal is to stay connected for as long as
// possible, recognizing that disconnects may occur. In Labs, the streaming
// endpoint does not include a way to recover Tweets that were missed while
// disconnected. Instead, the endpoint provides a 20-second keep alive
// heartbeat (it will look like a new line character). Use this signal to
// detect if you’re being disconnected.
_refreshTimeout() {
if (this._state !== State.CLOSED) {
if (this._timeout) {
Expand Down
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# twitter-v2 Examples

These code samples can all be run with `node`

```sh
node examples/get_tweets.js
```
2 changes: 1 addition & 1 deletion examples/get_tweets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Twitter = require('../src/twitter.js');
const Twitter = require('../build/twitter.js');

const credentials = require('./helpers/credentials.js');

Expand Down
2 changes: 1 addition & 1 deletion examples/search_tweets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Twitter = require('../src/twitter.js');
const Twitter = require('../build/twitter.js');

const credentials = require('./helpers/credentials.js');

Expand Down
2 changes: 1 addition & 1 deletion examples/stream_tweets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Twitter = require('../src/twitter.js');
const Twitter = require('../build/twitter.js');

const credentials = require('./helpers/credentials.js');

Expand Down
7 changes: 7 additions & 0 deletions src/Credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export declare type CredentialsArgs =
| ApplicationFullCredentials
| UserCredentials;

// For our non-typscript users which may pass nullable values in the credentials
// object, strip these.
function removeNullAndUndefined(obj: object) {
Object.keys(obj).forEach((key) => obj[key] == null && delete obj[key]);
}

function validate(credentials: CredentialsArgs) {
// Ensure all tokens are strings

Expand Down Expand Up @@ -196,6 +202,7 @@ export default class Credentials {
private _oauth?: OAuth;

constructor(args: CredentialsArgs) {
removeNullAndUndefined(args);
validate(args);

if ('consumer_key' in args) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,
"outDir": "build"
"outDir": "build",
"removeComments": true
},

"include": ["src/**/*"]
Expand Down

0 comments on commit 2bfd97c

Please sign in to comment.