@shopkit/data-layer 1.1.0 → 1.2.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/README.md CHANGED
@@ -26,6 +26,59 @@ A comprehensive, production-ready data layer package for e-commerce applications
26
26
  - **📋 Consistent Response Format**: Standardized `IResponse` format across all operations
27
27
  - **🪵 Advanced Logging**: Built-in logging with configurable levels and formatting
28
28
  - **🎯 Standardized Interface**: All methods follow consistent signature patterns
29
+ - **⚡ Build-time Static Generation**: Factory functions that auto-generate `generateStaticParams` for all dynamic routes — no manual env var configuration needed
30
+
31
+ ## Static Params (Build-time Route Generation)
32
+
33
+ Use the `static-params` subpath to automatically pre-build all collection and product pages at CI time. Replaces the manual `FEATURED_COLLECTIONS` env var pattern.
34
+
35
+ ```typescript
36
+ import {
37
+ createCollectionStaticParams,
38
+ createProductStaticParams,
39
+ createProductViaCollectionsStaticParams,
40
+ createCollectionProductStaticParams,
41
+ } from "@shopkit/data-layer";
42
+ import { getCommerceClient } from "@/config/commerce";
43
+
44
+ // /collections/[collectionname]/page.tsx
45
+ export const revalidate = 3600;
46
+ export const dynamicParams = true;
47
+ export const generateStaticParams = createCollectionStaticParams(getCommerceClient);
48
+
49
+ // /products/[handle]/page.tsx — backend supports getProducts directly
50
+ export const revalidate = 3600;
51
+ export const dynamicParams = true;
52
+ export const generateStaticParams = createProductStaticParams(getCommerceClient);
53
+
54
+ // /products/[handle]/page.tsx — backend has no products-list endpoint (derives handles from collections)
55
+ export const revalidate = 3600;
56
+ export const dynamicParams = true;
57
+ export const generateStaticParams = createProductViaCollectionsStaticParams(getCommerceClient);
58
+
59
+ // /collections/[collectionname]/products/[handle]/page.tsx
60
+ export const revalidate = 3600;
61
+ export const dynamicParams = true;
62
+ export const generateStaticParams = createCollectionProductStaticParams(getCommerceClient);
63
+ ```
64
+
65
+ All factories: catch API errors and return `[]` (build never fails), accept an optional `options` argument for custom limits and concurrency, and deduplicate product handles across collections.
66
+
67
+ `createProductViaCollectionsStaticParams` and `createCollectionProductStaticParams` also accept a `concurrency` option (default: `10`) that caps how many `getCollection` calls run simultaneously — prevents burst load on custom REST backends at build time:
68
+
69
+ ```typescript
70
+ // Custom backend with strict rate limits — lower concurrency
71
+ export const generateStaticParams = createProductViaCollectionsStaticParams(
72
+ getCommerceClient,
73
+ { collectionsLimit: 250, productsPerCollection: 100, concurrency: 5 }
74
+ );
75
+
76
+ // Fast internal backend — raise concurrency to build faster
77
+ export const generateStaticParams = createCollectionProductStaticParams(
78
+ getCommerceClient,
79
+ { collectionsLimit: 50, productsPerCollection: 20, concurrency: 20 }
80
+ );
81
+ ```
29
82
 
30
83
  ## Installation
31
84
 
@@ -1,5 +1,6 @@
1
- import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-Cnm8tn7H.mjs';
1
+ import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-DBKEK2oS.mjs';
2
2
  import '../models/index.mjs';
3
+ import '../interface-NJcjuqaL.mjs';
3
4
 
4
5
  /**
5
6
  * Shopify adapter for the commerce library
@@ -1,5 +1,6 @@
1
- import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-Dw9qj17-.js';
1
+ import { I as IShopifyConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from '../service-BHCLUEim.js';
2
2
  import '../models/index.js';
3
+ import '../interface-dqoKc63z.js';
3
4
 
4
5
  /**
5
6
  * Shopify adapter for the commerce library
package/dist/index.d.mts CHANGED
@@ -1,5 +1,7 @@
1
- import { I as IShopifyConfig, L as LoggerConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient, b as ICommerceClient } from './service-Cnm8tn7H.mjs';
1
+ import { I as ICommerceClient } from './interface-NJcjuqaL.mjs';
2
+ import { I as IShopifyConfig, L as LoggerConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from './service-DBKEK2oS.mjs';
2
3
  export { EAvailability, ECollectionSortKey, EFulfillmentStatus, EOrderSortKey, EOrderStatus, EPaymentStatus, EProductSortKey, IAddToCartRequest, IAddress, IArticleByHandleRequest, IBaseQueryParams, IBlogArticleRequest, ICartItemRequest, ICollectionFilterRequest, ICollectionQueryRequest, ICollectionsByhandlesRequest, ICreateCheckoutRequest, ICustomerQueryRequest, ICustomerRequest, IFetchRequest, IGetArticleRequest, IGraphqlRequest, IImage, IMetafield, IMoney, IOptionsRequest, IOrderByIdRequest, IOrderHistoryRequest, IOrderQueryRequest, IOrderRequest, IPaginationParams, IProductFilters, IProductQueryReqest, IProductRecommendationsRequest, IProductsByHandlesRequest, IRemoveFromCartRequest, IResponse, IResponseMeta, ISeo, ISingleCartRequest, ISingleCollectionRequest, ISingleCustomerRequest, ISingleOrderRequest, ISingleProductRequest, ITrendingArticleRequest, IUpdateCartRequest, IUpdateCustomerRequest, IUpdateOrderRequest } from './models/index.mjs';
4
+ export { createCollectionProductStaticParams, createCollectionStaticParams, createProductStaticParams, createProductViaCollectionsStaticParams } from './static-params/index.mjs';
3
5
 
4
6
  /**
5
7
  * Commerce Client Factory
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { I as IShopifyConfig, L as LoggerConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient, b as ICommerceClient } from './service-Dw9qj17-.js';
1
+ import { I as ICommerceClient } from './interface-dqoKc63z.js';
2
+ import { I as IShopifyConfig, L as LoggerConfig, S as ShopifyCommerceClient, a as ICustomConfig, C as CustomBackendClient } from './service-BHCLUEim.js';
2
3
  export { EAvailability, ECollectionSortKey, EFulfillmentStatus, EOrderSortKey, EOrderStatus, EPaymentStatus, EProductSortKey, IAddToCartRequest, IAddress, IArticleByHandleRequest, IBaseQueryParams, IBlogArticleRequest, ICartItemRequest, ICollectionFilterRequest, ICollectionQueryRequest, ICollectionsByhandlesRequest, ICreateCheckoutRequest, ICustomerQueryRequest, ICustomerRequest, IFetchRequest, IGetArticleRequest, IGraphqlRequest, IImage, IMetafield, IMoney, IOptionsRequest, IOrderByIdRequest, IOrderHistoryRequest, IOrderQueryRequest, IOrderRequest, IPaginationParams, IProductFilters, IProductQueryReqest, IProductRecommendationsRequest, IProductsByHandlesRequest, IRemoveFromCartRequest, IResponse, IResponseMeta, ISeo, ISingleCartRequest, ISingleCollectionRequest, ISingleCustomerRequest, ISingleOrderRequest, ISingleProductRequest, ITrendingArticleRequest, IUpdateCartRequest, IUpdateCustomerRequest, IUpdateOrderRequest } from './models/index.js';
4
+ export { createCollectionProductStaticParams, createCollectionStaticParams, createProductStaticParams, createProductViaCollectionsStaticParams } from './static-params/index.js';
3
5
 
4
6
  /**
5
7
  * Commerce Client Factory
package/dist/index.js CHANGED
@@ -784,7 +784,7 @@ function createLogger(config = {}) {
784
784
  }
785
785
  return new UniversalLogger(fullConfig);
786
786
  }
787
- createLogger({
787
+ var logger = createLogger({
788
788
  enabled: false,
789
789
  level: "info",
790
790
  prettyPrint: false
@@ -5530,6 +5530,137 @@ var EFulfillmentStatus = /* @__PURE__ */ ((EFulfillmentStatus2) => {
5530
5530
  return EFulfillmentStatus2;
5531
5531
  })(EFulfillmentStatus || {});
5532
5532
 
5533
+ // src/static-params/index.ts
5534
+ var log = logger.child({ module: "static-params" });
5535
+ async function withConcurrency(thunks, limit) {
5536
+ const results = new Array(thunks.length);
5537
+ let next = 0;
5538
+ async function worker() {
5539
+ while (next < thunks.length) {
5540
+ const i = next++;
5541
+ results[i] = await thunks[i]().catch(() => []);
5542
+ }
5543
+ }
5544
+ await Promise.all(Array.from({ length: Math.min(limit, thunks.length) }, worker));
5545
+ return results;
5546
+ }
5547
+ async function traverseCollectionProducts(client, collectionsLimit, productsPerCollection, concurrency) {
5548
+ const collectionsResult = await client.getCollections({ first: collectionsLimit });
5549
+ const collections = Array.isArray(collectionsResult?.data) ? collectionsResult.data : [];
5550
+ if (collections.length === 0) return [];
5551
+ if (collections.length >= collectionsLimit) {
5552
+ log.warn(
5553
+ "getCollections hit the collections limit \u2014 some collections may be missing from static build; increase options.collectionsLimit if needed",
5554
+ { count: collections.length, limit: collectionsLimit }
5555
+ );
5556
+ }
5557
+ const thunks = collections.map((collection) => async () => {
5558
+ const collectionname = collection?.handle;
5559
+ if (!collectionname) return [];
5560
+ try {
5561
+ const result = await client.getCollection({
5562
+ handle: collectionname,
5563
+ productLimit: productsPerCollection
5564
+ });
5565
+ const col = result?.data;
5566
+ const products = Array.isArray(col?.products) ? col.products : [];
5567
+ return products.map((p) => p?.handle).filter((h) => Boolean(h)).map((productHandle) => ({ collectionname, productHandle }));
5568
+ } catch (err) {
5569
+ log.warn(
5570
+ "Skipping collection during static generation \u2014 getCollection failed",
5571
+ { collection: collectionname, err: err instanceof Error ? err.message : String(err) }
5572
+ );
5573
+ return [];
5574
+ }
5575
+ });
5576
+ const pairsNested = await withConcurrency(thunks, concurrency);
5577
+ return pairsNested.flat();
5578
+ }
5579
+ function createCollectionStaticParams(getClient, options) {
5580
+ return async function generateStaticParams() {
5581
+ const limit = options?.limit ?? 250;
5582
+ try {
5583
+ const result = await getClient().getCollections({ first: limit });
5584
+ const collections = Array.isArray(result?.data) ? result.data : [];
5585
+ if (collections.length >= limit) {
5586
+ log.warn(
5587
+ "getCollections returned max limit \u2014 some collections may be missing from static build; increase options.limit if needed",
5588
+ { count: collections.length, limit }
5589
+ );
5590
+ }
5591
+ return collections.map((c) => c?.handle).filter((h) => Boolean(h)).map((handle) => ({ collectionname: handle }));
5592
+ } catch (error) {
5593
+ log.error("getCollections failed during static generation", error instanceof Error ? error : new Error(String(error)));
5594
+ return [];
5595
+ }
5596
+ };
5597
+ }
5598
+ function createProductStaticParams(getClient, options) {
5599
+ return async function generateStaticParams() {
5600
+ const limit = options?.limit ?? 250;
5601
+ try {
5602
+ const result = await getClient().getProducts({ first: limit });
5603
+ const products = Array.isArray(result?.data) ? result.data : [];
5604
+ if (products.length >= limit) {
5605
+ log.warn(
5606
+ "getProducts returned max limit \u2014 some products may be missing from static build; increase options.limit if needed",
5607
+ { count: products.length, limit }
5608
+ );
5609
+ }
5610
+ return products.map((p) => p?.handle).filter((h) => Boolean(h)).map((handle) => ({ handle }));
5611
+ } catch (error) {
5612
+ log.error("getProducts failed during static generation", error instanceof Error ? error : new Error(String(error)));
5613
+ return [];
5614
+ }
5615
+ };
5616
+ }
5617
+ function createProductViaCollectionsStaticParams(getClient, options) {
5618
+ return async function generateStaticParams() {
5619
+ try {
5620
+ const pairs = await traverseCollectionProducts(
5621
+ getClient(),
5622
+ options?.collectionsLimit ?? 250,
5623
+ options?.productsPerCollection ?? 100,
5624
+ options?.concurrency ?? 10
5625
+ );
5626
+ const seen = /* @__PURE__ */ new Set();
5627
+ return pairs.filter(({ productHandle }) => {
5628
+ if (seen.has(productHandle)) return false;
5629
+ seen.add(productHandle);
5630
+ return true;
5631
+ }).map(({ productHandle }) => ({ handle: productHandle }));
5632
+ } catch (error) {
5633
+ log.error("getProductHandlesViaCollections failed during static generation", error instanceof Error ? error : new Error(String(error)));
5634
+ return [];
5635
+ }
5636
+ };
5637
+ }
5638
+ function createCollectionProductStaticParams(getClient, options) {
5639
+ return async function generateStaticParams() {
5640
+ try {
5641
+ const pairs = await traverseCollectionProducts(
5642
+ getClient(),
5643
+ options?.collectionsLimit ?? 50,
5644
+ options?.productsPerCollection ?? 20,
5645
+ options?.concurrency ?? 10
5646
+ );
5647
+ const seen = /* @__PURE__ */ new Set();
5648
+ return pairs.filter(({ collectionname, productHandle }) => {
5649
+ const key = `${collectionname}:${productHandle}`;
5650
+ if (seen.has(key)) return false;
5651
+ seen.add(key);
5652
+ return true;
5653
+ }).map(({ collectionname, productHandle }) => ({
5654
+ collectionname,
5655
+ handle: productHandle
5656
+ }));
5657
+ } catch (error) {
5658
+ log.error("getCollectionProducts failed during static generation", error instanceof Error ? error : new Error(String(error)));
5659
+ return [];
5660
+ }
5661
+ };
5662
+ }
5663
+
5533
5664
  exports.EAvailability = EAvailability;
5534
5665
  exports.ECollectionSortKey = ECollectionSortKey;
5535
5666
  exports.EFulfillmentStatus = EFulfillmentStatus;
@@ -5537,8 +5668,12 @@ exports.EOrderSortKey = EOrderSortKey;
5537
5668
  exports.EOrderStatus = EOrderStatus;
5538
5669
  exports.EPaymentStatus = EPaymentStatus;
5539
5670
  exports.EProductSortKey = EProductSortKey;
5671
+ exports.createCollectionProductStaticParams = createCollectionProductStaticParams;
5672
+ exports.createCollectionStaticParams = createCollectionStaticParams;
5540
5673
  exports.createCommerceClient = createCommerceClient;
5541
5674
  exports.createCustomClient = createCustomClient;
5675
+ exports.createProductStaticParams = createProductStaticParams;
5676
+ exports.createProductViaCollectionsStaticParams = createProductViaCollectionsStaticParams;
5542
5677
  exports.createShopifyClient = createShopifyClient;
5543
5678
  //# sourceMappingURL=index.js.map
5544
5679
  //# sourceMappingURL=index.js.map