/
productList.ts
193 lines (160 loc) · 4.84 KB
/
productList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { Product } from "../../../commerce/types.ts";
import { STALE } from "../../../utils/fetch.ts";
import { AppContext } from "../../mod.ts";
import { toSegmentParams } from "../../utils/legacy.ts";
import { getSegmentFromBag, withSegmentCookie } from "../../utils/segment.ts";
import { withIsSimilarTo } from "../../utils/similars.ts";
import { toProduct } from "../../utils/transform.ts";
import type { LegacySort } from "../../utils/types.ts";
export interface CollectionProps extends CommonProps {
// TODO: pattern property isn't being handled by RJSF
/**
* @description Collection ID or (Product Cluster id). For more info: https://developers.vtex.com/docs/api-reference/search-api#get-/api/catalog_system/pub/products/search .
* @pattern \d*
*/
collection: string;
/**
* @description search sort parameter
*/
sort?: LegacySort;
/** @description total number of items to display */
count: number;
}
export interface TermProps extends CommonProps {
/** @description term to use on search */
term?: string;
/**
* @description search sort parameter
*/
sort?: LegacySort;
/** @description total number of items to display */
count: number;
}
export interface FQProps extends CommonProps {
/** @description fq's */
fq: string[];
/**
* @description search sort parameter
*/
sort?: LegacySort;
/** @description total number of items to display */
count: number;
}
export interface SkuIDProps extends CommonProps {
/**
* @description SKU ids to retrieve
*/
ids?: string[];
}
export interface ProductIDProps extends CommonProps {
/**
* @description Product ids to retrieve
*/
productIds?: string[];
}
export interface CommonProps {
/**
* @description Include similar products
* @deprecated Use product extensions instead
*/
similars?: boolean;
}
export type Props = {
props:
| CollectionProps
| TermProps
| ProductIDProps
| SkuIDProps
| FQProps;
};
// deno-lint-ignore no-explicit-any
const isCollectionProps = (p: any): p is CollectionProps =>
typeof p.collection === "string" && typeof p.count === "number";
// deno-lint-ignore no-explicit-any
const isValidArrayProp = (prop: any) => Array.isArray(prop) && prop.length > 0;
// deno-lint-ignore no-explicit-any
const isSKUIDProps = (p: any): p is SkuIDProps => isValidArrayProp(p.ids);
// deno-lint-ignore no-explicit-any
const isProductIDProps = (p: any): p is ProductIDProps =>
isValidArrayProp(p.productIds);
// deno-lint-ignore no-explicit-any
const isFQProps = (p: any): p is FQProps => isValidArrayProp(p.fq);
const fromProps = ({ props }: Props) => {
const params = { fq: [] } as {
_from?: number;
_to?: number;
fq: string[];
ft?: string;
O?: LegacySort;
};
if (isSKUIDProps(props)) {
const skuIds = props.ids || [];
skuIds.forEach((skuId) => params.fq.push(`skuId:${skuId}`));
params._from = 0;
params._to = Math.max(skuIds.length - 1, 0);
return params;
}
if (isProductIDProps(props)) {
const productIds = props.productIds || [];
productIds.forEach((productId) => params.fq.push(`productId:${productId}`));
params._from = 0;
params._to = Math.max(productIds.length - 1, 0);
return params;
}
const count = props.count ?? 12;
params._from = 0;
params._to = Math.max(count - 1, 0);
if (props.sort) {
params.O = props.sort;
}
if (isCollectionProps(props)) {
params.fq.push(`productClusterIds:${props.collection}`);
return params;
}
if (isFQProps(props)) {
params.fq = props.fq;
return params;
}
if (props.term) {
params.ft = encodeURIComponent(props.term);
}
return params;
};
/**
* @title VTEX Integration - Legacy Search
* @description Product List loader
*/
const loader = async (
expandedProps: Props,
req: Request,
ctx: AppContext,
): Promise<Product[] | null> => {
const props = expandedProps.props ??
(expandedProps as unknown as Props["props"]);
const { vcsDeprecated } = ctx;
const { url: baseUrl } = req;
const segment = getSegmentFromBag(ctx);
const segmentParams = toSegmentParams(segment);
const params = fromProps({ props });
const vtexProducts = await vcsDeprecated
["GET /api/catalog_system/pub/products/search/:term?"]({
...segmentParams,
...params,
}, { ...STALE, headers: withSegmentCookie(segment) })
.then((res) => res.json());
// Transform VTEX product format into schema.org's compatible format
// If a property is missing from the final `products` array you can add
// it in here
const products = vtexProducts.map((p) =>
toProduct(p, p.items[0], 0, {
baseUrl: baseUrl,
priceCurrency: "BRL", // config!.defaultPriceCurrency, // TODO fix currency
})
);
return Promise.all(
products.map((product) =>
props.similars ? withIsSimilarTo(req, ctx, product) : product
),
);
};
export default loader;