Refactor and remove getAll

This commit is contained in:
Andras Schmelczer 2025-03-27 19:24:35 +00:00
commit 60ed4e5109
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -18,6 +18,7 @@ export interface CheckConnectionResult {
} }
export class SyncService { export class SyncService {
private static readonly NETWORK_RETRY_INTERVAL_MS = 1000;
private client: Client<paths>; private client: Client<paths>;
private pingClient: Client<paths>; private pingClient: Client<paths>;
private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch; private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch;
@ -244,47 +245,27 @@ export class SyncService {
}); });
} }
public async getAll(
since?: VaultUpdateId
): Promise<components["schemas"]["FetchLatestDocumentsResponse"]> {
return this.withRetries(async () => {
const { vaultName } = this.settings.getSettings();
const response = await this.client.GET(
"/vaults/{vault_id}/documents",
{
params: {
path: {
vault_id: vaultName
},
header: {
authorization: `Bearer ${this.settings.getSettings().token}`
},
query: {
since_update_id: since
}
}
}
);
const { error } = response;
if (error) {
throw new Error(
`Failed to get documents: ${SyncService.formatError(response.error)}`
);
}
this.logger.debug(
`Got ${response.data.latestDocuments.length} document metadata`
);
return response.data;
});
}
public async checkConnection(): Promise<CheckConnectionResult> { public async checkConnection(): Promise<CheckConnectionResult> {
try { try {
const result = await this.ping(); const response = await this.pingClient.GET("/ping", {
params: {
header: {
authorization: `Bearer ${this.settings.getSettings().token}`
}
}
});
this.logger.debug(
`Ping response: ${JSON.stringify(response.data)}`
);
if (!response.data) {
throw new Error(
`Failed to ping server: ${SyncService.formatError(response.error)}`
);
}
const result = response.data;
if (result.isAuthenticated) { if (result.isAuthenticated) {
return { return {
isSuccessful: true, isSuccessful: true,
@ -304,27 +285,6 @@ export class SyncService {
} }
} }
// No retries
private async ping(): Promise<components["schemas"]["PingResponse"]> {
const response = await this.pingClient.GET("/ping", {
params: {
header: {
authorization: `Bearer ${this.settings.getSettings().token}`
}
}
});
this.logger.debug(`Ping response: ${JSON.stringify(response.data)}`);
if (!response.data) {
throw new Error(
`Failed to ping server: ${SyncService.formatError(response.error)}`
);
}
return response.data;
}
/** /**
* Create a client and a ping client for the given remote URI. * Create a client and a ping client for the given remote URI.
*/ */
@ -355,8 +315,10 @@ export class SyncService {
throw e; throw e;
} }
this.logger.error(`Failed network call (${e}), retrying`); this.logger.error(
await sleep(1000); `Failed network call (${e}), retryingin ${SyncService.NETWORK_RETRY_INTERVAL_MS}ms`
);
await sleep(SyncService.NETWORK_RETRY_INTERVAL_MS);
} }
} }
} }