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

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) {
@@ -61,14 +61,8 @@ class m {
61
61
  if (!e || e.length === 0) return !0;
62
62
  const t = [...e];
63
63
  for (const s of t)
64
- if (!((n = s.signal) != null && n.aborted)) {
65
- try {
66
- typeof s.listener == "function" ? s.listener.call(this, r) : s.listener.handleEvent(r);
67
- } catch (a) {
68
- console.error("Error in event listener:", a);
69
- }
70
- if (s.options.once && this.removeEventListener(r.type, s.listener, s.options), r.cancelBubble) break;
71
- }
64
+ if (!((n = s.signal) != null && n.aborted) && (typeof s.listener == "function" ? s.listener.call(this, r) : s.listener.handleEvent(r), s.options.once && this.removeEventListener(r.type, s.listener, s.options), r.cancelBubble))
65
+ break;
72
66
  return !r.defaultPrevented;
73
67
  }
74
68
  normalizeOptions(r) {
@@ -110,13 +104,13 @@ class m {
110
104
  r ? this.listeners.delete(r) : this.listeners.clear();
111
105
  }
112
106
  }
113
- const f = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
114
- function b(o) {
107
+ const u = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
108
+ function w(o) {
115
109
  const r = new URLSearchParams(), e = (t, n) => {
116
- f(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
110
+ u(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
117
111
  };
118
112
  for (const [t, n] of Object.entries(o))
119
- if (!f(n))
113
+ if (!u(n))
120
114
  if (typeof n == "string")
121
115
  r.append(t, String(n));
122
116
  else if (Array.isArray(n))
@@ -126,11 +120,11 @@ function b(o) {
126
120
  e(t, n);
127
121
  return r;
128
122
  }
129
- const x = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
130
- class v extends m {
123
+ const b = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
124
+ class k extends m {
131
125
  constructor(e) {
132
126
  super();
133
- u(this, "options", {
127
+ l(this, "options", {
134
128
  baseURL: "",
135
129
  apiKey: "",
136
130
  language: "",
@@ -185,7 +179,7 @@ class v extends m {
185
179
  return this.options.reflectContextToken;
186
180
  }
187
181
  parseOperation(e) {
188
- const t = x.exec(e);
182
+ const t = b.exec(e);
189
183
  if (!(t != null && t.groups))
190
184
  throw new Error("Invalid operation format");
191
185
  return {
@@ -202,10 +196,12 @@ class v extends m {
202
196
  });
203
197
  }
204
198
  async queryRaw(e, t) {
199
+ let n;
205
200
  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}`, {
201
+ 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)}` : "";
202
+ n = await fetch(`${this.options.baseURL}/store-api${c}${p}`, {
207
203
  ...t,
208
- method: n,
204
+ method: s,
209
205
  headers: {
210
206
  "Content-Type": "application/json",
211
207
  "sw-access-key": this.options.apiKey,
@@ -216,27 +212,28 @@ class v extends m {
216
212
  },
217
213
  body: t != null && t.body ? JSON.stringify(t.body) : void 0
218
214
  });
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(
215
+ } catch (s) {
216
+ const a = new h(
235
217
  "Failed to communicate with Shopware API",
236
218
  500,
237
- n instanceof Error ? n.message : void 0
219
+ s instanceof Error ? s.message : void 0
238
220
  );
221
+ throw this.emit("error", a), a;
239
222
  }
223
+ if (!n.ok) {
224
+ const s = new h(
225
+ `Shopware API request failed: ${n.statusText}`,
226
+ n.status,
227
+ await n.json().catch(() => {
228
+ })
229
+ );
230
+ throw this.emit("error", s), s;
231
+ }
232
+ if (this.options.reflectContextToken) {
233
+ const s = n.headers.get("sw-context-token");
234
+ s && (this.contextToken = s);
235
+ }
236
+ return n;
240
237
  }
241
238
  async query(e, t) {
242
239
  return (await this.queryRaw(e, t)).json().catch(() => {
@@ -244,6 +241,6 @@ class v extends m {
244
241
  }
245
242
  }
246
243
  export {
247
- p as ShopwareApiError,
248
- v as ShopwareClient
244
+ h as ShopwareApiError,
245
+ k as ShopwareClient
249
246
  };
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.10",
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>(
@@ -1,3 +1,5 @@
1
+ // TODO: Move naming to hooks - deprecate event emitter
2
+
1
3
  interface EventListenerEntry {
2
4
  listener: EventListenerOrEventListenerObject
3
5
  options: AddEventListenerOptions
@@ -68,15 +70,10 @@ export class EventEmitter implements EventTarget {
68
70
  for (const entry of entriesCopy) {
69
71
  if (entry.signal?.aborted) continue
70
72
 
71
- try {
72
- if (typeof entry.listener === 'function') {
73
- entry.listener.call(this, event)
74
- } else {
75
- entry.listener.handleEvent(event)
76
- }
77
- } catch (error) {
78
- // In a real implementation, you might want to handle errors differently
79
- console.error('Error in event listener:', error)
73
+ if (typeof entry.listener === 'function') {
74
+ entry.listener.call(this, event)
75
+ } else {
76
+ entry.listener.handleEvent(event)
80
77
  }
81
78
 
82
79
  // Remove if 'once' option was set