From 059c7a9bcb57a94a54b4456041663b145b1d8300 Mon Sep 17 00:00:00 2001 From: novakzaballa Date: Fri, 18 Aug 2023 16:51:56 -0400 Subject: [PATCH 1/2] fix: Default requestTimeout --- sdk/index.ts | 5 ++--- tests/sdk/flagsmith.test.ts | 11 ++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sdk/index.ts b/sdk/index.ts index 3b4c1b7..72d097c 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 ? 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..2bc2dd1 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('default request timeout in seconds', 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'))); From 9286ac506a9f51daf83b35c9074a329cf6152679 Mon Sep 17 00:00:00 2001 From: novakzaballa Date: Mon, 21 Aug 2023 09:34:35 -0400 Subject: [PATCH 2/2] More descriptive test name and readability optimization --- sdk/index.ts | 2 +- tests/sdk/flagsmith.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/index.ts b/sdk/index.ts index 72d097c..f52b425 100644 --- a/sdk/index.ts +++ b/sdk/index.ts @@ -87,7 +87,7 @@ export class Flagsmith { this.environmentKey = data.environmentKey; this.apiUrl = data.apiUrl || this.apiUrl; this.customHeaders = data.customHeaders; - this.requestTimeoutMs = 1000 * (data.requestTimeoutSeconds ? data.requestTimeoutSeconds : DEFAULT_REQUEST_TIMEOUT_SECONDS); + 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 2bc2dd1..525a659 100644 --- a/tests/sdk/flagsmith.test.ts +++ b/tests/sdk/flagsmith.test.ts @@ -193,7 +193,7 @@ test('default flag handler used when timeout occurs', async () => { expect(flag.value).toBe(defaultFlag.value); }) -test('default request timeout in seconds', async () => { +test('request timeout uses default if not provided', async () => { const flg = new Flagsmith({ environmentKey: 'key',