From 21c0dae20d2ee095918e9f8989337f849f218808 Mon Sep 17 00:00:00 2001 From: Gustavo Vasconcellos Date: Tue, 25 Jul 2023 23:07:28 -0300 Subject: [PATCH 1/6] fix(similarItems): add regex logic and remove slug from props --- .../loaders/products/similarItems.ts | 22 +++++++++++-------- packs/linxImpulse/utils/path.ts | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packs/linxImpulse/loaders/products/similarItems.ts b/packs/linxImpulse/loaders/products/similarItems.ts index 1dbd9963..8062ef2c 100644 --- a/packs/linxImpulse/loaders/products/similarItems.ts +++ b/packs/linxImpulse/loaders/products/similarItems.ts @@ -1,24 +1,21 @@ import type { Product } from "deco-sites/std/commerce/types.ts"; -import type { RequestURLParam } from "deco-sites/std/functions/requestToParam.ts"; import type { PagesRecommendationsResponse, Position, SearchProductsResponse, } 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 */ @@ -40,16 +37,23 @@ const loader = async ( ctx: Context, ): Promise => { const { configLinxImpulse: config } = ctx; - const { slug, position, feature } = props; + const { position, feature } = props; const url = new URL(req.url); const skuId = url.searchParams.get("skuId"); const requestHeaders = toRequestHeader(config!); + /** + * As the Linx APIs do not support slug searches, we need to get the product reference that every product has on URL + */ + const regex = /-(\d+)\/p/; + const match = url.pathname.match(regex); + + if (!match) return null; + try { const linxImpulse = paths(config!); - const productSlug = slug.replaceAll("-", " "); const { products: productsBySlug } = await fetchAPI( - `${linxImpulse.product.getProductBySlug.term(productSlug)}`, + `${linxImpulse.product.getProductBySlug.term(match[1])}`, { headers: requestHeaders }, ); diff --git a/packs/linxImpulse/utils/path.ts b/packs/linxImpulse/utils/path.ts index 3a206b32..4374d871 100644 --- a/packs/linxImpulse/utils/path.ts +++ b/packs/linxImpulse/utils/path.ts @@ -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) => From b79e647d28b2a448b422b75a7624d4969812168a Mon Sep 17 00:00:00 2001 From: Gustavo Vasconcellos Date: Wed, 26 Jul 2023 00:11:00 -0300 Subject: [PATCH 2/6] extract from param --- functions/extractIdFromParam.ts | 25 ++++ live.gen.ts | 6 +- .../loaders/products/similarItems.ts | 23 ++-- schemas.gen.json | 112 +++++++++++++++--- 4 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 functions/extractIdFromParam.ts diff --git a/functions/extractIdFromParam.ts b/functions/extractIdFromParam.ts new file mode 100644 index 00000000..5ca96bfc --- /dev/null +++ b/functions/extractIdFromParam.ts @@ -0,0 +1,25 @@ +import type { FunctionContext, LoaderFunction } from "$live/types.ts"; + +export type ExtractIDFromParam = string; + +export interface Props { + /** + * @default slug + * @description Param name to extract from the Request URL + */ + param: string; +} + +/** + * @title Extract ID from request parameters + * @description Set param to slug for routes of type /:slug + */ +const extractIdFromParam: LoaderFunction< + Props, + ExtractIDFromParam, + FunctionContext +> = (_req, ctx) => ({ + data: ctx.params[ctx.state.$live.param || "slug"], +}); + +export default extractIdFromParam; diff --git a/live.gen.ts b/live.gen.ts index 83486936..ecc8ac36 100644 --- a/live.gen.ts +++ b/live.gen.ts @@ -29,7 +29,8 @@ 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 $26 from "./functions/extractIdFromParam.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"; @@ -158,8 +159,9 @@ const manifest = { "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/extractIdFromParam.ts": $26, "deco-sites/std/functions/occProductDetailsPage.ts": $15, - "deco-sites/std/functions/requestToParam.ts": $26, + "deco-sites/std/functions/requestToParam.ts": $27, "deco-sites/std/functions/shopifyProductDetailsPage.ts": $24, "deco-sites/std/functions/shopifyProductList.ts": $23, "deco-sites/std/functions/shopifyProductListingPage.ts": $12, diff --git a/packs/linxImpulse/loaders/products/similarItems.ts b/packs/linxImpulse/loaders/products/similarItems.ts index 8062ef2c..74e61871 100644 --- a/packs/linxImpulse/loaders/products/similarItems.ts +++ b/packs/linxImpulse/loaders/products/similarItems.ts @@ -1,21 +1,24 @@ import type { Product } from "deco-sites/std/commerce/types.ts"; +import type { ExtractIDFromParam } from "deco-sites/std/functions/extractIdFromParam.ts"; import type { PagesRecommendationsResponse, Position, SearchProductsResponse, } 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 { HttpError } from "deco-sites/std/utils/HttpError.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"; export interface Props { + id: ExtractIDFromParam; + /** * @title Position */ @@ -37,23 +40,15 @@ const loader = async ( ctx: Context, ): Promise => { const { configLinxImpulse: config } = ctx; - const { position, feature } = props; + const { id, position, feature } = props; const url = new URL(req.url); const skuId = url.searchParams.get("skuId"); const requestHeaders = toRequestHeader(config!); - - /** - * As the Linx APIs do not support slug searches, we need to get the product reference that every product has on URL - */ - const regex = /-(\d+)\/p/; - const match = url.pathname.match(regex); - - if (!match) return null; + const linxImpulse = paths(config!); try { - const linxImpulse = paths(config!); const { products: productsBySlug } = await fetchAPI( - `${linxImpulse.product.getProductBySlug.term(match[1])}`, + `${linxImpulse.product.getProductBySlug.term(id)}`, { headers: requestHeaders }, ); diff --git a/schemas.gen.json b/schemas.gen.json index cff250b7..e5cd2bb9 100644 --- a/schemas.gen.json +++ b/schemas.gen.json @@ -21,6 +21,7 @@ "deco-sites/std/functions/butterCMSPostDetail.ts", "deco-sites/std/functions/butterCMSPosts.ts", "deco-sites/std/functions/butterCMSRelatedPosts.ts", + "deco-sites/std/functions/extractIdFromParam.ts", "deco-sites/std/functions/occProductDetailsPage.ts", "deco-sites/std/functions/requestToParam.ts", "deco-sites/std/functions/shopifyProductDetailsPage.ts", @@ -713,6 +714,54 @@ ], "title": "deco-sites/std/functions/butterCMSRelatedPosts.ts@Props" }, + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@ExtractIDFromParam": { + "anyOf": [ + { + "$ref": "#/definitions/Resolvable" + }, + { + "type": "string", + "title": "deco-sites/std/functions/extractIdFromParam.ts@ExtractIDFromParam" + }, + { + "title": "Extract ID from request parameters", + "description": "Set param to slug for routes of type /:slug", + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@Props" + } + ], + "required": [ + "__resolveType" + ], + "properties": { + "__resolveType": { + "type": "string", + "enum": [ + "deco-sites/std/functions/extractIdFromParam.ts" + ], + "default": "deco-sites/std/functions/extractIdFromParam.ts" + } + } + } + ] + }, + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@Props": { + "type": "object", + "properties": { + "param": { + "type": "string", + "default": "slug", + "description": "Param name to extract from the Request URL", + "title": "Param" + } + }, + "required": [ + "param" + ], + "title": "deco-sites/std/functions/extractIdFromParam.ts@Props" + }, "ZGVjby1zaXRlcy9zdGQvY29tbWVyY2UvdHlwZXMudHM=@Thing": { "title": "Thing" }, @@ -1786,7 +1835,7 @@ } }, { - "title": "VTEX Intelligent Search - Product Details Page", + "title": "VTEX product details page - Intelligent Search", "description": "Works on routes of type /:slug/p", "type": "object", "allOf": [ @@ -1808,7 +1857,7 @@ } }, { - "title": "VTEX Catalog - Product Details Page", + "title": "VTEX Legacy - Product Details page", "description": "For routes of type /:slug/p", "type": "object", "allOf": [ @@ -2076,7 +2125,7 @@ } }, { - "title": "VTEX Intelligent Search - Search Products", + "title": "VTEX product list - Intelligent Search", "description": "Useful for shelves and galleries.", "type": "object", "allOf": [ @@ -2098,7 +2147,7 @@ } }, { - "title": "VTEX Catalog - Search Products", + "title": "VTEX Legacy - Search Products", "description": "Use it in Shelves and static Galleries.", "type": "object", "allOf": [ @@ -2120,7 +2169,7 @@ } }, { - "title": "VTEX Related Products - Catalog", + "title": "VTEX related products - Portal", "description": "Works on routes of type /:slug/p", "type": "object", "allOf": [ @@ -2627,7 +2676,7 @@ } }, { - "title": "VTEX Catalog - Product Listing Page", + "title": "VTEX product listing page - Portal", "description": "Returns data ready for search pages like category,brand pages", "type": "object", "allOf": [ @@ -3457,7 +3506,7 @@ } }, { - "title": "VTEX Intelligent Search - Search Suggestions", + "title": "VTEX search suggestions - Intelligent Search", "type": "object", "allOf": [ { @@ -4858,9 +4907,9 @@ "ZGVjby1zaXRlcy9zdGQvcGFja3MvbGlueEltcHVsc2UvbG9hZGVycy9wcm9kdWN0cy9zaW1pbGFySXRlbXMudHM=@Props": { "type": "object", "properties": { - "slug": { - "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3JlcXVlc3RUb1BhcmFtLnRz@RequestURLParam", - "title": "Slug" + "id": { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@ExtractIDFromParam", + "title": "Id" }, "position": { "anyOf": [ @@ -4901,7 +4950,7 @@ } }, "required": [ - "slug", + "id", "position", "feature" ], @@ -11645,6 +11694,28 @@ } } }, + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==": { + "title": "Extract ID from request parameters", + "description": "Set param to slug for routes of type /:slug", + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@Props" + } + ], + "required": [ + "__resolveType" + ], + "properties": { + "__resolveType": { + "type": "string", + "enum": [ + "deco-sites/std/functions/extractIdFromParam.ts" + ], + "default": "deco-sites/std/functions/extractIdFromParam.ts" + } + } + }, "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL29jY1Byb2R1Y3REZXRhaWxzUGFnZS50cw==": { "title": "Oracle Commerce Cloud Product Page Loader", "description": "Works on routes of type /:slug/p", @@ -12485,7 +12556,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2ludGVsbGlnZW50U2VhcmNoL3Byb2R1Y3REZXRhaWxzUGFnZS50cw==": { - "title": "VTEX Intelligent Search - Product Details Page", + "title": "VTEX product details page - Intelligent Search", "description": "Works on routes of type /:slug/p", "type": "object", "allOf": [ @@ -12507,7 +12578,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2ludGVsbGlnZW50U2VhcmNoL3Byb2R1Y3RMaXN0LnRz": { - "title": "VTEX Intelligent Search - Search Products", + "title": "VTEX product list - Intelligent Search", "description": "Useful for shelves and galleries.", "type": "object", "allOf": [ @@ -12551,7 +12622,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2ludGVsbGlnZW50U2VhcmNoL3N1Z2dlc3Rpb25zLnRz": { - "title": "VTEX Intelligent Search - Search Suggestions", + "title": "VTEX search suggestions - Intelligent Search", "type": "object", "allOf": [ { @@ -12572,7 +12643,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2xlZ2FjeS9wcm9kdWN0RGV0YWlsc1BhZ2UudHM=": { - "title": "VTEX Catalog - Product Details Page", + "title": "VTEX Legacy - Product Details page", "description": "For routes of type /:slug/p", "type": "object", "allOf": [ @@ -12594,7 +12665,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2xlZ2FjeS9wcm9kdWN0TGlzdC50cw==": { - "title": "VTEX Catalog - Search Products", + "title": "VTEX Legacy - Search Products", "description": "Use it in Shelves and static Galleries.", "type": "object", "allOf": [ @@ -12616,7 +12687,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2xlZ2FjeS9wcm9kdWN0TGlzdGluZ1BhZ2UudHM=": { - "title": "VTEX Catalog - Product Listing Page", + "title": "VTEX product listing page - Portal", "description": "Returns data ready for search pages like category,brand pages", "type": "object", "allOf": [ @@ -12638,7 +12709,7 @@ } }, "ZGVjby1zaXRlcy9zdGQvbG9hZGVycy92dGV4L2xlZ2FjeS9yZWxhdGVkUHJvZHVjdHNMb2FkZXIudHM=": { - "title": "VTEX Related Products - Catalog", + "title": "VTEX related products - Portal", "description": "Works on routes of type /:slug/p", "type": "object", "allOf": [ @@ -14168,6 +14239,9 @@ { "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2J1dHRlckNNU1JlbGF0ZWRQb3N0cy50cw==" }, + { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==" + }, { "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL29jY1Byb2R1Y3REZXRhaWxzUGFnZS50cw==" }, @@ -14690,4 +14764,4 @@ } } } -} +} \ No newline at end of file From be8e5a908667c86f7fa8edb58f0387eb07aaf989 Mon Sep 17 00:00:00 2001 From: Gustavo Vasconcellos Date: Wed, 26 Jul 2023 00:28:53 -0300 Subject: [PATCH 3/6] implement extract id function --- functions/extractIdFromParam.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/extractIdFromParam.ts b/functions/extractIdFromParam.ts index 5ca96bfc..2f2a1f58 100644 --- a/functions/extractIdFromParam.ts +++ b/functions/extractIdFromParam.ts @@ -18,8 +18,9 @@ const extractIdFromParam: LoaderFunction< Props, ExtractIDFromParam, FunctionContext -> = (_req, ctx) => ({ - data: ctx.params[ctx.state.$live.param || "slug"], -}); +> = (_req, ctx) => { + const param = ctx.params[ctx.state.$live.param || "slug"]; + return { data: param.split("-")[param.length - 1] }; +}; export default extractIdFromParam; From 978adbc9cededc9f005e4d74675cd99709d1b5ce Mon Sep 17 00:00:00 2001 From: Gustavo Vasconcellos Date: Wed, 26 Jul 2023 01:04:28 -0300 Subject: [PATCH 4/6] fix extract logic --- functions/extractIdFromParam.ts | 3 ++- packs/linxImpulse/loaders/products/similarItems.ts | 1 + schemas.gen.json | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/functions/extractIdFromParam.ts b/functions/extractIdFromParam.ts index 2f2a1f58..82f10e1c 100644 --- a/functions/extractIdFromParam.ts +++ b/functions/extractIdFromParam.ts @@ -20,7 +20,8 @@ const extractIdFromParam: LoaderFunction< FunctionContext > = (_req, ctx) => { const param = ctx.params[ctx.state.$live.param || "slug"]; - return { data: param.split("-")[param.length - 1] }; + const paramArr = param.split("-"); + return { data: paramArr[paramArr.length - 1] }; }; export default extractIdFromParam; diff --git a/packs/linxImpulse/loaders/products/similarItems.ts b/packs/linxImpulse/loaders/products/similarItems.ts index 74e61871..937150cb 100644 --- a/packs/linxImpulse/loaders/products/similarItems.ts +++ b/packs/linxImpulse/loaders/products/similarItems.ts @@ -46,6 +46,7 @@ const loader = async ( const requestHeaders = toRequestHeader(config!); const linxImpulse = paths(config!); + console.log({ props }); try { const { products: productsBySlug } = await fetchAPI( `${linxImpulse.product.getProductBySlug.term(id)}`, diff --git a/schemas.gen.json b/schemas.gen.json index e5cd2bb9..5f068af6 100644 --- a/schemas.gen.json +++ b/schemas.gen.json @@ -14764,4 +14764,4 @@ } } } -} \ No newline at end of file +} From 7bc992de67b1c47d08b491b66fc9c96b5a70bd62 Mon Sep 17 00:00:00 2001 From: Gustavo Vasconcellos Date: Wed, 26 Jul 2023 01:15:37 -0300 Subject: [PATCH 5/6] remove position prop as it possible to search on every position --- .../loaders/products/similarItems.ts | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/packs/linxImpulse/loaders/products/similarItems.ts b/packs/linxImpulse/loaders/products/similarItems.ts index 937150cb..a9885631 100644 --- a/packs/linxImpulse/loaders/products/similarItems.ts +++ b/packs/linxImpulse/loaders/products/similarItems.ts @@ -2,31 +2,22 @@ import type { Product } from "deco-sites/std/commerce/types.ts"; import type { ExtractIDFromParam } from "deco-sites/std/functions/extractIdFromParam.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 { id: ExtractIDFromParam; - - /** - * @title Position - */ - position: Position; - - /** - * @title Feature - */ feature: "SimilarItems" | "FrequentlyBoughtTogether"; } @@ -40,13 +31,12 @@ const loader = async ( ctx: Context, ): Promise => { const { configLinxImpulse: config } = ctx; - const { id, 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!); - console.log({ props }); try { const { products: productsBySlug } = await fetchAPI( `${linxImpulse.product.getProductBySlug.term(id)}`, @@ -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, From fd5453bd7549ece0fb3cba9cd65b3b3511b5f1d3 Mon Sep 17 00:00:00 2001 From: Gustavo Vasconcellos Date: Thu, 27 Jul 2023 11:45:02 -0300 Subject: [PATCH 6/6] rename function to be more specific --- ...dFromParam.ts => productIdFromVTEXSlug.ts} | 17 ++- live.gen.ts | 72 +++++----- .../loaders/products/similarItems.ts | 4 +- schemas.gen.json | 136 +++++++++--------- 4 files changed, 114 insertions(+), 115 deletions(-) rename functions/{extractIdFromParam.ts => productIdFromVTEXSlug.ts} (50%) diff --git a/functions/extractIdFromParam.ts b/functions/productIdFromVTEXSlug.ts similarity index 50% rename from functions/extractIdFromParam.ts rename to functions/productIdFromVTEXSlug.ts index 82f10e1c..aeff2720 100644 --- a/functions/extractIdFromParam.ts +++ b/functions/productIdFromVTEXSlug.ts @@ -1,27 +1,26 @@ import type { FunctionContext, LoaderFunction } from "$live/types.ts"; +import type { RequestURLParam } from "./requestToParam.ts"; -export type ExtractIDFromParam = string; +export type ProductID = string; export interface Props { /** * @default slug * @description Param name to extract from the Request URL */ - param: string; + slug: RequestURLParam; } /** * @title Extract ID from request parameters * @description Set param to slug for routes of type /:slug */ -const extractIdFromParam: LoaderFunction< +const productIdFromVTEXSlug: LoaderFunction< Props, - ExtractIDFromParam, + ProductID, FunctionContext -> = (_req, ctx) => { - const param = ctx.params[ctx.state.$live.param || "slug"]; - const paramArr = param.split("-"); - return { data: paramArr[paramArr.length - 1] }; +> = (_req, _ctx, { slug }) => { + return { data: slug.split("-").at(-1) ?? "" }; }; -export default extractIdFromParam; +export default productIdFromVTEXSlug; diff --git a/live.gen.ts b/live.gen.ts index 017979c1..e64d3b53 100644 --- a/live.gen.ts +++ b/live.gen.ts @@ -12,24 +12,24 @@ 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/extractIdFromParam.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"; @@ -154,33 +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/extractIdFromParam.ts": $26, - "deco-sites/std/functions/occProductDetailsPage.ts": $15, + "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": $24, - "deco-sites/std/functions/shopifyProductList.ts": $23, - "deco-sites/std/functions/shopifyProductListingPage.ts": $12, + "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, diff --git a/packs/linxImpulse/loaders/products/similarItems.ts b/packs/linxImpulse/loaders/products/similarItems.ts index a9885631..a2267ab2 100644 --- a/packs/linxImpulse/loaders/products/similarItems.ts +++ b/packs/linxImpulse/loaders/products/similarItems.ts @@ -1,5 +1,5 @@ import type { Product } from "deco-sites/std/commerce/types.ts"; -import type { ExtractIDFromParam } from "deco-sites/std/functions/extractIdFromParam.ts"; +import type { ProductID } from "deco-sites/std/functions/productIdFromVTEXSlug.ts"; import type { PagesRecommendationsResponse, SearchProductsResponse, @@ -17,7 +17,7 @@ import { HttpError } from "deco-sites/std/utils/HttpError.ts"; import { fetchAPI } from "deco-sites/std/utils/fetch.ts"; export interface Props { - id: ExtractIDFromParam; + id: ProductID; feature: "SimilarItems" | "FrequentlyBoughtTogether"; } diff --git a/schemas.gen.json b/schemas.gen.json index ecf5b7b9..237a63e5 100644 --- a/schemas.gen.json +++ b/schemas.gen.json @@ -21,8 +21,8 @@ "deco-sites/std/functions/butterCMSPostDetail.ts", "deco-sites/std/functions/butterCMSPosts.ts", "deco-sites/std/functions/butterCMSRelatedPosts.ts", - "deco-sites/std/functions/extractIdFromParam.ts", "deco-sites/std/functions/occProductDetailsPage.ts", + "deco-sites/std/functions/productIdFromVTEXSlug.ts", "deco-sites/std/functions/requestToParam.ts", "deco-sites/std/functions/shopifyProductDetailsPage.ts", "deco-sites/std/functions/shopifyProductList.ts", @@ -717,54 +717,6 @@ ], "title": "deco-sites/std/functions/butterCMSRelatedPosts.ts@Props" }, - "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@ExtractIDFromParam": { - "anyOf": [ - { - "$ref": "#/definitions/Resolvable" - }, - { - "type": "string", - "title": "deco-sites/std/functions/extractIdFromParam.ts@ExtractIDFromParam" - }, - { - "title": "Extract ID from request parameters", - "description": "Set param to slug for routes of type /:slug", - "type": "object", - "allOf": [ - { - "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@Props" - } - ], - "required": [ - "__resolveType" - ], - "properties": { - "__resolveType": { - "type": "string", - "enum": [ - "deco-sites/std/functions/extractIdFromParam.ts" - ], - "default": "deco-sites/std/functions/extractIdFromParam.ts" - } - } - } - ] - }, - "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@Props": { - "type": "object", - "properties": { - "param": { - "type": "string", - "default": "slug", - "description": "Param name to extract from the Request URL", - "title": "Param" - } - }, - "required": [ - "param" - ], - "title": "deco-sites/std/functions/extractIdFromParam.ts@Props" - }, "ZGVjby1zaXRlcy9zdGQvY29tbWVyY2UvdHlwZXMudHM=@Thing": { "title": "Thing" }, @@ -1899,6 +1851,39 @@ } ] }, + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==@ProductID": { + "anyOf": [ + { + "$ref": "#/definitions/Resolvable" + }, + { + "type": "string", + "title": "deco-sites/std/functions/productIdFromVTEXSlug.ts@ProductID" + }, + { + "title": "Extract ID from request parameters", + "description": "Set param to slug for routes of type /:slug", + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==@Props" + } + ], + "required": [ + "__resolveType" + ], + "properties": { + "__resolveType": { + "type": "string", + "enum": [ + "deco-sites/std/functions/productIdFromVTEXSlug.ts" + ], + "default": "deco-sites/std/functions/productIdFromVTEXSlug.ts" + } + } + } + ] + }, "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3JlcXVlc3RUb1BhcmFtLnRz@RequestURLParam": { "anyOf": [ { @@ -1906,7 +1891,7 @@ }, { "type": "string", - "title": "deco-sites/std/functions/requestToParam.ts@RequestURLParam" + "title": "RequestURLParam" }, { "title": "Get params from request parameters", @@ -1932,6 +1917,21 @@ } ] }, + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==@Props": { + "type": "object", + "properties": { + "slug": { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3JlcXVlc3RUb1BhcmFtLnRz@RequestURLParam", + "default": "slug", + "description": "Param name to extract from the Request URL", + "title": "Slug" + } + }, + "required": [ + "slug" + ], + "title": "deco-sites/std/functions/productIdFromVTEXSlug.ts@Props" + }, "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3JlcXVlc3RUb1BhcmFtLnRz@Props": { "type": "object", "properties": { @@ -4973,7 +4973,7 @@ "type": "object", "properties": { "id": { - "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@ExtractIDFromParam", + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==@ProductID", "title": "Id" }, "feature": { @@ -11727,15 +11727,10 @@ } } }, - "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==": { - "title": "Extract ID from request parameters", - "description": "Set param to slug for routes of type /:slug", + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL29jY1Byb2R1Y3REZXRhaWxzUGFnZS50cw==": { + "title": "Oracle Commerce Cloud Product Page Loader", + "description": "Works on routes of type /:slug/p", "type": "object", - "allOf": [ - { - "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==@Props" - } - ], "required": [ "__resolveType" ], @@ -11743,16 +11738,21 @@ "__resolveType": { "type": "string", "enum": [ - "deco-sites/std/functions/extractIdFromParam.ts" + "deco-sites/std/functions/occProductDetailsPage.ts" ], - "default": "deco-sites/std/functions/extractIdFromParam.ts" + "default": "deco-sites/std/functions/occProductDetailsPage.ts" } } }, - "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL29jY1Byb2R1Y3REZXRhaWxzUGFnZS50cw==": { - "title": "Oracle Commerce Cloud Product Page Loader", - "description": "Works on routes of type /:slug/p", + "ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==": { + "title": "Extract ID from request parameters", + "description": "Set param to slug for routes of type /:slug", "type": "object", + "allOf": [ + { + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==@Props" + } + ], "required": [ "__resolveType" ], @@ -11760,9 +11760,9 @@ "__resolveType": { "type": "string", "enum": [ - "deco-sites/std/functions/occProductDetailsPage.ts" + "deco-sites/std/functions/productIdFromVTEXSlug.ts" ], - "default": "deco-sites/std/functions/occProductDetailsPage.ts" + "default": "deco-sites/std/functions/productIdFromVTEXSlug.ts" } } }, @@ -14331,10 +14331,10 @@ "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2J1dHRlckNNU1JlbGF0ZWRQb3N0cy50cw==" }, { - "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL2V4dHJhY3RJZEZyb21QYXJhbS50cw==" + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL29jY1Byb2R1Y3REZXRhaWxzUGFnZS50cw==" }, { - "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL29jY1Byb2R1Y3REZXRhaWxzUGFnZS50cw==" + "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3Byb2R1Y3RJZEZyb21WVEVYU2x1Zy50cw==" }, { "$ref": "#/definitions/ZGVjby1zaXRlcy9zdGQvZnVuY3Rpb25zL3JlcXVlc3RUb1BhcmFtLnRz"