Improve environment variable handling

This commit is contained in:
Dominic Ferrando
2026-06-23 15:55:10 -04:00
parent 720aa61c66
commit 39d3e5ab52
3 changed files with 28 additions and 12 deletions
+14
View File
@@ -0,0 +1,14 @@
function requireEnv(key: string): string {
const val = Bun.env[key];
if (!val) throw new Error(`Missing required environment variable: ${key}`);
return val;
}
export const env = {
PRINTFUL_AUTH: requireEnv("PRINTFUL_AUTH"),
PRINTFUL_STORE_ID: requireEnv("PRINTFUL_STORE_ID"),
WEBFLOW_SITE_ID: requireEnv("WEBFLOW_SITE_ID"),
WEBFLOW_COLLECTION_ID: requireEnv("WEBFLOW_COLLECTION_ID"),
WEBFLOW_AUTH: requireEnv("WEBFLOW_AUTH"),
WEBFLOW_WEBHOOK_SECRET: requireEnv("WEBFLOW_WEBHOOK_SECRET")
};
+7 -6
View File
@@ -3,6 +3,7 @@ import {
ProductSyncer,
WebflowClient,
} from "@blade-and-brawn/commerce";
import { env } from "../env";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -26,15 +27,15 @@ const validateEquality = (wValue: any, pValue: any): Validation => {
};
const printful = new PrintfulClient({
token: Bun.env.PRINTFUL_AUTH!,
storeId: Bun.env.PRINTFUL_STORE_ID!,
token: env.PRINTFUL_AUTH,
storeId: env.PRINTFUL_STORE_ID,
});
const webflow = new WebflowClient({
siteId: Bun.env.WEBFLOW_SITE_ID!,
collectionsId: Bun.env.WEBFLOW_COLLECTION_ID!,
token: Bun.env.WEBFLOW_AUTH!,
webhookSecret: Bun.env.WEBFLOW_WEBHOOK_SECRET!,
siteId: env.WEBFLOW_SITE_ID,
collectionsId: env.WEBFLOW_COLLECTION_ID,
token: env.WEBFLOW_AUTH,
webhookSecret: env.WEBFLOW_WEBHOOK_SECRET,
});
const syncer = new ProductSyncer(printful, webflow);
+7 -6
View File
@@ -20,6 +20,7 @@ import {
} from "@blade-and-brawn/commerce";
import zipcodesUs from "zipcodes-us";
import z from "zod";
import { env } from "./env";
// SERVICES
// -----------
@@ -28,15 +29,15 @@ const levelCalculator = new LevelCalculator(
);
const printful = new PrintfulClient({
token: Bun.env.PRINTFUL_AUTH!,
storeId: Bun.env.PRINTFUL_STORE_ID!,
token: env.PRINTFUL_AUTH,
storeId: env.PRINTFUL_STORE_ID,
});
const webflow = new WebflowClient({
siteId: Bun.env.WEBFLOW_SITE_ID!,
collectionsId: Bun.env.WEBFLOW_COLLECTION_ID!,
token: Bun.env.WEBFLOW_AUTH!,
webhookSecret: Bun.env.WEBFLOW_WEBHOOK_SECRET!,
siteId: env.WEBFLOW_SITE_ID,
collectionsId: env.WEBFLOW_COLLECTION_ID,
token: env.WEBFLOW_AUTH,
webhookSecret: env.WEBFLOW_WEBHOOK_SECRET,
});
const productSyncer = new ProductSyncer(printful, webflow);