diff --git a/pages/index.js b/pages/index.js index 383e240..dad5f25 100644 --- a/pages/index.js +++ b/pages/index.js @@ -10,6 +10,7 @@ import { GET_PRODUCTS_ENDPOINT, HEADER_FOOTER_ENDPOINT } from '../src/utils/cons * External Dependencies. */ import axios from 'axios'; +import { getProductsData } from '../src/utils/products'; export default function Home({ headerFooter, products }) { @@ -30,12 +31,12 @@ export default function Home({ headerFooter, products }) { export async function getStaticProps() { const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT ); - const { data: productsData } = await axios.get( GET_PRODUCTS_ENDPOINT ); + const { data: products } = await getProductsData(); return { props: { headerFooter: headerFooterData?.data ?? {}, - products: productsData?.products ?? {} + products: products ?? {} }, /** diff --git a/src/utils/products.js b/src/utils/products.js new file mode 100644 index 0000000..3532490 --- /dev/null +++ b/src/utils/products.js @@ -0,0 +1,22 @@ +const WooCommerceRestApi = require( '@woocommerce/woocommerce-rest-api' ).default; + +const api = new WooCommerceRestApi( { + url: process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL, + consumerKey: process.env.WC_CONSUMER_KEY, + consumerSecret: process.env.WC_CONSUMER_SECRET, + version: 'wc/v3', +} ); + +/** + * Get Products. + * + * @return {Promise} + */ +export const getProductsData = async ( perPage ) => { + return await api.get( + 'products', + { + per_page: perPage || 50, + }, + ); +};