Skip to content

Commit f32df9c

Browse files
authored
Merge pull request #11 from adobe-rnd/urlkey-search
fix: search by urlkey
2 parents c860784 + 9ccfde9 commit f32df9c

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed

src/config.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export async function resolveConfig(ctx, tenant, overrides = {}) {
5656
);
5757

5858
// merge configs
59-
return {
59+
/** @type {Config} */
60+
const resolved = {
6061
...paths.reduce((conf, key) => ({
6162
...conf,
6263
...confMap[key],
@@ -70,4 +71,13 @@ export async function resolveConfig(ctx, tenant, overrides = {}) {
7071
}),
7172
...overrides,
7273
};
74+
75+
// ensure validity
76+
// TODO: make this more robust
77+
if (!resolved.pageType) {
78+
ctx.log.warn('invalid config for tenant (missing pageType)', tenant);
79+
return null;
80+
}
81+
82+
return resolved;
7383
}

src/index.js

+52-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import { errorResponse, errorWithResponse, makeContext } from './util.js';
1515
import getProductQueryCS, { adapter } from './queries/cs-product.js';
1616
import getProductQueryCore from './queries/core-product.js';
17+
import getProductSKUQuery from './queries/core-product-sku.js';
1718
import HTML_TEMPLATE from './templates/html.js';
1819
import { resolveConfig } from './config.js';
1920

@@ -79,7 +80,7 @@ async function fetchProductCore(opt, config) {
7980

8081
const json = await resp.json();
8182
try {
82-
const [product] = json.data.products;
83+
const [product] = json.data.products.items;
8384
if (!product) {
8485
throw errorWithResponse(404, 'could not find product', json.errors);
8586
}
@@ -90,14 +91,57 @@ async function fetchProductCore(opt, config) {
9091
}
9192
}
9293

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.items;
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+
93127
/**
94128
* @param {Context} ctx
95129
* @param {Config} config
96130
*/
97131
async function handlePDPRequest(ctx, config) {
98-
const { sku, urlkey } = config.params;
132+
const { urlkey } = config.params;
133+
let { sku } = config.params;
134+
99135
if (!sku && !urlkey) {
100136
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);
101145
}
102146

103147
// const product = await fetchProductCore({ sku }, config);
@@ -146,13 +190,13 @@ export default {
146190
return errorResponse(404, 'missing route');
147191
}
148192

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-
155193
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+
156200
return handlers[route](ctx, config);
157201
} catch (e) {
158202
if (e.response) {

src/queries/core-product-sku.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import { gql } from '../util.js';
14+
15+
/**
16+
* @param {{ urlkey: string; }} param0
17+
*/
18+
export default ({ urlkey }) => gql`{
19+
products(
20+
filter: { url_key: { eq: "${urlkey}" } }
21+
) {
22+
items {
23+
sku
24+
}
25+
}
26+
}`;

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare global {
77
magentoEnvironmentId: string;
88
magentoWebsiteCode: string;
99
magentoStoreViewCode: string;
10+
magentoStoreCode: string;
1011
coreEndpoint: string;
1112
params: Record<string, string>;
1213
}

wrangler.toml

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ ENVIRONMENT = "dev"
2121
[env.ci]
2222
name = "adobe-commerce-api-ci"
2323

24+
kv_namespaces = [
25+
{ binding = "CONFIGS", id = "bb91e12a65a8462282396f32e63406f1", preview_id = "bb91e12a65a8462282396f32e63406f1" }
26+
]
27+
2428
[env.ci.vars]
2529
VERSION = "@@VERSION@@-ci"
2630
ENVIRONMENT = "ci"
@@ -31,6 +35,10 @@ ENVIRONMENT = "ci"
3135
[env.production]
3236
name = "adobe-commerce-api"
3337

38+
kv_namespaces = [
39+
{ binding = "CONFIGS", id = "bb91e12a65a8462282396f32e63406f1", preview_id = "bb91e12a65a8462282396f32e63406f1" }
40+
]
41+
3442
[env.production.vars]
3543
VERSION = "@@VERSION@@"
3644
ENVIRONMENT = "prod"

0 commit comments

Comments
 (0)