refactor oauth repo interface

This commit is contained in:
Ramnique Singh 2026-02-21 22:58:38 +05:30
parent 937c9be891
commit a358ae7051
5 changed files with 35 additions and 88 deletions

View file

@ -5,9 +5,9 @@ import { OAuthTokens } from './types.js';
import z from 'zod';
const ProviderConnectionSchema = z.object({
tokens: OAuthTokens,
clientId: z.string().optional(),
error: z.string().optional(),
tokens: OAuthTokens.nullable().optional(),
clientId: z.string().nullable().optional(),
error: z.string().nullable().optional(),
});
const OAuthConfigSchema = z.object({
@ -17,7 +17,7 @@ const OAuthConfigSchema = z.object({
const ClientFacingConfigSchema = z.record(z.string(), z.object({
connected: z.boolean(),
error: z.string().optional(),
error: z.string().nullable().optional(),
}));
const LegacyOauthConfigSchema = z.record(z.string(), OAuthTokens);
@ -28,13 +28,9 @@ const DEFAULT_CONFIG: z.infer<typeof OAuthConfigSchema> = {
};
export interface IOAuthRepo {
getTokens(provider: string): Promise<OAuthTokens | null>;
saveTokens(provider: string, tokens: OAuthTokens): Promise<void>;
clearTokens(provider: string): Promise<void>;
getClientId(provider: string): Promise<string | null>;
setClientId(provider: string, clientId: string): Promise<void>;
setError(provider: string, errorMessage: string): Promise<void>;
clearError(provider: string): Promise<void>;
read(provider: string): Promise<z.infer<typeof ProviderConnectionSchema>>;
upsert(provider: string, connection: Partial<z.infer<typeof ProviderConnectionSchema>>): Promise<void>;
delete(provider: string): Promise<void>;
getClientFacingConfig(): Promise<z.infer<typeof ClientFacingConfigSchema>>;
}
@ -92,71 +88,22 @@ export class FSOAuthRepo implements IOAuthRepo {
await fs.writeFile(this.configPath, JSON.stringify(config, null, 2));
}
async getTokens(provider: string): Promise<OAuthTokens | null> {
async read(provider: string): Promise<z.infer<typeof ProviderConnectionSchema>> {
const config = await this.readConfig();
const tokens = config.providers[provider]?.tokens;
return tokens ?? null;
return config.providers[provider] ?? {};
}
async saveTokens(provider: string, tokens: OAuthTokens): Promise<void> {
async upsert(provider: string, connection: Partial<z.infer<typeof ProviderConnectionSchema>>): Promise<void> {
const config = await this.readConfig();
if (config.providers[provider]) {
delete config.providers[provider];
}
config.providers[provider] = {
tokens,
};
config.providers[provider] = { ...config.providers[provider] ?? {}, ...connection };
await this.writeConfig(config);
}
async clearTokens(provider: string): Promise<void> {
async delete(provider: string): Promise<void> {
const config = await this.readConfig();
delete config.providers[provider];
await this.writeConfig(config);
}
async getClientId(provider: string): Promise<string | null> {
const config = await this.readConfig();
const clientId = config.providers[provider]?.clientId;
return clientId ?? null;
}
async setClientId(provider: string, clientId: string): Promise<void> {
const config = await this.readConfig();
if (!config.providers[provider]) {
throw new Error(`Provider ${provider} not found`);
}
config.providers[provider].clientId = clientId;
await this.writeConfig(config);
}
async clearClientId(provider: string): Promise<void> {
const config = await this.readConfig();
if (!config.providers[provider]) {
throw new Error(`Provider ${provider} not found`);
}
delete config.providers[provider].clientId;
await this.writeConfig(config);
}
async setError(provider: string, errorMessage: string): Promise<void> {
const config = await this.readConfig();
if (!config.providers[provider]) {
throw new Error(`Provider ${provider} not found`);
}
config.providers[provider].error = errorMessage;
await this.writeConfig(config);
}
async clearError(provider: string): Promise<void> {
const config = await this.readConfig();
if (!config.providers[provider]) {
throw new Error(`Provider ${provider} not found`);
}
delete config.providers[provider].error;
await this.writeConfig(config);
}
async getClientFacingConfig(): Promise<z.infer<typeof ClientFacingConfigSchema>> {
const config = await this.readConfig();
const clientFacingConfig: z.infer<typeof ClientFacingConfigSchema> = {};

View file

@ -31,7 +31,7 @@ export class FirefliesClientFactory {
*/
static async getClient(): Promise<Client | null> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const tokens = await oauthRepo.getTokens(this.PROVIDER_NAME);
const { tokens } = await oauthRepo.read(this.PROVIDER_NAME);
if (!tokens) {
this.clearCache();
@ -49,7 +49,7 @@ export class FirefliesClientFactory {
// Token expired, try to refresh
if (!tokens.refresh_token) {
console.log("[Fireflies] Token expired and no refresh token available.");
await oauthRepo.setError(this.PROVIDER_NAME, 'Missing refresh token. Please reconnect.');
await oauthRepo.upsert(this.PROVIDER_NAME, { error: 'Missing refresh token. Please reconnect.' });
this.clearCache();
return null;
}
@ -62,7 +62,7 @@ export class FirefliesClientFactory {
tokens.refresh_token,
existingScopes
);
await oauthRepo.saveTokens(this.PROVIDER_NAME, refreshedTokens);
await oauthRepo.upsert(this.PROVIDER_NAME, { tokens: refreshedTokens });
// Update cached tokens and recreate client
this.cache.tokens = refreshedTokens;
@ -77,7 +77,7 @@ export class FirefliesClientFactory {
return this.cache.client;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to refresh token for Fireflies';
await oauthRepo.setError(this.PROVIDER_NAME, message);
await oauthRepo.upsert(this.PROVIDER_NAME, { error: message });
console.error("[Fireflies] Failed to refresh token:", error);
this.clearCache();
return null;
@ -107,7 +107,7 @@ export class FirefliesClientFactory {
*/
static async hasValidCredentials(): Promise<boolean> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const tokens = await oauthRepo.getTokens(this.PROVIDER_NAME);
const { tokens } = await oauthRepo.read(this.PROVIDER_NAME);
return tokens !== null;
}

View file

@ -27,9 +27,9 @@ export class GoogleClientFactory {
private static async resolveClientId(): Promise<string> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const clientId = await oauthRepo.getClientId(this.PROVIDER_NAME);
const { clientId } = await oauthRepo.read(this.PROVIDER_NAME);
if (!clientId) {
await oauthRepo.setError(this.PROVIDER_NAME, 'Google client ID missing. Please reconnect.');
await oauthRepo.upsert(this.PROVIDER_NAME, { error: 'Google client ID missing. Please reconnect.' });
throw new Error('Google client ID missing. Please reconnect.');
}
return clientId;
@ -40,7 +40,7 @@ export class GoogleClientFactory {
*/
static async getClient(): Promise<OAuth2Client | null> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const tokens = await oauthRepo.getTokens(this.PROVIDER_NAME);
const { tokens } = await oauthRepo.read(this.PROVIDER_NAME);
if (!tokens) {
this.clearCache();
@ -64,7 +64,7 @@ export class GoogleClientFactory {
// Token expired, try to refresh
if (!tokens.refresh_token) {
console.log("[OAuth] Token expired and no refresh token available for Google.");
await oauthRepo.setError(this.PROVIDER_NAME, 'Missing refresh token. Please reconnect.');
await oauthRepo.upsert(this.PROVIDER_NAME, { error: 'Missing refresh token. Please reconnect.' });
this.clearCache();
return null;
}
@ -77,7 +77,7 @@ export class GoogleClientFactory {
tokens.refresh_token,
existingScopes
);
await oauthRepo.saveTokens(this.PROVIDER_NAME, refreshedTokens);
await oauthRepo.upsert(this.PROVIDER_NAME, { tokens: refreshedTokens });
// Update cached tokens and recreate client
this.cache.tokens = refreshedTokens;
@ -89,7 +89,7 @@ export class GoogleClientFactory {
return this.cache.client;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to refresh token for Google';
await oauthRepo.setError(this.PROVIDER_NAME, message);
await oauthRepo.upsert(this.PROVIDER_NAME, { error: message });
console.error("[OAuth] Failed to refresh token for Google:", error);
this.clearCache();
return null;
@ -116,7 +116,7 @@ export class GoogleClientFactory {
*/
static async hasValidCredentials(requiredScopes: string | string[]): Promise<boolean> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const tokens = await oauthRepo.getTokens(this.PROVIDER_NAME);
const { tokens } = await oauthRepo.read(this.PROVIDER_NAME);
if (!tokens) {
return false;
}