@gem-sdk/core 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/helpers/prefetch-queries.js +20 -26
- package/dist/cjs/helpers/queries/get-products.js +37 -42
- package/dist/cjs/hooks/shop/use-products-query.js +1 -1
- package/dist/esm/helpers/prefetch-queries.js +20 -26
- package/dist/esm/helpers/queries/get-products.js +37 -42
- package/dist/esm/hooks/shop/use-products-query.js +1 -1
- package/package.json +1 -1
|
@@ -14,45 +14,34 @@ const prefetchQueries = (input, isSample) => {
|
|
|
14
14
|
case 'Product': {
|
|
15
15
|
if (item.settings?.isAuto)
|
|
16
16
|
break;
|
|
17
|
+
const variables = { id: item.settings?.productSetting?.productId ?? 'latest', isSample };
|
|
17
18
|
data = {
|
|
18
|
-
key: useSWR.unstable_serialize([
|
|
19
|
-
'query/product',
|
|
20
|
-
{ id: item.settings?.productSetting?.productId ?? 'latest', isSample },
|
|
21
|
-
]),
|
|
19
|
+
key: useSWR.unstable_serialize(['query/product', variables]),
|
|
22
20
|
func: getProduct.getProduct,
|
|
23
|
-
variables
|
|
21
|
+
variables,
|
|
24
22
|
};
|
|
25
23
|
break;
|
|
26
24
|
}
|
|
27
25
|
case 'ProductList': {
|
|
28
26
|
if (item?.settings?.productSrc === 'Collection') {
|
|
27
|
+
const variables = {
|
|
28
|
+
id: item.settings?.collectionId ?? 'latest',
|
|
29
|
+
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
30
|
+
orderBy: item.settings?.orderBy,
|
|
31
|
+
isSample,
|
|
32
|
+
};
|
|
29
33
|
data = {
|
|
30
|
-
key: useSWR.unstable_serialize([
|
|
31
|
-
'query/collection',
|
|
32
|
-
{
|
|
33
|
-
id: item.settings?.collectionId ?? 'latest',
|
|
34
|
-
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
35
|
-
orderBy: item.settings?.orderBy,
|
|
36
|
-
isSample,
|
|
37
|
-
},
|
|
38
|
-
]),
|
|
34
|
+
key: useSWR.unstable_serialize(['query/collection', variables]),
|
|
39
35
|
func: getCollection.getCollection,
|
|
40
|
-
variables
|
|
41
|
-
id: item.settings?.collectionId ?? 'latest',
|
|
42
|
-
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
43
|
-
orderBy: item.settings?.orderBy,
|
|
44
|
-
isSample,
|
|
45
|
-
},
|
|
36
|
+
variables,
|
|
46
37
|
};
|
|
47
38
|
}
|
|
48
39
|
else {
|
|
40
|
+
const variables = { ids: [...(item?.settings?.productIds ?? [])].sort(), isSample };
|
|
49
41
|
data = {
|
|
50
|
-
key: useSWR.unstable_serialize([
|
|
51
|
-
'query/products',
|
|
52
|
-
{ ids: item?.settings?.productIds?.sort(), isSample },
|
|
53
|
-
]),
|
|
42
|
+
key: useSWR.unstable_serialize(['query/products', variables]),
|
|
54
43
|
func: getProducts.getProducts,
|
|
55
|
-
variables
|
|
44
|
+
variables,
|
|
56
45
|
};
|
|
57
46
|
}
|
|
58
47
|
break;
|
|
@@ -62,7 +51,12 @@ const prefetchQueries = (input, isSample) => {
|
|
|
62
51
|
queries.push(data);
|
|
63
52
|
}
|
|
64
53
|
});
|
|
65
|
-
return queries
|
|
54
|
+
return queries.reduce((accumulator, current) => {
|
|
55
|
+
if (!accumulator.some((x) => x.key === current.key)) {
|
|
56
|
+
return [...accumulator, current];
|
|
57
|
+
}
|
|
58
|
+
return accumulator;
|
|
59
|
+
}, []);
|
|
66
60
|
};
|
|
67
61
|
|
|
68
62
|
exports.prefetchQueries = prefetchQueries;
|
|
@@ -5,6 +5,7 @@ var getCollection = require('./get-collection.js');
|
|
|
5
5
|
var getProduct = require('./get-product.js');
|
|
6
6
|
|
|
7
7
|
const getProducts = async (fetcher, { ids, isSample }) => {
|
|
8
|
+
console.log(11111, ids);
|
|
8
9
|
const products = await loopFetchProducts(fetcher, { ids, isSample });
|
|
9
10
|
if (!products) {
|
|
10
11
|
throw new Error('Product not found');
|
|
@@ -13,7 +14,33 @@ const getProducts = async (fetcher, { ids, isSample }) => {
|
|
|
13
14
|
mergeVariantsToProducts(fetcher, products, isSample),
|
|
14
15
|
mergeMediasToProducts(fetcher, products, isSample),
|
|
15
16
|
]);
|
|
16
|
-
return {
|
|
17
|
+
return {
|
|
18
|
+
...products,
|
|
19
|
+
products: {
|
|
20
|
+
...products.products,
|
|
21
|
+
edges: products.products?.edges.map((edge) => {
|
|
22
|
+
const medias = mergedMedias.find((item) => item.productId === edge.node?.baseID);
|
|
23
|
+
const variants = mergedVariant.find((item) => item.productId === edge.node?.baseID);
|
|
24
|
+
if (edge.node) {
|
|
25
|
+
return {
|
|
26
|
+
...edge,
|
|
27
|
+
node: {
|
|
28
|
+
...edge.node,
|
|
29
|
+
variants: {
|
|
30
|
+
...edge.node?.variants,
|
|
31
|
+
edges: variants?.items ?? [],
|
|
32
|
+
},
|
|
33
|
+
medias: {
|
|
34
|
+
...edge.node?.medias,
|
|
35
|
+
edges: medias?.items ?? [],
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return edge;
|
|
41
|
+
}) ?? [],
|
|
42
|
+
},
|
|
43
|
+
};
|
|
17
44
|
};
|
|
18
45
|
const fetchProducts = async (fetcher, variables) => {
|
|
19
46
|
return fetcher([products_generated.ProductsDocument, variables]);
|
|
@@ -41,7 +68,7 @@ const loopFetchProducts = async (fetcher, { ids, isSample }) => {
|
|
|
41
68
|
where: {
|
|
42
69
|
isStorefront: true,
|
|
43
70
|
isSample: isSample,
|
|
44
|
-
...(ids?.length ? { baseIDIn: ids
|
|
71
|
+
...(ids?.length ? { baseIDIn: ids } : {}),
|
|
45
72
|
},
|
|
46
73
|
after: productAfterFetcher,
|
|
47
74
|
};
|
|
@@ -69,54 +96,22 @@ const loopFetchProducts = async (fetcher, { ids, isSample }) => {
|
|
|
69
96
|
};
|
|
70
97
|
const mergeVariantsToProducts = async (fetcher, products, isSample) => {
|
|
71
98
|
const productsWithVariants = await Promise.all(products?.products?.edges?.map(async (product) => {
|
|
72
|
-
|
|
73
|
-
const res = {
|
|
74
|
-
...product,
|
|
75
|
-
...(product.node
|
|
76
|
-
? {
|
|
77
|
-
node: {
|
|
78
|
-
...product.node,
|
|
79
|
-
variants: variants.length ? { edges: variants } : undefined,
|
|
80
|
-
},
|
|
81
|
-
}
|
|
82
|
-
: {}),
|
|
83
|
-
};
|
|
84
|
-
return res;
|
|
85
|
-
});
|
|
86
|
-
}) ?? []).then((res) => {
|
|
99
|
+
const variants = await getProduct.fetchVariants(fetcher, { id: product?.node?.baseID, isSample });
|
|
87
100
|
return {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
pageInfo: products?.products?.pageInfo,
|
|
91
|
-
},
|
|
101
|
+
productId: product?.node?.baseID,
|
|
102
|
+
items: variants,
|
|
92
103
|
};
|
|
93
|
-
});
|
|
104
|
+
}) ?? []);
|
|
94
105
|
return productsWithVariants;
|
|
95
106
|
};
|
|
96
107
|
const mergeMediasToProducts = async (fetcher, products, isSample) => {
|
|
97
108
|
const productsWithMedias = await Promise.all(products?.products?.edges?.map(async (product) => {
|
|
98
|
-
|
|
99
|
-
const res = {
|
|
100
|
-
...product,
|
|
101
|
-
...(product.node
|
|
102
|
-
? {
|
|
103
|
-
node: {
|
|
104
|
-
...product.node,
|
|
105
|
-
medias: media.length ? { edges: media } : undefined,
|
|
106
|
-
},
|
|
107
|
-
}
|
|
108
|
-
: {}),
|
|
109
|
-
};
|
|
110
|
-
return res;
|
|
111
|
-
});
|
|
112
|
-
}) ?? []).then((res) => {
|
|
109
|
+
const medias = await getProduct.fetchMedias(fetcher, { id: product?.node?.baseID, isSample });
|
|
113
110
|
return {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
pageInfo: products?.products?.pageInfo,
|
|
117
|
-
},
|
|
111
|
+
productId: product?.node?.baseID,
|
|
112
|
+
items: medias,
|
|
118
113
|
};
|
|
119
|
-
});
|
|
114
|
+
}) ?? []);
|
|
120
115
|
return productsWithMedias;
|
|
121
116
|
};
|
|
122
117
|
// future can be used for fetching products with paging ids
|
|
@@ -8,7 +8,7 @@ var useFetchHandle = require('../useFetchHandle.js');
|
|
|
8
8
|
const useProductsQuery = (ids, options) => {
|
|
9
9
|
const fetcher = useFetchHandle.useFetchHandle();
|
|
10
10
|
const isSample = shop.useIsSampleProduct();
|
|
11
|
-
return useSWR(['query/products', { ids, isSample }], async ([, arg]) => {
|
|
11
|
+
return useSWR(['query/products', { ids: [...(ids ?? [])]?.sort(), isSample }], async ([, arg]) => {
|
|
12
12
|
return getProducts.getProducts(fetcher, arg);
|
|
13
13
|
}, options);
|
|
14
14
|
};
|
|
@@ -12,45 +12,34 @@ const prefetchQueries = (input, isSample) => {
|
|
|
12
12
|
case 'Product': {
|
|
13
13
|
if (item.settings?.isAuto)
|
|
14
14
|
break;
|
|
15
|
+
const variables = { id: item.settings?.productSetting?.productId ?? 'latest', isSample };
|
|
15
16
|
data = {
|
|
16
|
-
key: unstable_serialize([
|
|
17
|
-
'query/product',
|
|
18
|
-
{ id: item.settings?.productSetting?.productId ?? 'latest', isSample },
|
|
19
|
-
]),
|
|
17
|
+
key: unstable_serialize(['query/product', variables]),
|
|
20
18
|
func: getProduct,
|
|
21
|
-
variables
|
|
19
|
+
variables,
|
|
22
20
|
};
|
|
23
21
|
break;
|
|
24
22
|
}
|
|
25
23
|
case 'ProductList': {
|
|
26
24
|
if (item?.settings?.productSrc === 'Collection') {
|
|
25
|
+
const variables = {
|
|
26
|
+
id: item.settings?.collectionId ?? 'latest',
|
|
27
|
+
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
28
|
+
orderBy: item.settings?.orderBy,
|
|
29
|
+
isSample,
|
|
30
|
+
};
|
|
27
31
|
data = {
|
|
28
|
-
key: unstable_serialize([
|
|
29
|
-
'query/collection',
|
|
30
|
-
{
|
|
31
|
-
id: item.settings?.collectionId ?? 'latest',
|
|
32
|
-
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
33
|
-
orderBy: item.settings?.orderBy,
|
|
34
|
-
isSample,
|
|
35
|
-
},
|
|
36
|
-
]),
|
|
32
|
+
key: unstable_serialize(['query/collection', variables]),
|
|
37
33
|
func: getCollection,
|
|
38
|
-
variables
|
|
39
|
-
id: item.settings?.collectionId ?? 'latest',
|
|
40
|
-
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
41
|
-
orderBy: item.settings?.orderBy,
|
|
42
|
-
isSample,
|
|
43
|
-
},
|
|
34
|
+
variables,
|
|
44
35
|
};
|
|
45
36
|
}
|
|
46
37
|
else {
|
|
38
|
+
const variables = { ids: [...(item?.settings?.productIds ?? [])].sort(), isSample };
|
|
47
39
|
data = {
|
|
48
|
-
key: unstable_serialize([
|
|
49
|
-
'query/products',
|
|
50
|
-
{ ids: item?.settings?.productIds?.sort(), isSample },
|
|
51
|
-
]),
|
|
40
|
+
key: unstable_serialize(['query/products', variables]),
|
|
52
41
|
func: getProducts,
|
|
53
|
-
variables
|
|
42
|
+
variables,
|
|
54
43
|
};
|
|
55
44
|
}
|
|
56
45
|
break;
|
|
@@ -60,7 +49,12 @@ const prefetchQueries = (input, isSample) => {
|
|
|
60
49
|
queries.push(data);
|
|
61
50
|
}
|
|
62
51
|
});
|
|
63
|
-
return queries
|
|
52
|
+
return queries.reduce((accumulator, current) => {
|
|
53
|
+
if (!accumulator.some((x) => x.key === current.key)) {
|
|
54
|
+
return [...accumulator, current];
|
|
55
|
+
}
|
|
56
|
+
return accumulator;
|
|
57
|
+
}, []);
|
|
64
58
|
};
|
|
65
59
|
|
|
66
60
|
export { prefetchQueries };
|
|
@@ -3,6 +3,7 @@ import { calculateFirstProduct } from './get-collection.js';
|
|
|
3
3
|
import { fetchVariants, fetchMedias } from './get-product.js';
|
|
4
4
|
|
|
5
5
|
const getProducts = async (fetcher, { ids, isSample }) => {
|
|
6
|
+
console.log(11111, ids);
|
|
6
7
|
const products = await loopFetchProducts(fetcher, { ids, isSample });
|
|
7
8
|
if (!products) {
|
|
8
9
|
throw new Error('Product not found');
|
|
@@ -11,7 +12,33 @@ const getProducts = async (fetcher, { ids, isSample }) => {
|
|
|
11
12
|
mergeVariantsToProducts(fetcher, products, isSample),
|
|
12
13
|
mergeMediasToProducts(fetcher, products, isSample),
|
|
13
14
|
]);
|
|
14
|
-
return {
|
|
15
|
+
return {
|
|
16
|
+
...products,
|
|
17
|
+
products: {
|
|
18
|
+
...products.products,
|
|
19
|
+
edges: products.products?.edges.map((edge) => {
|
|
20
|
+
const medias = mergedMedias.find((item) => item.productId === edge.node?.baseID);
|
|
21
|
+
const variants = mergedVariant.find((item) => item.productId === edge.node?.baseID);
|
|
22
|
+
if (edge.node) {
|
|
23
|
+
return {
|
|
24
|
+
...edge,
|
|
25
|
+
node: {
|
|
26
|
+
...edge.node,
|
|
27
|
+
variants: {
|
|
28
|
+
...edge.node?.variants,
|
|
29
|
+
edges: variants?.items ?? [],
|
|
30
|
+
},
|
|
31
|
+
medias: {
|
|
32
|
+
...edge.node?.medias,
|
|
33
|
+
edges: medias?.items ?? [],
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return edge;
|
|
39
|
+
}) ?? [],
|
|
40
|
+
},
|
|
41
|
+
};
|
|
15
42
|
};
|
|
16
43
|
const fetchProducts = async (fetcher, variables) => {
|
|
17
44
|
return fetcher([ProductsDocument, variables]);
|
|
@@ -39,7 +66,7 @@ const loopFetchProducts = async (fetcher, { ids, isSample }) => {
|
|
|
39
66
|
where: {
|
|
40
67
|
isStorefront: true,
|
|
41
68
|
isSample: isSample,
|
|
42
|
-
...(ids?.length ? { baseIDIn: ids
|
|
69
|
+
...(ids?.length ? { baseIDIn: ids } : {}),
|
|
43
70
|
},
|
|
44
71
|
after: productAfterFetcher,
|
|
45
72
|
};
|
|
@@ -67,54 +94,22 @@ const loopFetchProducts = async (fetcher, { ids, isSample }) => {
|
|
|
67
94
|
};
|
|
68
95
|
const mergeVariantsToProducts = async (fetcher, products, isSample) => {
|
|
69
96
|
const productsWithVariants = await Promise.all(products?.products?.edges?.map(async (product) => {
|
|
70
|
-
|
|
71
|
-
const res = {
|
|
72
|
-
...product,
|
|
73
|
-
...(product.node
|
|
74
|
-
? {
|
|
75
|
-
node: {
|
|
76
|
-
...product.node,
|
|
77
|
-
variants: variants.length ? { edges: variants } : undefined,
|
|
78
|
-
},
|
|
79
|
-
}
|
|
80
|
-
: {}),
|
|
81
|
-
};
|
|
82
|
-
return res;
|
|
83
|
-
});
|
|
84
|
-
}) ?? []).then((res) => {
|
|
97
|
+
const variants = await fetchVariants(fetcher, { id: product?.node?.baseID, isSample });
|
|
85
98
|
return {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
pageInfo: products?.products?.pageInfo,
|
|
89
|
-
},
|
|
99
|
+
productId: product?.node?.baseID,
|
|
100
|
+
items: variants,
|
|
90
101
|
};
|
|
91
|
-
});
|
|
102
|
+
}) ?? []);
|
|
92
103
|
return productsWithVariants;
|
|
93
104
|
};
|
|
94
105
|
const mergeMediasToProducts = async (fetcher, products, isSample) => {
|
|
95
106
|
const productsWithMedias = await Promise.all(products?.products?.edges?.map(async (product) => {
|
|
96
|
-
|
|
97
|
-
const res = {
|
|
98
|
-
...product,
|
|
99
|
-
...(product.node
|
|
100
|
-
? {
|
|
101
|
-
node: {
|
|
102
|
-
...product.node,
|
|
103
|
-
medias: media.length ? { edges: media } : undefined,
|
|
104
|
-
},
|
|
105
|
-
}
|
|
106
|
-
: {}),
|
|
107
|
-
};
|
|
108
|
-
return res;
|
|
109
|
-
});
|
|
110
|
-
}) ?? []).then((res) => {
|
|
107
|
+
const medias = await fetchMedias(fetcher, { id: product?.node?.baseID, isSample });
|
|
111
108
|
return {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
pageInfo: products?.products?.pageInfo,
|
|
115
|
-
},
|
|
109
|
+
productId: product?.node?.baseID,
|
|
110
|
+
items: medias,
|
|
116
111
|
};
|
|
117
|
-
});
|
|
112
|
+
}) ?? []);
|
|
118
113
|
return productsWithMedias;
|
|
119
114
|
};
|
|
120
115
|
// future can be used for fetching products with paging ids
|
|
@@ -6,7 +6,7 @@ import { useFetchHandle } from '../useFetchHandle.js';
|
|
|
6
6
|
const useProductsQuery = (ids, options) => {
|
|
7
7
|
const fetcher = useFetchHandle();
|
|
8
8
|
const isSample = useIsSampleProduct();
|
|
9
|
-
return useSWR(['query/products', { ids, isSample }], async ([, arg]) => {
|
|
9
|
+
return useSWR(['query/products', { ids: [...(ids ?? [])]?.sort(), isSample }], async ([, arg]) => {
|
|
10
10
|
return getProducts(fetcher, arg);
|
|
11
11
|
}, options);
|
|
12
12
|
};
|