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

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,33 +1,33 @@
1
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()
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);
4
+ import { z as i } from "zod";
5
+ const y = 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
+ }), w = i.object({
22
+ errors: i.array(y)
23
23
  });
24
24
  class p extends Error {
25
- constructor(e, t, s) {
25
+ constructor(e, t, n) {
26
26
  super(e);
27
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 : [];
28
+ this.status = t, this.response = n, this.name = "ShopwareApiError";
29
+ const { success: s, data: a } = w.safeParse(n);
30
+ this.errors = s ? a.errors : [];
31
31
  }
32
32
  }
33
33
  class m {
@@ -36,38 +36,38 @@ class m {
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((h) => h.listener === e && this.optionsMatch(h.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,24 +110,24 @@ 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 f = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
114
+ function b(o) {
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)));
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 (!f(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;
130
- class k extends m {
129
+ const x = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
130
+ class v extends m {
131
131
  constructor(e) {
132
132
  super();
133
133
  u(this, "options", {
@@ -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 = x.exec(e);
171
189
  if (!(t != null && t.groups))
172
190
  throw new Error("Invalid operation format");
173
191
  return {
@@ -177,17 +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) {
187
205
  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}`, {
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}`, {
189
207
  ...t,
190
- method: s,
208
+ method: n,
191
209
  headers: {
192
210
  "Content-Type": "application/json",
193
211
  "sw-access-key": this.options.apiKey,
@@ -209,14 +227,14 @@ class k extends m {
209
227
  }
210
228
  if (this.options.reflectContextToken) {
211
229
  const l = c.headers.get("sw-context-token");
212
- l && this.setContextToken(l);
230
+ l && (this.contextToken = l);
213
231
  }
214
232
  return c;
215
- } catch (s) {
216
- throw s instanceof p ? s : new p(
233
+ } catch (n) {
234
+ throw n instanceof p ? n : new p(
217
235
  "Failed to communicate with Shopware API",
218
236
  500,
219
- s instanceof Error ? s.message : void 0
237
+ n instanceof Error ? n.message : void 0
220
238
  );
221
239
  }
222
240
  }
@@ -227,5 +245,5 @@ class k extends m {
227
245
  }
228
246
  export {
229
247
  p as ShopwareApiError,
230
- k as ShopwareClient
248
+ v as ShopwareClient
231
249
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-shopware-api-client",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
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
@@ -146,7 +170,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
146
170
  if (this.options.reflectContextToken) {
147
171
  const contextToken = response.headers.get('sw-context-token')
148
172
  if (contextToken) {
149
- this.setContextToken(contextToken)
173
+ this.contextToken = contextToken
150
174
  }
151
175
  }
152
176