twitter-api-v2
Advanced tools
Comparing version 1.7.2 to 1.8.0
@@ -0,5 +1,10 @@ | ||
1.8.0 | ||
----- | ||
- Feat: Add OAuth2 user-context support | ||
- Feat: Add users/me v2 endpoint wrapper | ||
1.7.2 | ||
----- | ||
- Fix: Paginator can return multiple times the same results in some conditions | ||
- Feat: .done properties for paginators, to know when a next page is fetchable | ||
- Feat: .done property for paginators, to know when a next page is fetchable | ||
@@ -6,0 +11,0 @@ 1.7.1 |
export declare class OAuth2Helper { | ||
static getCodeVerifier(): string; | ||
static getCodeChallengeFromVerifier(verifier: string): string; | ||
static getAuthHeader(clientId: string, clientSecret: string): string; | ||
static generateRandomString(length: number): string; | ||
private static escapeBase64Url; | ||
} |
@@ -34,2 +34,7 @@ "use strict"; | ||
} | ||
static getAuthHeader(clientId, clientSecret) { | ||
const key = encodeURIComponent(clientId) + ':' + encodeURIComponent(clientSecret); | ||
return Buffer.from(key).toString('base64'); | ||
; | ||
} | ||
static generateRandomString(length) { | ||
@@ -36,0 +41,0 @@ let text = ''; |
@@ -61,2 +61,3 @@ /// <reference types="node" /> | ||
protected _clientId?: string; | ||
protected _clientSecret?: string; | ||
protected _oauth?: OAuth1Helper; | ||
@@ -63,0 +64,0 @@ protected _rateLimits: { |
@@ -12,2 +12,3 @@ "use strict"; | ||
const request_param_helper_1 = __importDefault(require("./request-param.helper")); | ||
const oauth2_helper_1 = require("./oauth2.helper"); | ||
class ClientRequestMaker { | ||
@@ -83,2 +84,6 @@ constructor() { | ||
} | ||
else if (this._clientId && this._clientSecret) { | ||
// Basic auth with clientId + clientSecret | ||
headers.Authorization = 'Basic ' + oauth2_helper_1.OAuth2Helper.getAuthHeader(this._clientId, this._clientSecret); | ||
} | ||
else if (this._consumerSecret && this._oauth) { | ||
@@ -85,0 +90,0 @@ // Merge query and body |
@@ -1,2 +0,2 @@ | ||
import { TClientTokens, TwitterApiBasicAuth, TwitterApiOAuth2Init, TwitterApiTokens, TwitterRateLimit, TwitterResponse, UserV1 } from './types'; | ||
import type { TClientTokens, TwitterApiBasicAuth, TwitterApiOAuth2Init, TwitterApiTokens, TwitterRateLimit, TwitterResponse, UserV1, UserV2Result } from './types'; | ||
import { ClientRequestMaker, TCustomizableRequestArgs, TRequestBody, TRequestQuery } from './client-mixins/request-maker.mixin'; | ||
@@ -49,12 +49,13 @@ import TweetStream from './stream/TweetStream'; | ||
protected _currentUser: UserV1 | null; | ||
protected _currentUserV2: UserV2Result | null; | ||
/** | ||
* Create a new TwitterApi object without authentification. | ||
* Create a new TwitterApi object without authentication. | ||
*/ | ||
constructor(); | ||
/** | ||
* Create a new TwitterApi object with OAuth 2.0 Bearer authentification. | ||
* Create a new TwitterApi object with OAuth 2.0 Bearer authentication. | ||
*/ | ||
constructor(bearerToken: string); | ||
/** | ||
* Create a new TwitterApi object with three-legged OAuth 1.0a authentification. | ||
* Create a new TwitterApi object with three-legged OAuth 1.0a authentication. | ||
*/ | ||
@@ -67,3 +68,3 @@ constructor(tokens: TwitterApiTokens); | ||
/** | ||
* Create a new TwitterApi object with Basic HTTP authentification. | ||
* Create a new TwitterApi object with Basic HTTP authentication. | ||
*/ | ||
@@ -95,2 +96,11 @@ constructor(credentials: TwitterApiBasicAuth); | ||
protected getCurrentUserObject(forceFetch?: boolean): Promise<UserV1>; | ||
/** | ||
* Get cached current user from v2 API. | ||
* This can only be the slimest available `UserV2` object, with only `id`, `name` and `username` properties defined. | ||
* | ||
* To get a customized `UserV2Result`, use `.v2.me()` | ||
* | ||
* OAuth2 scopes: `tweet.read` & `users.read` | ||
*/ | ||
protected getCurrentUserV2Object(forceFetch?: boolean): Promise<UserV2Result>; | ||
get<T = any>(url: string, query?: TRequestQuery, args?: TGetClientRequestArgsDataResponse): Promise<T>; | ||
@@ -97,0 +107,0 @@ get<T = any>(url: string, query?: TRequestQuery, args?: TGetClientRequestArgsFullResponse): Promise<TwitterResponse<T>>; |
@@ -11,2 +11,3 @@ "use strict"; | ||
this._currentUser = null; | ||
this._currentUserV2 = null; | ||
if (typeof token === 'string') { | ||
@@ -25,2 +26,3 @@ this._bearerToken = token; | ||
this._clientId = token._clientId; | ||
this._clientSecret = token._clientSecret; | ||
this._rateLimits = token._rateLimits; | ||
@@ -43,2 +45,3 @@ } | ||
this._clientId = token.clientId; | ||
this._clientSecret = token.clientSecret; | ||
} | ||
@@ -127,2 +130,18 @@ } | ||
} | ||
/** | ||
* Get cached current user from v2 API. | ||
* This can only be the slimest available `UserV2` object, with only `id`, `name` and `username` properties defined. | ||
* | ||
* To get a customized `UserV2Result`, use `.v2.me()` | ||
* | ||
* OAuth2 scopes: `tweet.read` & `users.read` | ||
*/ | ||
async getCurrentUserV2Object(forceFetch = false) { | ||
if (!forceFetch && this._currentUserV2) { | ||
return this._currentUserV2; | ||
} | ||
const currentUserV2 = await this.get('users/me', undefined, { prefix: 'https://api.twitter.com/2/' }); | ||
this._currentUserV2 = currentUserV2; | ||
return currentUserV2; | ||
} | ||
async get(url, query = {}, { fullResponse, prefix = this._prefix, ...rest } = {}) { | ||
@@ -129,0 +148,0 @@ if (prefix) |
@@ -16,5 +16,5 @@ import TwitterApi from '.'; | ||
* Fetch and cache current user. | ||
* This method can only be called with a OAuth 1.0a user authentification. | ||
* This method can only be called with a OAuth 1.0a user authentication. | ||
* | ||
* You can use this method to test if authentification was successful. | ||
* You can use this method to test if authentication was successful. | ||
* Next calls to this methods will use the cached user, unless `forceFetch: true` is given. | ||
@@ -66,3 +66,3 @@ */ | ||
/** | ||
* Enable application-only authentification. | ||
* Enable application-only authentication. | ||
* | ||
@@ -82,2 +82,7 @@ * To make the request, instanciate TwitterApi with consumer and secret. | ||
* | ||
* - **You can only use v2 API endpoints with this authentication method.** | ||
* - **You need to specify which scope you want to have when you create your auth link. Make sure it matches your needs.** | ||
* | ||
* See https://developer.twitter.com/en/docs/authentication/oauth-2-0/user-access-token for details. | ||
* | ||
* ```ts | ||
@@ -109,2 +114,4 @@ * // Instanciate TwitterApi with client ID | ||
* | ||
* You need to obtain `codeVerifier` from a call to `.generateOAuth2AuthLink`. | ||
* | ||
* ```ts | ||
@@ -111,0 +118,0 @@ * // Use the saved codeVerifier associated to state (present in query string of callback) |
@@ -28,5 +28,5 @@ "use strict"; | ||
* Fetch and cache current user. | ||
* This method can only be called with a OAuth 1.0a user authentification. | ||
* This method can only be called with a OAuth 1.0a user authentication. | ||
* | ||
* You can use this method to test if authentification was successful. | ||
* You can use this method to test if authentication was successful. | ||
* Next calls to this methods will use the cached user, unless `forceFetch: true` is given. | ||
@@ -41,3 +41,3 @@ */ | ||
} | ||
/* Authentification */ | ||
/* Authentication */ | ||
/** | ||
@@ -108,3 +108,3 @@ * Generate the OAuth request token link for user-based OAuth 1.0 auth. | ||
/** | ||
* Enable application-only authentification. | ||
* Enable application-only authentication. | ||
* | ||
@@ -123,3 +123,3 @@ * To make the request, instanciate TwitterApi with consumer and secret. | ||
throw new Error('You must setup TwitterApi instance with consumers to enable app-only login'); | ||
// Create a client with Basic authentification | ||
// Create a client with Basic authentication | ||
const basicClient = new _1.default({ username: this._consumerToken, password: this._consumerSecret }); | ||
@@ -130,6 +130,11 @@ const res = await basicClient.post('https://api.twitter.com/oauth2/token', { grant_type: 'client_credentials' }); | ||
} | ||
/* OAuth 2 user authentification */ | ||
/* OAuth 2 user authentication */ | ||
/** | ||
* Generate the OAuth request token link for user-based OAuth 2.0 auth. | ||
* | ||
* - **You can only use v2 API endpoints with this authentication method.** | ||
* - **You need to specify which scope you want to have when you create your auth link. Make sure it matches your needs.** | ||
* | ||
* See https://developer.twitter.com/en/docs/authentication/oauth-2-0/user-access-token for details. | ||
* | ||
* ```ts | ||
@@ -152,3 +157,3 @@ * // Instanciate TwitterApi with client ID | ||
if (!this._clientId) { | ||
throw new Error('Twitter API instance is not initialized with client ID. ' + | ||
throw new Error('Twitter API instance is not initialized with client ID. You can find your client ID in Twitter Developer Portal. ' + | ||
'Please build an instance with: new TwitterApi({ clientId: \'<yourClientId>\' })'); | ||
@@ -182,2 +187,4 @@ } | ||
* | ||
* You need to obtain `codeVerifier` from a call to `.generateOAuth2AuthLink`. | ||
* | ||
* ```ts | ||
@@ -210,2 +217,3 @@ * // Use the saved codeVerifier associated to state (present in query string of callback) | ||
client_id: this._clientId, | ||
client_secret: this._clientSecret, | ||
}); | ||
@@ -233,2 +241,3 @@ return this.parseOAuth2AccessTokenResult(accessTokenResult); | ||
client_id: this._clientId, | ||
client_secret: this._clientSecret, | ||
}); | ||
@@ -250,2 +259,3 @@ return this.parseOAuth2AccessTokenResult(accessTokenResult); | ||
client_id: this._clientId, | ||
client_secret: this._clientSecret, | ||
token, | ||
@@ -252,0 +262,0 @@ token_type_hint: tokenType, |
import type TwitterApi from '../client'; | ||
import { TypeOrArrayOf } from './shared.types'; | ||
export declare type TOAuth2Scope = 'tweet.read' | 'users.read' | 'account.follows.read' | 'account.follows.write' | 'offline.access' | 'space.read'; | ||
export declare type TOAuth2Scope = 'tweet.read' | 'tweet.write' | 'tweet.moderate.write' | 'users.read' | 'follows.read' | 'follows.write' | 'offline.access' | 'space.read' | 'mute.read' | 'mute.write' | 'like.read' | 'like.write' | 'list.read' | 'list.write' | 'block.read' | 'block.write'; | ||
export interface BuildOAuth2RequestLinkArgs { | ||
@@ -5,0 +5,0 @@ scope?: TypeOrArrayOf<TOAuth2Scope> | TypeOrArrayOf<string>; |
@@ -25,2 +25,3 @@ export declare enum ETwitterStreamEvent { | ||
clientId: string; | ||
clientSecret?: string; | ||
} | ||
@@ -27,0 +28,0 @@ export interface TwitterApiBasicAuth { |
@@ -101,3 +101,3 @@ "use strict"; | ||
EApiV1ErrorCode[EApiV1ErrorCode["NoLocationFound"] = 13] = "NoLocationFound"; | ||
// Authentification failures | ||
// Authentication failures | ||
EApiV1ErrorCode[EApiV1ErrorCode["AuthenticationFail"] = 32] = "AuthenticationFail"; | ||
@@ -104,0 +104,0 @@ EApiV1ErrorCode[EApiV1ErrorCode["InvalidOrExpiredToken"] = 89] = "InvalidOrExpiredToken"; |
@@ -85,2 +85,9 @@ import TwitterApiSubClient from '../client.subclient'; | ||
/** | ||
* Returns information about an authorized user. | ||
* https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-me | ||
* | ||
* OAuth2 scopes: `tweet.read` & `users.read` | ||
*/ | ||
me(options?: Partial<UsersV2Params>): Promise<UserV2Result>; | ||
/** | ||
* Returns a variety of information about a single user specified by the requested ID. | ||
@@ -117,3 +124,3 @@ * https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-id | ||
* | ||
* OAuth2 scope: `account.follows.read` | ||
* OAuth2 scope: `follows.read` | ||
*/ | ||
@@ -120,0 +127,0 @@ following(userId: string, options?: Partial<FollowersV2ParamsWithoutPaginator>): Promise<UserV2TimelineResult>; |
@@ -152,2 +152,11 @@ "use strict"; | ||
/** | ||
* Returns information about an authorized user. | ||
* https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-me | ||
* | ||
* OAuth2 scopes: `tweet.read` & `users.read` | ||
*/ | ||
me(options = {}) { | ||
return this.get('users/me', options); | ||
} | ||
/** | ||
* Returns a variety of information about a single user specified by the requested ID. | ||
@@ -154,0 +163,0 @@ * https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-id |
@@ -79,3 +79,3 @@ import TwitterApiv2ReadOnly from './client.v2.read'; | ||
* | ||
* OAuth2 scope: `account.follows.write` | ||
* OAuth2 scope: `follows.write` | ||
* | ||
@@ -89,3 +89,3 @@ * **Note**: You must specify the currently logged user ID ; you can obtain it through v1.1 API. | ||
* | ||
* OAuth2 scope: `account.follows.write` | ||
* OAuth2 scope: `follows.write` | ||
* | ||
@@ -92,0 +92,0 @@ * **Note**: You must specify the currently logged user ID ; you can obtain it through v1.1 API. |
@@ -141,3 +141,3 @@ "use strict"; | ||
* | ||
* OAuth2 scope: `account.follows.write` | ||
* OAuth2 scope: `follows.write` | ||
* | ||
@@ -153,3 +153,3 @@ * **Note**: You must specify the currently logged user ID ; you can obtain it through v1.1 API. | ||
* | ||
* OAuth2 scope: `account.follows.write` | ||
* OAuth2 scope: `follows.write` | ||
* | ||
@@ -156,0 +156,0 @@ * **Note**: You must specify the currently logged user ID ; you can obtain it through v1.1 API. |
{ | ||
"name": "twitter-api-v2", | ||
"version": "1.7.2", | ||
"version": "1.8.0", | ||
"description": "Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -82,3 +82,3 @@ # Twitter API v2 | ||
- Support for v1.1 and **v2 of Twitter API** | ||
- Make signed HTTP requests to Twitter with every auth type: **OAuth 1.0a**, **OAuth2** and **Basic** HTTP Authorization | ||
- Make signed HTTP requests to Twitter with every auth type: **OAuth 1.0a**, **OAuth2** (even brand new user context OAuth2!) and **Basic** HTTP Authorization | ||
- Helpers for numerous HTTP request methods (`GET`, `POST`, `PUT`, `DELETE` and `PATCH`), | ||
@@ -111,3 +111,3 @@ that handle query string parse & format, automatic body formatting and more | ||
- [Create a client and make your first request](./doc/basics.md) | ||
- [Handle Twitter authentification flows](./doc/auth.md) | ||
- [Handle Twitter authentication flows](./doc/auth.md) | ||
- [Explore some examples](./doc/examples.md) | ||
@@ -114,0 +114,0 @@ - Use endpoints wrappers — ensure typings of request & response |
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
406126
9048