@teamnovu/kit-shopware-api-client 0.0.8 → 0.0.9

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.mjs CHANGED
@@ -1,8 +1,8 @@
1
- var d = Object.defineProperty;
2
- var g = (o, r, e) => r in o ? d(o, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[r] = e;
3
- var u = (o, r, e) => g(o, typeof r != "symbol" ? r + "" : r, e);
1
+ var f = Object.defineProperty;
2
+ var d = (o, r, e) => r in o ? f(o, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[r] = e;
3
+ var l = (o, r, e) => d(o, typeof r != "symbol" ? r + "" : r, e);
4
4
  import { z as i } from "zod";
5
- const y = i.object({
5
+ const g = i.object({
6
6
  status: i.string(),
7
7
  code: i.string(),
8
8
  title: i.string().optional(),
@@ -18,21 +18,21 @@ const y = i.object({
18
18
  function: i.string()
19
19
  })
20
20
  ).optional()
21
- }), w = i.object({
22
- errors: i.array(y)
21
+ }), y = i.object({
22
+ errors: i.array(g)
23
23
  });
24
- class p extends Error {
24
+ class h extends Error {
25
25
  constructor(e, t, n) {
26
26
  super(e);
27
- u(this, "errors");
27
+ l(this, "errors");
28
28
  this.status = t, this.response = n, this.name = "ShopwareApiError";
29
- const { success: s, data: a } = w.safeParse(n);
29
+ const { success: s, data: a } = y.safeParse(n);
30
30
  this.errors = s ? a.errors : [];
31
31
  }
32
32
  }
33
33
  class m {
34
34
  constructor() {
35
- u(this, "listeners", /* @__PURE__ */ new Map());
35
+ l(this, "listeners", /* @__PURE__ */ new Map());
36
36
  }
37
37
  addEventListener(r, e, t) {
38
38
  if (!e) return;
@@ -52,7 +52,7 @@ class m {
52
52
  if (!e) return;
53
53
  const n = this.listeners.get(r);
54
54
  if (!n) return;
55
- const s = this.normalizeOptions(t), a = n.findIndex((h) => h.listener === e && this.optionsMatch(h.options, s));
55
+ const s = this.normalizeOptions(t), a = n.findIndex((c) => c.listener === e && this.optionsMatch(c.options, s));
56
56
  a !== -1 && (n.splice(a, 1), n.length === 0 && this.listeners.delete(r));
57
57
  }
58
58
  dispatchEvent(r) {
@@ -110,13 +110,13 @@ class m {
110
110
  r ? this.listeners.delete(r) : this.listeners.clear();
111
111
  }
112
112
  }
113
- const f = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
114
- function b(o) {
113
+ const u = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
114
+ function w(o) {
115
115
  const r = new URLSearchParams(), e = (t, n) => {
116
- f(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
116
+ u(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
117
117
  };
118
118
  for (const [t, n] of Object.entries(o))
119
- if (!f(n))
119
+ if (!u(n))
120
120
  if (typeof n == "string")
121
121
  r.append(t, String(n));
122
122
  else if (Array.isArray(n))
@@ -126,11 +126,11 @@ function b(o) {
126
126
  e(t, n);
127
127
  return r;
128
128
  }
129
- const x = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
130
- class v extends m {
129
+ const b = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
130
+ class k extends m {
131
131
  constructor(e) {
132
132
  super();
133
- u(this, "options", {
133
+ l(this, "options", {
134
134
  baseURL: "",
135
135
  apiKey: "",
136
136
  language: "",
@@ -185,7 +185,7 @@ class v extends m {
185
185
  return this.options.reflectContextToken;
186
186
  }
187
187
  parseOperation(e) {
188
- const t = x.exec(e);
188
+ const t = b.exec(e);
189
189
  if (!(t != null && t.groups))
190
190
  throw new Error("Invalid operation format");
191
191
  return {
@@ -202,10 +202,12 @@ class v extends m {
202
202
  });
203
203
  }
204
204
  async queryRaw(e, t) {
205
+ let n;
205
206
  try {
206
- const { method: n, url: s } = this.parseOperation(e), a = this.interpolateUrl(s, (t == null ? void 0 : t.params) ?? {}), h = t != null && t.query ? `?${b(t.query)}` : "", c = await fetch(`${this.options.baseURL}/store-api${a}${h}`, {
207
+ const { method: s, url: a } = this.parseOperation(e), c = this.interpolateUrl(a, (t == null ? void 0 : t.params) ?? {}), p = t != null && t.query ? `?${w(t.query)}` : "";
208
+ n = await fetch(`${this.options.baseURL}/store-api${c}${p}`, {
207
209
  ...t,
208
- method: n,
210
+ method: s,
209
211
  headers: {
210
212
  "Content-Type": "application/json",
211
213
  "sw-access-key": this.options.apiKey,
@@ -216,27 +218,28 @@ class v extends m {
216
218
  },
217
219
  body: t != null && t.body ? JSON.stringify(t.body) : void 0
218
220
  });
219
- if (!c.ok) {
220
- const l = new p(
221
- `Shopware API request failed: ${c.statusText}`,
222
- c.status,
223
- await c.json().catch(() => {
224
- })
225
- );
226
- throw this.emit("error", l), l;
227
- }
228
- if (this.options.reflectContextToken) {
229
- const l = c.headers.get("sw-context-token");
230
- l && (this.contextToken = l);
231
- }
232
- return c;
233
- } catch (n) {
234
- throw n instanceof p ? n : new p(
221
+ } catch (s) {
222
+ const a = new h(
235
223
  "Failed to communicate with Shopware API",
236
224
  500,
237
- n instanceof Error ? n.message : void 0
225
+ s instanceof Error ? s.message : void 0
238
226
  );
227
+ throw this.emit("error", a), a;
228
+ }
229
+ if (!n.ok) {
230
+ const s = new h(
231
+ `Shopware API request failed: ${n.statusText}`,
232
+ n.status,
233
+ await n.json().catch(() => {
234
+ })
235
+ );
236
+ throw this.emit("error", s), s;
237
+ }
238
+ if (this.options.reflectContextToken) {
239
+ const s = n.headers.get("sw-context-token");
240
+ s && (this.contextToken = s);
239
241
  }
242
+ return n;
240
243
  }
241
244
  async query(e, t) {
242
245
  return (await this.queryRaw(e, t)).json().catch(() => {
@@ -244,6 +247,6 @@ class v extends m {
244
247
  }
245
248
  }
246
249
  export {
247
- p as ShopwareApiError,
248
- v as ShopwareClient
250
+ h as ShopwareApiError,
251
+ k as ShopwareClient
249
252
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-shopware-api-client",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "A client for the Shopware API",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "dist/index.mjs",
package/src/client.ts CHANGED
@@ -134,6 +134,8 @@ export class ShopwareClient<Operations> extends EventEmitter {
134
134
  operation: OperationKey,
135
135
  options?: OperationOptions<Operations, OperationKey>,
136
136
  ): Promise<Response> {
137
+ let response: Response
138
+
137
139
  try {
138
140
  const { method, url } = this.parseOperation(operation)
139
141
 
@@ -141,7 +143,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
141
143
 
142
144
  const query = options?.query ? `?${createQueryParams(options.query)}` : ''
143
145
 
144
- const response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}${query}`, {
146
+ response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}${query}`, {
145
147
  ...options,
146
148
  method,
147
149
  headers: {
@@ -154,38 +156,38 @@ export class ShopwareClient<Operations> extends EventEmitter {
154
156
  },
155
157
  body: options?.body ? JSON.stringify(options.body) : undefined,
156
158
  })
159
+ } catch (error) {
160
+ const shopwareError = new ShopwareApiError(
161
+ 'Failed to communicate with Shopware API',
162
+ 500,
163
+ error instanceof Error ? error.message : undefined,
164
+ )
157
165
 
158
- if (!response.ok) {
159
- const error = new ShopwareApiError(
160
- `Shopware API request failed: ${response.statusText}`,
161
- response.status,
162
- await response.json().catch(() => undefined),
163
- )
166
+ this.emit('error', shopwareError)
164
167
 
165
- this.emit('error', error)
168
+ throw shopwareError
169
+ }
166
170
 
167
- throw error
168
- }
171
+ if (!response.ok) {
172
+ const error = new ShopwareApiError(
173
+ `Shopware API request failed: ${response.statusText}`,
174
+ response.status,
175
+ await response.json().catch(() => undefined),
176
+ )
169
177
 
170
- if (this.options.reflectContextToken) {
171
- const contextToken = response.headers.get('sw-context-token')
172
- if (contextToken) {
173
- this.contextToken = contextToken
174
- }
175
- }
178
+ this.emit('error', error)
176
179
 
177
- return response
178
- } catch (error) {
179
- if (error instanceof ShopwareApiError) {
180
- throw error
181
- }
180
+ throw error
181
+ }
182
182
 
183
- throw new ShopwareApiError(
184
- 'Failed to communicate with Shopware API',
185
- 500,
186
- error instanceof Error ? error.message : undefined,
187
- )
183
+ if (this.options.reflectContextToken) {
184
+ const contextToken = response.headers.get('sw-context-token')
185
+ if (contextToken) {
186
+ this.contextToken = contextToken
187
+ }
188
188
  }
189
+
190
+ return response
189
191
  }
190
192
 
191
193
  async query<OperationKey extends (keyof Operations) & string>(