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