├── .eslintignore
├── test
├── test.gif
├── stream.test.js
└── twitter.test.js
├── .husky
└── pre-commit
├── .travis.yml
├── .gitignore
├── .env.example
├── .idea
└── jsLibraryMappings.xml
├── tsconfig.json
├── .vscode
└── launch.json
├── .eslintrc.js
├── stream.js
├── .github
└── workflows
│ └── nodejs.yml
├── LICENSE
├── CHANGELOG.md
├── package.json
├── index.d.ts
├── twitter.js
└── README.md
/.eslintignore:
--------------------------------------------------------------------------------
1 | dist
2 | index.d.ts
--------------------------------------------------------------------------------
/test/test.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/draftbit/twitter-lite/HEAD/test/test.gif
--------------------------------------------------------------------------------
/.husky/pre-commit:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | . "$(dirname "$0")/_/husky.sh"
3 |
4 | npm run lint
5 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 |
3 | node_js:
4 | - "node"
5 |
6 | sudo: false
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .DS_Store
3 | *.log
4 | dist
5 | package-lock.json
6 | .env
7 |
--------------------------------------------------------------------------------
/.env.example:
--------------------------------------------------------------------------------
1 | TWITTER_CONSUMER_KEY=
2 | TWITTER_CONSUMER_SECRET=
3 | ACCESS_TOKEN_KEY=
4 | ACCESS_TOKEN_SECRET=
--------------------------------------------------------------------------------
/.idea/jsLibraryMappings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "target": "ES6",
5 | "lib": [
6 | "es6",
7 | "dom"
8 | ],
9 | "noImplicitAny": true,
10 | "noImplicitThis": true,
11 | "strictNullChecks": true,
12 | "strictFunctionTypes": true,
13 | "noEmit": true,
14 | "forceConsistentCasingInFileNames": true
15 | },
16 | "files": [
17 | "index.d.ts"
18 | ]
19 | }
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "node",
9 | "name": "vscode-jest-tests",
10 | "request": "launch",
11 | "args": ["--runInBand"],
12 | "cwd": "${workspaceFolder}",
13 | "console": "integratedTerminal",
14 | "internalConsoleOptions": "neverOpen",
15 | "program": "${workspaceFolder}/node_modules/jest/bin/jest",
16 | "skipFiles": ["/**"]
17 | }
18 | ]
19 | }
20 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | env: {
3 | browser: true,
4 | es6: true,
5 | node: true,
6 | 'jest/globals': true, // describe, test, expect
7 | },
8 | extends: 'eslint:recommended',
9 | parserOptions: {
10 | ecmaVersion: 2018,
11 | sourceType: 'module',
12 | },
13 | plugins: [
14 | 'jest',
15 | ],
16 | rules: {
17 | 'max-len': ['warn', {
18 | code: 128, // for GitHub
19 | ignoreUrls: true, ignoreStrings: true, ignoreTemplateLiterals: true, ignoreRegExpLiterals: true,
20 | }],
21 | indent: ['error', 2],
22 | semi: ['error', 'always'],
23 | quotes: ['warn', 'single', { avoidEscape: true }],
24 | 'comma-dangle': ['warn', 'always-multiline'],
25 | 'object-curly-spacing': ['error', 'always'],
26 | 'no-multi-spaces': ['error', { ignoreEOLComments: true }],
27 | },
28 | };
29 |
--------------------------------------------------------------------------------
/stream.js:
--------------------------------------------------------------------------------
1 | const EventEmitter = require('events');
2 | const END = '\r\n';
3 | const END_LENGTH = 2;
4 |
5 | class Stream extends EventEmitter {
6 | constructor() {
7 | super();
8 | this.buffer = '';
9 | }
10 |
11 | parse(buffer) {
12 | this.buffer += buffer.toString('utf8');
13 | let index;
14 | let json;
15 |
16 | while ((index = this.buffer.indexOf(END)) > -1) {
17 | json = this.buffer.slice(0, index);
18 | this.buffer = this.buffer.slice(index + END_LENGTH);
19 | if (json.length > 0) {
20 | try {
21 | json = JSON.parse(json);
22 | this.emit(json.event || 'data', json);
23 | } catch (error) {
24 | error.source = json;
25 | this.emit('error', error);
26 | }
27 | } else {
28 | this.emit('ping');
29 | }
30 | }
31 | }
32 | }
33 |
34 | module.exports = Stream;
35 |
--------------------------------------------------------------------------------
/.github/workflows/nodejs.yml:
--------------------------------------------------------------------------------
1 | name: Node CI
2 |
3 | on: [push]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 |
9 | strategy:
10 | matrix:
11 | node-version: ['12', '14']
12 |
13 | steps:
14 | - uses: actions/checkout@v2
15 | - name: Use Node.js ${{ matrix.node-version }}
16 | uses: actions/setup-node@v2
17 | with:
18 | node-version: ${{ matrix.node-version }}
19 | cache: 'yarn'
20 | - name: Verify lockfile and install dependencies
21 | run: yarn install --frozen-lockfile
22 | - name: lint
23 | run: yarn lint
24 | - name: test
25 | env: # twitter keys to run tests
26 | TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
27 | TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }}
28 | ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }}
29 | ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
30 | if: env.TWITTER_CONSUMER_KEY
31 | run: yarn test
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 | Copyright (c) 2018 Peter Piekarczyk
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 |
6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 |
8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 |
10 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # v0.9.3, 2019-Mar-25
2 |
3 | - Return `_headers` in stream creation errors as well
4 |
5 | # v0.9.1, 2019-Jan-16
6 |
7 | - Fix encoding of special characters in direct messages ([#38](https://github.com/draftbit/twitter-lite/issues/38))
8 |
9 | # v0.9, 2019-Jan-06
10 |
11 | ## Breaking changes
12 |
13 | - `.post()` now only takes two parameters: the resource and the body/parameters. If you were previously passing `null` for the body, just delete that, and the next parameter will become the body.
14 |
15 | ## Changes
16 |
17 | - Properly encode and sign POST parameters/body depending on whether the endpoint takes [`application/json`](https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event) or [`application/x-www-form-urlencoded`](https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature)
18 | - Support empty responses (e.g. those returned by [`direct_messages/indicate_typing`](https://developer.twitter.com/en/docs/direct-messages/typing-indicator-and-read-receipts/api-reference/new-typing-indicator)) (fix [#35](https://github.com/draftbit/twitter-lite/issues/35))
19 |
20 | # v0.8, 2018-Dec-13
21 |
22 | - Encode special characters in the POST body (fix [#36](https://github.com/draftbit/twitter-lite/issues/36))
23 |
24 | # v0.7, 2018-Jul-26
25 |
26 | ## Breaking changes
27 |
28 | - 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.
29 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "twitter-lite",
3 | "version": "1.1.0",
4 | "description": "Tiny, full-featured client/server REST/stream library for the Twitter API",
5 | "source": "twitter.js",
6 | "main": "dist/twitter.js",
7 | "module": "dist/twitter.m.js",
8 | "types": "index.d.ts",
9 | "files": [
10 | "dist",
11 | "index.d.ts"
12 | ],
13 | "repository": "draftbit/twitter-lite",
14 | "homepage": "https://github.com/draftbit/twitter-lite",
15 | "author": "Peter Piekarczyk ",
16 | "contributors": [
17 | "Dan Dascalescu (https://github.com/dandv)"
18 | ],
19 | "license": "MIT",
20 | "keywords": [
21 | "twitter",
22 | "rest",
23 | "api",
24 | "twitter api",
25 | "node-twitter",
26 | "twitter oauth",
27 | "twitter rest",
28 | "twitter stream"
29 | ],
30 | "dependencies": {
31 | "cross-fetch": "^3.0.0",
32 | "oauth-1.0a": "^2.2.4"
33 | },
34 | "devDependencies": {
35 | "@types/jest": "^25.2.1",
36 | "@types/node": "^14.17.9",
37 | "bundlesize": "^0.18.0",
38 | "dotenv": "^10.0.0",
39 | "eslint": "^6.8.0",
40 | "eslint-plugin-jest": "^23.8.2",
41 | "flow-bin": "^0.123.0",
42 | "husky": "^7.0.1",
43 | "jest": "^25.5.0",
44 | "microbundle": "^0.13.3",
45 | "typescript": "^4.3.5"
46 | },
47 | "scripts": {
48 | "lint": "eslint . && tsc index.d.ts",
49 | "fix": "eslint --fix .",
50 | "build": "microbundle {stream,twitter}.js && bundlesize",
51 | "test": "jest --detectOpenHandles",
52 | "release": "npm run -s build && npm run lint && npm test && git tag $npm_package_version && git push && git push --tags && npm publish",
53 | "prepare": "husky install"
54 | },
55 | "jest": {
56 | "testEnvironment": "node"
57 | },
58 | "bundlesize": [
59 | {
60 | "path": "dist/**.js",
61 | "maxSize": "3 kB"
62 | },
63 | {
64 | "path": "index.d.ts",
65 | "maxSize": "3 kB"
66 | }
67 | ]
68 | }
69 |
--------------------------------------------------------------------------------
/index.d.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Typings for twitter-lite
3 | *
4 | * @version 0.10-1.0
5 | * @author Floris de Bijl <@fdebijl>
6 | *
7 | * @example
8 | * const Twitter = require('twitter-lite')
9 | *
10 | * const twitter = new Twitter({
11 | * consumer_key: 'XYZ',
12 | * consumer_secret: 'XYZ',
13 | * access_token_key: 'XYZ',
14 | * access_token_secret: 'XYZ'
15 | * });
16 | *
17 | * @example
18 | * // Enable esModuleInterop in your tsconfig to import typings
19 | * import Twitter, { TwitterOptions } from 'twitter-lite'
20 | *
21 | * const config: TwitterOptions = {
22 | * consumer_key: 'XYZ',
23 | * consumer_secret: 'XYZ',
24 | * access_token_key: 'XYZ',
25 | * access_token_secret: 'XYZ'
26 | * };
27 | *
28 | * const twitter = new Twitter(config);
29 | */
30 |
31 | ///
32 | import { EventEmitter } from 'events';
33 | import * as OAuth from 'oauth-1.0a';
34 |
35 | export default class Twitter {
36 | private authType: AuthType;
37 | private url: string;
38 | private oauth: string;
39 | private config: TwitterOptions;
40 | private client: OAuth;
41 | private token: KeySecret;
42 |
43 | constructor(options: TwitterOptions);
44 |
45 | /**
46 | * Parse the JSON from a Response object and add the Headers under `_headers`
47 | */
48 | private static _handleResponse(response: Response): Promise