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

Fix Websockets protocol variable #229

Merged
merged 6 commits into from
Apr 17, 2024
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
7 changes: 7 additions & 0 deletions .changeset/little-hotels-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@quiltt/core": minor
"@quiltt/react": minor
"@quiltt/react-native": minor
---

Fix Websockets protocol variable
7 changes: 7 additions & 0 deletions .changeset/purple-buckets-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@quiltt/core": minor
"@quiltt/react": minor
"@quiltt/react-native": minor
---

Fix Websockets protocol variable
52 changes: 33 additions & 19 deletions packages/core/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
import { name as packageName, version as packageVersion } from '../package.json'

const QUILTT_API_INSECURE = (() => {
/**
* 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 => {
try {
return process.env.QUILTT_API_INSECURE
} catch {
return undefined
}
})()
let value: string | undefined

const QUILTT_API_DOMAIN = (() => {
try {
return process.env.QUILTT_API_DOMAIN
} catch {
// 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
}
})()
}

const QUILTT_DEBUG = (() => {
try {
return !!process.env.QUILTT_DEBUG || process.env.NODE_ENV !== 'production'
} catch {
return false
}
})()
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', process?.env?.NODE_ENV !== 'production')

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}`
Expand Down
22 changes: 22 additions & 0 deletions packages/core/tests/configuration/constants.test.ts
Original file line number Diff line number Diff line change
@@ -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
})
})
53 changes: 53 additions & 0 deletions packages/core/tests/configuration/get-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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')
})
})
33 changes: 33 additions & 0 deletions packages/core/tests/configuration/url-http.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
33 changes: 33 additions & 0 deletions packages/core/tests/configuration/url-https.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
Loading