Skip to content

Commit 385b72d

Browse files
committed
return token from endpoints
1 parent b055445 commit 385b72d

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/OAuth2User.ts

+42-35
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,37 @@ 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;
80+
interface GetTokenResponse {
81+
refresh_token?: string;
82+
access_token?: string;
83+
token_type?: string;
84+
expires_in?: number;
85+
scope?: string;
8686
}
8787

88-
/**
89-
* Twitter OAuth2 Authentication Client
90-
*/
91-
export class OAuth2User implements AuthClient {
88+
interface Token {
89+
refresh_token?: string;
9290
access_token?: string;
9391
token_type?: string;
9492
expires_at?: Date;
9593
scope?: string;
96-
refresh_token?: string;
94+
}
95+
96+
function processTokenResponse(token: GetTokenResponse): Token {
97+
const { expires_in, ...rest } = token;
98+
return {
99+
...rest,
100+
...(!!expires_in && {
101+
expires_at: new Date(Date.now() + expires_in * 1000),
102+
}),
103+
};
104+
}
105+
106+
/**
107+
* Twitter OAuth2 Authentication Client
108+
*/
109+
export class OAuth2User implements AuthClient {
110+
token?: Token;
97111
#options: OAuth2UserOptions;
98112
#code_verifier?: string;
99113
#code_challenge?: string;
@@ -104,16 +118,16 @@ export class OAuth2User implements AuthClient {
104118
/**
105119
* Refresh the access token
106120
*/
107-
async refreshAccessToken(): Promise<void> {
108-
const refresh_token = this.refresh_token;
121+
async refreshAccessToken(): Promise<{ token: Token }> {
122+
const refresh_token = this.token?.refresh_token;
109123
const { client_id, client_secret, request_options } = this.#options;
110124
if (!client_id) {
111125
throw new Error("client_id is required");
112126
}
113127
if (!refresh_token) {
114128
throw new Error("refresh_token is required");
115129
}
116-
const data = await rest<TokenResponse>({
130+
const data = await rest<GetTokenResponse>({
117131
...request_options,
118132
endpoint: `/2/oauth2/token`,
119133
params: {
@@ -130,26 +144,17 @@ export class OAuth2User implements AuthClient {
130144
}),
131145
},
132146
});
133-
this.updateToken(data);
134-
}
135-
136-
/**
137-
* Update token information
138-
*/
139-
updateToken(data: TokenResponse): void {
140-
this.refresh_token = data.refresh_token;
141-
this.access_token = data.access_token;
142-
this.token_type = data.token_type;
143-
this.expires_at = new Date(Date.now() + data.expires_in * 1000);
144-
this.scope = data.scope;
147+
const token = processTokenResponse(data);
148+
this.token = token;
149+
return { token };
145150
}
146151

147152
/**
148153
* Check if an access token is expired
149154
*/
150155
isAccessTokenExpired(): boolean {
151-
const refresh_token = this.refresh_token;
152-
const expires_at = this.expires_at;
156+
const refresh_token = this.token?.refresh_token;
157+
const expires_at = this.token?.expires_at;
153158
return (
154159
!!refresh_token &&
155160
(!expires_at || expires_at <= new Date(Date.now() + 1000))
@@ -159,7 +164,7 @@ export class OAuth2User implements AuthClient {
159164
/**
160165
* Request an access token
161166
*/
162-
async requestAccessToken(code?: string): Promise<void> {
167+
async requestAccessToken(code?: string): Promise<{ token: Token }> {
163168
const { client_id, client_secret, callback, request_options } =
164169
this.#options;
165170
const code_verifier = this.#code_verifier;
@@ -176,7 +181,7 @@ export class OAuth2User implements AuthClient {
176181
client_id,
177182
redirect_uri: callback,
178183
};
179-
const data = await rest<TokenResponse>({
184+
const data = await rest<GetTokenResponse>({
180185
...request_options,
181186
endpoint: `/2/oauth2/token`,
182187
params,
@@ -189,16 +194,18 @@ export class OAuth2User implements AuthClient {
189194
}),
190195
},
191196
});
192-
this.updateToken(data);
197+
const token = processTokenResponse(data);
198+
this.token = token;
199+
return { token };
193200
}
194201

195202
/**
196203
* Revoke an access token
197204
*/
198205
async revokeAccessToken(): Promise<RevokeAccessTokenResponse> {
199206
const { client_id, client_secret, request_options } = this.#options;
200-
const access_token = this.access_token;
201-
const refresh_token = this.refresh_token;
207+
const access_token = this.token?.access_token;
208+
const refresh_token = this.token?.refresh_token;
202209
if (!client_id) {
203210
throw new Error("client_id is required");
204211
}
@@ -260,10 +267,10 @@ export class OAuth2User implements AuthClient {
260267
}
261268

262269
async getAuthHeader(): Promise<AuthHeader> {
263-
if (!this.access_token) throw new Error("access_token is required");
270+
if (!this.token?.access_token) throw new Error("access_token is required");
264271
if (this.isAccessTokenExpired()) await this.refreshAccessToken();
265272
return {
266-
Authorization: `Bearer ${this.access_token}`,
273+
Authorization: `Bearer ${this.token.access_token}`,
267274
};
268275
}
269276
}

0 commit comments

Comments
 (0)