Skip to content

Commit

Permalink
split by query param or header
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Rugh <[email protected]>
  • Loading branch information
sirugh committed Dec 20, 2024
1 parent 4fa7c7e commit 3d37b35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
11 changes: 6 additions & 5 deletions scripts/commerce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable import/prefer-default-export, import/no-cycle */
import { getConfigValue, getCookie, getHeaders } from './configs.js';
import {
getConfigValue, getCookie, getQueryParams,
} from './configs.js';
import { getConsent } from './scripts.js';

/* Common query fragments */
Expand All @@ -22,10 +24,9 @@ export const priceFieldsFragment = `fragment priceFields on ProductViewPrice {
export async function commerceEndpointWithQueryParams() {
// Set Query Parameters so they can be appended to the endpoint
const urlWithQueryParams = new URL(await getConfigValue('commerce-endpoint'));
await getHeaders('pdp').then((headers) => {
Object.keys(headers).forEach((key) => {
// TODO: is it OK to apply all headers as query param even if they include things like api-key and content-type which was not previously a query param?
urlWithQueryParams.searchParams.append(key, headers[key]);
await getQueryParams('pdp').then((params) => {
Object.keys(params).forEach((key) => {
urlWithQueryParams.searchParams.append(key, params[key]);
});
});
return urlWithQueryParams;
Expand Down
21 changes: 20 additions & 1 deletion scripts/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export const getConfigValue = async (configParam, environment) => {
return configElements.find((c) => c.key === configParam)?.value;
};

/** Retrieves headers from config entries like commerce.headers.pdp.my-header, etc and
/**
* Retrieves headers from config entries like commerce.headers.pdp.my-header, etc and
* returns as object of all headers like { my-header: value, ... }
*/
export const getHeaders = async (scope, environment) => {
Expand All @@ -97,6 +98,24 @@ export const getHeaders = async (scope, environment) => {
}, {});
};

/**
* Retrieves query params from config entries like commerce.queryparams.pdp.my-param, etc and
* returns as object of all params like { my-query-param: value, ... }
*/
export const getQueryParams = async (scope, environment) => {
const env = environment || calcEnvironment();
const config = await getConfigForEnvironment(env);
const configElements = config.data.filter((el) => el?.key.includes(`queryparam.${scope}`));

return configElements.reduce((obj, item) => {
let { key } = item;
if (key.includes(`commerce.queryparam.${scope}.`)) {
key = key.replace(`commerce.queryparam.${scope}.`, '');
}
return { ...obj, [key]: item.value };
}, {});
};

export const getCookie = (cookieName) => {
const cookies = document.cookie.split(';');
let foundValue;
Expand Down
1 change: 0 additions & 1 deletion scripts/initializers/pdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ await initializeDropin(async () => {
setEndpoint(await commerceEndpointWithQueryParams());

// Set Fetch Headers (Service)
// TODO: is it OK to apply all headers even though the commerceEndpointWithQueryParams also applies them?
setFetchGraphQlHeaders(await getHeaders('pdp'));

const sku = getSkuFromUrl();
Expand Down

0 comments on commit 3d37b35

Please sign in to comment.