🧑💻 Improve env variables type safety and management (#718)
Closes #679
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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`)
|
||||
|
||||
@@ -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])
|
||||
)
|
||||
}
|
||||
|
||||
33
packages/lib/api/pricing.ts
Normal file
33
packages/lib/api/pricing.ts
Normal 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,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user