stores.ts•5.04 kB
import { z } from "zod";
// Store configuration schema
export const StoreConfigSchema = z.object({
id: z.enum(["id1", "id2", "id3"]),
name: z.string(),
description: z.string(),
domain: z.string(),
storefrontAccessToken: z.string(),
adminApiKey: z.string().optional(),
adminAccessToken: z.string().optional(),
apiVersion: z.string().default("2024-01"),
enabled: z.boolean().default(true),
settings: z.object({
currency: z.string().default("BRL"),
locale: z.string().default("pt-BR"),
timezone: z.string().default("America/Sao_Paulo"),
maxProductsPerPage: z.number().default(100),
enableCache: z.boolean().default(true),
cacheTimeout: z.number().default(300), // 5 minutes
}).optional(),
});
export type StoreConfig = z.infer<typeof StoreConfigSchema>;
// Store configuration mapping
export const STORE_CONFIGS: Record<string, StoreConfig> = {
id1: {
id: "id1",
name: "The Perfume Shop",
description: "Premium perfumes and fragrances",
domain: process.env.SHOPIFY_STORE_1_DOMAIN || "",
storefrontAccessToken: process.env.SHOPIFY_STORE_1_STOREFRONT_TOKEN || "",
adminApiKey: process.env.SHOPIFY_STORE_1_ADMIN_API_KEY,
adminAccessToken: process.env.SHOPIFY_STORE_1_ADMIN_TOKEN,
apiVersion: process.env.SHOPIFY_API_VERSION || "2024-01",
enabled: process.env.SHOPIFY_STORE_1_ENABLED !== "false",
settings: {
currency: "BRL",
locale: "pt-BR",
timezone: "America/Sao_Paulo",
maxProductsPerPage: 100,
enableCache: true,
cacheTimeout: 300,
},
},
id2: {
id: "id2",
name: "Perfumes Club",
description: "Exclusive perfume collections",
domain: process.env.SHOPIFY_STORE_2_DOMAIN || "",
storefrontAccessToken: process.env.SHOPIFY_STORE_2_STOREFRONT_TOKEN || "",
adminApiKey: process.env.SHOPIFY_STORE_2_ADMIN_API_KEY,
adminAccessToken: process.env.SHOPIFY_STORE_2_ADMIN_TOKEN,
apiVersion: process.env.SHOPIFY_API_VERSION || "2024-01",
enabled: process.env.SHOPIFY_STORE_2_ENABLED !== "false",
settings: {
currency: "BRL",
locale: "pt-BR",
timezone: "America/Sao_Paulo",
maxProductsPerPage: 100,
enableCache: true,
cacheTimeout: 300,
},
},
id3: {
id: "id3",
name: "Perfumes & Co",
description: "Luxury fragrances and cosmetics",
domain: process.env.SHOPIFY_STORE_3_DOMAIN || "",
storefrontAccessToken: process.env.SHOPIFY_STORE_3_STOREFRONT_TOKEN || "",
adminApiKey: process.env.SHOPIFY_STORE_3_ADMIN_API_KEY,
adminAccessToken: process.env.SHOPIFY_STORE_3_ADMIN_TOKEN,
apiVersion: process.env.SHOPIFY_API_VERSION || "2024-01",
enabled: process.env.SHOPIFY_STORE_3_ENABLED !== "false",
settings: {
currency: "BRL",
locale: "pt-BR",
timezone: "America/Sao_Paulo",
maxProductsPerPage: 100,
enableCache: true,
cacheTimeout: 300,
},
},
};
// Validation and utility functions
export function validateStoreConfig(config: unknown): StoreConfig {
return StoreConfigSchema.parse(config);
}
export function getStoreConfig(storeId: string): StoreConfig | null {
const config = STORE_CONFIGS[storeId];
if (!config) {
return null;
}
try {
return validateStoreConfig(config);
} catch (error) {
console.error(`Invalid configuration for store ${storeId}:`, error);
return null;
}
}
export function getAllStoreConfigs(): StoreConfig[] {
return Object.values(STORE_CONFIGS)
.map(config => {
try {
return validateStoreConfig(config);
} catch (error) {
console.error(`Invalid configuration for store ${config.id}:`, error);
return null;
}
})
.filter((config): config is StoreConfig => config !== null);
}
export function getEnabledStoreConfigs(): StoreConfig[] {
return getAllStoreConfigs().filter(config => config.enabled);
}
export function isStoreEnabled(storeId: string): boolean {
const config = getStoreConfig(storeId);
return config?.enabled ?? false;
}
export function getStoreNames(): Record<string, string> {
const configs = getAllStoreConfigs();
return configs.reduce((acc, config) => {
acc[config.id] = config.name;
return acc;
}, {} as Record<string, string>);
}
// Configuration validation on module load
export function validateAllConfigurations(): {
valid: StoreConfig[];
invalid: Array<{ storeId: string; error: string }>;
} {
const valid: StoreConfig[] = [];
const invalid: Array<{ storeId: string; error: string }> = [];
Object.entries(STORE_CONFIGS).forEach(([storeId, config]) => {
try {
const validatedConfig = validateStoreConfig(config);
// Additional validation for required fields
if (!validatedConfig.domain) {
throw new Error("Domain is required");
}
if (!validatedConfig.storefrontAccessToken) {
throw new Error("Storefront access token is required");
}
valid.push(validatedConfig);
} catch (error) {
invalid.push({
storeId,
error: error instanceof Error ? error.message : "Unknown error",
});
}
});
return { valid, invalid };
}
// Export store IDs for type safety
export const STORE_IDS = ["id1", "id2", "id3"] as const;
export type StoreId = typeof STORE_IDS[number];