Allow overriding fetch implementation

This commit is contained in:
Andras Schmelczer 2025-02-25 22:52:47 +00:00
parent 6999276af4
commit 9cebf53707
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 17 additions and 6 deletions

View file

@ -17,12 +17,13 @@ export interface CheckConnectionResult {
export class SyncService {
private client!: Client<paths>;
private clientWithoutRetries!: Client<paths>;
private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch;
public constructor(
private readonly settings: Settings,
private readonly logger: Logger
) {
this.createClient(settings.getSettings().remoteUri);
this.createClient(this.settings.getSettings().remoteUri);
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
if (newSettings.remoteUri === oldSettings.remoteUri) {
@ -33,6 +34,11 @@ export class SyncService {
});
}
public set fetchImplementation(fetch: typeof globalThis.fetch) {
this._fetchImplementation = fetch;
this.createClient(this.settings.getSettings().remoteUri);
}
private static formatError(
error: components["schemas"]["SerializedError"]
): string {
@ -289,7 +295,7 @@ export class SyncService {
private createClient(remoteUri: string): void {
this.client = createClient<paths>({
baseUrl: remoteUri,
fetch: retriedFetchFactory(this.logger)
fetch: retriedFetchFactory(this.logger, this._fetchImplementation)
});
this.clientWithoutRetries = createClient<paths>({