summary refs log tree commit diff
path: root/src/util/connections/RefreshableConnection.ts
blob: 0008cbc04450be52c4d6807612e4e0c518b0707d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { ConnectedAccount } from "../entities";
import { ConnectedAccountCommonOAuthTokenResponse } from "../interfaces";
import Connection from "./Connection";

/**
 * A connection that can refresh its token.
 */
export default abstract class RefreshableConnection extends Connection {
	refreshEnabled = true;
	/**
	 * Refreshes the token for a connected account.
	 * @param connectedAccount The connected account to refresh
	 */
	abstract refreshToken(
		connectedAccount: ConnectedAccount,
	): Promise<ConnectedAccountCommonOAuthTokenResponse>;

	/**
	 * Refreshes the token for a connected account and saves it to the database.
	 * @param connectedAccount The connected account to refresh
	 */
	async refresh(
		connectedAccount: ConnectedAccount,
	): Promise<ConnectedAccountCommonOAuthTokenResponse> {
		const tokenData = await this.refreshToken(connectedAccount);
		connectedAccount.token_data = tokenData;
		await connectedAccount.save();
		return tokenData;
	}
}