@arcteryx/js-cms
Advanced tools
Comparing version 2.4.0-alpha.0 to 2.4.0-test.0
@@ -7,96 +7,126 @@ 'use strict'; | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P ? value : new P(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
/*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 | ||
if (item.relationships) { | ||
for (const key in item.relationships) { | ||
// 4) iterate over the items in the related field's data array (or if not an array, skip straight to processing the object) | ||
if (item.relationships[key].data instanceof Array === false) { | ||
// Sometimes the relationships[key] is just an object, sometimes it's an array. Let's always treat them as arrays. | ||
item.relationships[key].data = [item.relationships[key].data]; | ||
// 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 | ||
if (item.relationships) { | ||
for (const key in item.relationships) { | ||
// 4) iterate over the items in the related field's data array (or if not an array, skip straight to processing the object) | ||
if (item.relationships[key].data instanceof Array === false) { | ||
// Sometimes the relationships[key] is just an object, sometimes it's an array. Let's always treat them as arrays. | ||
item.relationships[key].data = [item.relationships[key].data]; | ||
} | ||
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 === null || related === void 0 ? void 0 : related.id) === include.id)[0], included); | ||
}); | ||
} | ||
} | ||
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 | ||
); | ||
}); | ||
} | ||
return returnObject; | ||
} | ||
return returnObject; | ||
} | ||
} | ||
/* eslint-enable*/ | ||
const queryOptionsForCategoryIntros = (market, country, path, previewDraft) => { | ||
const options = {}; | ||
if (previewDraft) { | ||
options.resourceVersion = "rel:working-copy"; | ||
} else { | ||
options["filter[field_market]"] = market.toUpperCase(); | ||
options["filter[field_cat_intro_category_handle]"] = path; | ||
} | ||
if (country !== null) { | ||
options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} else { | ||
options["filter[country-filter][condition][path]"] = "field_allowed_countries"; | ||
options["filter[country-filter][condition][operator]"] = "IS NULL"; | ||
} | ||
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"; | ||
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"; | ||
options["fields[paragraph--cta]"] = "field_link,field_style"; | ||
options["fields[paragraph--product_callout_flag]"] = | ||
"field_callout_flag_colour,field_callout_flag_type,field_callout_flag_x_percentage,field_callout_flag_y_percentage,field_callout_flag_product,field_callout_arrow_direction"; | ||
options["fields[node--reusable_string]"] = "field_reusable_string_text"; | ||
options["fields[file--file]"] = "uri"; | ||
options["fields[node--product]"] = "field_product_product_name,field_product_product_url"; | ||
return options; | ||
const options = {}; | ||
if (previewDraft) { | ||
options.resourceVersion = "rel:working-copy"; | ||
} | ||
else { | ||
options["filter[field_market]"] = market.toUpperCase(); | ||
options["filter[field_cat_intro_category_handle]"] = path; | ||
} | ||
if (country !== null) { | ||
options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} | ||
else { | ||
options["filter[country-filter][condition][path]"] = "field_allowed_countries"; | ||
options["filter[country-filter][condition][operator]"] = "IS NULL"; | ||
} | ||
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"; | ||
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"; | ||
options["fields[paragraph--cta]"] = "field_link,field_style"; | ||
options["fields[paragraph--product_callout_flag]"] = | ||
"field_callout_flag_colour,field_callout_flag_type,field_callout_flag_x_percentage,field_callout_flag_y_percentage,field_callout_flag_product,field_callout_arrow_direction"; | ||
options["fields[node--reusable_string]"] = "field_reusable_string_text"; | ||
options["fields[file--file]"] = "uri"; | ||
options["fields[node--product]"] = "field_product_product_name,field_product_product_url"; | ||
return options; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
return jsUrl.toQueryParams({ | ||
"filter[field_path]": path, | ||
"filter[field_market]": market.toUpperCase(), | ||
"filter[field_allowed_countries]": country.toUpperCase(), | ||
"fields[node--simple_page]": | ||
"title,field_meta_page_title,field_meta_page_description,field_simple_page_sections,field_simple_page_breadcrumbs,field_simple_page_title,field_share_image", | ||
include: | ||
"field_simple_page_sections,field_simple_page_title,field_simple_page_sections.field_simple_blocks,field_simple_page_sections.field_simple_blocks.field_background_image,field_simple_page_sections.field_simple_blocks.field_cta,field_share_image", | ||
"fields[paragraph--simple_page_section]": | ||
"field_simple_page_section_header,field_simple_page_section_layout,field_simple_blocks", | ||
"fields[node--simple_block]": | ||
"title,field_background_image_x_focal,field_background_image_y_focal,field_content_align_x,field_content_align_y,field_header_text,field_header_svg,field_sub_header_text,field_video_link,field_video_background,field_background_image,field_cta,field_raw", | ||
"fields[file--file]": "uri", | ||
"fields[paragraph--cta]": "field_link,field_style", | ||
"fields[node--reusable_string]": "field_reusable_string_text", | ||
}); | ||
return jsUrl.toQueryParams({ | ||
"filter[field_path]": path, | ||
"filter[field_market]": market.toUpperCase(), | ||
"filter[field_allowed_countries]": country.toUpperCase(), | ||
"fields[node--simple_page]": "title,field_meta_page_title,field_meta_page_description,field_simple_page_sections,field_simple_page_breadcrumbs,field_simple_page_title,field_share_image", | ||
include: "field_simple_page_sections,field_simple_page_title,field_simple_page_sections.field_simple_blocks,field_simple_page_sections.field_simple_blocks.field_background_image,field_simple_page_sections.field_simple_blocks.field_cta,field_share_image", | ||
"fields[paragraph--simple_page_section]": "field_simple_page_section_header,field_simple_page_section_layout,field_simple_blocks", | ||
"fields[node--simple_block]": "title,field_background_image_x_focal,field_background_image_y_focal,field_content_align_x,field_content_align_y,field_header_text,field_header_svg,field_sub_header_text,field_video_link,field_video_background,field_background_image,field_cta,field_raw", | ||
"fields[file--file]": "uri", | ||
"fields[paragraph--cta]": "field_link,field_style", | ||
"fields[node--reusable_string]": "field_reusable_string_text", | ||
}); | ||
} | ||
function queryOptionsForBasicBlock(title) { | ||
return jsUrl.toQueryParams({ | ||
"filter[title]": title, | ||
"fields[node--basic_block]": "title,field_basic_content,field_css_class_names", | ||
}); | ||
return jsUrl.toQueryParams({ | ||
"filter[title]": title, | ||
"fields[node--basic_block]": "title,field_basic_content,field_css_class_names", | ||
}); | ||
} | ||
function queryOptionsForReusableString(stringsArray) { | ||
const filterObj = {}; | ||
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 jsUrl.toQueryParams(filterObj); | ||
const filterObj = {}; | ||
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 jsUrl.toQueryParams(filterObj); | ||
} | ||
/* | ||
@@ -109,21 +139,22 @@ Function getDrupalData | ||
*/ | ||
async function getDrupalData(fetchUrl, fetchOptions) { | ||
return await fetch(fetchUrl, fetchOptions) | ||
.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)); | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
return null; | ||
function getDrupalData(fetchUrl, fetchOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield fetch(fetchUrl, fetchOptions) | ||
.then((resp) => resp.json()) | ||
.then((json) => { | ||
var _a; | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return (_a = json.data) === null || _a === void 0 ? void 0 : _a.map((label) => flattenCMSJson(label, json.included)); | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
return null; | ||
}); | ||
}); | ||
} | ||
function GetDrupalData(fetchUrl) { | ||
return getDrupalData(fetchUrl); | ||
return getDrupalData(fetchUrl); | ||
} | ||
/* | ||
@@ -139,7 +170,8 @@ Function getSimplePage | ||
*/ | ||
async function getSimplePage(cmsUrl, market, country, language, path) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForSimplePages(market, country, path)); | ||
function getSimplePage(cmsUrl, market, country, language, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForSimplePages(market, country, path)); | ||
}); | ||
} | ||
/* | ||
@@ -153,7 +185,8 @@ Function getBasicBlock | ||
*/ | ||
async function getBasicBlock(cmsUrl, title, language) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForBasicBlock(title)); | ||
function getBasicBlock(cmsUrl, title, language) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForBasicBlock(title)); | ||
}); | ||
} | ||
/* | ||
@@ -167,7 +200,8 @@ Function getReusableData | ||
*/ | ||
async function getReusableData(cmsUrl, stringsArray, language) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForReusableString(stringsArray)); | ||
function getReusableData(cmsUrl, stringsArray, language) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForReusableString(stringsArray)); | ||
}); | ||
} | ||
/* | ||
@@ -185,12 +219,11 @@ Function getReusableText | ||
function getReusableText(titleKey, resuableStrings) { | ||
if (typeof resuableStrings !== "undefined") { | ||
for (let i = 0; i < resuableStrings.length; i++) { | ||
if (resuableStrings[i].title === titleKey) { | ||
return resuableStrings[i].field_reusable_string_text; | ||
} | ||
if (typeof resuableStrings !== "undefined") { | ||
for (let i = 0; i < resuableStrings.length; i++) { | ||
if (resuableStrings[i].title === titleKey) { | ||
return resuableStrings[i].field_reusable_string_text; | ||
} | ||
} | ||
} | ||
} | ||
return titleKey; | ||
return titleKey; | ||
} | ||
/* | ||
@@ -204,8 +237,9 @@ Function getCsPhoneNumber | ||
*/ | ||
async function getCsPhoneNumber(cmsUrl, market, country) { | ||
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`; | ||
const data = await GetDrupalData(drupalUrl); | ||
return data?.length ? data[0].title : null; | ||
function getCsPhoneNumber(cmsUrl, market, country) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
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`; | ||
const data = yield GetDrupalData(drupalUrl); | ||
return (data === null || data === void 0 ? void 0 : data.length) ? data[0].title : null; | ||
}); | ||
} | ||
/* | ||
@@ -221,18 +255,15 @@ 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) => json?.data[0]?.id) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
return null; | ||
const getCategoryIntroId = (cmsUrl, market, country, language, path) => __awaiter(void 0, void 0, void 0, function* () { | ||
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 yield fetch(requestUrl) | ||
.then((resp) => resp.json()) | ||
.then((json) => { var _a; return (_a = json === null || json === void 0 ? void 0 : json.data[0]) === null || _a === void 0 ? void 0 : _a.id; }) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
return null; | ||
}); | ||
}; | ||
}); | ||
/* | ||
@@ -246,30 +277,28 @@ Function getCategoryIntroPaths | ||
const getCategoryIntroPaths = (market, path) => { | ||
let str = ""; | ||
const paths = path.split("/").map((p, i) => { | ||
// ARC.COM GENDER SPECIFIC OVERRIDES | ||
if (market === "outdoor" || market === "sale") { | ||
if (!path.split("/").includes("womens") && !path.split("/").includes("mens") && i === 0) { | ||
str += `${p}`; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
str += `${p}`; | ||
} else if (i !== 0) { | ||
str += `/${p}`; | ||
} | ||
} else if (market == "urban" || market == "leaf") { | ||
str += p; | ||
let str = ""; | ||
const paths = path.split("/").map((p, i) => { | ||
// ARC.COM GENDER SPECIFIC OVERRIDES | ||
if (market === "outdoor" || market === "sale") { | ||
if (!path.split("/").includes("womens") && !path.split("/").includes("mens") && i === 0) { | ||
str += `${p}`; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
str += `${p}`; | ||
} | ||
else if (i !== 0) { | ||
str += `/${p}`; | ||
} | ||
} | ||
else if (market == "urban" || market == "leaf") { | ||
str += p; | ||
} | ||
return str; | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
paths.splice(1, 0, `mens/${path}`); | ||
paths.unshift("mens"); | ||
} | ||
return str; | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
paths.splice(1, 0, `mens/${path}`); | ||
paths.unshift("mens"); | ||
} | ||
return paths; | ||
return paths; | ||
}; | ||
/* | ||
@@ -286,17 +315,14 @@ 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 | ||
if (catIntroId === null || !catIntroId) { | ||
catIntroId = await getCategoryIntroId(cmsUrl, market, null, language, path); | ||
const getCmsApiForCategoryIntro = (cmsUrl, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
let cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/?`; | ||
if (previewDraft) { | ||
let catIntroId = yield 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 = yield getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
return cmsApi; | ||
}; | ||
return cmsApi; | ||
}); | ||
/* | ||
@@ -313,14 +339,9 @@ 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 requestUrls = catIntroPaths.map( | ||
(catPath) => cmsApi + jsUrl.toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft)) | ||
); | ||
return requestUrls; | ||
}; | ||
const generateRequestUrls = (cmsUrl, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
const catIntroPaths = getCategoryIntroPaths(market, path); | ||
const cmsApi = yield getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); | ||
// Request Url with country filter | ||
const requestUrls = catIntroPaths.map((catPath) => cmsApi + jsUrl.toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft))); | ||
return requestUrls; | ||
}); | ||
/* | ||
@@ -338,41 +359,36 @@ Function getCategoryIntro | ||
*/ | ||
const getCategoryIntro = async (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => { | ||
const fetchOptions = previewDraft | ||
? { | ||
headers: { | ||
"content-length": "0", | ||
authorization: `Basic ${basicAuthToken}`, | ||
}, | ||
} | ||
: {}; | ||
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?.length); | ||
/* | ||
The Drupal fetch returns an array of Cat Intros | ||
Womens and mens CDPs returns 2 Cat Intros | ||
Unisex CDPs return 2 or 3 Cat Intros depending if a unisex Cat Intro exists | ||
*/ | ||
// If the CDP is unisex AND a unisex Cat Intro exists then return the second item | ||
// e.g. ["mens", "rebird", "mens/rebird"] return "rebird" | ||
// Note: The order of the array never changes | ||
if (!path.includes("mens") && catIntrosNoEmpties?.length === 3) { | ||
return catIntrosNoEmpties[catIntrosNoEmpties?.length - 2]; | ||
const getCategoryIntro = (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
const fetchOptions = previewDraft | ||
? { | ||
headers: { | ||
"content-length": "0", | ||
authorization: `Basic ${basicAuthToken}`, | ||
}, | ||
} | ||
: {}; | ||
const requestUrls = yield generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
try { | ||
const catIntros = yield 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); | ||
/* | ||
The Drupal fetch returns an array of Cat Intros | ||
Womens and mens CDPs returns 2 Cat Intros | ||
Unisex CDPs return 2 or 3 Cat Intros depending if a unisex Cat Intro exists | ||
*/ | ||
// If the CDP is unisex AND a unisex Cat Intro exists then return the second item | ||
// e.g. ["mens", "rebird", "mens/rebird"] return "rebird" | ||
// 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 | ||
// 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]; | ||
} | ||
// 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?.length - 1]; | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}; | ||
catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}); | ||
/** | ||
@@ -388,15 +404,13 @@ * Query CMS message content type, filtering by market, country, language and field name. | ||
*/ | ||
const getCmsMessageByFieldName = async (cmsUrl, market, country, language, messageFieldName) => { | ||
let queryStrings = `filter[field_market]=${market}&filter[field_allowed_countries]=${country}&fields[node--message]=field_content`; | ||
if (messageFieldName) { | ||
queryStrings += `&filter[field_name]=${messageFieldName}`; | ||
} | ||
const getCmsMessageByFieldName = (cmsUrl, market, country, language, messageFieldName) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b; | ||
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}`; | ||
const data = yield getDrupalData(messageUrl); | ||
return (_b = (_a = data === null || data === void 0 ? void 0 : data[0]) === null || _a === void 0 ? void 0 : _a.field_content.processed) !== null && _b !== void 0 ? _b : null; | ||
}); | ||
const messageUrl = `${cmsUrl}/${language}/jsonapi/node/message?${queryStrings}`; | ||
const data = await getDrupalData(messageUrl); | ||
return data?.[0]?.field_content.processed ?? null; | ||
}; | ||
exports.GetDrupalData = GetDrupalData; | ||
@@ -403,0 +417,0 @@ exports.flattenCMSJson = flattenCMSJson; |
import { toQueryParams } from '@arcteryx/js-url'; | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P ? value : new P(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
/*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 | ||
if (item.relationships) { | ||
for (const key in item.relationships) { | ||
// 4) iterate over the items in the related field's data array (or if not an array, skip straight to processing the object) | ||
if (item.relationships[key].data instanceof Array === false) { | ||
// Sometimes the relationships[key] is just an object, sometimes it's an array. Let's always treat them as arrays. | ||
item.relationships[key].data = [item.relationships[key].data]; | ||
// 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 | ||
if (item.relationships) { | ||
for (const key in item.relationships) { | ||
// 4) iterate over the items in the related field's data array (or if not an array, skip straight to processing the object) | ||
if (item.relationships[key].data instanceof Array === false) { | ||
// Sometimes the relationships[key] is just an object, sometimes it's an array. Let's always treat them as arrays. | ||
item.relationships[key].data = [item.relationships[key].data]; | ||
} | ||
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 === null || related === void 0 ? void 0 : related.id) === include.id)[0], included); | ||
}); | ||
} | ||
} | ||
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 | ||
); | ||
}); | ||
} | ||
return returnObject; | ||
} | ||
return returnObject; | ||
} | ||
} | ||
/* eslint-enable*/ | ||
const queryOptionsForCategoryIntros = (market, country, path, previewDraft) => { | ||
const options = {}; | ||
if (previewDraft) { | ||
options.resourceVersion = "rel:working-copy"; | ||
} else { | ||
options["filter[field_market]"] = market.toUpperCase(); | ||
options["filter[field_cat_intro_category_handle]"] = path; | ||
} | ||
if (country !== null) { | ||
options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} else { | ||
options["filter[country-filter][condition][path]"] = "field_allowed_countries"; | ||
options["filter[country-filter][condition][operator]"] = "IS NULL"; | ||
} | ||
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"; | ||
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"; | ||
options["fields[paragraph--cta]"] = "field_link,field_style"; | ||
options["fields[paragraph--product_callout_flag]"] = | ||
"field_callout_flag_colour,field_callout_flag_type,field_callout_flag_x_percentage,field_callout_flag_y_percentage,field_callout_flag_product,field_callout_arrow_direction"; | ||
options["fields[node--reusable_string]"] = "field_reusable_string_text"; | ||
options["fields[file--file]"] = "uri"; | ||
options["fields[node--product]"] = "field_product_product_name,field_product_product_url"; | ||
return options; | ||
const options = {}; | ||
if (previewDraft) { | ||
options.resourceVersion = "rel:working-copy"; | ||
} | ||
else { | ||
options["filter[field_market]"] = market.toUpperCase(); | ||
options["filter[field_cat_intro_category_handle]"] = path; | ||
} | ||
if (country !== null) { | ||
options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} | ||
else { | ||
options["filter[country-filter][condition][path]"] = "field_allowed_countries"; | ||
options["filter[country-filter][condition][operator]"] = "IS NULL"; | ||
} | ||
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"; | ||
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"; | ||
options["fields[paragraph--cta]"] = "field_link,field_style"; | ||
options["fields[paragraph--product_callout_flag]"] = | ||
"field_callout_flag_colour,field_callout_flag_type,field_callout_flag_x_percentage,field_callout_flag_y_percentage,field_callout_flag_product,field_callout_arrow_direction"; | ||
options["fields[node--reusable_string]"] = "field_reusable_string_text"; | ||
options["fields[file--file]"] = "uri"; | ||
options["fields[node--product]"] = "field_product_product_name,field_product_product_url"; | ||
return options; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
return toQueryParams({ | ||
"filter[field_path]": path, | ||
"filter[field_market]": market.toUpperCase(), | ||
"filter[field_allowed_countries]": country.toUpperCase(), | ||
"fields[node--simple_page]": | ||
"title,field_meta_page_title,field_meta_page_description,field_simple_page_sections,field_simple_page_breadcrumbs,field_simple_page_title,field_share_image", | ||
include: | ||
"field_simple_page_sections,field_simple_page_title,field_simple_page_sections.field_simple_blocks,field_simple_page_sections.field_simple_blocks.field_background_image,field_simple_page_sections.field_simple_blocks.field_cta,field_share_image", | ||
"fields[paragraph--simple_page_section]": | ||
"field_simple_page_section_header,field_simple_page_section_layout,field_simple_blocks", | ||
"fields[node--simple_block]": | ||
"title,field_background_image_x_focal,field_background_image_y_focal,field_content_align_x,field_content_align_y,field_header_text,field_header_svg,field_sub_header_text,field_video_link,field_video_background,field_background_image,field_cta,field_raw", | ||
"fields[file--file]": "uri", | ||
"fields[paragraph--cta]": "field_link,field_style", | ||
"fields[node--reusable_string]": "field_reusable_string_text", | ||
}); | ||
return toQueryParams({ | ||
"filter[field_path]": path, | ||
"filter[field_market]": market.toUpperCase(), | ||
"filter[field_allowed_countries]": country.toUpperCase(), | ||
"fields[node--simple_page]": "title,field_meta_page_title,field_meta_page_description,field_simple_page_sections,field_simple_page_breadcrumbs,field_simple_page_title,field_share_image", | ||
include: "field_simple_page_sections,field_simple_page_title,field_simple_page_sections.field_simple_blocks,field_simple_page_sections.field_simple_blocks.field_background_image,field_simple_page_sections.field_simple_blocks.field_cta,field_share_image", | ||
"fields[paragraph--simple_page_section]": "field_simple_page_section_header,field_simple_page_section_layout,field_simple_blocks", | ||
"fields[node--simple_block]": "title,field_background_image_x_focal,field_background_image_y_focal,field_content_align_x,field_content_align_y,field_header_text,field_header_svg,field_sub_header_text,field_video_link,field_video_background,field_background_image,field_cta,field_raw", | ||
"fields[file--file]": "uri", | ||
"fields[paragraph--cta]": "field_link,field_style", | ||
"fields[node--reusable_string]": "field_reusable_string_text", | ||
}); | ||
} | ||
function queryOptionsForBasicBlock(title) { | ||
return toQueryParams({ | ||
"filter[title]": title, | ||
"fields[node--basic_block]": "title,field_basic_content,field_css_class_names", | ||
}); | ||
return toQueryParams({ | ||
"filter[title]": title, | ||
"fields[node--basic_block]": "title,field_basic_content,field_css_class_names", | ||
}); | ||
} | ||
function queryOptionsForReusableString(stringsArray) { | ||
const filterObj = {}; | ||
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); | ||
const filterObj = {}; | ||
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); | ||
} | ||
/* | ||
@@ -104,21 +134,22 @@ Function getDrupalData | ||
*/ | ||
async function getDrupalData(fetchUrl, fetchOptions) { | ||
return await fetch(fetchUrl, fetchOptions) | ||
.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)); | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
return null; | ||
function getDrupalData(fetchUrl, fetchOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield fetch(fetchUrl, fetchOptions) | ||
.then((resp) => resp.json()) | ||
.then((json) => { | ||
var _a; | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return (_a = json.data) === null || _a === void 0 ? void 0 : _a.map((label) => flattenCMSJson(label, json.included)); | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
return null; | ||
}); | ||
}); | ||
} | ||
function GetDrupalData(fetchUrl) { | ||
return getDrupalData(fetchUrl); | ||
return getDrupalData(fetchUrl); | ||
} | ||
/* | ||
@@ -134,7 +165,8 @@ Function getSimplePage | ||
*/ | ||
async function getSimplePage(cmsUrl, market, country, language, path) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForSimplePages(market, country, path)); | ||
function getSimplePage(cmsUrl, market, country, language, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForSimplePages(market, country, path)); | ||
}); | ||
} | ||
/* | ||
@@ -148,7 +180,8 @@ Function getBasicBlock | ||
*/ | ||
async function getBasicBlock(cmsUrl, title, language) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForBasicBlock(title)); | ||
function getBasicBlock(cmsUrl, title, language) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForBasicBlock(title)); | ||
}); | ||
} | ||
/* | ||
@@ -162,7 +195,8 @@ Function getReusableData | ||
*/ | ||
async function getReusableData(cmsUrl, stringsArray, language) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForReusableString(stringsArray)); | ||
function getReusableData(cmsUrl, stringsArray, language) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForReusableString(stringsArray)); | ||
}); | ||
} | ||
/* | ||
@@ -180,12 +214,11 @@ Function getReusableText | ||
function getReusableText(titleKey, resuableStrings) { | ||
if (typeof resuableStrings !== "undefined") { | ||
for (let i = 0; i < resuableStrings.length; i++) { | ||
if (resuableStrings[i].title === titleKey) { | ||
return resuableStrings[i].field_reusable_string_text; | ||
} | ||
if (typeof resuableStrings !== "undefined") { | ||
for (let i = 0; i < resuableStrings.length; i++) { | ||
if (resuableStrings[i].title === titleKey) { | ||
return resuableStrings[i].field_reusable_string_text; | ||
} | ||
} | ||
} | ||
} | ||
return titleKey; | ||
return titleKey; | ||
} | ||
/* | ||
@@ -199,8 +232,9 @@ Function getCsPhoneNumber | ||
*/ | ||
async function getCsPhoneNumber(cmsUrl, market, country) { | ||
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`; | ||
const data = await GetDrupalData(drupalUrl); | ||
return data?.length ? data[0].title : null; | ||
function getCsPhoneNumber(cmsUrl, market, country) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
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`; | ||
const data = yield GetDrupalData(drupalUrl); | ||
return (data === null || data === void 0 ? void 0 : data.length) ? data[0].title : null; | ||
}); | ||
} | ||
/* | ||
@@ -216,18 +250,15 @@ 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) => json?.data[0]?.id) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
return null; | ||
const getCategoryIntroId = (cmsUrl, market, country, language, path) => __awaiter(void 0, void 0, void 0, function* () { | ||
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 yield fetch(requestUrl) | ||
.then((resp) => resp.json()) | ||
.then((json) => { var _a; return (_a = json === null || json === void 0 ? void 0 : json.data[0]) === null || _a === void 0 ? void 0 : _a.id; }) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
return null; | ||
}); | ||
}; | ||
}); | ||
/* | ||
@@ -241,30 +272,28 @@ Function getCategoryIntroPaths | ||
const getCategoryIntroPaths = (market, path) => { | ||
let str = ""; | ||
const paths = path.split("/").map((p, i) => { | ||
// ARC.COM GENDER SPECIFIC OVERRIDES | ||
if (market === "outdoor" || market === "sale") { | ||
if (!path.split("/").includes("womens") && !path.split("/").includes("mens") && i === 0) { | ||
str += `${p}`; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
str += `${p}`; | ||
} else if (i !== 0) { | ||
str += `/${p}`; | ||
} | ||
} else if (market == "urban" || market == "leaf") { | ||
str += p; | ||
let str = ""; | ||
const paths = path.split("/").map((p, i) => { | ||
// ARC.COM GENDER SPECIFIC OVERRIDES | ||
if (market === "outdoor" || market === "sale") { | ||
if (!path.split("/").includes("womens") && !path.split("/").includes("mens") && i === 0) { | ||
str += `${p}`; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
str += `${p}`; | ||
} | ||
else if (i !== 0) { | ||
str += `/${p}`; | ||
} | ||
} | ||
else if (market == "urban" || market == "leaf") { | ||
str += p; | ||
} | ||
return str; | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
paths.splice(1, 0, `mens/${path}`); | ||
paths.unshift("mens"); | ||
} | ||
return str; | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
paths.splice(1, 0, `mens/${path}`); | ||
paths.unshift("mens"); | ||
} | ||
return paths; | ||
return paths; | ||
}; | ||
/* | ||
@@ -281,17 +310,14 @@ 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 | ||
if (catIntroId === null || !catIntroId) { | ||
catIntroId = await getCategoryIntroId(cmsUrl, market, null, language, path); | ||
const getCmsApiForCategoryIntro = (cmsUrl, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
let cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/?`; | ||
if (previewDraft) { | ||
let catIntroId = yield 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 = yield getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
return cmsApi; | ||
}; | ||
return cmsApi; | ||
}); | ||
/* | ||
@@ -308,14 +334,9 @@ 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 requestUrls = catIntroPaths.map( | ||
(catPath) => cmsApi + toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft)) | ||
); | ||
return requestUrls; | ||
}; | ||
const generateRequestUrls = (cmsUrl, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
const catIntroPaths = getCategoryIntroPaths(market, path); | ||
const cmsApi = yield 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; | ||
}); | ||
/* | ||
@@ -333,41 +354,36 @@ Function getCategoryIntro | ||
*/ | ||
const getCategoryIntro = async (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => { | ||
const fetchOptions = previewDraft | ||
? { | ||
headers: { | ||
"content-length": "0", | ||
authorization: `Basic ${basicAuthToken}`, | ||
}, | ||
} | ||
: {}; | ||
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?.length); | ||
/* | ||
The Drupal fetch returns an array of Cat Intros | ||
Womens and mens CDPs returns 2 Cat Intros | ||
Unisex CDPs return 2 or 3 Cat Intros depending if a unisex Cat Intro exists | ||
*/ | ||
// If the CDP is unisex AND a unisex Cat Intro exists then return the second item | ||
// e.g. ["mens", "rebird", "mens/rebird"] return "rebird" | ||
// Note: The order of the array never changes | ||
if (!path.includes("mens") && catIntrosNoEmpties?.length === 3) { | ||
return catIntrosNoEmpties[catIntrosNoEmpties?.length - 2]; | ||
const getCategoryIntro = (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
const fetchOptions = previewDraft | ||
? { | ||
headers: { | ||
"content-length": "0", | ||
authorization: `Basic ${basicAuthToken}`, | ||
}, | ||
} | ||
: {}; | ||
const requestUrls = yield generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
try { | ||
const catIntros = yield 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); | ||
/* | ||
The Drupal fetch returns an array of Cat Intros | ||
Womens and mens CDPs returns 2 Cat Intros | ||
Unisex CDPs return 2 or 3 Cat Intros depending if a unisex Cat Intro exists | ||
*/ | ||
// If the CDP is unisex AND a unisex Cat Intro exists then return the second item | ||
// e.g. ["mens", "rebird", "mens/rebird"] return "rebird" | ||
// 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 | ||
// 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]; | ||
} | ||
// 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?.length - 1]; | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}; | ||
catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}); | ||
/** | ||
@@ -383,16 +399,14 @@ * Query CMS message content type, filtering by market, country, language and field name. | ||
*/ | ||
const getCmsMessageByFieldName = async (cmsUrl, market, country, language, messageFieldName) => { | ||
let queryStrings = `filter[field_market]=${market}&filter[field_allowed_countries]=${country}&fields[node--message]=field_content`; | ||
if (messageFieldName) { | ||
queryStrings += `&filter[field_name]=${messageFieldName}`; | ||
} | ||
const getCmsMessageByFieldName = (cmsUrl, market, country, language, messageFieldName) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b; | ||
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}`; | ||
const data = yield getDrupalData(messageUrl); | ||
return (_b = (_a = data === null || data === void 0 ? void 0 : data[0]) === null || _a === void 0 ? void 0 : _a.field_content.processed) !== null && _b !== void 0 ? _b : null; | ||
}); | ||
const messageUrl = `${cmsUrl}/${language}/jsonapi/node/message?${queryStrings}`; | ||
const data = await getDrupalData(messageUrl); | ||
return data?.[0]?.field_content.processed ?? null; | ||
}; | ||
export { GetDrupalData, flattenCMSJson, generateRequestUrls, getBasicBlock, getCategoryIntro, getCategoryIntroId, getCategoryIntroPaths, getCmsApiForCategoryIntro, getCmsMessageByFieldName, getCsPhoneNumber, getDrupalData, getReusableData, getReusableText, getSimplePage, queryOptionsForBasicBlock, queryOptionsForCategoryIntros, queryOptionsForReusableString, queryOptionsForSimplePages }; | ||
//# sourceMappingURL=index.js.map |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@arcteryx/js-url')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@arcteryx/js-url'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (function () { | ||
var current = global.ARCTERYX && global.ARCTERYX.jscms; | ||
var exports = (global.ARCTERYX = global.ARCTERYX || {}, global.ARCTERYX.jscms = {}); | ||
factory(exports, global.jsUrl); | ||
exports.noConflict = function () { global.ARCTERYX.jscms = current; return exports; }; | ||
})()); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@arcteryx/js-url')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@arcteryx/js-url'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (function () { | ||
var current = global.ARCTERYX && global.ARCTERYX.jscms; | ||
var exports = (global.ARCTERYX = global.ARCTERYX || {}, global.ARCTERYX.jscms = {}); | ||
factory(exports, global.jsUrl); | ||
exports.noConflict = function () { global.ARCTERYX.jscms = current; return exports; }; | ||
})()); | ||
})(this, (function (exports, jsUrl) { 'use strict'; | ||
/*eslint-disable*/ | ||
function flattenCMSJson(item, included) { | ||
// 2) Extracts it's "attributes" values into the return object | ||
if (typeof item !== "undefined") { | ||
const returnObject = item.attributes; | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
// 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) { | ||
for (const key in item.relationships) { | ||
// 4) iterate over the items in the related field's data array (or if not an array, skip straight to processing the object) | ||
if (item.relationships[key].data instanceof Array === false) { | ||
// Sometimes the relationships[key] is just an object, sometimes it's an array. Let's always treat them as arrays. | ||
item.relationships[key].data = [item.relationships[key].data]; | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P ? value : new P(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
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 | ||
); | ||
}); | ||
} | ||
} | ||
return returnObject; | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
} | ||
/* eslint-enable*/ | ||
const queryOptionsForCategoryIntros = (market, country, path, previewDraft) => { | ||
const options = {}; | ||
if (previewDraft) { | ||
options.resourceVersion = "rel:working-copy"; | ||
} else { | ||
options["filter[field_market]"] = market.toUpperCase(); | ||
options["filter[field_cat_intro_category_handle]"] = path; | ||
/*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 | ||
if (item.relationships) { | ||
for (const key in item.relationships) { | ||
// 4) iterate over the items in the related field's data array (or if not an array, skip straight to processing the object) | ||
if (item.relationships[key].data instanceof Array === false) { | ||
// Sometimes the relationships[key] is just an object, sometimes it's an array. Let's always treat them as arrays. | ||
item.relationships[key].data = [item.relationships[key].data]; | ||
} | ||
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 === null || related === void 0 ? void 0 : related.id) === include.id)[0], included); | ||
}); | ||
} | ||
} | ||
return returnObject; | ||
} | ||
} | ||
if (country !== null) { | ||
options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} else { | ||
options["filter[country-filter][condition][path]"] = "field_allowed_countries"; | ||
options["filter[country-filter][condition][operator]"] = "IS NULL"; | ||
/* eslint-enable*/ | ||
const queryOptionsForCategoryIntros = (market, country, path, previewDraft) => { | ||
const options = {}; | ||
if (previewDraft) { | ||
options.resourceVersion = "rel:working-copy"; | ||
} | ||
else { | ||
options["filter[field_market]"] = market.toUpperCase(); | ||
options["filter[field_cat_intro_category_handle]"] = path; | ||
} | ||
if (country !== null) { | ||
options["filter[field_allowed_countries]"] = country.toUpperCase(); | ||
} | ||
else { | ||
options["filter[country-filter][condition][path]"] = "field_allowed_countries"; | ||
options["filter[country-filter][condition][operator]"] = "IS NULL"; | ||
} | ||
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"; | ||
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"; | ||
options["fields[paragraph--cta]"] = "field_link,field_style"; | ||
options["fields[paragraph--product_callout_flag]"] = | ||
"field_callout_flag_colour,field_callout_flag_type,field_callout_flag_x_percentage,field_callout_flag_y_percentage,field_callout_flag_product,field_callout_arrow_direction"; | ||
options["fields[node--reusable_string]"] = "field_reusable_string_text"; | ||
options["fields[file--file]"] = "uri"; | ||
options["fields[node--product]"] = "field_product_product_name,field_product_product_url"; | ||
return options; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
return jsUrl.toQueryParams({ | ||
"filter[field_path]": path, | ||
"filter[field_market]": market.toUpperCase(), | ||
"filter[field_allowed_countries]": country.toUpperCase(), | ||
"fields[node--simple_page]": "title,field_meta_page_title,field_meta_page_description,field_simple_page_sections,field_simple_page_breadcrumbs,field_simple_page_title,field_share_image", | ||
include: "field_simple_page_sections,field_simple_page_title,field_simple_page_sections.field_simple_blocks,field_simple_page_sections.field_simple_blocks.field_background_image,field_simple_page_sections.field_simple_blocks.field_cta,field_share_image", | ||
"fields[paragraph--simple_page_section]": "field_simple_page_section_header,field_simple_page_section_layout,field_simple_blocks", | ||
"fields[node--simple_block]": "title,field_background_image_x_focal,field_background_image_y_focal,field_content_align_x,field_content_align_y,field_header_text,field_header_svg,field_sub_header_text,field_video_link,field_video_background,field_background_image,field_cta,field_raw", | ||
"fields[file--file]": "uri", | ||
"fields[paragraph--cta]": "field_link,field_style", | ||
"fields[node--reusable_string]": "field_reusable_string_text", | ||
}); | ||
} | ||
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"; | ||
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"; | ||
options["fields[paragraph--cta]"] = "field_link,field_style"; | ||
options["fields[paragraph--product_callout_flag]"] = | ||
"field_callout_flag_colour,field_callout_flag_type,field_callout_flag_x_percentage,field_callout_flag_y_percentage,field_callout_flag_product,field_callout_arrow_direction"; | ||
options["fields[node--reusable_string]"] = "field_reusable_string_text"; | ||
options["fields[file--file]"] = "uri"; | ||
options["fields[node--product]"] = "field_product_product_name,field_product_product_url"; | ||
return options; | ||
}; | ||
function queryOptionsForSimplePages(market, country, path = "") { | ||
return jsUrl.toQueryParams({ | ||
"filter[field_path]": path, | ||
"filter[field_market]": market.toUpperCase(), | ||
"filter[field_allowed_countries]": country.toUpperCase(), | ||
"fields[node--simple_page]": | ||
"title,field_meta_page_title,field_meta_page_description,field_simple_page_sections,field_simple_page_breadcrumbs,field_simple_page_title,field_share_image", | ||
include: | ||
"field_simple_page_sections,field_simple_page_title,field_simple_page_sections.field_simple_blocks,field_simple_page_sections.field_simple_blocks.field_background_image,field_simple_page_sections.field_simple_blocks.field_cta,field_share_image", | ||
"fields[paragraph--simple_page_section]": | ||
"field_simple_page_section_header,field_simple_page_section_layout,field_simple_blocks", | ||
"fields[node--simple_block]": | ||
"title,field_background_image_x_focal,field_background_image_y_focal,field_content_align_x,field_content_align_y,field_header_text,field_header_svg,field_sub_header_text,field_video_link,field_video_background,field_background_image,field_cta,field_raw", | ||
"fields[file--file]": "uri", | ||
"fields[paragraph--cta]": "field_link,field_style", | ||
"fields[node--reusable_string]": "field_reusable_string_text", | ||
}); | ||
} | ||
function queryOptionsForBasicBlock(title) { | ||
return jsUrl.toQueryParams({ | ||
"filter[title]": title, | ||
"fields[node--basic_block]": "title,field_basic_content,field_css_class_names", | ||
}); | ||
} | ||
function queryOptionsForReusableString(stringsArray) { | ||
const filterObj = {}; | ||
filterObj["filter[title][operator]"] = "IN"; | ||
for (let i = 0; i < stringsArray.length; i++) { | ||
filterObj[`filter[title][value][${i}]`] = stringsArray[i]; | ||
function queryOptionsForBasicBlock(title) { | ||
return jsUrl.toQueryParams({ | ||
"filter[title]": title, | ||
"fields[node--basic_block]": "title,field_basic_content,field_css_class_names", | ||
}); | ||
} | ||
filterObj["fields[node--reusable_string]"] = "title,field_reusable_string_text"; | ||
return jsUrl.toQueryParams(filterObj); | ||
} | ||
/* | ||
Function getDrupalData | ||
Description: Get JSON:API response from Drupal and will flatten the JSON:API response | ||
@param fetchUrl - The JSON:API Drupal url containing the necessary filters and parameters | ||
Example: https://cms.arcteryx.com/en/jsonapi/node/basic_block/?filter%5Btitle%5D=OUTDOOR%20Empty%20Cart&fields%5Bnode--basic_block%5D=title%2Cfield_basic_content%2Cfield_css_class_names | ||
*/ | ||
async function getDrupalData(fetchUrl, fetchOptions) { | ||
return await fetch(fetchUrl, fetchOptions) | ||
.then((resp) => resp.json()) | ||
.then((json) => { | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
function queryOptionsForReusableString(stringsArray) { | ||
const filterObj = {}; | ||
filterObj["filter[title][operator]"] = "IN"; | ||
for (let i = 0; i < stringsArray.length; i++) { | ||
filterObj[`filter[title][value][${i}]`] = stringsArray[i]; | ||
} | ||
return json.data?.map((label) => flattenCMSJson(label, json.included)); | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
return null; | ||
}); | ||
} | ||
filterObj["fields[node--reusable_string]"] = "title,field_reusable_string_text"; | ||
return jsUrl.toQueryParams(filterObj); | ||
} | ||
/* | ||
Function getDrupalData | ||
Description: Get JSON:API response from Drupal and will flatten the JSON:API response | ||
function GetDrupalData(fetchUrl) { | ||
return getDrupalData(fetchUrl); | ||
} | ||
@param fetchUrl - The JSON:API Drupal url containing the necessary filters and parameters | ||
Example: https://cms.arcteryx.com/en/jsonapi/node/basic_block/?filter%5Btitle%5D=OUTDOOR%20Empty%20Cart&fields%5Bnode--basic_block%5D=title%2Cfield_basic_content%2Cfield_css_class_names | ||
*/ | ||
function getDrupalData(fetchUrl, fetchOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield fetch(fetchUrl, fetchOptions) | ||
.then((resp) => resp.json()) | ||
.then((json) => { | ||
var _a; | ||
if (!Array.isArray(json.data)) { | ||
json.data = new Array(json.data); | ||
} | ||
return (_a = json.data) === null || _a === void 0 ? void 0 : _a.map((label) => flattenCMSJson(label, json.included)); | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
return null; | ||
}); | ||
}); | ||
} | ||
function GetDrupalData(fetchUrl) { | ||
return getDrupalData(fetchUrl); | ||
} | ||
/* | ||
Function getSimplePage | ||
Description: Get data for a 'Simple Page' ContentType from Drupal based | ||
/* | ||
Function getSimplePage | ||
Description: Get data for a 'Simple Page' ContentType from Drupal based | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - outdoor, urban or leaf | ||
@param country - Country Code (e.g. ca, us) | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - The String path saved in the path field in Drupal (e.g. /home, /help) | ||
*/ | ||
function getSimplePage(cmsUrl, market, country, language, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForSimplePages(market, country, path)); | ||
}); | ||
} | ||
/* | ||
Function getBasicBlock | ||
Description: Get data for a 'Basic Block' ContentType from Drupal based | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - outdoor, urban or leaf | ||
@param country - Country Code (e.g. ca, us) | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - The String path saved in the path field in Drupal (e.g. /home, /help) | ||
*/ | ||
async function getSimplePage(cmsUrl, market, country, language, path) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/simple_page/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForSimplePages(market, country, path)); | ||
} | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param title - String saved in the Title field in Drupal | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
*/ | ||
function getBasicBlock(cmsUrl, title, language) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForBasicBlock(title)); | ||
}); | ||
} | ||
/* | ||
Function getReusableData | ||
Description: Get data for a 'Resuable Strings' ContentType from Drupal based | ||
/* | ||
Function getBasicBlock | ||
Description: Get data for a 'Basic Block' ContentType from Drupal based | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param stringsArray - Array of Strings that represents the title key in Drupal (e.g. ["New", "Revised", "Pro Exlusive"]) | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
*/ | ||
function getReusableData(cmsUrl, stringsArray, language) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
return yield GetDrupalData(cmsApi + queryOptionsForReusableString(stringsArray)); | ||
}); | ||
} | ||
/* | ||
Function getReusableText | ||
Description: Returns requested text value (field_reusable_string_text) if exists in the reusable_string object from Drupal | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param title - String saved in the Title field in Drupal | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
*/ | ||
async function getBasicBlock(cmsUrl, title, language) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/basic_block/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForBasicBlock(title)); | ||
} | ||
/* | ||
Function getReusableData | ||
Description: Get data for a 'Resuable Strings' ContentType from Drupal based | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param stringsArray - Array of Strings that represents the title key in Drupal (e.g. ["New", "Revised", "Pro Exlusive"]) | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
*/ | ||
async function getReusableData(cmsUrl, stringsArray, language) { | ||
const cmsApi = `${cmsUrl}/${language}/jsonapi/node/reusable_string/?`; | ||
return await GetDrupalData(cmsApi + queryOptionsForReusableString(stringsArray)); | ||
} | ||
/* | ||
Function getReusableText | ||
Description: Returns requested text value (field_reusable_string_text) if exists in the reusable_string object from Drupal | ||
@param titleKey - String saved in the Title field in Drupal | ||
@param resuableStrings - Array of node--reusable_string object which has title and field_reusable_string_text attributes | ||
Example; [ | ||
{ title: "Hello World", field_reusable_string_text: "Hello World" }, | ||
{ title: "Test", field_reusable_string_text: "This a test" } | ||
] | ||
*/ | ||
function getReusableText(titleKey, resuableStrings) { | ||
if (typeof resuableStrings !== "undefined") { | ||
for (let i = 0; i < resuableStrings.length; i++) { | ||
if (resuableStrings[i].title === titleKey) { | ||
return resuableStrings[i].field_reusable_string_text; | ||
@param titleKey - String saved in the Title field in Drupal | ||
@param resuableStrings - Array of node--reusable_string object which has title and field_reusable_string_text attributes | ||
Example; [ | ||
{ title: "Hello World", field_reusable_string_text: "Hello World" }, | ||
{ title: "Test", field_reusable_string_text: "This a test" } | ||
] | ||
*/ | ||
function getReusableText(titleKey, resuableStrings) { | ||
if (typeof resuableStrings !== "undefined") { | ||
for (let i = 0; i < resuableStrings.length; i++) { | ||
if (resuableStrings[i].title === titleKey) { | ||
return resuableStrings[i].field_reusable_string_text; | ||
} | ||
} | ||
} | ||
} | ||
return titleKey; | ||
} | ||
return titleKey; | ||
} | ||
/* | ||
Function getCsPhoneNumber | ||
Description: Returns the phone numver for Customer Service based on country and market | ||
/* | ||
Function getCsPhoneNumber | ||
Description: Returns the phone numver for Customer Service based on country and market | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - outdoor, urban or leaf | ||
@param country - Country Code (e.g. ca, us) | ||
*/ | ||
async function getCsPhoneNumber(cmsUrl, market, country) { | ||
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`; | ||
const data = await GetDrupalData(drupalUrl); | ||
return data?.length ? data[0].title : null; | ||
} | ||
/* | ||
Function getCategoryIntroId | ||
Description: Returns the node ID in Drupal for a Category Intro based on path | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - outdoor, urban or leaf | ||
@param country - Country Code (e.g. ca, us). If null, will query Drupal with no country filter | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - The String path saved in the path field in Drupal (e.g. womens/shell-jackets) | ||
*/ | ||
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`; | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - outdoor, urban or leaf | ||
@param country - Country Code (e.g. ca, us) | ||
*/ | ||
function getCsPhoneNumber(cmsUrl, market, country) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
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`; | ||
const data = yield GetDrupalData(drupalUrl); | ||
return (data === null || data === void 0 ? void 0 : data.length) ? data[0].title : null; | ||
}); | ||
} | ||
/* | ||
Function getCategoryIntroId | ||
Description: Returns the node ID in Drupal for a Category Intro based on path | ||
return await fetch(requestUrl) | ||
.then((resp) => resp.json()) | ||
.then((json) => json?.data[0]?.id) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
return null; | ||
}); | ||
}; | ||
@param cmsUrl - Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - outdoor, urban or leaf | ||
@param country - Country Code (e.g. ca, us). If null, will query Drupal with no country filter | ||
@param language - Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - The String path saved in the path field in Drupal (e.g. womens/shell-jackets) | ||
*/ | ||
const getCategoryIntroId = (cmsUrl, market, country, language, path) => __awaiter(void 0, void 0, void 0, function* () { | ||
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 yield fetch(requestUrl) | ||
.then((resp) => resp.json()) | ||
.then((json) => { var _a; return (_a = json === null || json === void 0 ? void 0 : json.data[0]) === null || _a === void 0 ? void 0 : _a.id; }) | ||
.catch((err) => { | ||
console.error(`Fetch error for ${requestUrl}`, err); | ||
return null; | ||
}); | ||
}); | ||
/* | ||
Function getCategoryIntroPaths | ||
Description: Returns an array of paths based on the url request | ||
/* | ||
Function getCategoryIntroPaths | ||
Description: Returns an array of paths based on the url request | ||
@param market - outdoor, urban or leaf | ||
@param path - The query slug from the url request (e.g. womens/shell-jackets) | ||
*/ | ||
const getCategoryIntroPaths = (market, path) => { | ||
let str = ""; | ||
const paths = path.split("/").map((p, i) => { | ||
// ARC.COM GENDER SPECIFIC OVERRIDES | ||
if (market === "outdoor" || market === "sale") { | ||
if (!path.split("/").includes("womens") && !path.split("/").includes("mens") && i === 0) { | ||
str += `${p}`; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
str += `${p}`; | ||
} | ||
else if (i !== 0) { | ||
str += `/${p}`; | ||
} | ||
} | ||
else if (market == "urban" || market == "leaf") { | ||
str += p; | ||
} | ||
return str; | ||
}); | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
paths.splice(1, 0, `mens/${path}`); | ||
paths.unshift("mens"); | ||
} | ||
return paths; | ||
}; | ||
/* | ||
Function: getCmsApiForCategoryIntro | ||
Description: Returns the base fetch URL to fetch a Category Intro from the CMS | ||
@param market - outdoor, urban or leaf | ||
@param path - The query slug from the url request (e.g. womens/shell-jackets) | ||
*/ | ||
const getCategoryIntroPaths = (market, path) => { | ||
let str = ""; | ||
const paths = path.split("/").map((p, i) => { | ||
// ARC.COM GENDER SPECIFIC OVERRIDES | ||
if (market === "outdoor" || market === "sale") { | ||
if (!path.split("/").includes("womens") && !path.split("/").includes("mens") && i === 0) { | ||
str += `${p}`; | ||
@param cmsUrl - (String) Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - (String) outdoor, urban or leaf | ||
@param country - (String) Country Code (e.g. ca, us) | ||
@param language - (String) Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - (String) path to Category Intro (e.g. mens/shell-jackets) | ||
@param previewDraft - (Boolean) If set to true will retrieve the draft version from Drupal | ||
*/ | ||
const getCmsApiForCategoryIntro = (cmsUrl, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
let cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/?`; | ||
if (previewDraft) { | ||
let catIntroId = yield 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 = yield getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
if (p == "mens" || p == "womens") { | ||
str += `${p}`; | ||
} else if (i !== 0) { | ||
str += `/${p}`; | ||
} | ||
} else if (market == "urban" || market == "leaf") { | ||
str += p; | ||
} | ||
return str; | ||
return cmsApi; | ||
}); | ||
/* | ||
Function: generateRequestUrls | ||
Description: Returns the list of fetch URLs to get the Category Intros from the CMS based on the query | ||
// Use mens as a fallback | ||
if (paths[0] !== "mens" && paths[0] !== "womens") { | ||
paths.splice(1, 0, `mens/${path}`); | ||
paths.unshift("mens"); | ||
} | ||
@param cmsUrl - (String) Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - (String) outdoor, urban or leaf | ||
@param country - (String) Country Code (e.g. ca, us) | ||
@param language - (String) Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - (String) path to Category Intro (e.g. mens/shell-jackets) | ||
@param previewDraft - (Boolean) If set to true will retrieve the draft version from Drupal | ||
*/ | ||
const generateRequestUrls = (cmsUrl, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
const catIntroPaths = getCategoryIntroPaths(market, path); | ||
const cmsApi = yield getCmsApiForCategoryIntro(cmsUrl, market, country, language, path, previewDraft); | ||
// Request Url with country filter | ||
const requestUrls = catIntroPaths.map((catPath) => cmsApi + jsUrl.toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft))); | ||
return requestUrls; | ||
}); | ||
/* | ||
Function getCategoryIntro | ||
Description: Returns the CategoryIntro data object from Drupal | ||
return paths; | ||
}; | ||
/* | ||
Function: getCmsApiForCategoryIntro | ||
Description: Returns the base fetch URL to fetch a Category Intro from the CMS | ||
@param cmsUrl - (String) Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - (String) outdoor, urban or leaf | ||
@param country - (String) Country Code (e.g. ca, us) | ||
@param language - (String) Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - (String) path to Category Intro (e.g. mens/shell-jackets) | ||
@param previewDraft - (Boolean) If set to true will retrieve the draft version from Drupal | ||
*/ | ||
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 | ||
if (catIntroId === null || !catIntroId) { | ||
catIntroId = await getCategoryIntroId(cmsUrl, market, null, language, path); | ||
} | ||
cmsApi = `${cmsUrl}/${language}/jsonapi/node/category_intro/${catIntroId}?`; | ||
} | ||
return cmsApi; | ||
}; | ||
/* | ||
Function: generateRequestUrls | ||
Description: Returns the list of fetch URLs to get the Category Intros from the CMS based on the query | ||
@param cmsUrl - (String) Drupal url (e.g. https://cms.arcteryx.com) | ||
@param market - (String) outdoor, urban or leaf | ||
@param country - (String) Country Code (e.g. ca, us) | ||
@param language - (String) Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - (String) path to Category Intro (e.g. mens/shell-jackets) | ||
@param previewDraft - (Boolean) If set to true will retrieve the draft version from Drupal | ||
*/ | ||
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 requestUrls = catIntroPaths.map( | ||
(catPath) => cmsApi + jsUrl.toQueryParams(queryOptionsForCategoryIntros(market, country, catPath, previewDraft)) | ||
); | ||
return requestUrls; | ||
}; | ||
/* | ||
Function getCategoryIntro | ||
Description: Returns the CategoryIntro data object from Drupal | ||
@param cmsUrl - (String) Drupal url (e.g. https://cms.arcteryx.com) | ||
@param basicAuthToken - (String) Base64 encoded credentials for CMS user with API role (e.g. apiuser) | ||
@param market - (String) outdoor, urban or leaf | ||
@param country - (String) Country Code (e.g. ca, us) | ||
@param language - (String) Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - (String) path to Category Intro (e.g. womens/shell-jackets, womens/insulated-jackets/sub-cat=casual) | ||
@param previewDraft - (Boolean) If set to true will retrieve the draft version from Drupal | ||
*/ | ||
const getCategoryIntro = async (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => { | ||
const fetchOptions = previewDraft | ||
? { | ||
headers: { | ||
"content-length": "0", | ||
authorization: `Basic ${basicAuthToken}`, | ||
}, | ||
@param cmsUrl - (String) Drupal url (e.g. https://cms.arcteryx.com) | ||
@param basicAuthToken - (String) Base64 encoded credentials for CMS user with API role (e.g. apiuser) | ||
@param market - (String) outdoor, urban or leaf | ||
@param country - (String) Country Code (e.g. ca, us) | ||
@param language - (String) Language code (e.g. "en", "fr", "de", "es", "no", "zh", "jp") | ||
@param path - (String) path to Category Intro (e.g. womens/shell-jackets, womens/insulated-jackets/sub-cat=casual) | ||
@param previewDraft - (Boolean) If set to true will retrieve the draft version from Drupal | ||
*/ | ||
const getCategoryIntro = (cmsUrl, basicAuthToken, market, country, language, path, previewDraft) => __awaiter(void 0, void 0, void 0, function* () { | ||
const fetchOptions = previewDraft | ||
? { | ||
headers: { | ||
"content-length": "0", | ||
authorization: `Basic ${basicAuthToken}`, | ||
}, | ||
} | ||
: {}; | ||
const requestUrls = yield generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
try { | ||
const catIntros = yield 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); | ||
/* | ||
The Drupal fetch returns an array of Cat Intros | ||
Womens and mens CDPs returns 2 Cat Intros | ||
Unisex CDPs return 2 or 3 Cat Intros depending if a unisex Cat Intro exists | ||
*/ | ||
// If the CDP is unisex AND a unisex Cat Intro exists then return the second item | ||
// e.g. ["mens", "rebird", "mens/rebird"] return "rebird" | ||
// 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 | ||
// 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]; | ||
} | ||
: {}; | ||
catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}); | ||
/** | ||
* Query CMS message content type, filtering by market, country, language and field name. | ||
* | ||
* @param {string} cmsUrl | ||
* @param {string} market | ||
* @param {string} country | ||
* @param {string} language | ||
* @param {string} messageFieldName | ||
* @returns {string|null} | ||
*/ | ||
const getCmsMessageByFieldName = (cmsUrl, market, country, language, messageFieldName) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b; | ||
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}`; | ||
const data = yield getDrupalData(messageUrl); | ||
return (_b = (_a = data === null || data === void 0 ? void 0 : data[0]) === null || _a === void 0 ? void 0 : _a.field_content.processed) !== null && _b !== void 0 ? _b : null; | ||
}); | ||
const requestUrls = await generateRequestUrls(cmsUrl, market, country, language, path, previewDraft); | ||
exports.GetDrupalData = GetDrupalData; | ||
exports.flattenCMSJson = flattenCMSJson; | ||
exports.generateRequestUrls = generateRequestUrls; | ||
exports.getBasicBlock = getBasicBlock; | ||
exports.getCategoryIntro = getCategoryIntro; | ||
exports.getCategoryIntroId = getCategoryIntroId; | ||
exports.getCategoryIntroPaths = getCategoryIntroPaths; | ||
exports.getCmsApiForCategoryIntro = getCmsApiForCategoryIntro; | ||
exports.getCmsMessageByFieldName = getCmsMessageByFieldName; | ||
exports.getCsPhoneNumber = getCsPhoneNumber; | ||
exports.getDrupalData = getDrupalData; | ||
exports.getReusableData = getReusableData; | ||
exports.getReusableText = getReusableText; | ||
exports.getSimplePage = getSimplePage; | ||
exports.queryOptionsForBasicBlock = queryOptionsForBasicBlock; | ||
exports.queryOptionsForCategoryIntros = queryOptionsForCategoryIntros; | ||
exports.queryOptionsForReusableString = queryOptionsForReusableString; | ||
exports.queryOptionsForSimplePages = queryOptionsForSimplePages; | ||
try { | ||
const catIntros = await Promise.all(requestUrls.map((url) => getDrupalData(url, fetchOptions))).then( | ||
(json) => json | ||
); | ||
const catIntrosNoEmpties = catIntros.filter((catIntro) => catIntro?.length); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/* | ||
The Drupal fetch returns an array of Cat Intros | ||
Womens and mens CDPs returns 2 Cat Intros | ||
Unisex CDPs return 2 or 3 Cat Intros depending if a unisex Cat Intro exists | ||
*/ | ||
// If the CDP is unisex AND a unisex Cat Intro exists then return the second item | ||
// e.g. ["mens", "rebird", "mens/rebird"] return "rebird" | ||
// Note: The order of the array never changes | ||
if (!path.includes("mens") && catIntrosNoEmpties?.length === 3) { | ||
return catIntrosNoEmpties[catIntrosNoEmpties?.length - 2]; | ||
} | ||
// 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?.length - 1]; | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Query CMS message content type, filtering by market, country, language and field name. | ||
* | ||
* @param {string} cmsUrl | ||
* @param {string} market | ||
* @param {string} country | ||
* @param {string} language | ||
* @param {string} messageFieldName | ||
* @returns {string|null} | ||
*/ | ||
const getCmsMessageByFieldName = async (cmsUrl, market, country, language, messageFieldName) => { | ||
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}`; | ||
const data = await getDrupalData(messageUrl); | ||
return data?.[0]?.field_content.processed ?? null; | ||
}; | ||
exports.GetDrupalData = GetDrupalData; | ||
exports.flattenCMSJson = flattenCMSJson; | ||
exports.generateRequestUrls = generateRequestUrls; | ||
exports.getBasicBlock = getBasicBlock; | ||
exports.getCategoryIntro = getCategoryIntro; | ||
exports.getCategoryIntroId = getCategoryIntroId; | ||
exports.getCategoryIntroPaths = getCategoryIntroPaths; | ||
exports.getCmsApiForCategoryIntro = getCmsApiForCategoryIntro; | ||
exports.getCmsMessageByFieldName = getCmsMessageByFieldName; | ||
exports.getCsPhoneNumber = getCsPhoneNumber; | ||
exports.getDrupalData = getDrupalData; | ||
exports.getReusableData = getReusableData; | ||
exports.getReusableText = getReusableText; | ||
exports.getSimplePage = getSimplePage; | ||
exports.queryOptionsForBasicBlock = queryOptionsForBasicBlock; | ||
exports.queryOptionsForCategoryIntros = queryOptionsForCategoryIntros; | ||
exports.queryOptionsForReusableString = queryOptionsForReusableString; | ||
exports.queryOptionsForSimplePages = queryOptionsForSimplePages; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@arcteryx/js-cms", | ||
"version": "2.4.0-alpha.0", | ||
"version": "2.4.0-test.0", | ||
"source": "src/index.js", | ||
@@ -23,3 +23,3 @@ "main": "dist/cjs/index.js", | ||
"license": "ISC", | ||
"gitHead": "62220ec46c7009487fa7953207a575eac9c02bb5", | ||
"gitHead": "bd85fc01cd3cd7b5b81f7d35889beec1d0dc2fe2", | ||
"files": [ | ||
@@ -26,0 +26,0 @@ "dist", |
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
217743
16
1660