@gem-sdk/core 1.0.8 → 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.
@@ -37,7 +37,7 @@ const prefetchQueries = (input, isSample) => {
37
37
  };
38
38
  }
39
39
  else {
40
- const variables = { ids: item?.settings?.productIds?.sort?.(), isSample };
40
+ const variables = { ids: [...(item?.settings?.productIds ?? [])].sort(), isSample };
41
41
  data = {
42
42
  key: useSWR.unstable_serialize(['query/products', variables]),
43
43
  func: getProducts.getProducts,
@@ -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 { ...mergedMedias, ...mergedVariant };
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.sort() } : {}),
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
- return getProduct.fetchVariants(fetcher, { id: product?.node?.baseID, isSample }).then((variants) => {
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
- products: {
89
- edges: res,
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
- return getProduct.fetchMedias(fetcher, { id: product?.node?.baseID, isSample }).then((media) => {
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
- products: {
115
- edges: res,
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
  };
@@ -35,7 +35,7 @@ const prefetchQueries = (input, isSample) => {
35
35
  };
36
36
  }
37
37
  else {
38
- const variables = { ids: item?.settings?.productIds?.sort?.(), isSample };
38
+ const variables = { ids: [...(item?.settings?.productIds ?? [])].sort(), isSample };
39
39
  data = {
40
40
  key: unstable_serialize(['query/products', variables]),
41
41
  func: getProducts,
@@ -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 { ...mergedMedias, ...mergedVariant };
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.sort() } : {}),
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
- return fetchVariants(fetcher, { id: product?.node?.baseID, isSample }).then((variants) => {
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
- products: {
87
- edges: res,
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
- return fetchMedias(fetcher, { id: product?.node?.baseID, isSample }).then((media) => {
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
- products: {
113
- edges: res,
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gem-sdk/core",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "main": "dist/cjs/index.js",