@teamnovu/kit-shopware-api-client 0.0.13 → 0.0.14

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
@@ -9,6 +9,8 @@ export type BrandedResponse<Operations, OperationKey extends (keyof Operations)
9
9
  export declare class ShopwareClient<Operations> extends EventEmitter {
10
10
  private options;
11
11
  constructor(options: ShopwareClientOptions);
12
+ set requestInitDefaults(defaults: RequestInit | undefined);
13
+ get requestInitDefaults(): RequestInit | undefined;
12
14
  set baseURL(baseURL: string);
13
15
  get baseURL(): string;
14
16
  set apiKey(apiKey: string);
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  var y = Object.defineProperty;
2
- var x = (o, s, e) => s in o ? y(o, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[s] = e;
3
- var c = (o, s) => y(o, "name", { value: s, configurable: !0 });
4
- var h = (o, s, e) => x(o, typeof s != "symbol" ? s + "" : s, e);
2
+ var k = (o, s, e) => s in o ? y(o, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[s] = e;
3
+ var l = (o, s) => y(o, "name", { value: s, configurable: !0 });
4
+ var h = (o, s, e) => k(o, typeof s != "symbol" ? s + "" : s, e);
5
5
  import { z as i } from "zod";
6
- const k = i.object({
6
+ const U = i.object({
7
7
  status: i.string(),
8
8
  code: i.string(),
9
9
  title: i.string().optional(),
@@ -19,20 +19,20 @@ const k = i.object({
19
19
  function: i.string()
20
20
  })
21
21
  ).optional()
22
- }), U = i.object({
23
- errors: i.array(k)
24
- }), p = class p extends Error {
22
+ }), T = i.object({
23
+ errors: i.array(U)
24
+ }), d = class d extends Error {
25
25
  constructor(e, t, r) {
26
26
  super(e);
27
27
  h(this, "errors");
28
28
  this.status = t, this.response = r, this.name = "ShopwareApiError";
29
- const { success: n, data: a } = U.safeParse(r);
29
+ const { success: n, data: a } = T.safeParse(r);
30
30
  this.errors = n ? a.errors : [];
31
31
  }
32
32
  };
33
- c(p, "ShopwareApiError");
34
- let u = p;
35
- const f = class f {
33
+ l(d, "ShopwareApiError");
34
+ let u = d;
35
+ const p = class p {
36
36
  constructor() {
37
37
  h(this, "listeners", /* @__PURE__ */ new Map());
38
38
  }
@@ -54,7 +54,7 @@ const f = class f {
54
54
  if (!e) return;
55
55
  const r = this.listeners.get(s);
56
56
  if (!r) return;
57
- const n = this.normalizeOptions(t), a = r.findIndex((l) => l.listener === e && this.optionsMatch(l.options, n));
57
+ const n = this.normalizeOptions(t), a = r.findIndex((c) => c.listener === e && this.optionsMatch(c.options, n));
58
58
  a !== -1 && (r.splice(a, 1), r.length === 0 && this.listeners.delete(s));
59
59
  }
60
60
  dispatchEvent(s) {
@@ -106,11 +106,11 @@ const f = class f {
106
106
  s ? this.listeners.delete(s) : this.listeners.clear();
107
107
  }
108
108
  };
109
- c(f, "EventEmitter");
110
- let d = f;
111
- const m = /* @__PURE__ */ c((o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function", "isIgnored");
112
- function T(o) {
113
- const s = new URLSearchParams(), e = /* @__PURE__ */ c((t, r) => {
109
+ l(p, "EventEmitter");
110
+ let f = p;
111
+ const m = /* @__PURE__ */ l((o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function", "isIgnored");
112
+ function I(o) {
113
+ const s = new URLSearchParams(), e = /* @__PURE__ */ l((t, r) => {
114
114
  m(r) || (typeof r == "object" && s.append(t, JSON.stringify(r)), s.append(t, String(r)));
115
115
  }, "appendNested");
116
116
  for (const [t, r] of Object.entries(o))
@@ -124,8 +124,8 @@ function T(o) {
124
124
  e(t, r);
125
125
  return s;
126
126
  }
127
- c(T, "createQueryParams");
128
- const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends d {
127
+ l(I, "createQueryParams");
128
+ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends f {
129
129
  constructor(e) {
130
130
  super();
131
131
  h(this, "options", {
@@ -137,7 +137,13 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
137
137
  reflectContextToken: !0,
138
138
  headers: {}
139
139
  });
140
- 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, this.headers = e.headers ?? this.options.headers;
140
+ this.requestInitDefaults = e.requestInitDefaults, 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, this.headers = e.headers ?? this.options.headers;
141
+ }
142
+ set requestInitDefaults(e) {
143
+ this.options.requestInitDefaults = e;
144
+ }
145
+ get requestInitDefaults() {
146
+ return this.options.requestInitDefaults;
141
147
  }
142
148
  set baseURL(e) {
143
149
  if (typeof e != "string")
@@ -207,15 +213,18 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
207
213
  });
208
214
  }
209
215
  async queryRaw(e, t) {
216
+ var n;
210
217
  let r;
211
218
  try {
212
- const { method: n, url: a } = this.parseOperation(e), l = this.interpolateUrl(a, (t == null ? void 0 : t.params) ?? {}), b = t != null && t.query ? `?${T(t.query)}` : "";
213
- r = await fetch(`${this.baseURL}/store-api${l}${b}`, {
219
+ const { method: a, url: c } = this.parseOperation(e), b = this.interpolateUrl(c, (t == null ? void 0 : t.params) ?? {}), x = t != null && t.query ? `?${I(t.query)}` : "";
220
+ r = await fetch(`${this.baseURL}/store-api${b}${x}`, {
221
+ ...this.requestInitDefaults,
214
222
  ...t,
215
- method: n,
223
+ method: a,
216
224
  headers: {
217
225
  "Content-Type": "application/json",
218
226
  "sw-access-key": this.apiKey,
227
+ ...(n = this.requestInitDefaults) == null ? void 0 : n.headers,
219
228
  ...this.contextToken && { "sw-context-token": this.contextToken },
220
229
  ...this.includeSeoUrls && { "sw-include-seo-urls": "true" },
221
230
  ...this.language && { "sw-language-id": this.language },
@@ -224,26 +233,26 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
224
233
  },
225
234
  body: t != null && t.body ? JSON.stringify(t.body) : void 0
226
235
  });
227
- } catch (n) {
228
- const a = new u(
236
+ } catch (a) {
237
+ const c = new u(
229
238
  "Failed to communicate with Shopware API",
230
239
  500,
231
- n instanceof Error ? n.message : void 0
240
+ a instanceof Error ? a.message : void 0
232
241
  );
233
- throw this.emit("error", a), a;
242
+ throw this.emit("error", c), c;
234
243
  }
235
244
  if (!r.ok) {
236
- const n = new u(
245
+ const a = new u(
237
246
  `Shopware API request failed: ${r.statusText}`,
238
247
  r.status,
239
248
  await r.json().catch(() => {
240
249
  })
241
250
  );
242
- throw this.emit("error", n), n;
251
+ throw this.emit("error", a), a;
243
252
  }
244
253
  if (this.options.reflectContextToken) {
245
- const n = r.headers.get("sw-context-token");
246
- n && (this.contextToken = n);
254
+ const a = r.headers.get("sw-context-token");
255
+ a && (this.contextToken = a);
247
256
  }
248
257
  return r;
249
258
  }
@@ -252,7 +261,7 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
252
261
  });
253
262
  }
254
263
  };
255
- c(g, "ShopwareClient");
264
+ l(g, "ShopwareClient");
256
265
  let w = g;
257
266
  export {
258
267
  u as ShopwareApiError,
package/dist/types.d.ts CHANGED
@@ -25,6 +25,7 @@ export interface ShopwareClientOptions {
25
25
  includeSeoUrls?: boolean;
26
26
  language?: string;
27
27
  headers?: HeadersInit;
28
+ requestInitDefaults?: RequestInit;
28
29
  /**
29
30
  * If true, the context token will be reflected in the response headers.
30
31
  * @default true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-shopware-api-client",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
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
@@ -33,6 +33,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
33
33
  constructor(options: ShopwareClientOptions) {
34
34
  super()
35
35
 
36
+ this.requestInitDefaults = options.requestInitDefaults
36
37
  this.baseURL = options.baseURL ?? this.options.baseURL
37
38
  this.apiKey = options.apiKey ?? this.options.apiKey
38
39
  this.language = options.language ?? this.options.language
@@ -42,6 +43,14 @@ export class ShopwareClient<Operations> extends EventEmitter {
42
43
  this.headers = options.headers ?? this.options.headers
43
44
  }
44
45
 
46
+ set requestInitDefaults(defaults: RequestInit | undefined) {
47
+ this.options.requestInitDefaults = defaults
48
+ }
49
+
50
+ get requestInitDefaults() {
51
+ return this.options.requestInitDefaults
52
+ }
53
+
45
54
  set baseURL(baseURL: string) {
46
55
  if (typeof baseURL !== 'string') {
47
56
  throw new Error('Invalid baseURL: It must be a string.')
@@ -154,11 +163,13 @@ export class ShopwareClient<Operations> extends EventEmitter {
154
163
  const query = options?.query ? `?${createQueryParams(options.query)}` : ''
155
164
 
156
165
  response = await fetch(`${this.baseURL}/store-api${interpolatedUrl}${query}`, {
166
+ ...this.requestInitDefaults,
157
167
  ...options,
158
168
  method,
159
169
  headers: {
160
170
  'Content-Type': 'application/json',
161
171
  'sw-access-key': this.apiKey,
172
+ ...this.requestInitDefaults?.headers,
162
173
  ...(this.contextToken && { 'sw-context-token': this.contextToken }),
163
174
  ...(this.includeSeoUrls && { 'sw-include-seo-urls': 'true' }),
164
175
  ...(this.language && { 'sw-language-id': this.language }),
package/src/types.ts CHANGED
@@ -43,6 +43,7 @@ export interface ShopwareClientOptions {
43
43
  includeSeoUrls?: boolean
44
44
  language?: string
45
45
  headers?: HeadersInit
46
+ requestInitDefaults?: RequestInit
46
47
 
47
48
  /**
48
49
  * If true, the context token will be reflected in the response headers.