@commerce-blocks/sdk 2.0.0-alpha.3 → 2.0.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
@@ -40,27 +40,24 @@ collection.dispose()
40
40
 
41
41
  ## Configuration
42
42
 
43
- | Option | Type | Required | Description |
44
- | ------------------- | ------------------------------ | -------- | ------------------------------------ |
45
- | `token` | `string` | Yes | Layers API public token |
46
- | `sorts` | `Sort[]` | Yes | Sort options `{ name, code }` |
47
- | `facets` | `Facet[]` | Yes | Facet fields `{ name, code }` |
48
- | `attributes` | `string[]` | No | Product attributes to fetch |
49
- | `baseUrl` | `string` | No | Custom API URL |
50
- | `fetch` | `CustomFetch` | No | Custom fetch (SSR, testing) |
51
- | `currency` | `string` | No | Currency for price formatting |
52
- | `formatPrice` | `(amount, currency) => string` | No | Custom price formatter |
53
- | `swatches` | `Swatch[]` | No | Color swatch definitions |
54
- | `options` | `string[]` | No | Product options to expose |
55
- | `productMetafields` | `{ namespace, key }[]` | No | Product metafields to fetch |
56
- | `variantMetafields` | `{ namespace, key }[]` | No | Variant metafields to fetch |
57
- | `transforms` | `Transforms` | No | Post-process results (see below) |
58
- | `filterAliases` | `FilterAliases` | No | URL-friendly filter key mapping |
59
- | `cacheLimit` | `number` | No | Max entries in cache |
60
- | `cacheLifetime` | `number` | No | TTL in milliseconds |
61
- | `storage` | `StorageAdapter` | No | Custom storage adapter |
62
- | `initialData` | `CacheData` | No | Pre-populate cache at init |
63
- | `restoreCache` | `boolean` | No | Restore from storage (default: true) |
43
+ | Option | Type | Required | Description |
44
+ | --------------- | ------------------------------ | -------- | ------------------------------------ |
45
+ | `token` | `string` | Yes | Layers API public token |
46
+ | `sorts` | `Sort[]` | Yes | Sort options `{ name, code }` |
47
+ | `facets` | `Facet[]` | Yes | Facet fields `{ name, code }` |
48
+ | `attributes` | `string[]` | No | Product attributes to fetch |
49
+ | `baseUrl` | `string` | No | Custom API URL |
50
+ | `fetch` | `CustomFetch` | No | Custom fetch (SSR, testing) |
51
+ | `currency` | `string` | No | Currency for price formatting |
52
+ | `formatPrice` | `(amount, currency) => string` | No | Custom price formatter |
53
+ | `swatches` | `Swatch[]` | No | Color swatch definitions |
54
+ | `transforms` | `Transforms` | No | Post-process results (see below) |
55
+ | `filterAliases` | `FilterAliases` | No | URL-friendly filter key mapping |
56
+ | `cacheLimit` | `number` | No | Max entries in cache |
57
+ | `cacheLifetime` | `number` | No | TTL in milliseconds |
58
+ | `storage` | `StorageAdapter` | No | Custom storage adapter |
59
+ | `initialData` | `CacheData` | No | Pre-populate cache at init |
60
+ | `restoreCache` | `boolean` | No | Restore from storage (default: true) |
64
61
 
65
62
  ## Controllers
66
63
 
@@ -398,7 +395,7 @@ if (result.error) {
398
395
  }
399
396
  ```
400
397
 
401
- Retryable errors (`TIMEOUT`, `CONNECTION_FAILED`, `RATE_LIMITED`, `SERVICE_UNAVAILABLE`) expose `retryable` and `retryAfterMs`:
398
+ `isRetryable` classifies errors by tag, code, and status — use it standalone or as a `shouldRetry` predicate:
402
399
 
403
400
  ```typescript
404
401
  import { isRetryable } from '@commerce-blocks/sdk'
@@ -417,7 +414,7 @@ The client exposes a reactive cache:
417
414
  const { cache } = client
418
415
 
419
416
  cache.get('cache-key') // CacheEntry<QueryResult> | null
420
- cache.invalidate('browse:*') // invalidate by pattern
417
+ cache.invalidate('browse') // invalidate keys containing 'browse'
421
418
  cache.persist() // save to storage
422
419
  cache.restore() // restore from storage
423
420
  cache.clear() // clear all
package/dist/index.d.ts CHANGED
@@ -19,7 +19,6 @@ export declare interface ApiError {
19
19
  readonly source: ApiSource;
20
20
  readonly message: string;
21
21
  readonly status?: number;
22
- readonly retryable: boolean;
23
22
  readonly retryAfterMs?: number;
24
23
  readonly cause?: unknown;
25
24
  }
@@ -34,6 +33,22 @@ declare interface ApiFetchConfig {
34
33
 
35
34
  declare type ApiSource = 'layers';
36
35
 
36
+ export declare interface Article {
37
+ title: string;
38
+ handle: string;
39
+ summary: string;
40
+ author: string;
41
+ tags: string[];
42
+ image: ArticleImage | null;
43
+ publishedAt: string;
44
+ }
45
+
46
+ declare interface ArticleImage {
47
+ url: string;
48
+ width: number;
49
+ height: number;
50
+ }
51
+
37
52
  export { batch }
38
53
 
39
54
  declare interface BlocksContext {
@@ -76,8 +91,8 @@ export declare interface BlocksResult extends QueryResult {
76
91
  }
77
92
 
78
93
  declare interface Cache_2 {
79
- get(key: string): CacheEntry<QueryResult> | null;
80
- set(key: string, result: QueryResult): void;
94
+ get(key: string): CacheEntry<CacheValue> | null;
95
+ set(key: string, result: CacheValue): void;
81
96
  isExpired(key: string): boolean;
82
97
  invalidate(pattern: string): void;
83
98
  persist(): void;
@@ -88,7 +103,7 @@ declare interface Cache_2 {
88
103
  };
89
104
  }
90
105
 
91
- declare type CacheData = Record<string, QueryResult>;
106
+ declare type CacheData = Record<string, CacheValue>;
92
107
 
93
108
  declare interface CacheEntry<T> {
94
109
  data: T;
@@ -103,6 +118,8 @@ export declare interface CacheOptions {
103
118
  storageAdapter?: StorageAdapter;
104
119
  }
105
120
 
121
+ declare type CacheValue = QueryResult | ContentResult;
122
+
106
123
  export declare interface Client {
107
124
  collection: (options: CollectionOptions) => CollectionController;
108
125
  blocks: (options: BlocksOptions) => BlocksController;
@@ -110,6 +127,7 @@ export declare interface Client {
110
127
  search: (options?: SearchQuery) => SearchController;
111
128
  uploadImage: (options: UploadImageOptions) => UploadImageController;
112
129
  searchByImage: (options: SearchByImageQuery) => SearchByImageController;
130
+ searchContent: (options?: SearchContentQuery) => SearchContentController;
113
131
  config: ClientConfig;
114
132
  cache: Cache_2;
115
133
  }
@@ -117,17 +135,8 @@ export declare interface Client {
117
135
  export declare interface ClientConfig extends ApiClientConfig {
118
136
  includeMeta?: boolean;
119
137
  currency?: string;
120
- formatPrice?: (amount: number, currencyCode: string) => string;
138
+ formatPrice?: FormatPriceFn;
121
139
  swatches?: Swatch[];
122
- options?: string[];
123
- productMetafields?: {
124
- namespace: string;
125
- key: string;
126
- }[];
127
- variantMetafields?: {
128
- namespace: string;
129
- key: string;
130
- }[];
131
140
  cacheLimit?: number;
132
141
  cacheLifetime?: number;
133
142
  /** Custom storage adapter for cache persistence. Defaults to localStorage in browser. */
@@ -173,6 +182,39 @@ export declare interface ConfigError {
173
182
 
174
183
  declare type ConfigErrorCode = 'MISSING_CONFIG' | 'INVALID_CONFIG';
175
184
 
185
+ declare interface ContentArticleRaw {
186
+ title: string;
187
+ handle: string;
188
+ summary_text: string;
189
+ author: string;
190
+ tags: string[];
191
+ image: {
192
+ url: string;
193
+ width: number;
194
+ height: number;
195
+ } | null;
196
+ published_at: string;
197
+ }
198
+
199
+ declare interface ContentResult {
200
+ articles: unknown[];
201
+ query: string;
202
+ contentType: string | null;
203
+ totalResults: number;
204
+ page: number;
205
+ resultsPerPage: number;
206
+ }
207
+
208
+ declare interface ContentSearchResponse {
209
+ results: ContentArticleRaw[];
210
+ query: string;
211
+ contentType: string | null;
212
+ totalResults: number;
213
+ totalPages: number;
214
+ page: number;
215
+ resultsPerPage: number;
216
+ }
217
+
176
218
  export declare interface Controller<TData, TQuery = void> {
177
219
  readonly state: ReadonlySignal<QueryState<TData>>;
178
220
  execute(query?: TQuery): Promise<Result<TData, ClientError>>;
@@ -278,6 +320,8 @@ declare enum FilterOperator {
278
320
 
279
321
  declare type FilterValue = string | number | boolean;
280
322
 
323
+ declare type FormatPriceFn = (amount: number, currencyCode: string) => string;
324
+
281
325
  export declare function getClient(): Result<Client, ConfigError>;
282
326
 
283
327
  export declare function gt(property: string, value: number): FilterExpression;
@@ -382,21 +426,10 @@ declare interface MatchedVariant extends LayersVariant {
382
426
  rn?: number;
383
427
  }
384
428
 
385
- export declare interface Metafield extends MetafieldIdentifier {
386
- value: string;
387
- type: string;
388
- }
389
-
390
- declare interface MetafieldIdentifier {
391
- namespace: string;
392
- key: string;
393
- }
394
-
395
429
  export declare interface NetworkError {
396
430
  readonly _tag: 'NetworkError';
397
431
  readonly code: NetworkErrorCode;
398
432
  readonly message: string;
399
- readonly retryable: boolean;
400
433
  readonly retryAfterMs?: number;
401
434
  readonly cause?: unknown;
402
435
  }
@@ -465,7 +498,7 @@ export declare interface PriceRangeData {
465
498
  compareAtPriceRange: PriceRange | null;
466
499
  }
467
500
 
468
- export declare type Product<T extends Record<string, unknown> = Record<string, unknown>> = ProductBase & T;
501
+ export declare type Product = ProductBase & Record<string, unknown>;
469
502
 
470
503
  export declare interface ProductBase {
471
504
  id: CommerceBlocksID;
@@ -480,7 +513,7 @@ export declare interface ProductBase {
480
513
  images: Image_2[];
481
514
  featuredMedia: FeaturedMedia | null;
482
515
  priceRange: PriceRange;
483
- compareAtPriceRange: PriceRange | undefined;
516
+ compareAtPriceRange: PriceRange | null;
484
517
  options: RichProductOption[];
485
518
  selectedOptions: ProductOption[];
486
519
  variants: ProductVariant[];
@@ -508,7 +541,7 @@ export declare interface ProductCardController {
508
541
  setSelectedOptions(options: ProductOption[]): void;
509
542
  setSelectedVariant(variantId: number): void;
510
543
  setCarouselPosition(position: number): void;
511
- subscribe(callback: (state: ProductCardState) => void): Unsubscribe_2;
544
+ subscribe(callback: (state: ProductCardState) => void): Unsubscribe;
512
545
  dispose(): void;
513
546
  }
514
547
 
@@ -591,7 +624,6 @@ declare interface ProductResult {
591
624
  to: number;
592
625
  compare_at_price: number;
593
626
  };
594
- options: Record<string, string[]>;
595
627
  options_v2: OptionV2[];
596
628
  body_html?: string | null;
597
629
  is_gift_card?: boolean | null;
@@ -658,6 +690,7 @@ declare type Result<T, E = Error> = {
658
690
  };
659
691
 
660
692
  export declare interface RichProductOption extends ProductOption {
693
+ code: string;
661
694
  swatch?: Swatch;
662
695
  }
663
696
 
@@ -672,6 +705,33 @@ export declare interface SearchByImageQuery extends QueryParams {
672
705
  tuning?: SearchTuning;
673
706
  }
674
707
 
708
+ export declare interface SearchContentController {
709
+ readonly state: ReadonlySignal<QueryState<SearchContentResult>>;
710
+ execute(query?: SearchContentQuery): Promise<Result<SearchContentResult, ClientError>>;
711
+ subscribe(callback: (state: QueryState<SearchContentResult>) => void): Unsubscribe;
712
+ dispose(): void;
713
+ }
714
+
715
+ export declare interface SearchContentQuery {
716
+ query?: string;
717
+ contentType?: string;
718
+ page?: number;
719
+ limit?: number;
720
+ tuning?: SearchTuning;
721
+ signal?: AbortSignal;
722
+ transformRequest?: RequestTransformer;
723
+ temporary?: boolean;
724
+ }
725
+
726
+ export declare interface SearchContentResult {
727
+ articles: Article[];
728
+ query: string;
729
+ contentType: string | null;
730
+ totalResults: number;
731
+ page: number;
732
+ resultsPerPage: number;
733
+ }
734
+
675
735
  export declare interface SearchController {
676
736
  readonly state: ReadonlySignal<QueryState<SearchResult>>;
677
737
  prepare(query?: SearchQuery): Promise<Result<SearchPrepareResult, ClientError>>;
@@ -783,13 +843,12 @@ export declare interface Transforms {
783
843
  block?: (result: BlocksResult, raw: LayersResponse & {
784
844
  block?: BlocksInfo;
785
845
  }) => BlocksResult;
846
+ searchContent?: (result: SearchContentResult, raw: ContentSearchResponse) => SearchContentResult;
786
847
  filters?: (filters: unknown) => FilterGroup | undefined;
787
848
  }
788
849
 
789
850
  export declare type Unsubscribe = () => void;
790
851
 
791
- declare type Unsubscribe_2 = () => void;
792
-
793
852
  export declare interface UploadImageController {
794
853
  readonly state: ReadonlySignal<QueryState<UploadImageResult>>;
795
854
  subscribe(callback: (state: QueryState<UploadImageResult>) => void): Unsubscribe;