Skip to content

Commit b055445

Browse files
committed
allow access to accessToken
1 parent 797123a commit b055445

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/OAuth2User.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,19 @@ interface RevokeAccessTokenResponse {
7777
revoked: boolean;
7878
}
7979

80+
interface TokenResponse {
81+
refresh_token: string;
82+
access_token: string;
83+
token_type: string;
84+
expires_in: number;
85+
scope: string;
86+
}
87+
8088
/**
8189
* Twitter OAuth2 Authentication Client
8290
*/
8391
export class OAuth2User implements AuthClient {
84-
#access_token?: string;
92+
access_token?: string;
8593
token_type?: string;
8694
expires_at?: Date;
8795
scope?: string;
@@ -105,7 +113,7 @@ export class OAuth2User implements AuthClient {
105113
if (!refresh_token) {
106114
throw new Error("refresh_token is required");
107115
}
108-
const data = await rest({
116+
const data = await rest<TokenResponse>({
109117
...request_options,
110118
endpoint: `/2/oauth2/token`,
111119
params: {
@@ -128,9 +136,9 @@ export class OAuth2User implements AuthClient {
128136
/**
129137
* Update token information
130138
*/
131-
updateToken(data: Record<string, any>): void {
139+
updateToken(data: TokenResponse): void {
132140
this.refresh_token = data.refresh_token;
133-
this.#access_token = data.access_token;
141+
this.access_token = data.access_token;
134142
this.token_type = data.token_type;
135143
this.expires_at = new Date(Date.now() + data.expires_in * 1000);
136144
this.scope = data.scope;
@@ -168,7 +176,7 @@ export class OAuth2User implements AuthClient {
168176
client_id,
169177
redirect_uri: callback,
170178
};
171-
const data = await rest({
179+
const data = await rest<TokenResponse>({
172180
...request_options,
173181
endpoint: `/2/oauth2/token`,
174182
params,
@@ -189,7 +197,7 @@ export class OAuth2User implements AuthClient {
189197
*/
190198
async revokeAccessToken(): Promise<RevokeAccessTokenResponse> {
191199
const { client_id, client_secret, request_options } = this.#options;
192-
const access_token = this.#access_token;
200+
const access_token = this.access_token;
193201
const refresh_token = this.refresh_token;
194202
if (!client_id) {
195203
throw new Error("client_id is required");
@@ -252,10 +260,10 @@ export class OAuth2User implements AuthClient {
252260
}
253261

254262
async getAuthHeader(): Promise<AuthHeader> {
255-
if (!this.#access_token) throw new Error("You do not have an access token");
263+
if (!this.access_token) throw new Error("access_token is required");
256264
if (this.isAccessTokenExpired()) await this.refreshAccessToken();
257265
return {
258-
Authorization: `Bearer ${this.#access_token}`,
266+
Authorization: `Bearer ${this.access_token}`,
259267
};
260268
}
261269
}

0 commit comments

Comments
 (0)