@shopkit/data-layer 1.0.0 → 1.1.0

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/index.mjs CHANGED
@@ -848,14 +848,25 @@ var BaseCommerceClient = class {
848
848
  }
849
849
  /**
850
850
  * Retrieve multiple products by their handles
851
- * @param handles - Array of product handles
851
+ * @param params - Object containing array of product handles
852
852
  * @param options - Request options (headers, timeout, etc.)
853
853
  * @returns Promise resolving to array of products
854
854
  * @throws NotImplementedError if not implemented in adapter
855
855
  */
856
- getProductsByHandles(handles, options) {
856
+ getProductsByHandles(params, options) {
857
857
  this.throwNotImplemented("getProductsByHandles");
858
858
  }
859
+ /**
860
+ * Retrieve product recommendations for a given product
861
+ * Uses platform-specific recommendation algorithms (e.g., Shopify's productRecommendations)
862
+ * @param productId - Product ID or handle to get recommendations for
863
+ * @param options - Request options (headers, timeout, etc.)
864
+ * @returns Promise resolving to array of recommended products
865
+ * @throws NotImplementedError if not implemented in adapter
866
+ */
867
+ getProductRecommendations(params, options) {
868
+ this.throwNotImplemented("getProductRecommendations");
869
+ }
859
870
  // ========================================
860
871
  // COLLECTION OPERATIONS
861
872
  // ========================================
@@ -897,8 +908,8 @@ var BaseCommerceClient = class {
897
908
  * @returns Promise resolving to collections data with individual query results
898
909
  * @throws NotImplementedError if not implemented in adapter
899
910
  */
900
- getCollectionsByHandels(params, options) {
901
- this.throwNotImplemented("getCollectionsByHandels");
911
+ getCollectionsByHandles(params, options) {
912
+ this.throwNotImplemented("getCollectionsByHandles");
902
913
  }
903
914
  /**
904
915
  * Retrieve available filters for collections
@@ -935,12 +946,12 @@ var BaseCommerceClient = class {
935
946
  }
936
947
  /**
937
948
  * Create a new order
938
- * @param data - Order data to create
949
+ * @param params - Order data to create
939
950
  * @param options - Request options (headers, timeout, etc.)
940
951
  * @returns Promise resolving to created order
941
952
  * @throws NotImplementedError if not implemented in adapter
942
953
  */
943
- createOrder(data, options) {
954
+ createOrder(params, options) {
944
955
  this.throwNotImplemented("createOrder");
945
956
  }
946
957
  /**
@@ -998,12 +1009,12 @@ var BaseCommerceClient = class {
998
1009
  }
999
1010
  /**
1000
1011
  * Create a new customer account
1001
- * @param data - Customer data to create
1012
+ * @param params - Customer data to create
1002
1013
  * @param options - Request options (headers, timeout, etc.)
1003
1014
  * @returns Promise resolving to created customer
1004
1015
  * @throws NotImplementedError if not implemented in adapter
1005
1016
  */
1006
- createCustomer(data, options) {
1017
+ createCustomer(params, options) {
1007
1018
  this.throwNotImplemented("createCustomer");
1008
1019
  }
1009
1020
  /**
@@ -1081,28 +1092,6 @@ var BaseCommerceClient = class {
1081
1092
  createCheckout(params, options) {
1082
1093
  this.throwNotImplemented("createCheckout");
1083
1094
  }
1084
- /**
1085
- * Update an existing checkout session
1086
- * @param id - Checkout identifier
1087
- * @param data - Checkout update data
1088
- * @param options - Request options (headers, timeout, etc.)
1089
- * @returns Promise resolving to updated checkout
1090
- * @throws NotImplementedError if not implemented in adapter
1091
- */
1092
- updateCheckout(id, data, options) {
1093
- this.throwNotImplemented("updateCheckout");
1094
- }
1095
- /**
1096
- * Complete a checkout and create an order
1097
- * @param id - Checkout identifier
1098
- * @param paymentData - Payment information
1099
- * @param options - Request options (headers, timeout, etc.)
1100
- * @returns Promise resolving to created order
1101
- * @throws NotImplementedError if not implemented in adapter
1102
- */
1103
- completeCheckout(id, paymentData, options) {
1104
- this.throwNotImplemented("completeCheckout");
1105
- }
1106
1095
  // ========================================
1107
1096
  // ARTICLE/BLOG OPERATIONS
1108
1097
  // ========================================
@@ -1151,22 +1140,21 @@ var BaseCommerceClient = class {
1151
1140
  // ========================================
1152
1141
  /**
1153
1142
  * Utility method to make GraphQL requests
1154
- * @param query - GraphQL query string
1155
- * @param variables - GraphQL variables
1143
+ * @param params - Object containing query string and optional variables
1156
1144
  * @returns Promise resolving to standardized response
1157
1145
  * @throws NotImplementedError if not implemented in adapter
1158
1146
  */
1159
- graphqlRequest(query, variables) {
1147
+ graphqlRequest(params) {
1160
1148
  this.throwNotImplemented("graphqlRequest");
1161
1149
  }
1162
1150
  /**
1163
1151
  * Utility method to make custom fetch requests to any endpoint
1164
- * @param url - Target URL for the request
1165
- * @param options - Request options
1152
+ * @param params - Object containing url and optional fetch options
1166
1153
  * @returns Promise resolving to standardized response
1167
1154
  */
1168
- async fetchRequest(url, fetchOptions) {
1155
+ async fetchRequest(params) {
1169
1156
  try {
1157
+ const { url, ...fetchOptions } = params;
1170
1158
  const response = await fetch(url, fetchOptions);
1171
1159
  const data = await response.json();
1172
1160
  if (response.ok) {
@@ -1624,6 +1612,92 @@ var GET_COLLECTION = `
1624
1612
  }
1625
1613
  }
1626
1614
  `;
1615
+ var GET_PRODUCT_RECOMMENDATIONS = `
1616
+ query GetProductRecommendations($productId: ID!) {
1617
+ productRecommendations(productId: $productId) {
1618
+ id
1619
+ title
1620
+ description
1621
+ handle
1622
+ productType
1623
+ vendor
1624
+ tags
1625
+ availableForSale
1626
+ options {
1627
+ id
1628
+ name
1629
+ values
1630
+ }
1631
+ variants(first: 10) {
1632
+ nodes {
1633
+ id
1634
+ title
1635
+ sku
1636
+ availableForSale
1637
+ selectedOptions {
1638
+ name
1639
+ value
1640
+ }
1641
+ price {
1642
+ amount
1643
+ currencyCode
1644
+ }
1645
+ compareAtPrice {
1646
+ amount
1647
+ currencyCode
1648
+ }
1649
+ image {
1650
+ id
1651
+ url
1652
+ altText
1653
+ width
1654
+ height
1655
+ }
1656
+ }
1657
+ }
1658
+ images(first: 10) {
1659
+ nodes {
1660
+ id
1661
+ url
1662
+ altText
1663
+ width
1664
+ height
1665
+ }
1666
+ }
1667
+ priceRange {
1668
+ minVariantPrice {
1669
+ amount
1670
+ currencyCode
1671
+ }
1672
+ maxVariantPrice {
1673
+ amount
1674
+ currencyCode
1675
+ }
1676
+ }
1677
+ compareAtPriceRange {
1678
+ minVariantPrice {
1679
+ amount
1680
+ currencyCode
1681
+ }
1682
+ maxVariantPrice {
1683
+ amount
1684
+ currencyCode
1685
+ }
1686
+ }
1687
+ metafields(
1688
+ identifiers: [
1689
+ { namespace: "reviews", key: "rating" },
1690
+ { namespace: "reviews", key: "rating_count" }
1691
+ ]
1692
+ ) {
1693
+ key
1694
+ namespace
1695
+ value
1696
+ type
1697
+ }
1698
+ }
1699
+ }
1700
+ `;
1627
1701
  var GET_COLLECTIONS = `
1628
1702
  query GetCollections(
1629
1703
  $first: Int,
@@ -1850,11 +1924,78 @@ var ProductService = class extends BaseShopifyService {
1850
1924
  });
1851
1925
  }
1852
1926
  }
1927
+ /**
1928
+ * Get product recommendations for a given product
1929
+ * Uses Shopify's productRecommendations API
1930
+ */
1931
+ async getProductRecommendations(params, options) {
1932
+ try {
1933
+ const { productId } = params;
1934
+ if (!productId) {
1935
+ return formatResponse({
1936
+ success: false,
1937
+ message: "No product ID provided."
1938
+ });
1939
+ }
1940
+ const formattedId = productId.includes("gid://") ? productId : `gid://shopify/Product/${productId}`;
1941
+ const query = options?.query || GET_PRODUCT_RECOMMENDATIONS;
1942
+ const variables = options?.variables || { productId: formattedId };
1943
+ const { result, errors } = await this.query(query, variables, options);
1944
+ if (errors && errors.length > 0) {
1945
+ this.logger.error(
1946
+ "GraphQL errors in GET_PRODUCT_RECOMMENDATIONS",
1947
+ void 0,
1948
+ { errors }
1949
+ );
1950
+ return formatResponse({
1951
+ success: false,
1952
+ message: "Failed to fetch product recommendations due to upstream errors.",
1953
+ error: {
1954
+ message: "GraphQL errors received.",
1955
+ details: errors
1956
+ }
1957
+ });
1958
+ }
1959
+ if (!result.data || !result.data.productRecommendations) {
1960
+ return formatResponse({
1961
+ success: true,
1962
+ message: "No product recommendations found.",
1963
+ data: [],
1964
+ meta: void 0
1965
+ });
1966
+ }
1967
+ return formatResponse({
1968
+ success: true,
1969
+ message: "Product recommendations retrieved successfully.",
1970
+ data: mapGetProductsToResponse({
1971
+ nodes: result.data.productRecommendations
1972
+ }),
1973
+ meta: {
1974
+ sourceProductId: productId,
1975
+ recommendationCount: result.data.productRecommendations.length
1976
+ }
1977
+ });
1978
+ } catch (error) {
1979
+ this.logger.error("Error in getProductRecommendations", void 0, {
1980
+ error
1981
+ });
1982
+ const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
1983
+ return formatResponse({
1984
+ success: false,
1985
+ message: "An error occurred while processing the request.",
1986
+ error: {
1987
+ message: safeMessage,
1988
+ details: error
1989
+ }
1990
+ });
1991
+ }
1992
+ }
1853
1993
  /**
1854
1994
  * Get multiple products by their handles using batch query
1855
1995
  */
1856
- async getProductsByHandles(handles, options) {
1996
+ async getProductsByHandles(params, options) {
1857
1997
  try {
1998
+ const { handles } = params;
1858
1999
  if (!handles || handles.length === 0) {
1859
2000
  return formatResponse({
1860
2001
  success: true,
@@ -3935,8 +4076,15 @@ var ShopifyCommerceClient = class extends BaseCommerceClient {
3935
4076
  /**
3936
4077
  * Get multiple products by their handles using batch query
3937
4078
  */
3938
- async getProductsByHandles(handles, options) {
3939
- return this.productService.getProductsByHandles(handles, options);
4079
+ async getProductsByHandles(params, options) {
4080
+ return this.productService.getProductsByHandles(params, options);
4081
+ }
4082
+ /**
4083
+ * Get product recommendations for a given product
4084
+ * Uses Shopify's productRecommendations API
4085
+ */
4086
+ async getProductRecommendations(params, options) {
4087
+ return this.productService.getProductRecommendations(params, options);
3940
4088
  }
3941
4089
  // ============================================================================
3942
4090
  // COLLECTION API METHODS - Now delegated to ProductService
@@ -3953,19 +4101,30 @@ var ShopifyCommerceClient = class extends BaseCommerceClient {
3953
4101
  async getCollection(params, options) {
3954
4102
  return this.productService.getCollection(params, options);
3955
4103
  }
3956
- /**
3957
- * Retrieve multiple collections by their handles with individual query parameters
3958
- * Each collection can have its own specific filtering, pagination, and sorting options
3959
- * @param params - Array of collection objects with handles and their respective query parameters
3960
- * @param options - Request options (headers, timeout, etc.)
3961
- * @returns Promise resolving to collections data with individual query results
3962
- */
3963
4104
  /**
3964
4105
  * Get multiple collections by their handles (preferred method name)
3965
4106
  */
3966
4107
  getCollectionsByHandles(params, options) {
3967
4108
  return this.productService.getCollectionsByHandles(params, options);
3968
4109
  }
4110
+ /**
4111
+ * Get collection filters (available filter options for a collection)
4112
+ */
4113
+ async getCollectionFilters(params, options) {
4114
+ const result = await this.productService.getCollection(
4115
+ { handle: params.handle },
4116
+ options
4117
+ );
4118
+ if (!result.success) {
4119
+ return result;
4120
+ }
4121
+ return formatResponse({
4122
+ success: true,
4123
+ message: "Collection filters retrieved successfully",
4124
+ data: result.meta?.filters || [],
4125
+ meta: void 0
4126
+ });
4127
+ }
3969
4128
  // ============================================================================
3970
4129
  // CART API METHODS - Delegated to CartService
3971
4130
  // ============================================================================
@@ -4067,7 +4226,8 @@ var ShopifyCommerceClient = class extends BaseCommerceClient {
4067
4226
  /**
4068
4227
  * Utility method to make GraphQL requests using Shopify client
4069
4228
  */
4070
- async graphqlRequest(query, variables) {
4229
+ async graphqlRequest(params) {
4230
+ const { query, variables } = params;
4071
4231
  this.logger.info("Making GraphQL request", {
4072
4232
  query: query.substring(0, 100) + "..."
4073
4233
  });
@@ -4233,7 +4393,7 @@ var PRODUCT_ENDPOINTS = {
4233
4393
  getCollections: (v = DEFAULT_VERSION) => `/api/${v}/collections`,
4234
4394
  getCollectionByHandle: (v = DEFAULT_VERSION, h) => `/api/${v}/collections/handle/${h}`,
4235
4395
  getCollectionById: (v = DEFAULT_VERSION, id) => `/api/${v}/collections/${id}`,
4236
- getCollectionsByHandels: (v = DEFAULT_VERSION) => `/api/${v}/collections/details`,
4396
+ getCollectionsByHandles: (v = DEFAULT_VERSION) => `/api/${v}/collections/details`,
4237
4397
  getCollectionFilters: (v = DEFAULT_VERSION, collectionHandle) => `/api/${v}/storefront/collections/${collectionHandle}/filters`
4238
4398
  };
4239
4399
 
@@ -4345,8 +4505,9 @@ var ProductService2 = class extends BaseCustomService {
4345
4505
  /**
4346
4506
  * Get multiple products by their handles using batch query
4347
4507
  */
4348
- async getProductsByHandles(handles, options) {
4508
+ async getProductsByHandles(params, options) {
4349
4509
  try {
4510
+ const { handles } = params;
4350
4511
  if (!handles || handles.length === 0) {
4351
4512
  return formatResponse({
4352
4513
  success: true,
@@ -4512,7 +4673,7 @@ var ProductService2 = class extends BaseCustomService {
4512
4673
  * Get multiple collections by their handles with individual query parameters
4513
4674
  * Each collection can have its own specific filtering, pagination, and sorting options
4514
4675
  */
4515
- async getCollectionsByHandels(params, options) {
4676
+ async getCollectionsByHandles(params, options) {
4516
4677
  try {
4517
4678
  const { handles } = params;
4518
4679
  if (!handles || handles.length === 0) {
@@ -4547,7 +4708,7 @@ var ProductService2 = class extends BaseCustomService {
4547
4708
  );
4548
4709
  }
4549
4710
  const result = await this.request(
4550
- PRODUCT_ENDPOINTS.getCollectionsByHandels(options?.version),
4711
+ PRODUCT_ENDPOINTS.getCollectionsByHandles(options?.version),
4551
4712
  "POST",
4552
4713
  payload,
4553
4714
  options
@@ -4560,7 +4721,9 @@ var ProductService2 = class extends BaseCustomService {
4560
4721
  meta: { pagination: void 0 }
4561
4722
  });
4562
4723
  }
4563
- const mappedCollections = result.collections.map((collection) => ({
4724
+ const mappedCollections = result.collections.filter(
4725
+ (collection) => collection !== null && collection !== void 0
4726
+ ).map((collection) => ({
4564
4727
  handle: collection.handle,
4565
4728
  data: mapGetCollectionsByHandelsResponse(collection),
4566
4729
  meta: {
@@ -4576,7 +4739,7 @@ var ProductService2 = class extends BaseCustomService {
4576
4739
  meta: void 0
4577
4740
  });
4578
4741
  } catch (error) {
4579
- this.logger.error("Error in getCollectionsByHandels", void 0, {
4742
+ this.logger.error("Error in getCollectionsByHandles", void 0, {
4580
4743
  error
4581
4744
  });
4582
4745
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
@@ -5177,9 +5340,9 @@ var CustomBackendClient = class extends BaseCommerceClient {
5177
5340
  async getProducts(params, options) {
5178
5341
  return this.productService.getProducts(params, filterRestOptions(options));
5179
5342
  }
5180
- async getProductsByHandles(handles, options) {
5343
+ async getProductsByHandles(params, options) {
5181
5344
  return this.productService.getProductsByHandles(
5182
- handles,
5345
+ params,
5183
5346
  filterRestOptions(options)
5184
5347
  );
5185
5348
  }
@@ -5198,7 +5361,7 @@ var CustomBackendClient = class extends BaseCommerceClient {
5198
5361
  /**
5199
5362
  * Get a collection by ID
5200
5363
  */
5201
- getCollection(params, options) {
5364
+ async getCollection(params, options) {
5202
5365
  return this.productService.getCollection(
5203
5366
  params,
5204
5367
  filterRestOptions(options)
@@ -5211,8 +5374,8 @@ var CustomBackendClient = class extends BaseCommerceClient {
5211
5374
  * @param options - Request options (headers, timeout, etc.)
5212
5375
  * @returns Promise resolving to collections data with individual query results
5213
5376
  */
5214
- getCollectionsByHandels(params, options) {
5215
- return this.productService.getCollectionsByHandels(
5377
+ async getCollectionsByHandles(params, options) {
5378
+ return this.productService.getCollectionsByHandles(
5216
5379
  params,
5217
5380
  filterRestOptions(options)
5218
5381
  );
@@ -5249,12 +5412,6 @@ var CustomBackendClient = class extends BaseCommerceClient {
5249
5412
  async createCheckout(params, options) {
5250
5413
  return this.cartService.createCheckout(params, filterRestOptions(options));
5251
5414
  }
5252
- async updateCheckout(id, data, options) {
5253
- throw new Error("Checkout service not implemented in custom adapter");
5254
- }
5255
- async completeCheckout(id, paymentData, options) {
5256
- throw new Error("Checkout service not implemented in custom adapter");
5257
- }
5258
5415
  // ========================================
5259
5416
  // CUSTOMER OPERATIONS
5260
5417
  // ========================================