@arcteryx/js-cms
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -11,2 +11,3 @@ 'use strict'; | ||
*/ | ||
/* | ||
@@ -19,4 +20,2 @@ * Converts a simple object to query parameters | ||
*/ | ||
function toQueryParams(obj) { | ||
@@ -27,3 +26,2 @@ const url = Object.keys(obj).map(x => { | ||
} | ||
return `${encodeURIComponent(x)}=${encodeURIComponent(obj[x])}`; | ||
@@ -35,8 +33,8 @@ }).join("&"); | ||
/*eslint-disable*/ | ||
function flattenCMSJson(item, included) { | ||
// 2) Extracts it's "attributes" values into the return object | ||
if (typeof item !== "undefined") { | ||
const returnObject = item.attributes; // 3) Checks for a "relationships" attribute, if it does, add a memeber to the return object named the same as the relationship key | ||
const returnObject = item.attributes; | ||
// 3) Checks for a "relationships" attribute, if it does, add a memeber to the return object named the same as the relationship key | ||
if (item.relationships) { | ||
@@ -49,6 +47,6 @@ for (const key in item.relationships) { | ||
} | ||
returnObject[key] = item.relationships[key].data.map(related => { | ||
// 6) call getAttributes on the real entity object, put the results into the new member on the return object (from 3) | ||
return flattenCMSJson( // 5) get the real entity object from the "included" array | ||
return flattenCMSJson( | ||
// 5) get the real entity object from the "included" array | ||
included.filter(include => (related === null || related === void 0 ? void 0 : related.id) === include.id)[0], included); | ||
@@ -58,3 +56,2 @@ }); | ||
} | ||
return returnObject; | ||
@@ -67,3 +64,2 @@ } | ||
const options = {}; | ||
if (previewDraft) { | ||
@@ -75,3 +71,2 @@ options.resourceVersion = "rel:working-copy"; | ||
} | ||
if (country !== null) { | ||
@@ -83,3 +78,2 @@ options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} | ||
options["fields[node--category_intro]"] = "title,field_cat_intro_category_handle,field_cat_intro_background_image,field_simple_page_title,field_breadcrumb_override,field_breadcrumb_max_width,field_cat_intro_meta_description,field_cta,field_cat_intro_body_copy,field_cat_intro_product_callouts,field_background_image_position,field_text_colour"; | ||
@@ -94,3 +88,4 @@ options.include = "field_cat_intro_background_image,field_simple_page_title,field_breadcrumb_override,field_cta,field_cat_intro_meta_description,field_cat_intro_product_callouts.field_callout_flag_product,field_cat_intro_product_callouts.field_callout_outfit_product"; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
function queryOptionsForSimplePages(market, country) { | ||
let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ""; | ||
return toQueryParams({ | ||
@@ -118,10 +113,9 @@ "filter[field_path]": path, | ||
filterObj["filter[title][operator]"] = "IN"; | ||
for (let i = 0; i < stringsArray.length; i++) { | ||
filterObj[`filter[title][value][${i}]`] = stringsArray[i]; | ||
} | ||
filterObj["fields[node--reusable_string]"] = "title,field_reusable_string_text"; | ||
return toQueryParams(filterObj); | ||
} | ||
/* | ||
@@ -134,11 +128,8 @@ Function getDrupalData | ||
*/ | ||
async function getDrupalData(fetchUrl, fetchOptions) { | ||
return await fetch(fetchUrl, fetchOptions).then(resp => resp.json()).then(json => { | ||
var _json$data; | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return (_json$data = json.data) === null || _json$data === void 0 ? void 0 : _json$data.map(label => flattenCMSJson(label, json.included)); | ||
@@ -153,2 +144,3 @@ }).catch(e => { | ||
} | ||
/* | ||
@@ -164,3 +156,2 @@ Function getSimplePage | ||
*/ | ||
async function getSimplePage(cmsUrl, market, country, language, path) { | ||
@@ -170,2 +161,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
} | ||
/* | ||
@@ -179,3 +171,2 @@ Function getBasicBlock | ||
*/ | ||
async function getBasicBlock(cmsUrl, title, language) { | ||
@@ -185,2 +176,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
} | ||
/* | ||
@@ -194,3 +186,2 @@ Function getReusableData | ||
*/ | ||
async function getReusableData(cmsUrl, stringsArray, language) { | ||
@@ -200,2 +191,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
} | ||
/* | ||
@@ -212,3 +204,2 @@ Function getReusableText | ||
*/ | ||
function getReusableText(titleKey, resuableStrings) { | ||
@@ -222,5 +213,5 @@ if (typeof resuableStrings !== "undefined") { | ||
} | ||
return titleKey; | ||
} | ||
/* | ||
@@ -234,3 +225,2 @@ Function getCsPhoneNumber | ||
*/ | ||
async function getCsPhoneNumber(cmsUrl, market, country) { | ||
@@ -241,2 +231,3 @@ const drupalUrl = `https://${cmsUrl}/jsonapi/node/cs_phone_number/?filter%5Bfield_market%5D=${market}&filter%5Bfield_allowed_countries%5D=${country}&fields%5Bnode--cs_phone_number%5D=title`; | ||
} | ||
/* | ||
@@ -252,13 +243,9 @@ Function getCategoryIntroId | ||
*/ | ||
const getCategoryIntroId = async (cmsUrl, market, country, language, path) => { | ||
let requestUrl = `${cmsUrl}/${language}/jsonapi/node/category_intro/?filter%5Bfield_market%5D=${market}&filter%5Bfield_cat_intro_category_handle%5D=${path}&fields%5Bnode--category_intro%5D=id`; | ||
if (country !== null) { | ||
requestUrl = `${cmsUrl}/${language}/jsonapi/node/category_intro/?filter%5Bfield_market%5D=${market}&filter%5Bfield_cat_intro_category_handle%5D=${path}&filter%5Bfield_allowed_countries%5D=${country.toUpperCase()}&fields%5Bnode--category_intro%5D=id`; | ||
} | ||
return await fetch(requestUrl).then(resp => resp.json()).then(json => { | ||
var _json$data$; | ||
return json === null || json === void 0 ? void 0 : (_json$data$ = json.data[0]) === null || _json$data$ === void 0 ? void 0 : _json$data$.id; | ||
@@ -270,2 +257,3 @@ }).catch(err => { | ||
}; | ||
/* | ||
@@ -278,3 +266,2 @@ Function getCategoryIntroPaths | ||
*/ | ||
const getCategoryIntroPaths = (market, path) => { | ||
@@ -288,3 +275,2 @@ let str = ""; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
@@ -298,6 +284,6 @@ str += `${p}`; | ||
} | ||
return str; | ||
}); // Use mens as a fallback | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
@@ -307,5 +293,5 @@ paths.splice(1, 0, `mens/${path}`); | ||
} | ||
return paths; | ||
}; | ||
/* | ||
@@ -322,18 +308,15 @@ Function: getCmsApiForCategoryIntro | ||
*/ | ||
const getCmsApiForCategoryIntro = async (cmsUrl, market, country, language, path, previewDraft) => { | ||
let cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/?`; | ||
if (previewDraft) { | ||
let catIntroId = await getCategoryIntroId(cmsUrl, market, country, language, path); // If No catIntroId found with country filter, then try to find with no country set in CMS | ||
let catIntroId = await getCategoryIntroId(cmsUrl, market, country, language, path); | ||
// If No catIntroId found with country filter, then try to find with no country set in CMS | ||
if (catIntroId === null || !catIntroId) { | ||
catIntroId = await getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
return cmsApi; | ||
}; | ||
/* | ||
@@ -350,10 +333,11 @@ Function: generateRequestUrls | ||
*/ | ||
const generateRequestUrls = async (cmsUrl, market, country, language, path, previewDraft) => { | ||
const catIntroPaths = getCategoryIntroPaths(market, path); | ||
const cmsApi = await getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); // Request Url with country filter | ||
const cmsApi = await getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); | ||
// Request Url with country filter | ||
const requestUrls = catIntroPaths.map(catPath => cmsApi + toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft))); | ||
return requestUrls; | ||
}; | ||
/* | ||
@@ -371,3 +355,2 @@ Function getCategoryIntro | ||
*/ | ||
const getCategoryIntro = async (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => { | ||
@@ -381,6 +364,6 @@ const fetchOptions = previewDraft ? { | ||
const requestUrls = await generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
try { | ||
const catIntros = await Promise.all(requestUrls.map(url => getDrupalData(url, fetchOptions))).then(json => json); | ||
const catIntrosNoEmpties = catIntros.filter(catIntro => catIntro === null || catIntro === void 0 ? void 0 : catIntro.length); | ||
/* | ||
@@ -394,10 +377,8 @@ The Drupal fetch returns an array of Cat Intros | ||
// Note: The order of the array never changes | ||
if (!path.includes("mens") && (catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) === 3) { | ||
return catIntrosNoEmpties[(catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) - 2]; | ||
} // Else CDP is gendered OR a unisex Cat Intro doesn't exist then return the last item | ||
} | ||
// Else CDP is gendered OR a unisex Cat Intro doesn't exist then return the last item | ||
// e.g. ["mens", "mens/accessories"] return "mens/accessories" | ||
// e.g. ["womens"] return "womens" | ||
return catIntrosNoEmpties[(catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) - 1]; | ||
@@ -409,2 +390,3 @@ } catch (e) { | ||
}; | ||
/** | ||
@@ -420,12 +402,8 @@ * Query CMS message content type, filtering by market, country, language and field name. | ||
*/ | ||
const getCmsMessageByFieldName = async (cmsUrl, market, country, language, messageFieldName) => { | ||
var _data$0$field_content, _data$; | ||
let queryStrings = `filter[field_market]=${market}&filter[field_allowed_countries]=${country}&fields[node--message]=field_content`; | ||
if (messageFieldName) { | ||
queryStrings += `&filter[field_name]=${messageFieldName}`; | ||
} | ||
const messageUrl = `${cmsUrl}/${language}/jsonapi/node/message?${queryStrings}`; | ||
@@ -432,0 +410,0 @@ const data = await getDrupalData(messageUrl); |
@@ -7,2 +7,3 @@ /** | ||
*/ | ||
/* | ||
@@ -15,4 +16,2 @@ * Converts a simple object to query parameters | ||
*/ | ||
function toQueryParams(obj) { | ||
@@ -23,3 +22,2 @@ const url = Object.keys(obj).map(x => { | ||
} | ||
return `${encodeURIComponent(x)}=${encodeURIComponent(obj[x])}`; | ||
@@ -31,8 +29,8 @@ }).join("&"); | ||
/*eslint-disable*/ | ||
function flattenCMSJson(item, included) { | ||
// 2) Extracts it's "attributes" values into the return object | ||
if (typeof item !== "undefined") { | ||
const returnObject = item.attributes; // 3) Checks for a "relationships" attribute, if it does, add a memeber to the return object named the same as the relationship key | ||
const returnObject = item.attributes; | ||
// 3) Checks for a "relationships" attribute, if it does, add a memeber to the return object named the same as the relationship key | ||
if (item.relationships) { | ||
@@ -45,6 +43,6 @@ for (const key in item.relationships) { | ||
} | ||
returnObject[key] = item.relationships[key].data.map(related => { | ||
// 6) call getAttributes on the real entity object, put the results into the new member on the return object (from 3) | ||
return flattenCMSJson( // 5) get the real entity object from the "included" array | ||
return flattenCMSJson( | ||
// 5) get the real entity object from the "included" array | ||
included.filter(include => (related === null || related === void 0 ? void 0 : related.id) === include.id)[0], included); | ||
@@ -54,3 +52,2 @@ }); | ||
} | ||
return returnObject; | ||
@@ -63,3 +60,2 @@ } | ||
const options = {}; | ||
if (previewDraft) { | ||
@@ -71,3 +67,2 @@ options.resourceVersion = "rel:working-copy"; | ||
} | ||
if (country !== null) { | ||
@@ -79,3 +74,2 @@ options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} | ||
options["fields[node--category_intro]"] = "title,field_cat_intro_category_handle,field_cat_intro_background_image,field_simple_page_title,field_breadcrumb_override,field_breadcrumb_max_width,field_cat_intro_meta_description,field_cta,field_cat_intro_body_copy,field_cat_intro_product_callouts,field_background_image_position,field_text_colour"; | ||
@@ -90,3 +84,4 @@ options.include = "field_cat_intro_background_image,field_simple_page_title,field_breadcrumb_override,field_cta,field_cat_intro_meta_description,field_cat_intro_product_callouts.field_callout_flag_product,field_cat_intro_product_callouts.field_callout_outfit_product"; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
function queryOptionsForSimplePages(market, country) { | ||
let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ""; | ||
return toQueryParams({ | ||
@@ -114,10 +109,9 @@ "filter[field_path]": path, | ||
filterObj["filter[title][operator]"] = "IN"; | ||
for (let i = 0; i < stringsArray.length; i++) { | ||
filterObj[`filter[title][value][${i}]`] = stringsArray[i]; | ||
} | ||
filterObj["fields[node--reusable_string]"] = "title,field_reusable_string_text"; | ||
return toQueryParams(filterObj); | ||
} | ||
/* | ||
@@ -130,11 +124,8 @@ Function getDrupalData | ||
*/ | ||
async function getDrupalData(fetchUrl, fetchOptions) { | ||
return await fetch(fetchUrl, fetchOptions).then(resp => resp.json()).then(json => { | ||
var _json$data; | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return (_json$data = json.data) === null || _json$data === void 0 ? void 0 : _json$data.map(label => flattenCMSJson(label, json.included)); | ||
@@ -149,2 +140,3 @@ }).catch(e => { | ||
} | ||
/* | ||
@@ -160,3 +152,2 @@ Function getSimplePage | ||
*/ | ||
async function getSimplePage(cmsUrl, market, country, language, path) { | ||
@@ -166,2 +157,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
} | ||
/* | ||
@@ -175,3 +167,2 @@ Function getBasicBlock | ||
*/ | ||
async function getBasicBlock(cmsUrl, title, language) { | ||
@@ -181,2 +172,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
} | ||
/* | ||
@@ -190,3 +182,2 @@ Function getReusableData | ||
*/ | ||
async function getReusableData(cmsUrl, stringsArray, language) { | ||
@@ -196,2 +187,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
} | ||
/* | ||
@@ -208,3 +200,2 @@ Function getReusableText | ||
*/ | ||
function getReusableText(titleKey, resuableStrings) { | ||
@@ -218,5 +209,5 @@ if (typeof resuableStrings !== "undefined") { | ||
} | ||
return titleKey; | ||
} | ||
/* | ||
@@ -230,3 +221,2 @@ Function getCsPhoneNumber | ||
*/ | ||
async function getCsPhoneNumber(cmsUrl, market, country) { | ||
@@ -237,2 +227,3 @@ const drupalUrl = `https://${cmsUrl}/jsonapi/node/cs_phone_number/?filter%5Bfield_market%5D=${market}&filter%5Bfield_allowed_countries%5D=${country}&fields%5Bnode--cs_phone_number%5D=title`; | ||
} | ||
/* | ||
@@ -248,13 +239,9 @@ Function getCategoryIntroId | ||
*/ | ||
const getCategoryIntroId = async (cmsUrl, market, country, language, path) => { | ||
let requestUrl = `${cmsUrl}/${language}/jsonapi/node/category_intro/?filter%5Bfield_market%5D=${market}&filter%5Bfield_cat_intro_category_handle%5D=${path}&fields%5Bnode--category_intro%5D=id`; | ||
if (country !== null) { | ||
requestUrl = `${cmsUrl}/${language}/jsonapi/node/category_intro/?filter%5Bfield_market%5D=${market}&filter%5Bfield_cat_intro_category_handle%5D=${path}&filter%5Bfield_allowed_countries%5D=${country.toUpperCase()}&fields%5Bnode--category_intro%5D=id`; | ||
} | ||
return await fetch(requestUrl).then(resp => resp.json()).then(json => { | ||
var _json$data$; | ||
return json === null || json === void 0 ? void 0 : (_json$data$ = json.data[0]) === null || _json$data$ === void 0 ? void 0 : _json$data$.id; | ||
@@ -266,2 +253,3 @@ }).catch(err => { | ||
}; | ||
/* | ||
@@ -274,3 +262,2 @@ Function getCategoryIntroPaths | ||
*/ | ||
const getCategoryIntroPaths = (market, path) => { | ||
@@ -284,3 +271,2 @@ let str = ""; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
@@ -294,6 +280,6 @@ str += `${p}`; | ||
} | ||
return str; | ||
}); // Use mens as a fallback | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
@@ -303,5 +289,5 @@ paths.splice(1, 0, `mens/${path}`); | ||
} | ||
return paths; | ||
}; | ||
/* | ||
@@ -318,18 +304,15 @@ Function: getCmsApiForCategoryIntro | ||
*/ | ||
const getCmsApiForCategoryIntro = async (cmsUrl, market, country, language, path, previewDraft) => { | ||
let cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/?`; | ||
if (previewDraft) { | ||
let catIntroId = await getCategoryIntroId(cmsUrl, market, country, language, path); // If No catIntroId found with country filter, then try to find with no country set in CMS | ||
let catIntroId = await getCategoryIntroId(cmsUrl, market, country, language, path); | ||
// If No catIntroId found with country filter, then try to find with no country set in CMS | ||
if (catIntroId === null || !catIntroId) { | ||
catIntroId = await getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
return cmsApi; | ||
}; | ||
/* | ||
@@ -346,10 +329,11 @@ Function: generateRequestUrls | ||
*/ | ||
const generateRequestUrls = async (cmsUrl, market, country, language, path, previewDraft) => { | ||
const catIntroPaths = getCategoryIntroPaths(market, path); | ||
const cmsApi = await getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); // Request Url with country filter | ||
const cmsApi = await getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); | ||
// Request Url with country filter | ||
const requestUrls = catIntroPaths.map(catPath => cmsApi + toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft))); | ||
return requestUrls; | ||
}; | ||
/* | ||
@@ -367,3 +351,2 @@ Function getCategoryIntro | ||
*/ | ||
const getCategoryIntro = async (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => { | ||
@@ -377,6 +360,6 @@ const fetchOptions = previewDraft ? { | ||
const requestUrls = await generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
try { | ||
const catIntros = await Promise.all(requestUrls.map(url => getDrupalData(url, fetchOptions))).then(json => json); | ||
const catIntrosNoEmpties = catIntros.filter(catIntro => catIntro === null || catIntro === void 0 ? void 0 : catIntro.length); | ||
/* | ||
@@ -390,10 +373,8 @@ The Drupal fetch returns an array of Cat Intros | ||
// Note: The order of the array never changes | ||
if (!path.includes("mens") && (catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) === 3) { | ||
return catIntrosNoEmpties[(catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) - 2]; | ||
} // Else CDP is gendered OR a unisex Cat Intro doesn't exist then return the last item | ||
} | ||
// Else CDP is gendered OR a unisex Cat Intro doesn't exist then return the last item | ||
// e.g. ["mens", "mens/accessories"] return "mens/accessories" | ||
// e.g. ["womens"] return "womens" | ||
return catIntrosNoEmpties[(catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) - 1]; | ||
@@ -405,2 +386,3 @@ } catch (e) { | ||
}; | ||
/** | ||
@@ -416,12 +398,8 @@ * Query CMS message content type, filtering by market, country, language and field name. | ||
*/ | ||
const getCmsMessageByFieldName = async (cmsUrl, market, country, language, messageFieldName) => { | ||
var _data$0$field_content, _data$; | ||
let queryStrings = `filter[field_market]=${market}&filter[field_allowed_countries]=${country}&fields[node--message]=field_content`; | ||
if (messageFieldName) { | ||
queryStrings += `&filter[field_name]=${messageFieldName}`; | ||
} | ||
const messageUrl = `${cmsUrl}/${language}/jsonapi/node/message?${queryStrings}`; | ||
@@ -428,0 +406,0 @@ const data = await getDrupalData(messageUrl); |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, (function () { | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (function () { | ||
var current = global.ARCTERYX && global.ARCTERYX.jscms; | ||
@@ -9,4 +9,4 @@ var exports = (global.ARCTERYX = global.ARCTERYX || {}, global.ARCTERYX.jscms = {}); | ||
exports.noConflict = function () { global.ARCTERYX.jscms = current; return exports; }; | ||
}())); | ||
}(this, (function (exports) { 'use strict'; | ||
})()); | ||
})(this, (function (exports) { 'use strict'; | ||
@@ -19,2 +19,3 @@ /** | ||
*/ | ||
/* | ||
@@ -27,4 +28,2 @@ * Converts a simple object to query parameters | ||
*/ | ||
function toQueryParams(obj) { | ||
@@ -35,3 +34,2 @@ const url = Object.keys(obj).map(x => { | ||
} | ||
return `${encodeURIComponent(x)}=${encodeURIComponent(obj[x])}`; | ||
@@ -43,8 +41,8 @@ }).join("&"); | ||
/*eslint-disable*/ | ||
function flattenCMSJson(item, included) { | ||
// 2) Extracts it's "attributes" values into the return object | ||
if (typeof item !== "undefined") { | ||
const returnObject = item.attributes; // 3) Checks for a "relationships" attribute, if it does, add a memeber to the return object named the same as the relationship key | ||
const returnObject = item.attributes; | ||
// 3) Checks for a "relationships" attribute, if it does, add a memeber to the return object named the same as the relationship key | ||
if (item.relationships) { | ||
@@ -57,6 +55,6 @@ for (const key in item.relationships) { | ||
} | ||
returnObject[key] = item.relationships[key].data.map(related => { | ||
// 6) call getAttributes on the real entity object, put the results into the new member on the return object (from 3) | ||
return flattenCMSJson( // 5) get the real entity object from the "included" array | ||
return flattenCMSJson( | ||
// 5) get the real entity object from the "included" array | ||
included.filter(include => (related === null || related === void 0 ? void 0 : related.id) === include.id)[0], included); | ||
@@ -66,3 +64,2 @@ }); | ||
} | ||
return returnObject; | ||
@@ -75,3 +72,2 @@ } | ||
const options = {}; | ||
if (previewDraft) { | ||
@@ -83,3 +79,2 @@ options.resourceVersion = "rel:working-copy"; | ||
} | ||
if (country !== null) { | ||
@@ -91,3 +86,2 @@ options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} | ||
options["fields[node--category_intro]"] = "title,field_cat_intro_category_handle,field_cat_intro_background_image,field_simple_page_title,field_breadcrumb_override,field_breadcrumb_max_width,field_cat_intro_meta_description,field_cta,field_cat_intro_body_copy,field_cat_intro_product_callouts,field_background_image_position,field_text_colour"; | ||
@@ -102,3 +96,4 @@ options.include = "field_cat_intro_background_image,field_simple_page_title,field_breadcrumb_override,field_cta,field_cat_intro_meta_description,field_cat_intro_product_callouts.field_callout_flag_product,field_cat_intro_product_callouts.field_callout_outfit_product"; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
function queryOptionsForSimplePages(market, country) { | ||
let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ""; | ||
return toQueryParams({ | ||
@@ -126,10 +121,9 @@ "filter[field_path]": path, | ||
filterObj["filter[title][operator]"] = "IN"; | ||
for (let i = 0; i < stringsArray.length; i++) { | ||
filterObj[`filter[title][value][${i}]`] = stringsArray[i]; | ||
} | ||
filterObj["fields[node--reusable_string]"] = "title,field_reusable_string_text"; | ||
return toQueryParams(filterObj); | ||
} | ||
/* | ||
@@ -142,11 +136,8 @@ Function getDrupalData | ||
*/ | ||
async function getDrupalData(fetchUrl, fetchOptions) { | ||
return await fetch(fetchUrl, fetchOptions).then(resp => resp.json()).then(json => { | ||
var _json$data; | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return (_json$data = json.data) === null || _json$data === void 0 ? void 0 : _json$data.map(label => flattenCMSJson(label, json.included)); | ||
@@ -161,2 +152,3 @@ }).catch(e => { | ||
} | ||
/* | ||
@@ -172,3 +164,2 @@ Function getSimplePage | ||
*/ | ||
async function getSimplePage(cmsUrl, market, country, language, path) { | ||
@@ -178,2 +169,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
} | ||
/* | ||
@@ -187,3 +179,2 @@ Function getBasicBlock | ||
*/ | ||
async function getBasicBlock(cmsUrl, title, language) { | ||
@@ -193,2 +184,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
} | ||
/* | ||
@@ -202,3 +194,2 @@ Function getReusableData | ||
*/ | ||
async function getReusableData(cmsUrl, stringsArray, language) { | ||
@@ -208,2 +199,3 @@ const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
} | ||
/* | ||
@@ -220,3 +212,2 @@ Function getReusableText | ||
*/ | ||
function getReusableText(titleKey, resuableStrings) { | ||
@@ -230,5 +221,5 @@ if (typeof resuableStrings !== "undefined") { | ||
} | ||
return titleKey; | ||
} | ||
/* | ||
@@ -242,3 +233,2 @@ Function getCsPhoneNumber | ||
*/ | ||
async function getCsPhoneNumber(cmsUrl, market, country) { | ||
@@ -249,2 +239,3 @@ const drupalUrl = `https://${cmsUrl}/jsonapi/node/cs_phone_number/?filter%5Bfield_market%5D=${market}&filter%5Bfield_allowed_countries%5D=${country}&fields%5Bnode--cs_phone_number%5D=title`; | ||
} | ||
/* | ||
@@ -260,13 +251,9 @@ Function getCategoryIntroId | ||
*/ | ||
const getCategoryIntroId = async (cmsUrl, market, country, language, path) => { | ||
let requestUrl = `${cmsUrl}/${language}/jsonapi/node/category_intro/?filter%5Bfield_market%5D=${market}&filter%5Bfield_cat_intro_category_handle%5D=${path}&fields%5Bnode--category_intro%5D=id`; | ||
if (country !== null) { | ||
requestUrl = `${cmsUrl}/${language}/jsonapi/node/category_intro/?filter%5Bfield_market%5D=${market}&filter%5Bfield_cat_intro_category_handle%5D=${path}&filter%5Bfield_allowed_countries%5D=${country.toUpperCase()}&fields%5Bnode--category_intro%5D=id`; | ||
} | ||
return await fetch(requestUrl).then(resp => resp.json()).then(json => { | ||
var _json$data$; | ||
return json === null || json === void 0 ? void 0 : (_json$data$ = json.data[0]) === null || _json$data$ === void 0 ? void 0 : _json$data$.id; | ||
@@ -278,2 +265,3 @@ }).catch(err => { | ||
}; | ||
/* | ||
@@ -286,3 +274,2 @@ Function getCategoryIntroPaths | ||
*/ | ||
const getCategoryIntroPaths = (market, path) => { | ||
@@ -296,3 +283,2 @@ let str = ""; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
@@ -306,6 +292,6 @@ str += `${p}`; | ||
} | ||
return str; | ||
}); // Use mens as a fallback | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
@@ -315,5 +301,5 @@ paths.splice(1, 0, `mens/${path}`); | ||
} | ||
return paths; | ||
}; | ||
/* | ||
@@ -330,18 +316,15 @@ Function: getCmsApiForCategoryIntro | ||
*/ | ||
const getCmsApiForCategoryIntro = async (cmsUrl, market, country, language, path, previewDraft) => { | ||
let cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/?`; | ||
if (previewDraft) { | ||
let catIntroId = await getCategoryIntroId(cmsUrl, market, country, language, path); // If No catIntroId found with country filter, then try to find with no country set in CMS | ||
let catIntroId = await getCategoryIntroId(cmsUrl, market, country, language, path); | ||
// If No catIntroId found with country filter, then try to find with no country set in CMS | ||
if (catIntroId === null || !catIntroId) { | ||
catIntroId = await getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
return cmsApi; | ||
}; | ||
/* | ||
@@ -358,10 +341,11 @@ Function: generateRequestUrls | ||
*/ | ||
const generateRequestUrls = async (cmsUrl, market, country, language, path, previewDraft) => { | ||
const catIntroPaths = getCategoryIntroPaths(market, path); | ||
const cmsApi = await getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); // Request Url with country filter | ||
const cmsApi = await getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); | ||
// Request Url with country filter | ||
const requestUrls = catIntroPaths.map(catPath => cmsApi + toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft))); | ||
return requestUrls; | ||
}; | ||
/* | ||
@@ -379,3 +363,2 @@ Function getCategoryIntro | ||
*/ | ||
const getCategoryIntro = async (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => { | ||
@@ -389,6 +372,6 @@ const fetchOptions = previewDraft ? { | ||
const requestUrls = await generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
try { | ||
const catIntros = await Promise.all(requestUrls.map(url => getDrupalData(url, fetchOptions))).then(json => json); | ||
const catIntrosNoEmpties = catIntros.filter(catIntro => catIntro === null || catIntro === void 0 ? void 0 : catIntro.length); | ||
/* | ||
@@ -402,10 +385,8 @@ The Drupal fetch returns an array of Cat Intros | ||
// Note: The order of the array never changes | ||
if (!path.includes("mens") && (catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) === 3) { | ||
return catIntrosNoEmpties[(catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) - 2]; | ||
} // Else CDP is gendered OR a unisex Cat Intro doesn't exist then return the last item | ||
} | ||
// Else CDP is gendered OR a unisex Cat Intro doesn't exist then return the last item | ||
// e.g. ["mens", "mens/accessories"] return "mens/accessories" | ||
// e.g. ["womens"] return "womens" | ||
return catIntrosNoEmpties[(catIntrosNoEmpties === null || catIntrosNoEmpties === void 0 ? void 0 : catIntrosNoEmpties.length) - 1]; | ||
@@ -417,2 +398,3 @@ } catch (e) { | ||
}; | ||
/** | ||
@@ -428,12 +410,8 @@ * Query CMS message content type, filtering by market, country, language and field name. | ||
*/ | ||
const getCmsMessageByFieldName = async (cmsUrl, market, country, language, messageFieldName) => { | ||
var _data$0$field_content, _data$; | ||
let queryStrings = `filter[field_market]=${market}&filter[field_allowed_countries]=${country}&fields[node--message]=field_content`; | ||
if (messageFieldName) { | ||
queryStrings += `&filter[field_name]=${messageFieldName}`; | ||
} | ||
const messageUrl = `${cmsUrl}/${language}/jsonapi/node/message?${queryStrings}`; | ||
@@ -465,3 +443,3 @@ const data = await getDrupalData(messageUrl); | ||
}))); | ||
})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@arcteryx/js-cms", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"source": "src/index.js", | ||
@@ -15,14 +15,4 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"@arcteryx/js-url": "^1.27.12" | ||
"@arcteryx/js-url": "^1.28.0" | ||
}, | ||
"lint-staged": { | ||
"**/*.+(js|jsx|css|less|scss|json|sass)": [ | ||
"prettier '**/*.{js,jsx,css,less,scss,json,sass}' --write", | ||
"git add" | ||
], | ||
"**/*.+(js|jsx)": [ | ||
"eslint . --fix --quiet", | ||
"git add" | ||
] | ||
}, | ||
"repository": { | ||
@@ -33,3 +23,3 @@ "type": "git", | ||
"license": "ISC", | ||
"gitHead": "d0a58ddc88f88a88e2d433170765cdac412ea21b", | ||
"gitHead": "ec3795a7d5bb844c4d9931d9f32cf1416d09e081", | ||
"files": [ | ||
@@ -36,0 +26,0 @@ "dist", |
@@ -17,7 +17,7 @@ import { toQueryParams } from "@arcteryx/js-url"; | ||
} | ||
returnObject[key] = item.relationships[key].data.map(related => { | ||
returnObject[key] = item.relationships[key].data.map((related) => { | ||
// 6) call getAttributes on the real entity object, put the results into the new member on the return object (from 3) | ||
return flattenCMSJson( | ||
// 5) get the real entity object from the "included" array | ||
included.filter(include => related?.id === include.id)[0], | ||
included.filter((include) => related?.id === include.id)[0], | ||
included | ||
@@ -107,10 +107,10 @@ ); | ||
return await fetch(fetchUrl, fetchOptions) | ||
.then(resp => resp.json()) | ||
.then(json => { | ||
.then((resp) => resp.json()) | ||
.then((json) => { | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return json.data?.map(label => flattenCMSJson(label, json.included)); | ||
return json.data?.map((label) => flattenCMSJson(label, json.included)); | ||
}) | ||
.catch(e => { | ||
.catch((e) => { | ||
console.error(e.message); | ||
@@ -220,5 +220,5 @@ return null; | ||
return await fetch(requestUrl) | ||
.then(resp => resp.json()) | ||
.then(json => json?.data[0]?.id) | ||
.catch(err => { | ||
.then((resp) => resp.json()) | ||
.then((json) => json?.data[0]?.id) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
@@ -308,3 +308,3 @@ return null; | ||
const requestUrls = catIntroPaths.map( | ||
catPath => cmsApi + toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft)) | ||
(catPath) => cmsApi + toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft)) | ||
); | ||
@@ -340,4 +340,6 @@ | ||
try { | ||
const catIntros = await Promise.all(requestUrls.map(url => getDrupalData(url, fetchOptions))).then(json => json); | ||
const catIntrosNoEmpties = catIntros.filter(catIntro => catIntro?.length); | ||
const catIntros = await Promise.all(requestUrls.map((url) => getDrupalData(url, fetchOptions))).then( | ||
(json) => json | ||
); | ||
const catIntrosNoEmpties = catIntros.filter((catIntro) => catIntro?.length); | ||
@@ -344,0 +346,0 @@ /* |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1460
175392
10
Updated@arcteryx/js-url@^1.28.0