summary refs log tree commit diff
path: root/src/util/connections/RefreshableConnection.ts
blob: 87f5f6dd3d7b1c2f494e6b3eaf772a0f28c982c4 (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, fetched_at: Date.now() };
		await connectedAccount.save();
		return tokenData;
	}
}