@devite/shopware-client 1.3.3 → 1.4.1

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/dist/index.cjs CHANGED
@@ -11803,7 +11803,12 @@ class ContextTokenEntry {
11803
11803
  return this.token !== null;
11804
11804
  }
11805
11805
  save(response) {
11806
- this.token = response.headers?.get("sw-context-token") || null;
11806
+ const headerToken = response.headers?.get("sw-context-token") || null;
11807
+ if (headerToken?.includes(", ")) {
11808
+ this.token = headerToken.split(", ")[1];
11809
+ } else {
11810
+ this.token = headerToken;
11811
+ }
11807
11812
  }
11808
11813
  clear() {
11809
11814
  this.token = null;
@@ -12085,7 +12090,10 @@ class CartClient extends Client {
12085
12090
  * @throws {Error} if the request failed
12086
12091
  */
12087
12092
  async getOrCreateCart() {
12088
- const response = await this.get("/checkout/cart");
12093
+ const response = await this.get(
12094
+ "/checkout/cart",
12095
+ this.client.withContextToken()
12096
+ );
12089
12097
  if (response.statusCode === 200)
12090
12098
  return response.body.data;
12091
12099
  throw new ShopwareError("Failed to get or create cart", response);
@@ -12094,7 +12102,10 @@ class CartClient extends Client {
12094
12102
  * @throws {Error} if the request failed
12095
12103
  */
12096
12104
  async deleteCart() {
12097
- const response = await this.delete("/checkout/cart");
12105
+ const response = await this.delete(
12106
+ "/checkout/cart",
12107
+ this.client.withContextToken()
12108
+ );
12098
12109
  if (response.statusCode === 200)
12099
12110
  return response.body.data;
12100
12111
  throw new ShopwareError("Failed to delete cart", response);
@@ -12103,9 +12114,12 @@ class CartClient extends Client {
12103
12114
  * @throws {Error} if the request failed
12104
12115
  */
12105
12116
  async addLineItems(request) {
12106
- const response = await this.post("/checkout/cart/line-item", {
12107
- body: new JsonPayload(request)
12108
- });
12117
+ const response = await this.post(
12118
+ "/checkout/cart/line-item",
12119
+ this.client.withContextToken({
12120
+ body: new JsonPayload(request)
12121
+ })
12122
+ );
12109
12123
  if (response.statusCode === 200)
12110
12124
  return response.body.data;
12111
12125
  throw new ShopwareError("Failed to add line items to cart", response);
@@ -12114,9 +12128,12 @@ class CartClient extends Client {
12114
12128
  * @throws {Error} if the request failed
12115
12129
  */
12116
12130
  async removeLineItems(request) {
12117
- const response = await this.post("/checkout/cart/line-item/delete", {
12118
- body: new JsonPayload(request)
12119
- });
12131
+ const response = await this.post(
12132
+ "/checkout/cart/line-item/delete",
12133
+ this.client.withContextToken({
12134
+ body: new JsonPayload(request)
12135
+ })
12136
+ );
12120
12137
  if (response.statusCode === 200)
12121
12138
  return response.body.data;
12122
12139
  throw new ShopwareError("Failed to remove line items from cart", response);
@@ -12125,9 +12142,12 @@ class CartClient extends Client {
12125
12142
  * @throws {Error} if the request failed
12126
12143
  */
12127
12144
  async updateLineItems(request) {
12128
- const response = await this.patch("/checkout/cart/line-item", {
12129
- body: new JsonPayload(request)
12130
- });
12145
+ const response = await this.patch(
12146
+ "/checkout/cart/line-item",
12147
+ this.client.withContextToken({
12148
+ body: new JsonPayload(request)
12149
+ })
12150
+ );
12131
12151
  if (response.statusCode === 200)
12132
12152
  return response.body.data;
12133
12153
  throw new ShopwareError("Failed to update line items in cart", response);
@@ -12222,17 +12242,27 @@ class ContextClient extends Client {
12222
12242
  * @throws {Error} if the request failed
12223
12243
  */
12224
12244
  async getContext() {
12225
- const response = await this.get("/context");
12226
- if (response.statusCode === 200) return response.body;
12245
+ const entry = this.client.authStore.getEntry(
12246
+ AuthenticationType.CONTEXT_TOKEN
12247
+ );
12248
+ const response = await this.get(
12249
+ "/context",
12250
+ entry ? this.client.withContextToken() : void 0
12251
+ );
12252
+ if (response.statusCode === 200) {
12253
+ entry?.save(response);
12254
+ return response.body.data;
12255
+ }
12227
12256
  throw new ShopwareError("Failed to fetch context", response);
12228
12257
  }
12229
12258
  /**
12230
12259
  * @throws {Error} if the request failed
12231
12260
  */
12232
12261
  async updateContext(context) {
12233
- const response = await this.patch("/context", {
12234
- body: new JsonPayload(context)
12235
- });
12262
+ const response = await this.patch(
12263
+ "/context",
12264
+ this.client.withContextToken({ body: new JsonPayload(context) })
12265
+ );
12236
12266
  if (response.statusCode === 200)
12237
12267
  return response.body.data;
12238
12268
  throw new ShopwareError("Failed to update context", response);
@@ -12314,7 +12344,7 @@ class NewsletterClient extends Client {
12314
12344
  const response = await this.post(`/newsletter/confirm`, {
12315
12345
  body: new JsonPayload(request)
12316
12346
  });
12317
- if (response.statusCode === 200) return;
12347
+ if (response.statusCode === 204) return;
12318
12348
  throw new ShopwareError("Failed to confirm newsletter subscription", response);
12319
12349
  }
12320
12350
  /**
@@ -12324,7 +12354,7 @@ class NewsletterClient extends Client {
12324
12354
  const response = await this.post(`/newsletter/subscribe`, {
12325
12355
  body: new JsonPayload(request)
12326
12356
  });
12327
- if (response.statusCode === 200) return;
12357
+ if (response.statusCode === 204) return;
12328
12358
  throw new ShopwareError("Failed to update newsletter subscription", response);
12329
12359
  }
12330
12360
  /**
@@ -12334,7 +12364,7 @@ class NewsletterClient extends Client {
12334
12364
  const response = await this.post(`/newsletter/unsubscribe`, {
12335
12365
  body: new JsonPayload(request)
12336
12366
  });
12337
- if (response.statusCode === 200) return;
12367
+ if (response.statusCode === 204) return;
12338
12368
  throw new ShopwareError("Failed to unsubscribe newsletter subscription", response);
12339
12369
  }
12340
12370
  }
@@ -12707,6 +12737,13 @@ class StoreShopwareClient extends ShopwareClient {
12707
12737
  }
12708
12738
  });
12709
12739
  }
12740
+ setContextToken(token) {
12741
+ this.authStore.getOrCreateEntry(new ContextTokenEntry()).save({
12742
+ statusCode: 200,
12743
+ statusMessage: "OK",
12744
+ headers: new Headers({ "sw-context-token": token })
12745
+ });
12746
+ }
12710
12747
  withContextToken(options = {}) {
12711
12748
  const entry = this.authStore.getEntry(
12712
12749
  AuthenticationType.CONTEXT_TOKEN