@shopkit/data-layer 1.4.5 → 1.5.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.
@@ -1,6 +1,6 @@
1
- import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-DBKEK2oS.mjs';
1
+ import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-BPzeuSLS.mjs';
2
2
  import '../models/index.mjs';
3
- import '../interface-NJcjuqaL.mjs';
3
+ import '../interface-CgPiwpPe.mjs';
4
4
 
5
5
  /**
6
6
  * Shopify adapter for the commerce library
@@ -1,6 +1,6 @@
1
- import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-BHCLUEim.js';
1
+ import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-Cb074q9F.js';
2
2
  import '../models/index.js';
3
- import '../interface-dqoKc63z.js';
3
+ import '../interface-BGV7d5F8.js';
4
4
 
5
5
  /**
6
6
  * Shopify adapter for the commerce library
@@ -298,6 +298,16 @@ var BaseCommerceClient = class {
298
298
  getCollectionFilters(params, options) {
299
299
  this.throwNotImplemented("getCollectionFilters");
300
300
  }
301
+ /**
302
+ * Read a page of collections, with their products and variants, from one query document
303
+ * @param params - Query document (filter, fields, include, sort, paging)
304
+ * @param options - Request options (headers, timeout, etc.)
305
+ * @returns Promise resolving to collections, with paging in meta.pagination
306
+ * @throws NotImplementedError if not implemented in adapter
307
+ */
308
+ queryCollections(params, options) {
309
+ this.throwNotImplemented("queryCollections");
310
+ }
301
311
  // ========================================
302
312
  // ORDER OPERATIONS
303
313
  // ========================================
@@ -3737,6 +3747,11 @@ var BaseCustomService = class {
3737
3747
  `Request failed: ${response.status} ${response.statusText}`
3738
3748
  );
3739
3749
  error.status = response.status;
3750
+ try {
3751
+ error.body = await response.json();
3752
+ } catch {
3753
+ error.body = void 0;
3754
+ }
3740
3755
  throw error;
3741
3756
  }
3742
3757
  this.logger.info("API request successful", {
@@ -3777,6 +3792,9 @@ function mapGetCollectionsByHandelsResponse(response) {
3777
3792
  function mapGetCollectionFiltersResponse(response) {
3778
3793
  return response;
3779
3794
  }
3795
+ function mapQueryCollectionsResponse(response) {
3796
+ return response?.collections || [];
3797
+ }
3780
3798
 
3781
3799
  // src/adapters/custom/product/urls.ts
3782
3800
  var DEFAULT_VERSION = "v1";
@@ -3785,10 +3803,11 @@ var PRODUCT_ENDPOINTS = {
3785
3803
  getProduct: (v = DEFAULT_VERSION) => `/api/${v}/product/details`,
3786
3804
  getProductsByHandles: (v = DEFAULT_VERSION) => `/api/${v}/product/details`,
3787
3805
  getCollections: (v = DEFAULT_VERSION) => `/api/${v}/collections`,
3788
- getCollectionByHandle: (v = DEFAULT_VERSION, h) => `/api/${v}/collections/handle/${h}`,
3789
- getCollectionById: (v = DEFAULT_VERSION, id) => `/api/${v}/collections/${id}`,
3806
+ getCollectionByHandle: (v = DEFAULT_VERSION, h) => `/api/${v}/collections/handle/${encodeURIComponent(h)}`,
3807
+ getCollectionById: (v = DEFAULT_VERSION, id) => `/api/${v}/collections/${encodeURIComponent(id)}`,
3790
3808
  getCollectionsByHandles: (v = DEFAULT_VERSION) => `/api/${v}/collections/details`,
3791
- getCollectionFilters: (v = DEFAULT_VERSION, collectionHandle) => `/api/${v}/storefront/collections/${collectionHandle}/filters`
3809
+ getCollectionFilters: (v = DEFAULT_VERSION, collectionHandle) => `/api/${v}/storefront/collections/${encodeURIComponent(collectionHandle)}/filters`,
3810
+ queryCollections: (v = DEFAULT_VERSION) => `/api/${v}/collections/query`
3792
3811
  };
3793
3812
 
3794
3813
  // src/adapters/custom/product/service.ts
@@ -4365,7 +4384,123 @@ var ProductService2 = class extends BaseCustomService {
4365
4384
  });
4366
4385
  }
4367
4386
  }
4387
+ /**
4388
+ * Read a page of collections, with their products and variants, from one
4389
+ * query document. A POST because the document is too nested for a URL; it is
4390
+ * still a read and answers 200.
4391
+ */
4392
+ async queryCollections(params, options) {
4393
+ try {
4394
+ const payload = buildCollectionsQueryPayload(params);
4395
+ const handles = handlesNamedBy(payload.filter);
4396
+ const result = await this.request(
4397
+ PRODUCT_ENDPOINTS.queryCollections(options?.version),
4398
+ "POST",
4399
+ payload,
4400
+ {
4401
+ ...options || {},
4402
+ // Same coverage as the other collection reads: the broad tag for
4403
+ // catalog-wide purges, plus one per handle the filter names, so a
4404
+ // collection.updated / product.updated webhook reaches every page built
4405
+ // from this read. A filter that names no handle (published: true, a
4406
+ // $like, ...) cannot know its result set in advance — broad tag only.
4407
+ // No `revalidate`, as with getCollectionsByHandles.
4408
+ next: {
4409
+ ...options?.next || {},
4410
+ tags: [
4411
+ cacheTags.CacheTags.collections(),
4412
+ ...handles.map((h) => cacheTags.CacheTags.collection(h)),
4413
+ ...options?.next?.tags || []
4414
+ ]
4415
+ }
4416
+ }
4417
+ );
4418
+ const errors = Array.isArray(result?.errors) ? result.errors : [];
4419
+ if (!result?.data) {
4420
+ return formatResponse({
4421
+ success: false,
4422
+ message: "Collections query returned no data.",
4423
+ error: {
4424
+ code: "UPSTREAM_UNAVAILABLE",
4425
+ message: "No data in collections query response."
4426
+ },
4427
+ meta: { requestId: result?.request_id }
4428
+ });
4429
+ }
4430
+ return formatResponse({
4431
+ success: true,
4432
+ message: "Collections retrieved successfully.",
4433
+ data: mapQueryCollectionsResponse(result.data),
4434
+ meta: {
4435
+ pagination: result.data.paging,
4436
+ requestId: result.request_id,
4437
+ ...errors.length > 0 ? { errors } : {}
4438
+ }
4439
+ });
4440
+ } catch (error) {
4441
+ this.logger.error("Error in queryCollections", void 0, { error });
4442
+ const body = error?.body;
4443
+ if (isUpstreamUnavailable(error)) {
4444
+ return formatResponse({
4445
+ success: false,
4446
+ message: "An error occurred while processing the request.",
4447
+ error: {
4448
+ code: "UPSTREAM_UNAVAILABLE",
4449
+ message: typeof error?.status === "number" ? `Request failed: ${error.status}` : "Collections query did not complete."
4450
+ },
4451
+ meta: { requestId: body?.request_id }
4452
+ });
4453
+ }
4454
+ const upstreamErrors = Array.isArray(body?.errors) ? body.errors : [];
4455
+ return formatResponse({
4456
+ success: false,
4457
+ message: "The collections query was rejected.",
4458
+ error: {
4459
+ code: "QUERY_REJECTED",
4460
+ message: upstreamErrors[0]?.message || error.message,
4461
+ details: upstreamErrors
4462
+ },
4463
+ meta: { requestId: body?.request_id }
4464
+ });
4465
+ }
4466
+ }
4368
4467
  };
4468
+ var COLLECTIONS_QUERY_KEYS = [
4469
+ "filter",
4470
+ "fields",
4471
+ "include",
4472
+ "sort",
4473
+ "limit",
4474
+ "skip",
4475
+ "after",
4476
+ "count",
4477
+ "metafields",
4478
+ "availability",
4479
+ "ratings"
4480
+ ];
4481
+ function buildCollectionsQueryPayload(params) {
4482
+ const source = params || {};
4483
+ const payload = {};
4484
+ for (const key of COLLECTIONS_QUERY_KEYS) {
4485
+ if (source[key] !== void 0) payload[key] = source[key];
4486
+ }
4487
+ const filter = payload.filter && typeof payload.filter === "object" ? payload.filter : {};
4488
+ const routeHandle = source.useRouteHandle === true ? source.handle : void 0;
4489
+ if (typeof routeHandle === "string" && routeHandle.trim() !== "" && filter.handle === void 0) {
4490
+ payload.filter = { ...filter, handle: routeHandle };
4491
+ }
4492
+ return payload;
4493
+ }
4494
+ function handlesNamedBy(filter) {
4495
+ const handle = filter?.handle;
4496
+ const candidates = typeof handle === "string" ? [handle] : handle && typeof handle === "object" ? [
4497
+ handle.$eq,
4498
+ ...Array.isArray(handle.$in) ? handle.$in : []
4499
+ ] : [];
4500
+ return candidates.filter(
4501
+ (h) => typeof h === "string" && h.trim() !== ""
4502
+ );
4503
+ }
4369
4504
 
4370
4505
  // src/adapters/custom/order/urls.ts
4371
4506
  var DEFAULT_VERSION2 = "v1";
@@ -4947,6 +5082,12 @@ var CustomBackendClient = class extends BaseCommerceClient {
4947
5082
  filterRestOptions(options)
4948
5083
  );
4949
5084
  }
5085
+ async queryCollections(params, options) {
5086
+ return this.productService.queryCollections(
5087
+ params,
5088
+ filterRestOptions(options)
5089
+ );
5090
+ }
4950
5091
  // ========================================
4951
5092
  // CART OPERATIONS
4952
5093
  // ========================================