2
0

🧑‍💻 Improve env variables type safety and management (#718)

Closes #679
This commit is contained in:
Baptiste Arnaud
2023-08-28 09:13:53 +02:00
committed by GitHub
parent a23a8c4456
commit 786e5cb582
148 changed files with 1550 additions and 1293 deletions

View File

@ -1,8 +1,9 @@
import { Credentials } from '@typebot.io/schemas/features/credentials'
import { decryptV1 } from './encryptionV1'
import { env } from '@typebot.io/env'
const algorithm = 'AES-GCM'
const secretKey = process.env.ENCRYPTION_SECRET
const secretKey = env.ENCRYPTION_SECRET
export const encrypt = async (
data: object

View File

@ -1,7 +1,8 @@
import { env } from '@typebot.io/env'
import { createDecipheriv } from 'crypto'
const algorithm = 'aes-256-gcm'
const secretKey = process.env.ENCRYPTION_SECRET
const secretKey = env.ENCRYPTION_SECRET
export const decryptV1 = (encryptedData: string, auth: string): object => {
if (!secretKey) throw new Error(`ENCRYPTION_SECRET is not in environment`)

View File

@ -1,3 +1,4 @@
import { env } from '@typebot.io/env'
import { Prisma, PrismaClient } from '@typebot.io/prisma'
import { InputBlockType, Typebot } from '@typebot.io/schemas'
import { Client } from 'minio'
@ -95,32 +96,26 @@ const deleteFilesFromBucket = async ({
}: {
urls: string[]
}): Promise<void> => {
if (
!process.env.S3_ENDPOINT ||
!process.env.S3_ACCESS_KEY ||
!process.env.S3_SECRET_KEY
)
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const useSSL =
process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true
const minioClient = new Client({
endPoint: process.env.S3_ENDPOINT,
port: process.env.S3_PORT ? parseInt(process.env.S3_PORT) : undefined,
useSSL,
accessKey: process.env.S3_ACCESS_KEY,
secretKey: process.env.S3_SECRET_KEY,
region: process.env.S3_REGION,
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const bucket = process.env.S3_BUCKET ?? 'typebot'
const bucket = env.S3_BUCKET ?? 'typebot'
return minioClient.removeObjects(
bucket,
urls
.filter((url) => url.includes(process.env.S3_ENDPOINT as string))
.filter((url) => url.includes(env.S3_ENDPOINT as string))
.map((url) => url.split(`/${bucket}/`)[1])
)
}

View File

@ -0,0 +1,33 @@
import { env } from '@typebot.io/env'
import { Plan } from '@typebot.io/prisma'
export const priceIds = {
[Plan.STARTER]: {
base: {
monthly: env.STRIPE_STARTER_MONTHLY_PRICE_ID,
yearly: env.STRIPE_STARTER_YEARLY_PRICE_ID,
},
chats: {
monthly: env.STRIPE_STARTER_CHATS_MONTHLY_PRICE_ID,
yearly: env.STRIPE_STARTER_CHATS_YEARLY_PRICE_ID,
},
storage: {
monthly: env.STRIPE_STARTER_STORAGE_MONTHLY_PRICE_ID,
yearly: env.STRIPE_STARTER_STORAGE_YEARLY_PRICE_ID,
},
},
[Plan.PRO]: {
base: {
monthly: env.STRIPE_PRO_MONTHLY_PRICE_ID,
yearly: env.STRIPE_PRO_YEARLY_PRICE_ID,
},
chats: {
monthly: env.STRIPE_PRO_CHATS_MONTHLY_PRICE_ID,
yearly: env.STRIPE_PRO_CHATS_YEARLY_PRICE_ID,
},
storage: {
monthly: env.STRIPE_PRO_STORAGE_MONTHLY_PRICE_ID,
yearly: env.STRIPE_PRO_STORAGE_YEARLY_PRICE_ID,
},
},
}

View File

@ -1,3 +1,4 @@
import { env } from '@typebot.io/env'
import { config, Endpoint, S3 } from 'aws-sdk'
type GeneratePresignedUrlProps = {
@ -14,34 +15,26 @@ export const generatePresignedUrl = ({
fileType,
sizeLimit = tenMB,
}: GeneratePresignedUrlProps): S3.PresignedPost => {
if (
!process.env.S3_ENDPOINT ||
!process.env.S3_ACCESS_KEY ||
!process.env.S3_SECRET_KEY
)
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const sslEnabled =
process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true
config.update({
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
region: process.env.S3_REGION,
sslEnabled,
accessKeyId: env.S3_ACCESS_KEY,
secretAccessKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
sslEnabled: env.S3_SSL,
})
const protocol = sslEnabled ? 'https' : 'http'
const protocol = env.S3_SSL ? 'https' : 'http'
const s3 = new S3({
endpoint: new Endpoint(
`${protocol}://${process.env.S3_ENDPOINT}${
process.env.S3_PORT ? `:${process.env.S3_PORT}` : ''
}`
`${protocol}://${env.S3_ENDPOINT}${env.S3_PORT ? `:${env.S3_PORT}` : ''}`
),
})
const presignedUrl = s3.createPresignedPost({
Bucket: process.env.S3_BUCKET ?? 'typebot',
Bucket: env.S3_BUCKET ?? 'typebot',
Fields: {
key: filePath,
'Content-Type': fileType,

View File

@ -13,10 +13,10 @@
"@typebot.io/tsconfig": "workspace:*",
"@types/nodemailer": "6.4.8",
"aws-sdk": "2.1415.0",
"dotenv": "16.3.1",
"next": "13.4.3",
"nodemailer": "6.9.3",
"typescript": "5.1.6"
"typescript": "5.1.6",
"@typebot.io/env": "workspace:*"
},
"peerDependencies": {
"aws-sdk": "2.1152.0",

View File

@ -1,24 +1,5 @@
import { PlaywrightTestConfig } from '@playwright/test'
import path from 'path'
import fs from 'fs'
const builderLocalEnvPath = path.join(
__dirname,
'../../../apps/builder/.env.local'
)
const localViewerEnvPath = path.join(
__dirname,
'../../../apps/viewer/.env.local'
)
if (fs.existsSync(builderLocalEnvPath))
require('dotenv').config({
path: builderLocalEnvPath,
})
if (fs.existsSync(localViewerEnvPath))
require('dotenv').config({
path: localViewerEnvPath,
})
export const playwrightBaseConfig: PlaywrightTestConfig = {
globalSetup: require.resolve(path.join(__dirname, 'globalSetup')),

View File

@ -3,37 +3,6 @@ import { Plan } from '@typebot.io/prisma'
const infinity = -1
export const priceIds = {
[Plan.STARTER]: {
base: {
monthly: process.env.STRIPE_STARTER_MONTHLY_PRICE_ID,
yearly: process.env.STRIPE_STARTER_YEARLY_PRICE_ID,
},
chats: {
monthly: process.env.STRIPE_STARTER_CHATS_MONTHLY_PRICE_ID,
yearly: process.env.STRIPE_STARTER_CHATS_YEARLY_PRICE_ID,
},
storage: {
monthly: process.env.STRIPE_STARTER_STORAGE_MONTHLY_PRICE_ID,
yearly: process.env.STRIPE_STARTER_STORAGE_YEARLY_PRICE_ID,
},
},
[Plan.PRO]: {
base: {
monthly: process.env.STRIPE_PRO_MONTHLY_PRICE_ID,
yearly: process.env.STRIPE_PRO_YEARLY_PRICE_ID,
},
chats: {
monthly: process.env.STRIPE_PRO_CHATS_MONTHLY_PRICE_ID,
yearly: process.env.STRIPE_PRO_CHATS_YEARLY_PRICE_ID,
},
storage: {
monthly: process.env.STRIPE_PRO_STORAGE_MONTHLY_PRICE_ID,
yearly: process.env.STRIPE_PRO_STORAGE_YEARLY_PRICE_ID,
},
},
}
export const prices = {
[Plan.STARTER]: 39,
[Plan.PRO]: 89,

View File

@ -1,18 +1,18 @@
import got from 'got'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { isEmpty, isNotEmpty } from '../utils'
import { isNotEmpty } from '../utils'
import { env } from '@typebot.io/env'
export const sendTelemetryEvents = async (events: TelemetryEvent[]) => {
if (events.length === 0) return { message: 'No events to send' }
if (isEmpty(process.env.TELEMETRY_WEBHOOK_URL))
return { message: 'Telemetry not enabled' }
if (!env.TELEMETRY_WEBHOOK_URL) return { message: 'Telemetry not enabled' }
try {
await got.post(process.env.TELEMETRY_WEBHOOK_URL, {
await got.post(env.TELEMETRY_WEBHOOK_URL, {
json: { events },
headers: {
authorization: isNotEmpty(process.env.TELEMETRY_WEBHOOK_BEARER_TOKEN)
? `Bearer ${process.env.TELEMETRY_WEBHOOK_BEARER_TOKEN}`
authorization: env.TELEMETRY_WEBHOOK_BEARER_TOKEN
? `Bearer ${env.TELEMETRY_WEBHOOK_BEARER_TOKEN}`
: undefined,
},
})

View File

@ -19,6 +19,7 @@ import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/enu
import { LogicBlockType } from '@typebot.io/schemas/features/blocks/logic/enums'
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/enums'
import { PictureChoiceBlock } from '@typebot.io/schemas/features/blocks/inputs/pictureChoice'
import { env } from '@typebot.io/env'
export const sendRequest = async <ResponseData>(
params:
@ -249,16 +250,6 @@ export const uploadFiles = async ({
declare const window: any
export const env = (key = ''): string | undefined => {
if (typeof window === 'undefined')
return isEmpty(process.env['NEXT_PUBLIC_' + key])
? undefined
: (process.env['NEXT_PUBLIC_' + key] as string)
if (typeof window !== 'undefined' && window.__env)
return isEmpty(window.__env[key]) ? undefined : window.__env[key]
}
export const hasValue = (
value: string | undefined | null
): value is NonNullable<string> =>
@ -268,10 +259,8 @@ export const hasValue = (
value !== 'undefined' &&
value !== 'null'
export const getViewerUrl = (props?: {
returnAll?: boolean
}): string | undefined =>
props?.returnAll ? env('VIEWER_URL') : env('VIEWER_URL')?.split(',')[0]
export const getViewerUrl = () =>
env.NEXT_PUBLIC_VIEWER_INTERNAL_URL ?? env.NEXT_PUBLIC_VIEWER_URL[0]
export const parseNumberWithCommas = (num: number) =>
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')