Skip to content

Commit

Permalink
Merge pull request #36 from adobe-rnd/overrides
Browse files Browse the repository at this point in the history
feat: template overrides
  • Loading branch information
dylandepass authored Nov 5, 2024
2 parents 67c7bc0 + e3d2852 commit 23a75aa
Show file tree
Hide file tree
Showing 25 changed files with 937 additions and 745 deletions.
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

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 @@ -105,7 +104,8 @@ export async function resolveConfig(ctx, overrides = {}) {
org,
site,
route,
siteOverrides: siteOverrides[siteKey],
siteKey,
matchedPatterns: paths,
...overrides,
};

Expand Down
4 changes: 2 additions & 2 deletions src/content/adobe-commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { errorResponse, errorWithResponse, ffetch } from '../utils/http.js';
import getProductQuery, { adapter as productAdapter } from './queries/cs-product.js';
import getVariantsQuery, { adapter as variantsAdapter } from './queries/cs-variants.js';
import getProductSKUQuery from './queries/core-product-sku.js';
import HTML_TEMPLATE from '../templates/html.js';
import htmlTemplateFromContext from '../templates/html/index.js';

/**
* @param {string} sku
Expand Down Expand Up @@ -173,7 +173,7 @@ export async function handle(ctx, config) {
fetchProduct(sku.toUpperCase(), config),
fetchVariants(sku.toUpperCase(), config),
]);
const html = HTML_TEMPLATE(config, product, variants);
const html = htmlTemplateFromContext(ctx, product, variants).render();
return new Response(html, {
status: 200,
headers: {
Expand Down
22 changes: 1 addition & 21 deletions src/content/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* governing permissions and limitations under the License.
*/

import { matchConfigPath } from '../utils/product.js';
import { errorResponse } from '../utils/http.js';
import { handle as handleAdobeCommerce } from './adobe-commerce.js';
import { handle as handleHelixCommerce } from './helix-commerce.js';
Expand All @@ -30,26 +29,7 @@ export default async function contentHandler(ctx, config) {
if (!config.pageType) {
return errorResponse(400, 'invalid config for tenant site (missing pageType)');
}

const { pathname } = ctx.url;
const productPath = pathname.includes('/content/product')
? pathname.split('/content/product')[1]
: null;

if (productPath) {
const matchedPath = matchConfigPath(config, productPath);
const matchedPathConfig = config.confMap && config.confMap[matchedPath]
? config.confMap[matchedPath]
: null;
if (matchedPathConfig) {
config.matchedPath = matchedPath;
config.matchedPathConfig = matchedPathConfig;
} else {
return errorResponse(400, 'No matching configuration for product path');
}
} else {
return errorResponse(400, 'invalid product path');
}
console.log('config: ', JSON.stringify(config, null, 2));

if (config.catalogSource === 'helix') {
return handleHelixCommerce(ctx, config);
Expand Down
4 changes: 2 additions & 2 deletions src/content/helix-commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { errorResponse } from '../utils/http.js';
import { fetchProduct } from '../utils/r2.js';
import HTML_TEMPLATE from '../templates/html.js';
import htmlTemplateFromContext from '../templates/html/index.js';

/**
* @param {Context} ctx
Expand All @@ -28,7 +28,7 @@ export async function handle(ctx, config) {
}

const product = await fetchProduct(ctx, config, sku);
const html = HTML_TEMPLATE(config, product, product.variants);
const html = htmlTemplateFromContext(ctx, product, product.variants).render();
return new Response(html, {
status: 200,
headers: {
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export function makeContext(pctx, req, env) {
/** @type {Context} */
// @ts-ignore
const ctx = pctx;
// @ts-ignore
ctx.attributes = {};
ctx.env = env;
ctx.url = new URL(req.url);
ctx.log = console;
Expand All @@ -58,6 +60,8 @@ export default {
try {
const overrides = Object.fromEntries(ctx.url.searchParams.entries());
const config = await resolveConfig(ctx, overrides);
ctx.config = config;

console.debug('resolved config: ', JSON.stringify(config));
if (!config) {
return errorResponse(404, 'config not found');
Expand Down
31 changes: 0 additions & 31 deletions src/overrides/index.js

This file was deleted.

Loading

0 comments on commit 23a75aa

Please sign in to comment.