Skip to content

Commit

Permalink
chore: add gateway connection params in dev env and hardcode env vars…
Browse files Browse the repository at this point in the history
… in production
  • Loading branch information
geclos committed Sep 24, 2024
1 parent acc4632 commit 95c287e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
8 changes: 7 additions & 1 deletion apps/web/src/app/(private)/_lib/createSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { compactObject } from '@latitude-data/core/lib/compactObject'
import { Result } from '@latitude-data/core/lib/Result'
import { LatitudeApiKeysRepository } from '@latitude-data/core/repositories'
import { LatitudeSdk } from '@latitude-data/sdk-js'
import env from '$/env'
import { getCurrentUser } from '$/services/auth/getCurrentUser'

// NOTE: this would be a great candidate for a cache function with redis
Expand All @@ -21,7 +22,12 @@ export async function createSdk(projectId?: number) {

const latitudeApiKey = result.value.token

const gateway = {
host: env.GATEWAY_HOSTNAME,
port: env.GATEWAY_PORT,
ssl: env.GATEWAY_SSL,
}
return Result.ok(
new LatitudeSdk(latitudeApiKey, compactObject({ projectId })),
new LatitudeSdk(latitudeApiKey, compactObject({ gateway, projectId })),
)
}
3 changes: 2 additions & 1 deletion packages/sdks/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "LGPL-3.0",
"scripts": {
"dev": "rollup -w -c ./rollup.config.mjs",
"build": "rollup -c ./rollup.config.mjs",
"build": "NODE_ENV=production rollup -c ./rollup.config.mjs",
"test": "vitest run --pool=forks",
"test:watch": "vitest",
"tc": "tsc --noEmit",
Expand All @@ -29,6 +29,7 @@
"@latitude-data/eslint-config": "workspace:*",
"@latitude-data/typescript-config": "workspace:^",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-replace": "^6.0.1",
"@rollup/plugin-typescript": "^11.1.6",
"@types/eventsource": "^1.1.15",
"msw": "^2.3.5",
Expand Down
5 changes: 5 additions & 0 deletions packages/sdks/typescript/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as url from 'url'
import alias from '@rollup/plugin-alias'
import typescript from '@rollup/plugin-typescript'
import { dts } from 'rollup-plugin-dts'
import replace from '@rollup/plugin-replace'

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const aliasEntries = {
Expand All @@ -28,6 +29,10 @@ const config = [
typescript({
exclude: ['**/__tests__', '**/*.test.ts'],
}),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
preventAssignment: true,
})
],
external: EXTERNALS,
},
Expand Down
16 changes: 13 additions & 3 deletions packages/sdks/typescript/src/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { createEnv } from '@t3-oss/env-core'
import { z } from 'zod'

let GATEWAY_HOSTNAME, GATEWAY_PORT, GATEWAY_SSL
if (process.env.NODE_ENV === 'development') {
GATEWAY_HOSTNAME = 'localhost'
GATEWAY_PORT = '8787'
GATEWAY_SSL = 'false'
} else {
GATEWAY_HOSTNAME = 'gateway.latitude.so'
GATEWAY_SSL = 'true'
}

export default createEnv({
server: {
GATEWAY_HOSTNAME: z.string(),
Expand All @@ -10,8 +20,8 @@ export default createEnv({
.transform((value) => value === 'true'),
},
runtimeEnv: {
GATEWAY_HOSTNAME: process.env.GATEWAY_HOSTNAME ?? 'localhost',
GATEWAY_PORT: process.env.GATEWAY_PORT ?? '8787',
GATEWAY_SSL: process.env.GATEWAY_SSL ?? 'false',
GATEWAY_HOSTNAME,
GATEWAY_PORT,
GATEWAY_SSL,
},
})
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 95c287e

Please sign in to comment.