Skip to content

Commit

Permalink
remove use of query params except for singular cache buster
Browse files Browse the repository at this point in the history
  • Loading branch information
sirugh committed Jan 15, 2025
1 parent 85d850f commit 4b62eda
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
10 changes: 3 additions & 7 deletions scripts/commerce.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/prefer-default-export, import/no-cycle */
import {
getConfigValue, getCookie, getHeaders, getQueryParams,
getConfigValue, getCookie, getHeaders,
} from './configs.js';
import { getConsent } from './scripts.js';

Expand All @@ -22,13 +22,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 getQueryParams('cs').then((params) => {
Object.keys(params).forEach((key) => {
if (params[key]) urlWithQueryParams.searchParams.append(key, params[key]);
});
});
// Set some query parameters for use as a cache-buster. No other purpose.
urlWithQueryParams.searchParams.append('ac-storecode', await getConfigValue('commerce.headers.cs.Magento-Store-Code'));
return urlWithQueryParams;
}

Expand Down
18 changes: 0 additions & 18 deletions scripts/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,6 @@ export const getHeaders = async (scope, environment) => {
}, {});
};

/**
* Retrieves query params from config entries like commerce.queryparam.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
5 changes: 5 additions & 0 deletions tools/pdp-metadata/pdp-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ async function performCatalogServiceQuery(config, query, variables) {
const headers = {
'Content-Type': 'application/json',
'x-api-key': config['commerce.headers.cs.x-api-key'],
'Magento-Customer-Group': config['commerce.headers.cs.Magento-Customer-Group'],
'Magento-Environment-Id': config['commerce.headers.cs.Magento-Environment-Id'],
'Magento-Store-Code': config['commerce.headers.cs.Magento-Store-Code'],
'Magento-Store-View-Code': config['commerce.headers.cs.Magento-Store-View-Code'],
'Magento-Website-Code': config['commerce.headers.cs.Magento-Website-Code'],
};

const apiCall = await commerceEndpointWithQueryParams();
Expand Down
17 changes: 6 additions & 11 deletions tools/picker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,17 @@ const blocks = {

async function performCatalogServiceQuery(query, config, variables) {
const headers = {
'x-api-key': config['commerce.headers.cs.x-api-key'],
'Content-Type': 'application/json',
'x-api-key': config['commerce.headers.cs.x-api-key'],
'Magento-Customer-Group': config['commerce.headers.cs.Magento-Customer-Group'],
'Magento-Environment-Id': config['commerce.headers.cs.Magento-Environment-Id'],
'Magento-Store-Code': config['commerce.headers.cs.Magento-Store-Code'],
'Magento-Store-View-Code': config['commerce.headers.cs.Magento-Store-View-Code'],
'Magento-Website-Code': config['commerce.headers.cs.Magento-Website-Code'],
};

// set query params if provided, or fall back to header value
const apiCall = new URL(config['commerce-endpoint']);
apiCall.searchParams.append("Magento-Environment-Id",
config['commerce.queryparam.cs.Magento-Environment-Id'] || config['commerce.headers.cs.Magento-Environment-Id']);
apiCall.searchParams.append("Magento-Website-Code",
config['commerce.queryparam.cs.Magento-Website-Code']) || config['commerce.headers.cs.Magento-Website-Code']);
apiCall.searchParams.append("Magento-Store-View-Code",
config['commerce.queryparam.cs.Magento-Store-View-Code']) || config['commerce.headers.cs.Magento-Store-View-Code']);
apiCall.searchParams.append("Magento-Store-Code",
config['commerce.queryparam.cs.Magento-Store-Code']) || config['commerce.headers.cs.Magento-Store-Code']);
apiCall.searchParams.append("Magento-Customer-Group",
config['commerce.headers.cs.Magento-Customer-Group']) || config['commerce.headers.cs.Magento-Customer-Group']);
apiCall.searchParams.append('query', query.replace(/(?:\r\n|\r|\n|\t|[\s]{4})/g, ' ')
.replace(/\s\s+/g, ' '));
apiCall.searchParams.append('variables', variables ? JSON.stringify(variables) : null);
Expand Down

0 comments on commit 4b62eda

Please sign in to comment.