feat: added new smarthint product listing loader#1571
feat: added new smarthint product listing loader#1571aline-pereira wants to merge 1 commit intomainfrom
Conversation
Tagging OptionsShould a new tag be published when this PR is merged?
|
📝 WalkthroughWalkthroughThis PR introduces a new SmartHint product list loader ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@smarthint/loaders/productList.ts`:
- Around line 70-84: The code calls recs["GET
/recommendationByPage/withProducts"] and immediately parses and uses the
response as an array (variable data) then runs .find to create positionItem; to
harden this, first capture the raw response, check response.ok and handle non-OK
(log/throw or return an empty result), then attempt to parse JSON inside a
try/catch to handle invalid JSON, and after parsing validate Array.isArray(data)
before calling .find (if not an array, set data = [] or handle accordingly).
Update the call site around recs["GET /recommendationByPage/withProducts"], the
parsing step that assigns data, and the subsequent creation of positionItem to
follow these guards.
In `@smarthint/loaders/recommendations.ts`:
- Around line 38-41: Reformat the declaration of getProductParam to match deno
fmt: run deno fmt and update the function signature/spacing so it matches the
formatter's output (ensure parameter spacing and placement of the opening brace
follow deno fmt conventions for export function getProductParam(pagetype:
ComplexPageType, productsParam: string[])). After running deno fmt, commit the
updated signature so CI passes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 832ffeda-0a19-41a9-88e2-37fd67c02465
📒 Files selected for processing (3)
smarthint/loaders/productList.tssmarthint/loaders/recommendations.tssmarthint/manifest.gen.ts
| const data = await recs["GET /recommendationByPage/withProducts"]({ | ||
| shcode, | ||
| anonymous, | ||
| categories, | ||
| channel, | ||
| filter: filters, | ||
| pageIdentifier, | ||
| pagetype: pagetype.type, | ||
| position, | ||
| products: productsString, | ||
| }).then((r) => r.json()); | ||
|
|
||
| const positionItem = data.find((item) => | ||
| Number(item.SmartHintPosition) == Number(position) | ||
| ); |
There was a problem hiding this comment.
Guard external response before parsing and .find().
This path assumes a successful JSON array response. A non-OK status, invalid JSON, or non-array payload will throw and break the loader.
🛡️ Suggested hardening
- const data = await recs["GET /recommendationByPage/withProducts"]({
+ const response = await recs["GET /recommendationByPage/withProducts"]({
shcode,
anonymous,
categories,
channel,
filter: filters,
pageIdentifier,
pagetype: pagetype.type,
position,
products: productsString,
- }).then((r) => r.json());
+ });
+
+ if (!response.ok) return null;
+
+ let data: unknown;
+ try {
+ data = await response.json();
+ } catch {
+ return null;
+ }
+ if (!Array.isArray(data)) return null;
const positionItem = data.find((item) =>
Number(item.SmartHintPosition) == Number(position)
);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const data = await recs["GET /recommendationByPage/withProducts"]({ | |
| shcode, | |
| anonymous, | |
| categories, | |
| channel, | |
| filter: filters, | |
| pageIdentifier, | |
| pagetype: pagetype.type, | |
| position, | |
| products: productsString, | |
| }).then((r) => r.json()); | |
| const positionItem = data.find((item) => | |
| Number(item.SmartHintPosition) == Number(position) | |
| ); | |
| const response = await recs["GET /recommendationByPage/withProducts"]({ | |
| shcode, | |
| anonymous, | |
| categories, | |
| channel, | |
| filter: filters, | |
| pageIdentifier, | |
| pagetype: pagetype.type, | |
| position, | |
| products: productsString, | |
| }); | |
| if (!response.ok) return null; | |
| let data: unknown; | |
| try { | |
| data = await response.json(); | |
| } catch { | |
| return null; | |
| } | |
| if (!Array.isArray(data)) return null; | |
| const positionItem = data.find((item) => | |
| Number(item.SmartHintPosition) == Number(position) | |
| ); |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@smarthint/loaders/productList.ts` around lines 70 - 84, The code calls
recs["GET /recommendationByPage/withProducts"] and immediately parses and uses
the response as an array (variable data) then runs .find to create positionItem;
to harden this, first capture the raw response, check response.ok and handle
non-OK (log/throw or return an empty result), then attempt to parse JSON inside
a try/catch to handle invalid JSON, and after parsing validate
Array.isArray(data) before calling .find (if not an array, set data = [] or
handle accordingly). Update the call site around recs["GET
/recommendationByPage/withProducts"], the parsing step that assigns data, and
the subsequent creation of positionItem to follow these guards.
| export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) { | ||
| if (productsParam.length) { | ||
| return productsParam.map((productId) => `productid:${productId}`).join("&"); | ||
| } |
There was a problem hiding this comment.
Fix formatting for getProductParam declaration to unblock CI.
deno fmt --check is failing on this declaration; reformat this signature exactly as deno fmt expects.
💡 Suggested patch
-export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) {
+export function getProductParam(
+ pagetype: ComplexPageType,
+ productsParam: string[],
+) {📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) { | |
| if (productsParam.length) { | |
| return productsParam.map((productId) => `productid:${productId}`).join("&"); | |
| } | |
| export function getProductParam( | |
| pagetype: ComplexPageType, | |
| productsParam: string[], | |
| ) { | |
| if (productsParam.length) { | |
| return productsParam.map((productId) => `productid:${productId}`).join("&"); | |
| } |
🧰 Tools
🪛 GitHub Actions: ci
[error] 38-41: deno fmt --check failed. Found 1 not formatted file in 2112 files. Formatting diff indicates function declaration 'getProductParam' needs reformatting (parameter list spanning multiple lines).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@smarthint/loaders/recommendations.ts` around lines 38 - 41, Reformat the
declaration of getProductParam to match deno fmt: run deno fmt and update the
function signature/spacing so it matches the formatter's output (ensure
parameter spacing and placement of the opening brace follow deno fmt conventions
for export function getProductParam(pagetype: ComplexPageType, productsParam:
string[])). After running deno fmt, commit the updated signature so CI passes.
What is this Contribution About?
Please provide a brief description of the changes or enhancements you are proposing in this pull request.
Issue Link
Please link to the relevant issue that this pull request addresses:
Loom Video
Demonstration Link
Summary by cubic
Adds a SmartHint product list loader that builds a
Product[]from recommendations by page and position. This enables ProductShelf to show SmartHint-driven products with filters, categories, and session awareness.New Features
smarthint/loaders/productList.tscalling/recommendationByPage/withProducts, flattening products from Products, Promotional, and Combination combos, and returningProduct[] | null. Registered insmarthint/manifest.gen.ts.Refactors
getProductParamfromsmarthint/loaders/recommendations.tsfor reuse.Written for commit 4d64c76. Summary will update on new commits.
Summary by CodeRabbit