Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/client/src/relewise.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export interface RelewiseClientOptions {
}

export interface RelewiseRequestOptions {
/** Cancels the request when the provided abort signal is triggered. */
abortSignal?: AbortSignal;
/** Allows the request to outlive the page, such as when tracking events during page unload or navigation. */
keepalive?: boolean;
}

export class ProblemDetailsError extends Error {
Expand Down Expand Up @@ -68,6 +71,7 @@ export abstract class RelewiseClient {
},
body: JSON.stringify(data),
signal: options?.abortSignal,
keepalive: options?.keepalive,
cache: this.cache,
});

Expand Down Expand Up @@ -152,4 +156,4 @@ export abstract class RelewiseClient {
return undefined;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Tracker } from '../../src/tracker';

describe('RelewiseRequestOptions', () => {
const originalFetch = global.fetch;

afterEach(() => {
global.fetch = originalFetch;
jest.restoreAllMocks();
});

test('passes keepalive to fetch when tracking events', async () => {
const fetchMock = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockRejectedValue(new Error('No content')),
} as unknown as Response);

global.fetch = fetchMock;

const tracker = new Tracker('dataset-id', 'api-key');

await tracker.trackProductView({
productId: 'product-id',
user: {
temporaryId: 'temporary-id',
},
}, { keepalive: true });

expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
keepalive: true,
}));
});
});