/**
* Fetches the specified custom fields from an outreach account
*
* @example
* window.lr_analytics.fetchOutreachAccountCustomFields({
* domain: 'logrocket.com',
* fields: [17, 18],
* });
*
* @param {Object} options
* @param {string} options.domain - The domain name of the account
* @param {number[]} options.fields - The custom field numbers to fetch
*
* @returns {Object} A javascript object with a property for every custom field
* that was requested. The properties are named `custom{field_number}`. So if
* you requested `fields: [17, 18]`, you would get a object that looks like
* this:
*
* ```json
* {
* "custom17": "Whatever custom17 is set to",
* "custom18": "Whatever custom18 is set to"
* }
* ```
*/
export async function fetchOutreachAccountCustomFields({ domain, fields }) {
if (!domain) {
throw new Error("`options.domain` is required");
}
if (!Array.isArray(fields)) {
throw new Error("`options.fields` is not an array");
}
const custom = fields.filter((i) => Number.isInteger(i)).join(",");
const customFieldURL = `https://us-central1-lr-marketing-and-sales-ops.cloudfunctions.net/getOutreachAccountCustomField?domain=${domain}&custom=${custom}`;
const res = await fetch(customFieldURL);
const resText = await res.text();
if (!res.ok) {
throw new Error(resText);
}
return JSON.parse(resText);
}