env handling improvement

This commit is contained in:
Dominic Ferrando
2025-09-22 09:46:04 -04:00
parent 4f395e9a7c
commit 3aef14a0fc
2 changed files with 61 additions and 42 deletions
+28 -16
View File
@@ -2,15 +2,6 @@ import { formatSlug, type DeepPartial } from "./util";
import type { Webflow } from "./webflow";
export namespace Printful {
export const API_URL = "https://api.printful.com";
const AUTH_TOKEN = typeof Bun === "undefined" ? "" : Bun.env.PRINTFUL_AUTH;
const STORE_ID = typeof Bun === "undefined" ? "" : Bun.env.PRINTFUL_STORE_ID;
const AUTH_HEADERS = {
"Authorization": `Bearer ${AUTH_TOKEN}`,
"X-PF-Store-Id": `${STORE_ID}`
};
export namespace Webhook {
// meta
interface MetaData {
@@ -144,13 +135,14 @@ export namespace Printful {
while (true) {
try {
const res = await fetch(`${Printful.API_URL}/store/products?offset=${offset}`, {
const res = await fetch(`${env().API_URL}/store/products?offset=${offset}`, {
method: "GET",
headers: { ...AUTH_HEADERS }
headers: { ...env().AUTH_HEADERS }
})
if (!res.ok) {
console.log(res.statusText);
console.log(await res.json());
throw new Error("Failed to get all Printful products",);
}
const payload: MetaDataMulti<SyncProduct> = await res.json();
@@ -170,9 +162,9 @@ export namespace Printful {
}
export async function get(printfulProductId: number): Promise<Product> {
const res = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, {
const res = await fetch(`${env().API_URL}/store/products/${printfulProductId}`, {
method: "GET",
headers: { ...AUTH_HEADERS }
headers: { ...env().AUTH_HEADERS }
});
if (!res.ok) {
@@ -185,11 +177,11 @@ export namespace Printful {
}
export async function update(printfulProductId: number, printfulProduct: DeepPartial<Product>) {
const res = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, {
const res = await fetch(`${env().API_URL}/store/products/${printfulProductId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
...AUTH_HEADERS
...env().AUTH_HEADERS
},
body: JSON.stringify(printfulProduct)
});
@@ -228,4 +220,24 @@ export namespace Printful {
return webflowVariants;
}
}
export const env = () => {
if (typeof Bun === "undefined") {
throw new Error("Must be in a server context. Make sure to run using --bun.");
}
const vars = {
API_URL: "https://api.printful.com",
AUTH_TOKEN: Bun.env.PRINTFUL_AUTH,
STORE_ID: Bun.env.PRINTFUL_STORE_ID
};
return {
...vars,
AUTH_HEADERS: {
"Authorization": `Bearer ${vars.AUTH_TOKEN}`,
"X-PF-Store-Id": `${vars.STORE_ID}`
}
};
};
}
+33 -26
View File
@@ -2,15 +2,6 @@ import { Printful } from "$lib/services/commerce/printful"
import { formatSlug, getProductImageUrls, type DeepPartial } from "./util";
export namespace Webflow {
const SITE_ID = typeof Bun === "undefined" ? "" : Bun.env.WEBFLOW_SITE_ID;
const COLLECTIONS_ID = typeof Bun === "undefined" ? "" : Bun.env.WEBFLOW_COLLECTION_ID;
const AUTH_TOKEN = typeof Bun === "undefined" ? "" : Bun.env.WEBFLOW_AUTH;
export const API_SITES_URL = `https://api.webflow.com/v2/sites/${SITE_ID}`;
export const API_COLLECTIONS_URL = `https://api.webflow.com/v2/collections/${COLLECTIONS_ID}`;
const AUTH_HEADER = { "Authorization": `bearer ${AUTH_TOKEN}` };
export namespace Products {
export namespace Skus {
export type Sku = {
@@ -35,11 +26,11 @@ export namespace Webflow {
}
export async function create(webflowProductId: string, skus: DeepPartial<Sku>[]) {
const res = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}/skus`, {
const res = await fetch(`${env().API_SITES_URL}/products/${webflowProductId}/skus`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...AUTH_HEADER
...env().AUTH_HEADER
},
body: JSON.stringify({
"skus": skus
@@ -53,11 +44,11 @@ export namespace Webflow {
}
export async function update(webflowProductId: string, webflowSkuId: string, webflowSku: DeepPartial<Sku>) {
const res = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}/skus/${webflowSkuId}`, {
const res = await fetch(`${env().API_SITES_URL}/products/${webflowProductId}/skus/${webflowSkuId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
...AUTH_HEADER
...env().AUTH_HEADER
},
body: JSON.stringify({
"sku": webflowSku
@@ -102,11 +93,11 @@ export namespace Webflow {
}
export async function create(webflowProductAndSku: DeepPartial<ProductAndSku>) {
const res = await fetch(`${Webflow.API_SITES_URL}/products`, {
const res = await fetch(`${env().API_SITES_URL}/products`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...AUTH_HEADER
...env().AUTH_HEADER
},
body: JSON.stringify(webflowProductAndSku)
});
@@ -184,10 +175,10 @@ export namespace Webflow {
}
export async function getAll(): Promise<ProductAndSkus[]> {
const res = await fetch(`${Webflow.API_SITES_URL}/products`, {
const res = await fetch(`${env().API_SITES_URL}/products`, {
method: "GET",
headers: {
...AUTH_HEADER,
...env().AUTH_HEADER,
}
});
@@ -201,10 +192,10 @@ export namespace Webflow {
}
export async function get(webflowProductId: string): Promise<ProductAndSkus | undefined> {
const res = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
const res = await fetch(`${env().API_SITES_URL}/products/${webflowProductId}`, {
method: "GET",
headers: {
...AUTH_HEADER,
...env().AUTH_HEADER,
}
});
@@ -222,11 +213,11 @@ export namespace Webflow {
}
export async function update(webflowProductId: string, webflowProduct: DeepPartial<ProductAndSku>) {
const res = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
const res = await fetch(`${env().API_SITES_URL}/products/${webflowProductId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
...AUTH_HEADER
...env().AUTH_HEADER
},
body: JSON.stringify(webflowProduct)
});
@@ -291,11 +282,11 @@ export namespace Webflow {
}
export async function remove(webflowProductId: string) {
const res = await fetch(`${Webflow.API_COLLECTIONS_URL}/items/${webflowProductId}`, {
const res = await fetch(`${env().API_COLLECTIONS_URL}/items/${webflowProductId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
...AUTH_HEADER
...env().AUTH_HEADER
},
body: JSON.stringify({})
});
@@ -308,16 +299,32 @@ export namespace Webflow {
export async function syncImages(webflowProductAndSkus: ProductAndSkus) {
const productSlug = webflowProductAndSkus.product.fieldData.slug;
console.log(productSlug);
const productImageUrls = await getProductImageUrls(productSlug);
console.log(productImageUrls);
for (const sku of webflowProductAndSkus.skus) {
sku.fieldData["main-image"] = productImageUrls[0] ?? sku.fieldData["main-image"];
sku.fieldData["more-images"] = productImageUrls.slice(1).map(url => ({ url }));
console.log(sku.fieldData["more-images"]);
await Webflow.Products.Skus.update(webflowProductAndSkus.product.id, sku.id, sku);
}
}
}
const env = () => {
if (typeof Bun === "undefined") {
throw new Error("Must be in a server context. Make sure to run using --bun.");
}
const vars = {
SITE_ID: Bun.env.WEBFLOW_SITE_ID,
COLLECTIONS_ID: Bun.env.WEBFLOW_COLLECTION_ID,
AUTH_TOKEN: Bun.env.WEBFLOW_AUTH,
};
return {
...vars,
API_SITES_URL: `https://api.webflow.com/v2/sites/${vars.SITE_ID}`,
API_COLLECTIONS_URL: `https://api.webflow.com/v2/collections/${vars.COLLECTIONS_ID}`,
AUTH_HEADER: { "Authorization": `bearer ${vars.AUTH_TOKEN}` }
};
};
}