Skip to content

Commit e85e687

Browse files
authored
0.1.0 (#1)
1 parent abf2a81 commit e85e687

14 files changed

+772
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Mac OS
2+
.DS_Store
3+
4+
# Ignore Node Modules
5+
node_modules/
6+
7+
# Logs
8+
*.log
9+
npm-debug.log*
10+
11+
# Ignore backup files.
12+
*~
13+
14+
# Ignore Vim swap files.
15+
.*.swp

.npmignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Mac OS
2+
.DS_Store
3+
4+
# Ignore Node Modules
5+
node_modules/
6+
7+
# Logs
8+
*.log
9+
npm-debug.log*
10+
11+
# Ignore backup files.
12+
*~
13+
14+
# Ignore Vim swap files.
15+
.*.swp
16+
17+
# Ignore Examples
18+
examples/
19+
20+
# Ignore prettier
21+
.prettierignore
22+
.prettierrc.json

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore Node Modules
2+
node_modules/

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"arrowParens": "always",
3+
"endOfLine": "lf",
4+
"printWidth": 80,
5+
"singleQuote": true,
6+
"trailingComma": "es5"
7+
}

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Twitter V2 API for Node.js
2+
3+
An asynchronous client library for the Twitter REST and Streaming [V2 API's](https://developer.twitter.com/en/docs/twitter-api/early-access).
4+
5+
[![NPM](https://nodei.co/npm/twitter-v2.png)](https://nodei.co/npm/twitter-v2/)
6+
7+
```javascript
8+
const Twitter = require('twitter-v2');
9+
10+
const client = new Twitter({
11+
consumer_key: '',
12+
consumer_secret: '',
13+
access_token_key: '',
14+
access_token_secret: '',
15+
});
16+
17+
const { data } = await client.get('tweets', { ids: '1228393702244134912' });
18+
console.log(data);
19+
```
20+
21+
## Installation
22+
23+
`npm install twitter-v2`
24+
25+
## Quick Start
26+
27+
You will need valid Twitter developer credentials in the form of a set of consumer and access tokens/keys. You can get early access V2 keys [here](https://developer.twitter.com/en/apply-for-access).
28+
29+
## For User based authentication:
30+
31+
```javascript
32+
const client = new Twitter({
33+
consumer_key: '',
34+
consumer_secret: '',
35+
access_token_key: '',
36+
access_token_secret: '',
37+
});
38+
```
39+
40+
## For App based authentication:
41+
42+
```javascript
43+
const client = new Twitter({
44+
consumer_key: '',
45+
consumer_secret: '',
46+
});
47+
```
48+
49+
Note - You will not have access to all endpoints while using Application Only authentication, but you will have access to higher API limits.
50+
51+
## REST API
52+
53+
You can make GET and POST requests against the REST API via the convenience methods.
54+
55+
```javascript
56+
client.get(path, urlParams);
57+
client.post(path, body, urlParams);
58+
```
59+
60+
The REST API convenience methods return Promises.
61+
62+
## Streaming API
63+
64+
Use the streaming convenience methods for any stream APIs.
65+
66+
```javascript
67+
client.stream(path, urlParams);
68+
```
69+
70+
The Streaming API will return an async iterator with the convenience method `close()`.
71+
72+
```javascript
73+
const stream = client.stream(path, urlParams);
74+
75+
for await (const { data } of stream) {
76+
console.log(data);
77+
}
78+
79+
stream.close();
80+
```
81+
82+
## V1.1 API Support
83+
84+
This module does not support previous versions of the Twitter API, however it works well with the following V1.1 modules
85+
86+
[![NPM](https://nodei.co/npm/twitter.png?compact=true)](https://nodei.co/npm/twitter/)
87+
[![NPM](https://nodei.co/npm/twit.png?compact=true)](https://nodei.co/npm/twit/)

examples/get_tweets.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const Credentials = require('./helpers/credentials.js');
2+
const Twitter = require('../src/twitter.js');
3+
4+
async function main() {
5+
const args = require('minimist')(process.argv.slice(2));
6+
const credentials = await Credentials.createFromCLI(args);
7+
8+
const client = new Twitter(credentials);
9+
console.log(await client.get('tweets', { ids: '1228393702244134912' }));
10+
}
11+
12+
if (require.main === module) {
13+
main().catch((error) => {
14+
console.error(error);
15+
process.exit(1);
16+
});
17+
}

examples/helpers/credentials.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const enquirer = require('enquirer');
2+
3+
async function createFromCLI(args) {
4+
const credentials = {
5+
consumer_key: args['consumer-key'],
6+
consumer_secret: args['consumer-secret'],
7+
bearer_token: args['bearer-token'],
8+
access_token: args['access-token'],
9+
access_token_secret: args['access-token-secret'],
10+
};
11+
12+
const prompts = [];
13+
if (!credentials.consumer_key) {
14+
prompts.push({
15+
type: 'input',
16+
name: 'consumer_key',
17+
message: `consumer-key:`,
18+
});
19+
}
20+
if (!credentials.consumer_secret) {
21+
prompts.push({
22+
type: 'input',
23+
name: 'consumer_secret',
24+
message: `consumer-secret:`,
25+
});
26+
}
27+
28+
const promptResults = await enquirer.prompt(prompts);
29+
return { ...credentials, ...promptResults };
30+
}
31+
32+
module.exports = {
33+
createFromCLI,
34+
};

examples/search_tweets.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const Credentials = require('./helpers/credentials.js');
2+
const Twitter = require('../src/twitter.js');
3+
4+
async function main() {
5+
const args = require('minimist')(process.argv.slice(2));
6+
const credentials = await Credentials.createFromCLI(args);
7+
8+
const client = new Twitter(credentials);
9+
const { data: tweets, meta } = await client.get('tweets/search/recent', {
10+
query: 'url:"https://medium.com" -is:retweet lang:en',
11+
max_results: 10,
12+
tweet: {
13+
fields: [
14+
'created_at',
15+
'entities',
16+
'in_reply_to_user_id',
17+
'public_metrics',
18+
'referenced_tweets',
19+
'source',
20+
'author_id',
21+
],
22+
},
23+
});
24+
25+
for (const tweet of tweets) {
26+
console.log(tweet);
27+
}
28+
console.log(meta);
29+
}
30+
31+
if (require.main === module) {
32+
main().catch((error) => {
33+
console.error(error);
34+
process.exit(1);
35+
});
36+
}

examples/stream_tweets.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Credentials = require('./helpers/credentials.js');
2+
const Twitter = require('../src/twitter.js');
3+
4+
async function main() {
5+
const args = require('minimist')(process.argv.slice(2));
6+
const credentials = await Credentials.createFromCLI(args);
7+
8+
const client = new Twitter(credentials);
9+
const stream = client.stream('tweets/sample/stream');
10+
11+
// Close the stream after 30 seconds
12+
setTimeout(() => {
13+
stream.close();
14+
}, 30 * 1000);
15+
16+
// Read data from the stream
17+
for await (const { data } of stream) {
18+
console.log(`${data.id}: ${data.text.replace(/\s/g, ' ')}`);
19+
}
20+
21+
console.log('Stream closed.');
22+
}
23+
24+
if (require.main === module) {
25+
main().catch((error) => {
26+
console.error(error);
27+
process.exit(1);
28+
});
29+
}

package-lock.json

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "twitter-v2",
3+
"version": "0.1.0",
4+
"description": "An asynchronous client library for the Twitter REST and Streaming V2 API's.",
5+
"main": "src/twitter.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/HunterLarco/twitter-v2.git"
12+
},
13+
"keywords": [
14+
"twitter",
15+
"v2",
16+
"api",
17+
"stream",
18+
"streaming",
19+
"rest",
20+
"oauth",
21+
"twit"
22+
],
23+
"author": "Hunter Larco",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/HunterLarco/twitter-v2/issues"
27+
},
28+
"engines": {
29+
"node": ">=0.10.0"
30+
},
31+
"homepage": "https://github.com/HunterLarco/twitter-v2#readme",
32+
"devDependencies": {
33+
"enquirer": "2.3.6",
34+
"minimist": "1.2.5",
35+
"prettier": "2.1.1"
36+
},
37+
"dependencies": {
38+
"abort-controller": "3.0.0",
39+
"crypto": "1.0.1",
40+
"node-fetch": "2.6.0",
41+
"oauth-1.0a": "2.2.6",
42+
"split": "1.0.1"
43+
}
44+
}

0 commit comments

Comments
 (0)