@shopkit/data-layer 1.4.6 → 1.6.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";
@@ -3788,7 +3806,8 @@ var PRODUCT_ENDPOINTS = {
3788
3806
  getCollectionByHandle: (v = DEFAULT_VERSION, h) => `/api/${v}/collections/handle/${encodeURIComponent(h)}`,
3789
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/${encodeURIComponent(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,149 @@ 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
+ if (selectedHandles(params)?.length === 0) {
4396
+ return formatResponse({
4397
+ success: true,
4398
+ message: "No collections selected.",
4399
+ data: [],
4400
+ meta: { pagination: { limit: 0, skip: 0, has_more: false, total: 0, next_cursor: null } }
4401
+ });
4402
+ }
4403
+ const handles = handlesNamedBy(payload.filter);
4404
+ const result = await this.request(
4405
+ PRODUCT_ENDPOINTS.queryCollections(options?.version),
4406
+ "POST",
4407
+ payload,
4408
+ {
4409
+ ...options || {},
4410
+ // Same coverage as the other collection reads: the broad tag for
4411
+ // catalog-wide purges, plus one per handle the filter names, so a
4412
+ // collection.updated / product.updated webhook reaches every page built
4413
+ // from this read. A filter that names no handle (published: true, a
4414
+ // $like, ...) cannot know its result set in advance — broad tag only.
4415
+ // No `revalidate`, as with getCollectionsByHandles.
4416
+ next: {
4417
+ ...options?.next || {},
4418
+ tags: [
4419
+ cacheTags.CacheTags.collections(),
4420
+ ...handles.map((h) => cacheTags.CacheTags.collection(h)),
4421
+ ...options?.next?.tags || []
4422
+ ]
4423
+ }
4424
+ }
4425
+ );
4426
+ const errors = Array.isArray(result?.errors) ? result.errors : [];
4427
+ if (!result?.data) {
4428
+ return formatResponse({
4429
+ success: false,
4430
+ message: "Collections query returned no data.",
4431
+ error: {
4432
+ code: "UPSTREAM_UNAVAILABLE",
4433
+ message: "No data in collections query response."
4434
+ },
4435
+ meta: { requestId: result?.request_id }
4436
+ });
4437
+ }
4438
+ return formatResponse({
4439
+ success: true,
4440
+ message: "Collections retrieved successfully.",
4441
+ data: mapQueryCollectionsResponse(result.data),
4442
+ meta: {
4443
+ pagination: result.data.paging,
4444
+ requestId: result.request_id,
4445
+ ...errors.length > 0 ? { errors } : {}
4446
+ }
4447
+ });
4448
+ } catch (error) {
4449
+ this.logger.error("Error in queryCollections", void 0, { error });
4450
+ const body = error?.body;
4451
+ if (isUpstreamUnavailable(error)) {
4452
+ return formatResponse({
4453
+ success: false,
4454
+ message: "An error occurred while processing the request.",
4455
+ error: {
4456
+ code: "UPSTREAM_UNAVAILABLE",
4457
+ message: typeof error?.status === "number" ? `Request failed: ${error.status}` : "Collections query did not complete."
4458
+ },
4459
+ meta: { requestId: body?.request_id }
4460
+ });
4461
+ }
4462
+ const upstreamErrors = Array.isArray(body?.errors) ? body.errors : [];
4463
+ return formatResponse({
4464
+ success: false,
4465
+ message: "The collections query was rejected.",
4466
+ error: {
4467
+ code: "QUERY_REJECTED",
4468
+ message: upstreamErrors[0]?.message || error.message,
4469
+ details: upstreamErrors
4470
+ },
4471
+ meta: { requestId: body?.request_id }
4472
+ });
4473
+ }
4474
+ }
4368
4475
  };
4476
+ var COLLECTIONS_QUERY_KEYS = [
4477
+ "filter",
4478
+ "fields",
4479
+ "include",
4480
+ "sort",
4481
+ "limit",
4482
+ "skip",
4483
+ "after",
4484
+ "count",
4485
+ "metafields",
4486
+ "availability",
4487
+ "ratings"
4488
+ ];
4489
+ function buildCollectionsQueryPayload(params) {
4490
+ const source = params || {};
4491
+ const payload = {};
4492
+ for (const key of COLLECTIONS_QUERY_KEYS) {
4493
+ if (source[key] !== void 0) payload[key] = source[key];
4494
+ }
4495
+ const filter = payload.filter && typeof payload.filter === "object" ? payload.filter : {};
4496
+ const handles = selectedHandles(params);
4497
+ if (handles) {
4498
+ payload.filter = { ...filter, handle: { $in: handles } };
4499
+ if (typeof payload.limit === "number" && payload.limit < handles.length) {
4500
+ payload.limit = handles.length;
4501
+ }
4502
+ return payload;
4503
+ }
4504
+ const routeHandle = source.useRouteHandle === true ? source.handle : void 0;
4505
+ if (typeof routeHandle === "string" && routeHandle.trim() !== "" && filter.handle === void 0) {
4506
+ payload.filter = { ...filter, handle: routeHandle };
4507
+ }
4508
+ return payload;
4509
+ }
4510
+ var MAX_COLLECTIONS_PER_QUERY = 10;
4511
+ function selectedHandles(params) {
4512
+ const source = params || {};
4513
+ if (!Array.isArray(source.handles)) return void 0;
4514
+ const filter = source.filter;
4515
+ if (filter && typeof filter === "object" && filter.handle !== void 0) {
4516
+ return void 0;
4517
+ }
4518
+ return source.handles.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h !== "").slice(0, MAX_COLLECTIONS_PER_QUERY);
4519
+ }
4520
+ function handlesNamedBy(filter) {
4521
+ const handle = filter?.handle;
4522
+ const candidates = typeof handle === "string" ? [handle] : handle && typeof handle === "object" ? [
4523
+ handle.$eq,
4524
+ ...Array.isArray(handle.$in) ? handle.$in : []
4525
+ ] : [];
4526
+ return candidates.filter(
4527
+ (h) => typeof h === "string" && h.trim() !== ""
4528
+ );
4529
+ }
4369
4530
 
4370
4531
  // src/adapters/custom/order/urls.ts
4371
4532
  var DEFAULT_VERSION2 = "v1";
@@ -4947,6 +5108,12 @@ var CustomBackendClient = class extends BaseCommerceClient {
4947
5108
  filterRestOptions(options)
4948
5109
  );
4949
5110
  }
5111
+ async queryCollections(params, options) {
5112
+ return this.productService.queryCollections(
5113
+ params,
5114
+ filterRestOptions(options)
5115
+ );
5116
+ }
4950
5117
  // ========================================
4951
5118
  // CART OPERATIONS
4952
5119
  // ========================================