This code fetches a complete product catalog from the Zuora API, handling pagination to retrieve all products across multiple pages.
npm run import -- "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;
/**
* 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:
Initialization:
catalog
: An empty array to store the fetched products.API Request:
request.request
: Makes a GET request to the Zuora API endpoint for the catalog.
uri
: The base URL for the catalog, with an optional next
parameter for pagination.method
: 'GET' for retrieving data.headers
: Authorization headers obtained from getAuthHeaders(zuoraConfig)
.Response Handling:
r.body.products
: Extracts the list of products from the API response.catalog.concat(r.body.products)
: Appends the fetched products to the catalog
array.if (r.body.nextPage)
: Checks if there are more pages to fetch.
getCatalog
with the nextPage
URL and concatenates the results.return catalog
: Returns the complete catalog
array when all pages are fetched.Export:
module.exports = getCatalog
: Makes the getCatalog
function available for use in other parts of the application.In essence, this code efficiently retrieves a complete product catalog from the Zuora API by handling pagination and returning a consolidated array of products.