Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Config Flag to Disable fetch cache Option #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 4 additions & 2 deletions packages/js/src/fetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import fetchRetry from 'fetch-retry';
import { parseLimitFromResponse, Limit, LimitType } from './limit.js';

export class FetchClient {
constructor(public config: { headers: HeadersInit; baseUrl: string; timeout: number }) {}
constructor(
public config: { headers: HeadersInit; baseUrl: string; timeout: number; disableFetchCashNoStore: boolean },
) {}

async doReq<T>(
endpoint: string,
Expand All @@ -29,7 +31,7 @@ export class FetchClient {
method,
body: init.body ? init.body : undefined,
signal: AbortSignal.timeout(timeout),
cache: 'no-cache',
...(this.config.disableFetchCacheNoStore ? {} : { cache: 'no-cache' }),
});

if (resp.status === 204) {
Expand Down
9 changes: 8 additions & 1 deletion packages/js/src/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ export interface ClientOptions {
* need to change this unless you are using a self-hosted version of Axiom.
*/
url?: string;
/**
* Disables the cache for the request, defaults to false.
* Fixes Cloudflare compatibility issue:
* https://developers.cloudflare.com/workers/configuration/compatibility-flags/#enable-cache-no-store-http-standard-api
*/
disableFetchCacheNoStore?: boolean;
onError?: (error: Error) => void;
}

export default abstract class HTTPClient {
protected readonly client: FetchClient;

constructor({ orgId = '', token, url }: ClientOptions) {
constructor({ orgId = '', token, url, disableFetchCacheNoStore }: ClientOptions) {
if (!token) {
console.warn('Missing Axiom token');
}
Expand All @@ -60,6 +66,7 @@ export default abstract class HTTPClient {
baseUrl,
headers,
timeout: 20_000,
disableFetchCacheNoStore,
});
}
}
Loading