zuora to eloqua | zuora export service | zuora export service test | Search

This code fetches a complete product catalog from the Zuora API, handling pagination to retrieve all products across multiple pages.

Run example

npm run import -- "zuora export catalog"

zuora export catalog


function getCatalog(zuoraConfig, next) {
    var catalog = [];
    return request.request({
        followAllRedirects: true,
        uri: zuoraConfig.rest_api_url + (typeof next !== 'undefined' ? next.replace(/\/v1/ig, '') : '/catalog/products'),
        method: 'GET',
        headers: getAuthHeaders(zuoraConfig)
    }).then(r => {
        catalog = catalog.concat(r.body.products);
        if(r.body.nextPage) {
            return getCatalog(zuoraConfig, r.body.nextPage).then(r => catalog.concat(r));
        }
        return catalog;
    })
}
module.exports = getCatalog;

What the code could have been:

/**
 * Retrieves a catalog of products from Zuora.
 * 
 * @param {Object} zuoraConfig - Configuration object containing Zuora API settings.
 * @param {string} zuoraConfig.rest_api_url - Base URL of the Zuora REST API.
 * @param {string} zuoraConfig.client_id - Client ID for authentication.
 * @param {string} zuoraConfig.client_secret - Client secret for authentication.
 * @param {string} [nextPage] - Optional next page URL for pagination.
 * @returns {Promise>} A promise resolving to an array of products.
 */
function getCatalog(zuoraConfig, nextPage) {
    // Initialize an empty array to store the catalog of products
    const catalog = [];

    // Define the API request options
    const options = {
        followAllRedirects: true,
        uri: zuoraConfig.rest_api_url + (nextPage? nextPage.replace(/\/v1/ig, '') : '/catalog/products'),
        method: 'GET',
        headers: getAuthHeaders(zuoraConfig)
    };

    // Send the API request and process the response
    return request.request(options)
       .then(response => {
            // Get the products from the response body
            const products = response.body.products;

            // Add the products to the catalog
            catalog.push(...products);

            // Check if there is a next page and recursively fetch it
            if (response.body.nextPage) {
                // Use Promise.resolve to handle the case where nextPage is falsy
                return Promise.resolve(getCatalog(zuoraConfig, response.body.nextPage))
                   .then(results => catalog.push(...results));
            }

            // Return the catalog when there are no more pages
            return catalog;
        })
       .catch(error => {
            // Handle any errors that occur during the request
            // TODO: Implement error handling and logging
            globalThis.console.error('Error fetching catalog:', error);
            return [];
        });
}

// Export the function
module.exports = getCatalog;

This code defines a function getCatalog that fetches a complete product catalog from the Zuora API, handling pagination.

Here's a breakdown:

  1. Initialization:

  2. API Request:

  3. Response Handling:

  4. Export:

In essence, this code efficiently retrieves a complete product catalog from the Zuora API by handling pagination and returning a consolidated array of products.