Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Linx Impulse Loader (similarItems) #164

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions functions/productIdFromVTEXSlug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { FunctionContext, LoaderFunction } from "$live/types.ts";
import type { RequestURLParam } from "./requestToParam.ts";

export type ProductID = string;

export interface Props {
/**
* @default slug
* @description Param name to extract from the Request URL
*/
slug: RequestURLParam;
}

/**
* @title Extract ID from request parameters
* @description Set param to slug for routes of type /:slug
*/
const productIdFromVTEXSlug: LoaderFunction<
Props,
ProductID,
FunctionContext
> = (_req, _ctx, { slug }) => {
return { data: slug.split("-").at(-1) ?? "" };
};

export default productIdFromVTEXSlug;
74 changes: 38 additions & 36 deletions live.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ import * as $5 from "./functions/butterCMSPostDetail.ts";
import * as $6 from "./functions/vndaProductDetailsPage.ts";
import * as $7 from "./functions/vtexLegacyProductDetailsPage.ts";
import * as $8 from "./functions/vtexSuggestions.ts";
import * as $9 from "./functions/vtexNavbar.ts";
import * as $10 from "./functions/butterCMSPlaces.ts";
import * as $11 from "./functions/vtexWishlist.ts";
import * as $12 from "./functions/shopifyProductListingPage.ts";
import * as $13 from "./functions/vtexProductList.ts";
import * as $14 from "./functions/butterCMSFeaturedPosts.ts";
import * as $15 from "./functions/occProductDetailsPage.ts";
import * as $16 from "./functions/butterCMSBrands.ts";
import * as $17 from "./functions/vndaProductListingPage.ts";
import * as $18 from "./functions/butterCMSRelatedPosts.ts";
import * as $19 from "./functions/vtexLegacyProductListingPage.ts";
import * as $20 from "./functions/vtexProductDetailsPage.ts";
import * as $21 from "./functions/vtexLegacyProductList.ts";
import * as $22 from "./functions/butterCMSCategories.ts";
import * as $23 from "./functions/shopifyProductList.ts";
import * as $24 from "./functions/shopifyProductDetailsPage.ts";
import * as $25 from "./functions/vtexLegacyRelatedProductsLoader.ts";
import * as $26 from "./functions/requestToParam.ts";
import * as $9 from "./functions/productIdFromVTEXSlug.ts";
import * as $10 from "./functions/vtexNavbar.ts";
import * as $11 from "./functions/butterCMSPlaces.ts";
import * as $12 from "./functions/vtexWishlist.ts";
import * as $13 from "./functions/shopifyProductListingPage.ts";
import * as $14 from "./functions/vtexProductList.ts";
import * as $15 from "./functions/butterCMSFeaturedPosts.ts";
import * as $16 from "./functions/occProductDetailsPage.ts";
import * as $17 from "./functions/butterCMSBrands.ts";
import * as $18 from "./functions/vndaProductListingPage.ts";
import * as $19 from "./functions/butterCMSRelatedPosts.ts";
import * as $20 from "./functions/vtexLegacyProductListingPage.ts";
import * as $21 from "./functions/vtexProductDetailsPage.ts";
import * as $22 from "./functions/vtexLegacyProductList.ts";
import * as $23 from "./functions/butterCMSCategories.ts";
import * as $24 from "./functions/shopifyProductList.ts";
import * as $25 from "./functions/shopifyProductDetailsPage.ts";
import * as $26 from "./functions/vtexLegacyRelatedProductsLoader.ts";
import * as $27 from "./functions/requestToParam.ts";
import * as $$0 from "./accounts/butterCMS.ts";
import * as $$1 from "./accounts/linxImpulse.ts";
import * as $$2 from "./accounts/vnda.ts";
Expand Down Expand Up @@ -153,32 +154,33 @@ import * as i1$$$$$$$2 from "$live/actions/workflows/start.ts";
const manifest = {
"functions": {
"deco-sites/std/functions/butterCMSAds.ts": $4,
"deco-sites/std/functions/butterCMSBrands.ts": $16,
"deco-sites/std/functions/butterCMSCategories.ts": $22,
"deco-sites/std/functions/butterCMSFeaturedPosts.ts": $14,
"deco-sites/std/functions/butterCMSBrands.ts": $17,
"deco-sites/std/functions/butterCMSCategories.ts": $23,
"deco-sites/std/functions/butterCMSFeaturedPosts.ts": $15,
"deco-sites/std/functions/butterCMSPage.ts": $1,
"deco-sites/std/functions/butterCMSPlaces.ts": $10,
"deco-sites/std/functions/butterCMSPlaces.ts": $11,
"deco-sites/std/functions/butterCMSPostDetail.ts": $5,
"deco-sites/std/functions/butterCMSPosts.ts": $0,
"deco-sites/std/functions/butterCMSRelatedPosts.ts": $18,
"deco-sites/std/functions/occProductDetailsPage.ts": $15,
"deco-sites/std/functions/requestToParam.ts": $26,
"deco-sites/std/functions/shopifyProductDetailsPage.ts": $24,
"deco-sites/std/functions/shopifyProductList.ts": $23,
"deco-sites/std/functions/shopifyProductListingPage.ts": $12,
"deco-sites/std/functions/butterCMSRelatedPosts.ts": $19,
"deco-sites/std/functions/occProductDetailsPage.ts": $16,
"deco-sites/std/functions/productIdFromVTEXSlug.ts": $9,
"deco-sites/std/functions/requestToParam.ts": $27,
"deco-sites/std/functions/shopifyProductDetailsPage.ts": $25,
"deco-sites/std/functions/shopifyProductList.ts": $24,
"deco-sites/std/functions/shopifyProductListingPage.ts": $13,
"deco-sites/std/functions/vndaProductDetailsPage.ts": $6,
"deco-sites/std/functions/vndaProductList.ts": $3,
"deco-sites/std/functions/vndaProductListingPage.ts": $17,
"deco-sites/std/functions/vndaProductListingPage.ts": $18,
"deco-sites/std/functions/vtexLegacyProductDetailsPage.ts": $7,
"deco-sites/std/functions/vtexLegacyProductList.ts": $21,
"deco-sites/std/functions/vtexLegacyProductListingPage.ts": $19,
"deco-sites/std/functions/vtexLegacyRelatedProductsLoader.ts": $25,
"deco-sites/std/functions/vtexNavbar.ts": $9,
"deco-sites/std/functions/vtexProductDetailsPage.ts": $20,
"deco-sites/std/functions/vtexProductList.ts": $13,
"deco-sites/std/functions/vtexLegacyProductList.ts": $22,
"deco-sites/std/functions/vtexLegacyProductListingPage.ts": $20,
"deco-sites/std/functions/vtexLegacyRelatedProductsLoader.ts": $26,
"deco-sites/std/functions/vtexNavbar.ts": $10,
"deco-sites/std/functions/vtexProductDetailsPage.ts": $21,
"deco-sites/std/functions/vtexProductList.ts": $14,
"deco-sites/std/functions/vtexProductListingPage.ts": $2,
"deco-sites/std/functions/vtexSuggestions.ts": $8,
"deco-sites/std/functions/vtexWishlist.ts": $11,
"deco-sites/std/functions/vtexWishlist.ts": $12,
},
"accounts": {
"deco-sites/std/accounts/butterCMS.ts": $$0,
Expand Down
43 changes: 15 additions & 28 deletions packs/linxImpulse/loaders/products/similarItems.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import type { Product } from "deco-sites/std/commerce/types.ts";
import type { RequestURLParam } from "deco-sites/std/functions/requestToParam.ts";
import type { ProductID } from "deco-sites/std/functions/productIdFromVTEXSlug.ts";
import type {
PagesRecommendationsResponse,
Position,
SearchProductsResponse,
Shelf,
} from "deco-sites/std/packs/linxImpulse/types.ts";

import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import {
toProduct,
toProductLinxImpulse,
toRequestHeader,
} from "deco-sites/std/packs/linxImpulse/utils/transform.ts";
import { paths } from "deco-sites/std/packs/linxImpulse/utils/path.ts";
import type { Context } from "deco-sites/std/packs/linxImpulse/accounts/linxImpulse.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";
import { HttpError } from "deco-sites/std/utils/HttpError.ts";
import { fetchAPI } from "deco-sites/std/utils/fetch.ts";

export interface Props {
slug: RequestURLParam;

/**
* @title Position
*/
position: Position;

/**
* @title Feature
*/
id: ProductID;
feature: "SimilarItems" | "FrequentlyBoughtTogether";
}

Expand All @@ -40,16 +31,15 @@ const loader = async (
ctx: Context,
): Promise<Product[] | null> => {
const { configLinxImpulse: config } = ctx;
const { slug, position, feature } = props;
const { id, feature } = props;
const url = new URL(req.url);
const skuId = url.searchParams.get("skuId");
const requestHeaders = toRequestHeader(config!);
const linxImpulse = paths(config!);

try {
const linxImpulse = paths(config!);
const productSlug = slug.replaceAll("-", " ");
const { products: productsBySlug } = await fetchAPI<SearchProductsResponse>(
`${linxImpulse.product.getProductBySlug.term(productSlug)}`,
`${linxImpulse.product.getProductBySlug.term(id)}`,
{ headers: requestHeaders },
);

Expand All @@ -65,17 +55,14 @@ const loader = async (
`${linxImpulse.product.similarItems.productId(product.id)}`,
{ headers: requestHeaders },
);
let shelfs;

if (position) {
shelfs = recommendationsResponse[position];
}

if (feature) {
shelfs = shelfs?.filter((shelf) => shelf.feature === feature);
}
const checkFeature = (shelf: Shelf) => shelf.feature === feature;
const topShelfs = recommendationsResponse.top.filter(checkFeature);
const middleShelfs = recommendationsResponse.middle.filter(checkFeature);
const bottomShelfs = recommendationsResponse.bottom.filter(checkFeature);
const shelfs = [...topShelfs, ...middleShelfs, ...bottomShelfs];

if (!shelfs) return null;
if (!shelfs.length) return null;

const options = {
baseUrl: req.url,
Expand Down
2 changes: 1 addition & 1 deletion packs/linxImpulse/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const paths = ({ apiKey, secretKey }: Account) => {
product: {
getProductBySlug: {
term: (term: string) =>
`${searchBaseUrl}/search?apiKey=${apiKey}&productFormat=complete&terms=${term}&hide=quickFilters&hide=suggestions&hide=filters`,
`${searchBaseUrl}/search?apiKey=${apiKey}&productFormat=complete&terms=${term}&hide=quickFilters&hide=suggestions&hide=filters&resultsPerPage=1`,
},
similarItems: {
productId: (productId: string) =>
Expand Down
Loading