perspectapi-ts-sdk 6.1.2 → 6.3.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.
@@ -3,12 +3,13 @@
3
3
  * Cloudflare Workers compatible - uses native fetch API
4
4
  */
5
5
 
6
- import type {
7
- ApiResponse,
8
- ApiError,
9
- RequestOptions,
10
- PerspectApiConfig
6
+ import type {
7
+ ApiResponse,
8
+ ApiError,
9
+ RequestOptions,
10
+ PerspectApiConfig
11
11
  } from '../types';
12
+ import { PerspectApiError } from '../types';
12
13
 
13
14
  export class HttpClient {
14
15
  private baseUrl: string;
@@ -266,12 +267,12 @@ export class HttpClient {
266
267
 
267
268
  if (!response.ok) {
268
269
  console.error(`[HTTP Client - Response] Error response received`);
269
- const error: ApiError = {
270
+ const error = new PerspectApiError({
270
271
  message: data?.error || data?.message || `HTTP ${response.status}: ${response.statusText}`,
271
272
  status: response.status,
272
273
  code: data?.code,
273
274
  details: data,
274
- };
275
+ });
275
276
  console.error(`[HTTP Client - Response] Throwing error:`, error);
276
277
  throw error;
277
278
  }
@@ -26,15 +26,31 @@ export class CollectionsV2Client extends BaseV2Client {
26
26
  }
27
27
 
28
28
  async create(siteName: string, data: V2CollectionCreateParams): Promise<V2Collection> {
29
- return this.post<V2Collection>(this.sitePath(siteName, 'collections'), data);
29
+ const result = await this.post<V2Collection>(this.sitePath(siteName, 'collections'), data);
30
+ await this.invalidateCache({ keys: [this.sitePath(siteName, 'collections')] });
31
+ return result;
30
32
  }
31
33
 
32
34
  async update(siteName: string, id: string, data: V2CollectionUpdateParams): Promise<V2Collection> {
33
- return this.patchOne<V2Collection>(this.sitePath(siteName, 'collections', id), data);
35
+ const result = await this.patchOne<V2Collection>(this.sitePath(siteName, 'collections', id), data);
36
+ await this.invalidateCache({
37
+ keys: [
38
+ this.sitePath(siteName, 'collections'),
39
+ this.sitePath(siteName, 'collections', id),
40
+ ],
41
+ });
42
+ return result;
34
43
  }
35
44
 
36
45
  async del(siteName: string, id: string): Promise<V2Deleted> {
37
- return this.deleteOne(this.sitePath(siteName, 'collections', id));
46
+ const result = await this.deleteOne(this.sitePath(siteName, 'collections', id));
47
+ await this.invalidateCache({
48
+ keys: [
49
+ this.sitePath(siteName, 'collections'),
50
+ this.sitePath(siteName, 'collections', id),
51
+ ],
52
+ });
53
+ return result;
38
54
  }
39
55
 
40
56
  // --- Items ---
@@ -50,15 +66,23 @@ export class CollectionsV2Client extends BaseV2Client {
50
66
  collectionId: string,
51
67
  data: { product_id: string; max_quantity?: number | null; position?: number },
52
68
  ): Promise<V2CollectionItem> {
53
- return this.post<V2CollectionItem>(
69
+ const result = await this.post<V2CollectionItem>(
54
70
  this.sitePath(siteName, 'collections', `${collectionId}/items`),
55
71
  data,
56
72
  );
73
+ await this.invalidateCache({
74
+ keys: [this.sitePath(siteName, 'collections', `${collectionId}/items`)],
75
+ });
76
+ return result;
57
77
  }
58
78
 
59
79
  async removeItem(siteName: string, collectionId: string, itemId: string): Promise<V2Deleted> {
60
- return this.deleteOne(
80
+ const result = await this.deleteOne(
61
81
  this.sitePath(siteName, 'collections', `${collectionId}/items/${itemId}`),
62
82
  );
83
+ await this.invalidateCache({
84
+ keys: [this.sitePath(siteName, 'collections', `${collectionId}/items`)],
85
+ });
86
+ return result;
63
87
  }
64
88
  }