Refactor WooCommerce API to lazy initialization for build compatibility
This commit is contained in:
@@ -1,11 +1,27 @@
|
||||
import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";
|
||||
|
||||
const api = new WooCommerceRestApi({
|
||||
url: process.env.NEXT_PUBLIC_WOOCOMMERCE_URL || "",
|
||||
consumerKey: process.env.NEXT_PUBLIC_WOOCOMMERCE_CONSUMER_KEY || "",
|
||||
consumerSecret: process.env.NEXT_PUBLIC_WOOCOMMERCE_CONSUMER_SECRET || "",
|
||||
version: "wc/v3",
|
||||
});
|
||||
// Lazy initialization - only create API client when needed
|
||||
let apiInstance: WooCommerceRestApi | null = null;
|
||||
|
||||
function getApi(): WooCommerceRestApi {
|
||||
if (!apiInstance) {
|
||||
const url = process.env.NEXT_PUBLIC_WOOCOMMERCE_URL;
|
||||
const consumerKey = process.env.NEXT_PUBLIC_WOOCOMMERCE_CONSUMER_KEY;
|
||||
const consumerSecret = process.env.NEXT_PUBLIC_WOOCOMMERCE_CONSUMER_SECRET;
|
||||
|
||||
if (!url || !consumerKey || !consumerSecret) {
|
||||
throw new Error("WooCommerce API credentials not configured");
|
||||
}
|
||||
|
||||
apiInstance = new WooCommerceRestApi({
|
||||
url,
|
||||
consumerKey,
|
||||
consumerSecret,
|
||||
version: "wc/v3",
|
||||
});
|
||||
}
|
||||
return apiInstance;
|
||||
}
|
||||
|
||||
export interface WooProduct {
|
||||
id: number;
|
||||
@@ -34,6 +50,7 @@ export interface WooCategory {
|
||||
|
||||
export async function getProducts(perPage = 100): Promise<WooProduct[]> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("products", { per_page: perPage });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
@@ -44,6 +61,7 @@ export async function getProducts(perPage = 100): Promise<WooProduct[]> {
|
||||
|
||||
export async function getProduct(id: number): Promise<WooProduct | null> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get(`products/${id}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
@@ -54,6 +72,7 @@ export async function getProduct(id: number): Promise<WooProduct | null> {
|
||||
|
||||
export async function getProductBySlug(slug: string): Promise<WooProduct | null> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("products", { slug });
|
||||
return response.data[0] || null;
|
||||
} catch (error) {
|
||||
@@ -64,6 +83,7 @@ export async function getProductBySlug(slug: string): Promise<WooProduct | null>
|
||||
|
||||
export async function getCategories(): Promise<WooCategory[]> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("product-categories", { per_page: 100 });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
@@ -76,6 +96,7 @@ export async function getProductsByCategory(
|
||||
categoryId: number
|
||||
): Promise<WooProduct[]> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("products", {
|
||||
category: categoryId,
|
||||
per_page: 100,
|
||||
@@ -104,4 +125,4 @@ export function getProductImage(product: WooProduct): string {
|
||||
return "/placeholder-product.jpg";
|
||||
}
|
||||
|
||||
export default api;
|
||||
export default getApi;
|
||||
|
||||
Reference in New Issue
Block a user