Skip to content

Commit

Permalink
fix: add site overrides setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Nov 2, 2024
1 parent 91a48ff commit b4de3e3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import { errorWithResponse } from './utils/http.js';
import { siteOverrides } from './overrides/index.js';

/**
* This function finds ordered matches between a list of patterns and a given path.
Expand Down Expand Up @@ -104,7 +105,9 @@ export async function resolveConfig(ctx, overrides = {}) {
org,
site,
route,
siteOverrides: siteOverrides[siteKey],
...overrides,
};

return resolved;
}
2 changes: 1 addition & 1 deletion src/content/helix-commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function handle(ctx, config) {
}

const product = await fetchProduct(ctx, config, sku);
const html = HTML_TEMPLATE(product, product.variants);
const html = HTML_TEMPLATE(config, product, product.variants);
return new Response(html, {
status: 200,
headers: {
Expand Down
31 changes: 31 additions & 0 deletions src/overrides/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

export const siteOverrides = {
'thepixel--bul-eds': {
constructProductUrl: (config, product, variant) => {
const { host, matchedPath } = config;
const productPath = matchedPath
.replace('{{urlkey}}', product.urlKey)
.replace('{{sku}}', encodeURIComponent(product.sku.toLowerCase()));

const productUrl = `${host}${productPath}`;

if (variant) {
const options = variant.selections.map((selection) => atob(selection)).join(',').replace(/configurable\//g, '').replace(/\//g, '-');
return `${productUrl}?pid=${variant.externalId}&o=${btoa(options)}`;
}

return productUrl;
},
},
};
6 changes: 4 additions & 2 deletions src/templates/json-ld.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default (config, product, variants) => {
prices,
} = product;

const productUrl = constructProductUrl(config, product);
const productUrl = config.siteOverrides?.constructProductUrl(config, product)
?? constructProductUrl(config, product);
const image = images?.[0]?.url ?? findProductImage(product, variants)?.url;
const brandName = attributes?.find((attr) => attr.name === 'brand')?.value;
return JSON.stringify(pruneUndefined({
Expand All @@ -53,7 +54,8 @@ export default (config, product, variants) => {
priceCurrency: prices?.final?.currency,
}) : undefined,
...variants.map((v) => {
const offerUrl = constructProductUrl(config, product, v);
const offerUrl = config.siteOverrides?.constructProductUrl(config, product, v)
?? constructProductUrl(config, product, v);

const offer = {
'@type': 'Offer',
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare global {
matchedPath: string;
matchedPathConfig: Config;
attributeOverrides: AttributeOverrides;
siteOverrides: Record<string, Record<string, unknown>>;
}

export interface Env {
Expand Down

0 comments on commit b4de3e3

Please sign in to comment.