🐛 Improve parse runtime env reading function
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/js",
|
"name": "@typebot.io/js",
|
||||||
"version": "0.1.18",
|
"version": "0.1.19",
|
||||||
"description": "Javascript library to display typebots on your website",
|
"description": "Javascript library to display typebots on your website",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ import type { ScriptToExecute } from '@typebot.io/schemas'
|
|||||||
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
|
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
|
||||||
|
|
||||||
export const executeScript = async ({ content, args }: ScriptToExecute) => {
|
export const executeScript = async ({ content, args }: ScriptToExecute) => {
|
||||||
const func = AsyncFunction(
|
|
||||||
...args.map((arg) => arg.id),
|
|
||||||
parseContent(content)
|
|
||||||
)
|
|
||||||
try {
|
try {
|
||||||
|
const func = AsyncFunction(
|
||||||
|
...args.map((arg) => arg.id),
|
||||||
|
parseContent(content)
|
||||||
|
)
|
||||||
await func(...args.map((arg) => arg.value))
|
await func(...args.map((arg) => arg.value))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { env } from '@typebot.io/env'
|
import { getRuntimeVariable } from '@typebot.io/env/getRuntimeVariable'
|
||||||
|
|
||||||
const cloudViewerUrl = 'https://viewer.typebot.io'
|
const cloudViewerUrl = 'https://viewer.typebot.io'
|
||||||
|
|
||||||
export const guessApiHost = () =>
|
export const guessApiHost = () =>
|
||||||
env.NEXT_PUBLIC_VIEWER_INTERNAL_URL ??
|
getRuntimeVariable('NEXT_PUBLIC_VIEWER_INTERNAL_URL') ??
|
||||||
env.NEXT_PUBLIC_VIEWER_URL[0] ??
|
getRuntimeVariable('NEXT_PUBLIC_VIEWER_URL')?.split(',')[0] ??
|
||||||
cloudViewerUrl
|
cloudViewerUrl
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/nextjs",
|
"name": "@typebot.io/nextjs",
|
||||||
"version": "0.1.18",
|
"version": "0.1.19",
|
||||||
"description": "Convenient library to display typebots on your Next.js website",
|
"description": "Convenient library to display typebots on your Next.js website",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/react",
|
"name": "@typebot.io/react",
|
||||||
"version": "0.1.18",
|
"version": "0.1.19",
|
||||||
"description": "Convenient library to display typebots on your React app",
|
"description": "Convenient library to display typebots on your React app",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
10
packages/env/env.ts
vendored
10
packages/env/env.ts
vendored
@@ -1,14 +1,6 @@
|
|||||||
import { createEnv } from '@t3-oss/env-nextjs'
|
import { createEnv } from '@t3-oss/env-nextjs'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { getRuntimeVariable } from './getRuntimeVariable'
|
||||||
declare const window: {
|
|
||||||
__ENV: any
|
|
||||||
}
|
|
||||||
|
|
||||||
const getRuntimeVariable = (key: string) => {
|
|
||||||
if (typeof window === 'undefined') return process.env[key]
|
|
||||||
return window.__ENV[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
const boolean = z.enum(['true', 'false']).transform((value) => value === 'true')
|
const boolean = z.enum(['true', 'false']).transform((value) => value === 'true')
|
||||||
|
|
||||||
|
|||||||
10
packages/env/getRuntimeVariable.ts
vendored
Normal file
10
packages/env/getRuntimeVariable.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
declare const window: {
|
||||||
|
__ENV?: any
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBrowser = () => Boolean(typeof window !== 'undefined' && window.__ENV)
|
||||||
|
|
||||||
|
export const getRuntimeVariable = (key: string) => {
|
||||||
|
if (isBrowser()) return window.__ENV[key]
|
||||||
|
return process.env[key]
|
||||||
|
}
|
||||||
2
packages/env/package.json
vendored
2
packages/env/package.json
vendored
@@ -15,6 +15,6 @@
|
|||||||
"esbuild": "^0.19.1"
|
"esbuild": "^0.19.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "esbuild env.ts --packages=external --outfile=dist/env.mjs"
|
"build": "esbuild env.ts --bundle --packages=external --format=esm --outfile=dist/env.mjs"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user