From b636f1dde971da0ecb7f85f24aaf5ca98495236d Mon Sep 17 00:00:00 2001 From: Ruben Izmailyan Date: Tue, 16 Apr 2024 11:28:30 -0500 Subject: [PATCH 1/6] Fix Websockets protocol variable --- packages/core/src/configuration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/configuration.ts b/packages/core/src/configuration.ts index 772e727c..8473941b 100644 --- a/packages/core/src/configuration.ts +++ b/packages/core/src/configuration.ts @@ -26,7 +26,7 @@ const QUILTT_DEBUG = (() => { const domain = QUILTT_API_DOMAIN || 'quiltt.io' const protocolHttp = `http${QUILTT_API_INSECURE ? '' : 's'}` -const protocolWebsockets = `ws${QUILTT_API_DOMAIN ? '' : 's'}` +const protocolWebsockets = `ws${QUILTT_API_INSECURE ? '' : 's'}` export const debugging = QUILTT_DEBUG export const version = `${packageName}: v${packageVersion}` From a3bfa601a9e30147234e1de64a1c11857743c90f Mon Sep 17 00:00:00 2001 From: Zubair Aziz Date: Thu, 18 Apr 2024 01:33:58 +0800 Subject: [PATCH 2/6] Update 'core/src/configuration.ts' and update tests --- packages/core/src/configuration.ts | 60 ++++++++++++----- .../tests/configuration/constants.test.ts | 22 +++++++ .../core/tests/configuration/get-env.test.ts | 65 +++++++++++++++++++ .../core/tests/configuration/url-http.test.ts | 33 ++++++++++ .../tests/configuration/url-https.test.ts | 33 ++++++++++ 5 files changed, 195 insertions(+), 18 deletions(-) create mode 100644 packages/core/tests/configuration/constants.test.ts create mode 100644 packages/core/tests/configuration/get-env.test.ts create mode 100644 packages/core/tests/configuration/url-http.test.ts create mode 100644 packages/core/tests/configuration/url-https.test.ts diff --git a/packages/core/src/configuration.ts b/packages/core/src/configuration.ts index 8473941b..f32137c4 100644 --- a/packages/core/src/configuration.ts +++ b/packages/core/src/configuration.ts @@ -1,28 +1,52 @@ +/// + import { name as packageName, version as packageVersion } from '../package.json' -const QUILTT_API_INSECURE = (() => { - try { - return process.env.QUILTT_API_INSECURE - } catch { - return undefined +const getImportMetaEnv = (key: string): string | undefined => { + return import.meta.env[key] +} + +type EnvValue = string | number | boolean | null | undefined + +/** + * Retrieves the environment variable by key, with fallback and type conversion, + * supporting Node.js, Vite, and potentially other runtime environments. + */ +export const getEnv = (key: string, fallback: EnvValue = undefined): EnvValue => { + let value: string | undefined + + // Check if running under Node.js and use process.env + if (typeof process !== 'undefined' && process.env) { + value = process.env[key] + } + + // If running under Vite, use import.meta.env + if (!value) { + const viteKey = `VITE_${key}` + value = getImportMetaEnv(viteKey) + } + + // Return the value after type conversion if necessary or use fallback + if (value === undefined || value === null) { + return fallback } -})() -const QUILTT_API_DOMAIN = (() => { - try { - return process.env.QUILTT_API_DOMAIN - } catch { - return undefined + // Convert to boolean if the value is 'true' or 'false' + if (value === 'true' || value === 'false') { + return value === 'true' } -})() -const QUILTT_DEBUG = (() => { - try { - return !!process.env.QUILTT_DEBUG || process.env.NODE_ENV !== 'production' - } catch { - return false + // Convert to number if it's numeric + if (!isNaN(Number(value))) { + return Number(value) } -})() + + return value +} + +const QUILTT_API_INSECURE = getEnv('QUILTT_API_INSECURE', false) +const QUILTT_API_DOMAIN = getEnv('QUILTT_API_DOMAIN', 'quiltt.io') +const QUILTT_DEBUG = getEnv('QUILTT_DEBUG', import.meta.env.MODE !== 'production') const domain = QUILTT_API_DOMAIN || 'quiltt.io' const protocolHttp = `http${QUILTT_API_INSECURE ? '' : 's'}` diff --git a/packages/core/tests/configuration/constants.test.ts b/packages/core/tests/configuration/constants.test.ts new file mode 100644 index 00000000..d9aa5e04 --- /dev/null +++ b/packages/core/tests/configuration/constants.test.ts @@ -0,0 +1,22 @@ +import { describe, it, expect, vi } from 'vitest' +import { cdnBase, debugging, version } from '@/configuration' +import { name as packageName, version as packageVersion } from '../../package.json' + +describe('Configuration Constants', async () => { + it('should default to the correct domain if not provided', () => { + expect(cdnBase).toContain('quiltt.io') + }) + + it('should format the version correctly', () => { + expect(version).toBe(`${packageName}: v${packageVersion}`) + }) + + it('should reflect the correct debugging status based on environment', () => { + vi.stubGlobal('process', { + env: { + NODE_ENV: 'development', + }, + }) + expect(debugging).toBeTruthy() // Assuming the logic toggles based on NODE_ENV or similar + }) +}) diff --git a/packages/core/tests/configuration/get-env.test.ts b/packages/core/tests/configuration/get-env.test.ts new file mode 100644 index 00000000..43848b3d --- /dev/null +++ b/packages/core/tests/configuration/get-env.test.ts @@ -0,0 +1,65 @@ +import { describe, it, expect, vi } from 'vitest' +import { getEnv } from '@/configuration' + +describe('getEnv', () => { + it('should return the correct environment variable from process.env', () => { + vi.stubGlobal('process', { + env: { + TEST_VAR: '123', + }, + }) + + const result = getEnv('TEST_VAR') + expect(result).toBe(123) + }) + + it('should return the fallback value if the environment variable is not set', () => { + const result = getEnv('NON_EXISTENT_VAR', 'default') + expect(result).toBe('default') + }) + + it('should correctly convert boolean environment variables', () => { + vi.stubGlobal('process', { + env: { + BOOL_VAR: 'true', + }, + }) + + const result = getEnv('BOOL_VAR') + expect(result).toBe(true) + }) + + it('should correctly convert numeric environment variables', () => { + vi.stubGlobal('process', { + env: { + NUM_VAR: '42', + }, + }) + + const result = getEnv('NUM_VAR') + expect(result).toBe(42) + }) + + it('should return a non-numeric, non-boolean string as is', () => { + vi.stubGlobal('process', { + env: { + STRING_VAR: 'HelloWorld', + }, + }) + + const result = getEnv('STRING_VAR') + expect(result).toBe('HelloWorld') + }) + + // Example to test Vite's import.meta.env if needed + describe('getEnv with injected environment', () => { + it('should return the correct environment variable from an injected env source', () => { + const mockEnv = { + VITE_TEST_VAR: 'abc', + } + + const result = getEnv('TEST_VAR', mockEnv.VITE_TEST_VAR) + expect(result).toBe('abc') + }) + }) +}) diff --git a/packages/core/tests/configuration/url-http.test.ts b/packages/core/tests/configuration/url-http.test.ts new file mode 100644 index 00000000..8c300314 --- /dev/null +++ b/packages/core/tests/configuration/url-http.test.ts @@ -0,0 +1,33 @@ +// Tests are split by https/http to simplify the setup and avoid dynamic module reloading within each test. +import { describe, it, expect, beforeEach, vi } from 'vitest' + +let config: any + +beforeEach(async () => { + vi.stubGlobal('process', { + env: { QUILTT_API_INSECURE: 'true' }, + }) + config = await import('@/configuration') +}) + +describe('Configuration under HTTP', () => { + it('should use HTTP for CDN Base', () => { + expect(config.cdnBase).toBe('http://cdn.quiltt.io') + }) + + it('should use WS for Websockets', () => { + expect(config.endpointWebsockets).toBe('ws://api.quiltt.io/websockets') + }) + + // Additional tests specific to HTTP configuration +}) + +describe('Endpoint Accessibility under HTTP', () => { + it('should access auth endpoint over HTTP', () => { + expect(config.endpointAuth).toBe('http://auth.quiltt.io/v1/users/session') + }) + + it('should access GraphQL endpoint over HTTP', () => { + expect(config.endpointGraphQL).toBe('http://api.quiltt.io/v1/graphql') + }) +}) diff --git a/packages/core/tests/configuration/url-https.test.ts b/packages/core/tests/configuration/url-https.test.ts new file mode 100644 index 00000000..4fd51882 --- /dev/null +++ b/packages/core/tests/configuration/url-https.test.ts @@ -0,0 +1,33 @@ +// Tests are split by https/http to simplify the setup and avoid dynamic module reloading within each test. +import { describe, it, expect, beforeEach, vi } from 'vitest' + +let config: any + +beforeEach(async () => { + vi.stubGlobal('process', { + env: { QUILTT_API_INSECURE: 'false' }, + }) + config = await import('@/configuration') +}) + +describe('Configuration under HTTPS', () => { + it('should use HTTPS for CDN Base', () => { + expect(config.cdnBase).toBe('https://cdn.quiltt.io') + }) + + it('should use WSS for Websockets', () => { + expect(config.endpointWebsockets).toBe('wss://api.quiltt.io/websockets') + }) + + // Additional tests specific to HTTPS configuration +}) + +describe('Endpoint Accessibility under HTTPS', () => { + it('should access auth endpoint over HTTPS', () => { + expect(config.endpointAuth).toBe('https://auth.quiltt.io/v1/users/session') + }) + + it('should access GraphQL endpoint over HTTPS', () => { + expect(config.endpointGraphQL).toBe('https://api.quiltt.io/v1/graphql') + }) +}) From 94ed08a6fe6fe76de8b8de80e070197fcedc1afa Mon Sep 17 00:00:00 2001 From: Zubair Aziz Date: Thu, 18 Apr 2024 01:38:53 +0800 Subject: [PATCH 3/6] Fix type errors --- packages/core/src/configuration.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/src/configuration.ts b/packages/core/src/configuration.ts index f32137c4..9d746f8f 100644 --- a/packages/core/src/configuration.ts +++ b/packages/core/src/configuration.ts @@ -6,13 +6,11 @@ const getImportMetaEnv = (key: string): string | undefined => { return import.meta.env[key] } -type EnvValue = string | number | boolean | null | undefined - /** * Retrieves the environment variable by key, with fallback and type conversion, * supporting Node.js, Vite, and potentially other runtime environments. */ -export const getEnv = (key: string, fallback: EnvValue = undefined): EnvValue => { +export const getEnv = (key: string, fallback: any = undefined): any => { let value: string | undefined // Check if running under Node.js and use process.env From 3c31a596f6651c93b613c5665b1aeea094b7d5a9 Mon Sep 17 00:00:00 2001 From: Zubair Aziz Date: Thu, 18 Apr 2024 01:47:07 +0800 Subject: [PATCH 4/6] Remove vite support --- packages/core/src/configuration.ts | 62 ++++++++----------- .../core/tests/configuration/get-env.test.ts | 12 ---- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/packages/core/src/configuration.ts b/packages/core/src/configuration.ts index 9d746f8f..b0da1ae2 100644 --- a/packages/core/src/configuration.ts +++ b/packages/core/src/configuration.ts @@ -1,50 +1,42 @@ -/// - import { name as packageName, version as packageVersion } from '../package.json' -const getImportMetaEnv = (key: string): string | undefined => { - return import.meta.env[key] -} - /** * Retrieves the environment variable by key, with fallback and type conversion, * supporting Node.js, Vite, and potentially other runtime environments. */ export const getEnv = (key: string, fallback: any = undefined): any => { - let value: string | undefined - - // Check if running under Node.js and use process.env - if (typeof process !== 'undefined' && process.env) { - value = process.env[key] + try { + let value: string | undefined + + // Check if running under Node.js and use process.env + if (typeof process !== 'undefined' && process.env) { + value = process.env[key] + } + + // Return the value after type conversion if necessary or use fallback + if (value === undefined || value === null) { + return fallback + } + + // Convert to boolean if the value is 'true' or 'false' + if (value === 'true' || value === 'false') { + return value === 'true' + } + + // Convert to number if it's numeric + if (!isNaN(Number(value))) { + return Number(value) + } + + return value + } catch (error) { + return undefined } - - // If running under Vite, use import.meta.env - if (!value) { - const viteKey = `VITE_${key}` - value = getImportMetaEnv(viteKey) - } - - // Return the value after type conversion if necessary or use fallback - if (value === undefined || value === null) { - return fallback - } - - // Convert to boolean if the value is 'true' or 'false' - if (value === 'true' || value === 'false') { - return value === 'true' - } - - // Convert to number if it's numeric - if (!isNaN(Number(value))) { - return Number(value) - } - - return value } const QUILTT_API_INSECURE = getEnv('QUILTT_API_INSECURE', false) const QUILTT_API_DOMAIN = getEnv('QUILTT_API_DOMAIN', 'quiltt.io') -const QUILTT_DEBUG = getEnv('QUILTT_DEBUG', import.meta.env.MODE !== 'production') +const QUILTT_DEBUG = getEnv('QUILTT_DEBUG', process?.env?.NODE_ENV !== 'production') const domain = QUILTT_API_DOMAIN || 'quiltt.io' const protocolHttp = `http${QUILTT_API_INSECURE ? '' : 's'}` diff --git a/packages/core/tests/configuration/get-env.test.ts b/packages/core/tests/configuration/get-env.test.ts index 43848b3d..ca380985 100644 --- a/packages/core/tests/configuration/get-env.test.ts +++ b/packages/core/tests/configuration/get-env.test.ts @@ -50,16 +50,4 @@ describe('getEnv', () => { const result = getEnv('STRING_VAR') expect(result).toBe('HelloWorld') }) - - // Example to test Vite's import.meta.env if needed - describe('getEnv with injected environment', () => { - it('should return the correct environment variable from an injected env source', () => { - const mockEnv = { - VITE_TEST_VAR: 'abc', - } - - const result = getEnv('TEST_VAR', mockEnv.VITE_TEST_VAR) - expect(result).toBe('abc') - }) - }) }) From 1f2d770c0172aedd4ae953c2871ac9cbe5908cb6 Mon Sep 17 00:00:00 2001 From: Zubair Aziz Date: Thu, 18 Apr 2024 01:48:36 +0800 Subject: [PATCH 5/6] Add changeset --- .changeset/purple-buckets-clean.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/purple-buckets-clean.md diff --git a/.changeset/purple-buckets-clean.md b/.changeset/purple-buckets-clean.md new file mode 100644 index 00000000..c6928a11 --- /dev/null +++ b/.changeset/purple-buckets-clean.md @@ -0,0 +1,7 @@ +--- +"@quiltt/core": minor +"@quiltt/react": minor +"@quiltt/react-native": minor +--- + +Fix Websockets protocol variable From 97e61c055a3ed25228095f2012d3750adc29a38a Mon Sep 17 00:00:00 2001 From: Zubair Date: Thu, 18 Apr 2024 01:56:39 +0800 Subject: [PATCH 6/6] Create little-hotels-turn.md --- .changeset/little-hotels-turn.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/little-hotels-turn.md diff --git a/.changeset/little-hotels-turn.md b/.changeset/little-hotels-turn.md new file mode 100644 index 00000000..c6928a11 --- /dev/null +++ b/.changeset/little-hotels-turn.md @@ -0,0 +1,7 @@ +--- +"@quiltt/core": minor +"@quiltt/react": minor +"@quiltt/react-native": minor +--- + +Fix Websockets protocol variable