|
14 | 14 | import { errorResponse, errorWithResponse, makeContext } from './util.js';
|
15 | 15 | import getProductQueryCS, { adapter } from './queries/cs-product.js';
|
16 | 16 | import getProductQueryCore from './queries/core-product.js';
|
| 17 | +import getProductSKUQuery from './queries/core-product-sku.js'; |
17 | 18 | import HTML_TEMPLATE from './templates/html.js';
|
18 | 19 | import { resolveConfig } from './config.js';
|
19 | 20 |
|
@@ -90,14 +91,57 @@ async function fetchProductCore(opt, config) {
|
90 | 91 | }
|
91 | 92 | }
|
92 | 93 |
|
| 94 | +/** |
| 95 | + * @param {string} urlkey |
| 96 | + * @param {Config} config |
| 97 | + */ |
| 98 | +async function lookupProductSKU(urlkey, config) { |
| 99 | + const query = getProductSKUQuery({ urlkey }); |
| 100 | + const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, { |
| 101 | + headers: { |
| 102 | + origin: 'https://api.adobecommerce.live', |
| 103 | + 'x-api-key': config.apiKey, |
| 104 | + 'Magento-Environment-Id': config.magentoEnvironmentId, |
| 105 | + 'Magento-Website-Code': config.magentoWebsiteCode, |
| 106 | + 'Magento-Store-View-Code': config.magentoStoreViewCode, |
| 107 | + }, |
| 108 | + }); |
| 109 | + if (!resp.ok) { |
| 110 | + console.warn('failed to fetch product sku: ', resp.status, resp.statusText); |
| 111 | + throw errorWithResponse(resp.status, 'failed to fetch product sku'); |
| 112 | + } |
| 113 | + |
| 114 | + const json = await resp.json(); |
| 115 | + try { |
| 116 | + const [product] = json.data.products; |
| 117 | + if (!product) { |
| 118 | + throw errorWithResponse(404, 'could not find product sku', json.errors); |
| 119 | + } |
| 120 | + return product.sku; |
| 121 | + } catch (e) { |
| 122 | + console.error('failed to parse product sku: ', e); |
| 123 | + throw errorWithResponse(500, 'failed to parse product sku response'); |
| 124 | + } |
| 125 | +} |
| 126 | + |
93 | 127 | /**
|
94 | 128 | * @param {Context} ctx
|
95 | 129 | * @param {Config} config
|
96 | 130 | */
|
97 | 131 | async function handlePDPRequest(ctx, config) {
|
98 |
| - const { sku, urlkey } = config.params; |
| 132 | + const { urlkey } = config.params; |
| 133 | + let { sku } = config.params; |
| 134 | + |
99 | 135 | if (!sku && !urlkey) {
|
100 | 136 | return errorResponse(404, 'missing sku or urlkey');
|
| 137 | + } else if (!sku && !config.coreEndpoint) { |
| 138 | + return errorResponse(400, 'missing sku and coreEndpoint'); |
| 139 | + } |
| 140 | + |
| 141 | + if (!sku) { |
| 142 | + // lookup sku by urlkey with core |
| 143 | + // TODO: test if livesearch if enabled |
| 144 | + sku = await lookupProductSKU(urlkey, config); |
101 | 145 | }
|
102 | 146 |
|
103 | 147 | // const product = await fetchProductCore({ sku }, config);
|
@@ -146,13 +190,13 @@ export default {
|
146 | 190 | return errorResponse(404, 'missing route');
|
147 | 191 | }
|
148 | 192 |
|
149 |
| - const overrides = Object.fromEntries(ctx.url.searchParams.entries()); |
150 |
| - const config = await resolveConfig(ctx, tenant, overrides); |
151 |
| - if (!config) { |
152 |
| - return errorResponse(404, 'config not found'); |
153 |
| - } |
154 |
| - |
155 | 193 | try {
|
| 194 | + const overrides = Object.fromEntries(ctx.url.searchParams.entries()); |
| 195 | + const config = await resolveConfig(ctx, tenant, overrides); |
| 196 | + if (!config) { |
| 197 | + return errorResponse(404, 'config not found'); |
| 198 | + } |
| 199 | + |
156 | 200 | return handlers[route](ctx, config);
|
157 | 201 | } catch (e) {
|
158 | 202 | if (e.response) {
|
|
0 commit comments