diff --git a/sdk/index.ts b/sdk/index.ts index 3b4c1b7..f52b425 100644 --- a/sdk/index.ts +++ b/sdk/index.ts @@ -24,13 +24,13 @@ export { EnvironmentDataPollingManager } from './polling_manager'; export { FlagsmithCache, FlagsmithConfig } from './types'; const DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/'; +const DEFAULT_REQUEST_TIMEOUT_SECONDS = 10; export class Flagsmith { environmentKey?: string; apiUrl: string = DEFAULT_API_URL; customHeaders?: { [key: string]: any }; - requestTimeoutSeconds?: number = 10; agent: RequestInit['agent']; requestTimeoutMs?: number; enableLocalEvaluation?: boolean = false; @@ -87,8 +87,7 @@ export class Flagsmith { this.environmentKey = data.environmentKey; this.apiUrl = data.apiUrl || this.apiUrl; this.customHeaders = data.customHeaders; - this.requestTimeoutSeconds = data.requestTimeoutSeconds; - this.requestTimeoutMs = data.requestTimeoutSeconds ? data.requestTimeoutSeconds * 1000 : undefined; + this.requestTimeoutMs = 1000 * (data.requestTimeoutSeconds ?? DEFAULT_REQUEST_TIMEOUT_SECONDS); this.enableLocalEvaluation = data.enableLocalEvaluation; this.environmentRefreshIntervalSeconds = data.environmentRefreshIntervalSeconds || this.environmentRefreshIntervalSeconds; diff --git a/tests/sdk/flagsmith.test.ts b/tests/sdk/flagsmith.test.ts index 7edbe56..525a659 100644 --- a/tests/sdk/flagsmith.test.ts +++ b/tests/sdk/flagsmith.test.ts @@ -183,7 +183,7 @@ test('default flag handler used when timeout occurs', async () => { const flg = new Flagsmith({ environmentKey: 'key', defaultFlagHandler: defaultFlagHandler, - requestTimeoutSeconds: 0.1, + requestTimeoutSeconds: 0.001, }); const flags = await flg.getEnvironmentFlags(); @@ -193,6 +193,15 @@ test('default flag handler used when timeout occurs', async () => { expect(flag.value).toBe(defaultFlag.value); }) +test('request timeout uses default if not provided', async () => { + + const flg = new Flagsmith({ + environmentKey: 'key', + }); + + expect(flg.requestTimeoutMs).toBe(10000); +}) + test('test_throws_when_no_identity_flags_returned_due_to_error', async () => { // @ts-ignore fetch.mockReturnValue(Promise.resolve(new Response('bad data')));