@teamnovu/kit-shopware-api-client 0.0.7 → 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/client.d.ts CHANGED
@@ -4,17 +4,23 @@ export interface ResponseType<TKey> {
4
4
  __response?: TKey;
5
5
  }
6
6
  export type BrandedResponse<Operations, OperationKey extends (keyof Operations) & string> = ResponseType<OperationKey> & OperationProp<Operations, OperationKey, 'response'> & {
7
- __BLBL: string;
7
+ __brand: string;
8
8
  };
9
9
  export declare class ShopwareClient<Operations> extends EventEmitter {
10
10
  private options;
11
11
  constructor(options: ShopwareClientOptions);
12
- setBaseURL(baseURL: string): void;
13
- setApiKey(apiKey: string): void;
14
- setLanguage(language: string | undefined): void;
15
- setContextToken(contextToken: string | undefined): void;
16
- setIncludeSeoUrls(includeSeoUrls?: boolean): void;
17
- setReflectContextToken(reflectContextToken?: boolean): void;
12
+ set baseURL(baseURL: string);
13
+ get baseURL(): string;
14
+ set apiKey(apiKey: string);
15
+ get apiKey(): string;
16
+ set language(language: string | undefined);
17
+ get language(): string | undefined;
18
+ set contextToken(contextToken: string | undefined);
19
+ get contextToken(): string | undefined;
20
+ set includeSeoUrls(includeSeoUrls: boolean | undefined);
21
+ get includeSeoUrls(): boolean | undefined;
22
+ set reflectContextToken(reflectContextToken: boolean | undefined);
23
+ get reflectContextToken(): boolean | undefined;
18
24
  private parseOperation;
19
25
  private interpolateUrl;
20
26
  queryRaw<OperationKey extends (keyof Operations) & string>(operation: OperationKey, options?: OperationOptions<Operations, OperationKey>): Promise<Response>;
package/dist/index.mjs CHANGED
@@ -1,73 +1,73 @@
1
- var d = Object.defineProperty;
2
- var g = (i, r, e) => r in i ? d(i, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[r] = e;
3
- var u = (i, r, e) => g(i, typeof r != "symbol" ? r + "" : r, e);
4
- import { z as o } from "zod";
5
- const y = o.object({
6
- status: o.string(),
7
- code: o.string(),
8
- title: o.string().optional(),
9
- detail: o.string().optional(),
10
- meta: o.record(o.unknown()).optional(),
11
- source: o.object({
12
- pointer: o.string().optional()
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
+ import { z as i } from "zod";
5
+ const g = i.object({
6
+ status: i.string(),
7
+ code: i.string(),
8
+ title: i.string().optional(),
9
+ detail: i.string().optional(),
10
+ meta: i.record(i.unknown()).optional(),
11
+ source: i.object({
12
+ pointer: i.string().optional()
13
13
  }).optional(),
14
- trace: o.array(
15
- o.object({
16
- file: o.string(),
17
- line: o.number(),
18
- function: o.string()
14
+ trace: i.array(
15
+ i.object({
16
+ file: i.string(),
17
+ line: i.number(),
18
+ function: i.string()
19
19
  })
20
20
  ).optional()
21
- }), w = o.object({
22
- errors: o.array(y)
21
+ }), y = i.object({
22
+ errors: i.array(g)
23
23
  });
24
- class p extends Error {
25
- constructor(e, t, s) {
24
+ class h extends Error {
25
+ constructor(e, t, n) {
26
26
  super(e);
27
- u(this, "errors");
28
- this.status = t, this.response = s, this.name = "ShopwareApiError";
29
- const { success: n, data: a } = w.safeParse(s);
30
- this.errors = n ? a.errors : [];
27
+ l(this, "errors");
28
+ this.status = t, this.response = n, this.name = "ShopwareApiError";
29
+ const { success: s, data: a } = y.safeParse(n);
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;
39
- const s = this.normalizeOptions(t), n = {
39
+ const n = this.normalizeOptions(t), s = {
40
40
  listener: e,
41
- options: s
41
+ options: n
42
42
  };
43
- if (s.signal) {
44
- if (s.signal.aborted) return;
45
- n.signal = s.signal, s.signal.addEventListener("abort", () => {
43
+ if (n.signal) {
44
+ if (n.signal.aborted) return;
45
+ s.signal = n.signal, n.signal.addEventListener("abort", () => {
46
46
  this.removeEventListener(r, e, t);
47
47
  }, { once: !0 });
48
48
  }
49
- this.listeners.has(r) || this.listeners.set(r, []), this.listeners.get(r).push(n);
49
+ this.listeners.has(r) || this.listeners.set(r, []), this.listeners.get(r).push(s);
50
50
  }
51
51
  removeEventListener(r, e, t) {
52
52
  if (!e) return;
53
- const s = this.listeners.get(r);
54
- if (!s) return;
55
- const n = this.normalizeOptions(t), a = s.findIndex((h) => h.listener === e && this.optionsMatch(h.options, n));
56
- a !== -1 && (s.splice(a, 1), s.length === 0 && this.listeners.delete(r));
53
+ const n = this.listeners.get(r);
54
+ if (!n) return;
55
+ const s = this.normalizeOptions(t), a = n.findIndex((c) => c.listener === e && this.optionsMatch(c.options, s));
56
+ a !== -1 && (n.splice(a, 1), n.length === 0 && this.listeners.delete(r));
57
57
  }
58
58
  dispatchEvent(r) {
59
- var s;
59
+ var n;
60
60
  const e = this.listeners.get(r.type);
61
61
  if (!e || e.length === 0) return !0;
62
62
  const t = [...e];
63
- for (const n of t)
64
- if (!((s = n.signal) != null && s.aborted)) {
63
+ for (const s of t)
64
+ if (!((n = s.signal) != null && n.aborted)) {
65
65
  try {
66
- typeof n.listener == "function" ? n.listener.call(this, r) : n.listener.handleEvent(r);
66
+ typeof s.listener == "function" ? s.listener.call(this, r) : s.listener.handleEvent(r);
67
67
  } catch (a) {
68
68
  console.error("Error in event listener:", a);
69
69
  }
70
- if (n.options.once && this.removeEventListener(r.type, n.listener, n.options), r.cancelBubble) break;
70
+ if (s.options.once && this.removeEventListener(r.type, s.listener, s.options), r.cancelBubble) break;
71
71
  }
72
72
  return !r.defaultPrevented;
73
73
  }
@@ -90,14 +90,14 @@ class m {
90
90
  this.removeEventListener(r, e, t);
91
91
  }
92
92
  once(r, e, t) {
93
- const s = typeof t == "boolean" ? {
93
+ const n = typeof t == "boolean" ? {
94
94
  capture: t,
95
95
  once: !0
96
96
  } : {
97
97
  ...t,
98
98
  once: !0
99
99
  };
100
- this.addEventListener(r, e, s);
100
+ this.addEventListener(r, e, n);
101
101
  }
102
102
  emit(r, e) {
103
103
  const t = new CustomEvent(r, { detail: e });
@@ -110,27 +110,27 @@ class m {
110
110
  r ? this.listeners.delete(r) : this.listeners.clear();
111
111
  }
112
112
  }
113
- const f = (i) => i == null || typeof i == "number" && isNaN(i) || typeof i == "function";
114
- function b(i) {
115
- const r = new URLSearchParams(), e = (t, s) => {
116
- f(s) || (typeof s == "object" && r.append(t, JSON.stringify(s)), r.append(t, String(s)));
113
+ const u = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
114
+ function w(o) {
115
+ const r = new URLSearchParams(), e = (t, n) => {
116
+ u(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
117
117
  };
118
- for (const [t, s] of Object.entries(i))
119
- if (!f(s))
120
- if (typeof s == "string")
121
- r.append(t, String(s));
122
- else if (Array.isArray(s))
123
- for (const n of s)
124
- e(`${t}[]`, n);
118
+ for (const [t, n] of Object.entries(o))
119
+ if (!u(n))
120
+ if (typeof n == "string")
121
+ r.append(t, String(n));
122
+ else if (Array.isArray(n))
123
+ for (const s of n)
124
+ e(`${t}[]`, s);
125
125
  else
126
- e(t, s);
126
+ e(t, n);
127
127
  return r;
128
128
  }
129
- const E = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
129
+ const b = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
130
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: "",
@@ -138,36 +138,54 @@ class k extends m {
138
138
  includeSeoUrls: !1,
139
139
  reflectContextToken: !0
140
140
  });
141
- this.setBaseURL(e.baseURL ?? this.options.baseURL), this.setApiKey(e.apiKey ?? this.options.apiKey), this.setLanguage(e.language ?? this.options.language), this.setContextToken(e.contextToken ?? this.options.contextToken), this.setIncludeSeoUrls(e.includeSeoUrls ?? this.options.includeSeoUrls), this.setReflectContextToken(e.reflectContextToken ?? this.options.reflectContextToken);
141
+ this.baseURL = e.baseURL ?? this.options.baseURL, this.apiKey = e.apiKey ?? this.options.apiKey, this.language = e.language ?? this.options.language, this.contextToken = e.contextToken ?? this.options.contextToken, this.includeSeoUrls = e.includeSeoUrls ?? this.options.includeSeoUrls, this.reflectContextToken = e.reflectContextToken ?? this.options.reflectContextToken;
142
142
  }
143
- setBaseURL(e) {
143
+ set baseURL(e) {
144
144
  if (typeof e != "string")
145
145
  throw new Error("Invalid baseURL: It must be a string.");
146
146
  this.options.baseURL = e;
147
147
  }
148
- setApiKey(e) {
148
+ get baseURL() {
149
+ return this.options.baseURL;
150
+ }
151
+ set apiKey(e) {
149
152
  if (typeof e != "string")
150
153
  throw new Error("Invalid apiKey: It must be a string.");
151
154
  this.options.apiKey = e;
152
155
  }
153
- setLanguage(e) {
156
+ get apiKey() {
157
+ return this.options.apiKey;
158
+ }
159
+ set language(e) {
154
160
  if (e && typeof e != "string")
155
161
  throw new Error("Invalid language: If provided, it must be a string.");
156
162
  this.options.language = e;
157
163
  }
158
- setContextToken(e) {
164
+ get language() {
165
+ return this.options.language;
166
+ }
167
+ set contextToken(e) {
159
168
  if (e && typeof e != "string")
160
169
  throw new Error("Invalid contextToken: If provided, it must be a string.");
161
170
  this.options.contextToken = e, this.emit("contextToken", e);
162
171
  }
163
- setIncludeSeoUrls(e = !0) {
172
+ get contextToken() {
173
+ return this.options.contextToken;
174
+ }
175
+ set includeSeoUrls(e) {
164
176
  this.options.includeSeoUrls = e;
165
177
  }
166
- setReflectContextToken(e = !0) {
178
+ get includeSeoUrls() {
179
+ return this.options.includeSeoUrls;
180
+ }
181
+ set reflectContextToken(e) {
167
182
  this.options.reflectContextToken = e;
168
183
  }
184
+ get reflectContextToken() {
185
+ return this.options.reflectContextToken;
186
+ }
169
187
  parseOperation(e) {
170
- const t = E.exec(e);
188
+ const t = b.exec(e);
171
189
  if (!(t != null && t.groups))
172
190
  throw new Error("Invalid operation format");
173
191
  return {
@@ -177,15 +195,17 @@ class k extends m {
177
195
  };
178
196
  }
179
197
  interpolateUrl(e, t) {
180
- return e.replace(/{([^}]+?)}/g, (s, n) => {
181
- if (!(n in t))
182
- throw new Error(`Missing required URL parameter: ${n}`);
183
- return t[n];
198
+ return e.replace(/{([^}]+?)}/g, (n, s) => {
199
+ if (!(s in t))
200
+ throw new Error(`Missing required URL parameter: ${s}`);
201
+ return t[s];
184
202
  });
185
203
  }
186
204
  async queryRaw(e, t) {
205
+ let n;
187
206
  try {
188
- const { method: s, url: n } = this.parseOperation(e), a = this.interpolateUrl(n, (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}`, {
189
209
  ...t,
190
210
  method: s,
191
211
  headers: {
@@ -198,27 +218,28 @@ class k extends m {
198
218
  },
199
219
  body: t != null && t.body ? JSON.stringify(t.body) : void 0
200
220
  });
201
- if (!c.ok) {
202
- const l = new p(
203
- `Shopware API request failed: ${c.statusText}`,
204
- c.status,
205
- await c.json().catch(() => {
206
- })
207
- );
208
- throw this.emit("error", l), l;
209
- }
210
- if (this.options.reflectContextToken) {
211
- const l = c.headers.get("sw-context-token");
212
- l && this.setContextToken(l);
213
- }
214
- return c;
215
221
  } catch (s) {
216
- throw s instanceof p ? s : new p(
222
+ const a = new h(
217
223
  "Failed to communicate with Shopware API",
218
224
  500,
219
225
  s instanceof Error ? s.message : void 0
220
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);
221
241
  }
242
+ return n;
222
243
  }
223
244
  async query(e, t) {
224
245
  return (await this.queryRaw(e, t)).json().catch(() => {
@@ -226,6 +247,6 @@ class k extends m {
226
247
  }
227
248
  }
228
249
  export {
229
- p as ShopwareApiError,
250
+ h as ShopwareApiError,
230
251
  k as ShopwareClient
231
252
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-shopware-api-client",
3
- "version": "0.0.7",
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
@@ -17,7 +17,7 @@ export interface ResponseType<TKey> {
17
17
  }
18
18
 
19
19
  export type BrandedResponse<Operations, OperationKey extends (keyof Operations) & string> =
20
- ResponseType<OperationKey> & OperationProp<Operations, OperationKey, 'response'> & { __BLBL: string }
20
+ ResponseType<OperationKey> & OperationProp<Operations, OperationKey, 'response'> & { __brand: string }
21
21
 
22
22
  export class ShopwareClient<Operations> extends EventEmitter {
23
23
  private options: ShopwareClientOptions = {
@@ -32,15 +32,15 @@ export class ShopwareClient<Operations> extends EventEmitter {
32
32
  constructor(options: ShopwareClientOptions) {
33
33
  super()
34
34
 
35
- this.setBaseURL(options.baseURL ?? this.options.baseURL)
36
- this.setApiKey(options.apiKey ?? this.options.apiKey)
37
- this.setLanguage(options.language ?? this.options.language)
38
- this.setContextToken(options.contextToken ?? this.options.contextToken)
39
- this.setIncludeSeoUrls(options.includeSeoUrls ?? this.options.includeSeoUrls)
40
- this.setReflectContextToken(options.reflectContextToken ?? this.options.reflectContextToken)
35
+ this.baseURL = options.baseURL ?? this.options.baseURL
36
+ this.apiKey = options.apiKey ?? this.options.apiKey
37
+ this.language = options.language ?? this.options.language
38
+ this.contextToken = options.contextToken ?? this.options.contextToken
39
+ this.includeSeoUrls = options.includeSeoUrls ?? this.options.includeSeoUrls
40
+ this.reflectContextToken = options.reflectContextToken ?? this.options.reflectContextToken
41
41
  }
42
42
 
43
- public setBaseURL(baseURL: string) {
43
+ set baseURL(baseURL: string) {
44
44
  if (typeof baseURL !== 'string') {
45
45
  throw new Error('Invalid baseURL: It must be a string.')
46
46
  }
@@ -48,7 +48,11 @@ export class ShopwareClient<Operations> extends EventEmitter {
48
48
  this.options.baseURL = baseURL
49
49
  }
50
50
 
51
- public setApiKey(apiKey: string) {
51
+ get baseURL() {
52
+ return this.options.baseURL
53
+ }
54
+
55
+ set apiKey(apiKey: string) {
52
56
  if (typeof apiKey !== 'string') {
53
57
  throw new Error('Invalid apiKey: It must be a string.')
54
58
  }
@@ -56,7 +60,11 @@ export class ShopwareClient<Operations> extends EventEmitter {
56
60
  this.options.apiKey = apiKey
57
61
  }
58
62
 
59
- public setLanguage(language: string | undefined) {
63
+ get apiKey() {
64
+ return this.options.apiKey
65
+ }
66
+
67
+ set language(language: string | undefined) {
60
68
  if (language && typeof language !== 'string') {
61
69
  throw new Error('Invalid language: If provided, it must be a string.')
62
70
  }
@@ -64,7 +72,11 @@ export class ShopwareClient<Operations> extends EventEmitter {
64
72
  this.options.language = language
65
73
  }
66
74
 
67
- public setContextToken(contextToken: string | undefined) {
75
+ get language() {
76
+ return this.options.language
77
+ }
78
+
79
+ set contextToken(contextToken: string | undefined) {
68
80
  if (contextToken && typeof contextToken !== 'string') {
69
81
  throw new Error('Invalid contextToken: If provided, it must be a string.')
70
82
  }
@@ -73,14 +85,26 @@ export class ShopwareClient<Operations> extends EventEmitter {
73
85
  this.emit('contextToken', contextToken)
74
86
  }
75
87
 
76
- public setIncludeSeoUrls(includeSeoUrls: boolean = true) {
88
+ get contextToken() {
89
+ return this.options.contextToken
90
+ }
91
+
92
+ set includeSeoUrls(includeSeoUrls: boolean | undefined) {
77
93
  this.options.includeSeoUrls = includeSeoUrls
78
94
  }
79
95
 
80
- public setReflectContextToken(reflectContextToken: boolean = true) {
96
+ get includeSeoUrls() {
97
+ return this.options.includeSeoUrls
98
+ }
99
+
100
+ set reflectContextToken(reflectContextToken: boolean | undefined) {
81
101
  this.options.reflectContextToken = reflectContextToken
82
102
  }
83
103
 
104
+ get reflectContextToken() {
105
+ return this.options.reflectContextToken
106
+ }
107
+
84
108
  private parseOperation(operation: keyof Operations & string): {
85
109
  name: string
86
110
  method: string
@@ -110,6 +134,8 @@ export class ShopwareClient<Operations> extends EventEmitter {
110
134
  operation: OperationKey,
111
135
  options?: OperationOptions<Operations, OperationKey>,
112
136
  ): Promise<Response> {
137
+ let response: Response
138
+
113
139
  try {
114
140
  const { method, url } = this.parseOperation(operation)
115
141
 
@@ -117,7 +143,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
117
143
 
118
144
  const query = options?.query ? `?${createQueryParams(options.query)}` : ''
119
145
 
120
- const response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}${query}`, {
146
+ response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}${query}`, {
121
147
  ...options,
122
148
  method,
123
149
  headers: {
@@ -130,38 +156,38 @@ export class ShopwareClient<Operations> extends EventEmitter {
130
156
  },
131
157
  body: options?.body ? JSON.stringify(options.body) : undefined,
132
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
+ )
133
165
 
134
- if (!response.ok) {
135
- const error = new ShopwareApiError(
136
- `Shopware API request failed: ${response.statusText}`,
137
- response.status,
138
- await response.json().catch(() => undefined),
139
- )
166
+ this.emit('error', shopwareError)
140
167
 
141
- this.emit('error', error)
168
+ throw shopwareError
169
+ }
142
170
 
143
- throw error
144
- }
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
+ )
145
177
 
146
- if (this.options.reflectContextToken) {
147
- const contextToken = response.headers.get('sw-context-token')
148
- if (contextToken) {
149
- this.setContextToken(contextToken)
150
- }
151
- }
178
+ this.emit('error', error)
152
179
 
153
- return response
154
- } catch (error) {
155
- if (error instanceof ShopwareApiError) {
156
- throw error
157
- }
180
+ throw error
181
+ }
158
182
 
159
- throw new ShopwareApiError(
160
- 'Failed to communicate with Shopware API',
161
- 500,
162
- error instanceof Error ? error.message : undefined,
163
- )
183
+ if (this.options.reflectContextToken) {
184
+ const contextToken = response.headers.get('sw-context-token')
185
+ if (contextToken) {
186
+ this.contextToken = contextToken
187
+ }
164
188
  }
189
+
190
+ return response
165
191
  }
166
192
 
167
193
  async query<OperationKey extends (keyof Operations) & string>(