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

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
@@ -21,6 +21,8 @@ export declare class ShopwareClient<Operations> extends EventEmitter {
21
21
  get includeSeoUrls(): boolean | undefined;
22
22
  set reflectContextToken(reflectContextToken: boolean | undefined);
23
23
  get reflectContextToken(): boolean | undefined;
24
+ set headers(headers: HeadersInit | undefined);
25
+ get headers(): HeadersInit | undefined;
24
26
  private parseOperation;
25
27
  private interpolateUrl;
26
28
  queryRaw<OperationKey extends (keyof Operations) & string>(operation: OperationKey, options?: OperationOptions<Operations, OperationKey>): Promise<Response>;
package/dist/index.mjs CHANGED
@@ -1,40 +1,40 @@
1
1
  var y = Object.defineProperty;
2
- var x = (i, s, e) => s in i ? y(i, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[s] = e;
3
- var c = (i, s) => y(i, "name", { value: s, configurable: !0 });
4
- var u = (i, s, e) => x(i, typeof s != "symbol" ? s + "" : s, e);
5
- import { z as o } from "zod";
6
- const k = o.object({
7
- status: o.string(),
8
- code: o.string(),
9
- title: o.string().optional(),
10
- detail: o.string().optional(),
11
- meta: o.record(o.unknown()).optional(),
12
- source: o.object({
13
- pointer: o.string().optional()
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);
5
+ import { z as i } from "zod";
6
+ const k = i.object({
7
+ status: i.string(),
8
+ code: i.string(),
9
+ title: i.string().optional(),
10
+ detail: i.string().optional(),
11
+ meta: i.record(i.unknown()).optional(),
12
+ source: i.object({
13
+ pointer: i.string().optional()
14
14
  }).optional(),
15
- trace: o.array(
16
- o.object({
17
- file: o.string(),
18
- line: o.number(),
19
- function: o.string()
15
+ trace: i.array(
16
+ i.object({
17
+ file: i.string(),
18
+ line: i.number(),
19
+ function: i.string()
20
20
  })
21
21
  ).optional()
22
- }), U = o.object({
23
- errors: o.array(k)
24
- }), f = class f extends Error {
22
+ }), U = i.object({
23
+ errors: i.array(k)
24
+ }), p = class p extends Error {
25
25
  constructor(e, t, r) {
26
26
  super(e);
27
- u(this, "errors");
27
+ h(this, "errors");
28
28
  this.status = t, this.response = r, this.name = "ShopwareApiError";
29
29
  const { success: n, data: a } = U.safeParse(r);
30
30
  this.errors = n ? a.errors : [];
31
31
  }
32
32
  };
33
- c(f, "ShopwareApiError");
34
- let h = f;
35
- const d = class d {
33
+ c(p, "ShopwareApiError");
34
+ let u = p;
35
+ const f = class f {
36
36
  constructor() {
37
- u(this, "listeners", /* @__PURE__ */ new Map());
37
+ h(this, "listeners", /* @__PURE__ */ new Map());
38
38
  }
39
39
  addEventListener(s, e, t) {
40
40
  if (!e) return;
@@ -106,14 +106,14 @@ const d = class d {
106
106
  s ? this.listeners.delete(s) : this.listeners.clear();
107
107
  }
108
108
  };
109
- c(d, "EventEmitter");
110
- let p = d;
111
- const m = /* @__PURE__ */ c((i) => i == null || typeof i == "number" && isNaN(i) || typeof i == "function", "isIgnored");
112
- function T(i) {
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
113
  const s = new URLSearchParams(), e = /* @__PURE__ */ c((t, r) => {
114
114
  m(r) || (typeof r == "object" && s.append(t, JSON.stringify(r)), s.append(t, String(r)));
115
115
  }, "appendNested");
116
- for (const [t, r] of Object.entries(i))
116
+ for (const [t, r] of Object.entries(o))
117
117
  if (!m(r))
118
118
  if (typeof r == "string")
119
119
  s.append(t, String(r));
@@ -125,18 +125,19 @@ function T(i) {
125
125
  return s;
126
126
  }
127
127
  c(T, "createQueryParams");
128
- const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends p {
128
+ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends d {
129
129
  constructor(e) {
130
130
  super();
131
- u(this, "options", {
131
+ h(this, "options", {
132
132
  baseURL: "",
133
133
  apiKey: "",
134
134
  language: "",
135
135
  contextToken: "",
136
136
  includeSeoUrls: !1,
137
- reflectContextToken: !0
137
+ reflectContextToken: !0,
138
+ headers: {}
138
139
  });
139
- 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;
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
141
  }
141
142
  set baseURL(e) {
142
143
  if (typeof e != "string")
@@ -182,6 +183,12 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
182
183
  get reflectContextToken() {
183
184
  return this.options.reflectContextToken;
184
185
  }
186
+ set headers(e) {
187
+ this.options.headers = e;
188
+ }
189
+ get headers() {
190
+ return this.options.headers;
191
+ }
185
192
  parseOperation(e) {
186
193
  const t = v.exec(e);
187
194
  if (!(t != null && t.groups))
@@ -203,21 +210,22 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
203
210
  let r;
204
211
  try {
205
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)}` : "";
206
- r = await fetch(`${this.options.baseURL}/store-api${l}${b}`, {
213
+ r = await fetch(`${this.baseURL}/store-api${l}${b}`, {
207
214
  ...t,
208
215
  method: n,
209
216
  headers: {
210
217
  "Content-Type": "application/json",
211
- "sw-access-key": this.options.apiKey,
212
- ...this.options.contextToken && { "sw-context-token": this.options.contextToken },
213
- ...this.options.includeSeoUrls && { "sw-include-seo-urls": "true" },
214
- ...this.options.language && { "sw-language-id": this.options.language },
218
+ "sw-access-key": this.apiKey,
219
+ ...this.contextToken && { "sw-context-token": this.contextToken },
220
+ ...this.includeSeoUrls && { "sw-include-seo-urls": "true" },
221
+ ...this.language && { "sw-language-id": this.language },
222
+ ...this.headers,
215
223
  ...t == null ? void 0 : t.headers
216
224
  },
217
225
  body: t != null && t.body ? JSON.stringify(t.body) : void 0
218
226
  });
219
227
  } catch (n) {
220
- const a = new h(
228
+ const a = new u(
221
229
  "Failed to communicate with Shopware API",
222
230
  500,
223
231
  n instanceof Error ? n.message : void 0
@@ -225,7 +233,7 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
225
233
  throw this.emit("error", a), a;
226
234
  }
227
235
  if (!r.ok) {
228
- const n = new h(
236
+ const n = new u(
229
237
  `Shopware API request failed: ${r.statusText}`,
230
238
  r.status,
231
239
  await r.json().catch(() => {
@@ -247,6 +255,6 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
247
255
  c(g, "ShopwareClient");
248
256
  let w = g;
249
257
  export {
250
- h as ShopwareApiError,
258
+ u as ShopwareApiError,
251
259
  w as ShopwareClient
252
260
  };
package/dist/types.d.ts CHANGED
@@ -24,6 +24,7 @@ export interface ShopwareClientOptions {
24
24
  apiKey: string;
25
25
  includeSeoUrls?: boolean;
26
26
  language?: string;
27
+ headers?: HeadersInit;
27
28
  /**
28
29
  * If true, the context token will be reflected in the response headers.
29
30
  * @default true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-shopware-api-client",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
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
@@ -27,6 +27,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
27
27
  contextToken: '',
28
28
  includeSeoUrls: false,
29
29
  reflectContextToken: true,
30
+ headers: {},
30
31
  }
31
32
 
32
33
  constructor(options: ShopwareClientOptions) {
@@ -38,6 +39,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
38
39
  this.contextToken = options.contextToken ?? this.options.contextToken
39
40
  this.includeSeoUrls = options.includeSeoUrls ?? this.options.includeSeoUrls
40
41
  this.reflectContextToken = options.reflectContextToken ?? this.options.reflectContextToken
42
+ this.headers = options.headers ?? this.options.headers
41
43
  }
42
44
 
43
45
  set baseURL(baseURL: string) {
@@ -105,6 +107,14 @@ export class ShopwareClient<Operations> extends EventEmitter {
105
107
  return this.options.reflectContextToken
106
108
  }
107
109
 
110
+ set headers(headers: HeadersInit | undefined) {
111
+ this.options.headers = headers
112
+ }
113
+
114
+ get headers() {
115
+ return this.options.headers
116
+ }
117
+
108
118
  private parseOperation(operation: keyof Operations & string): {
109
119
  name: string
110
120
  method: string
@@ -143,15 +153,16 @@ export class ShopwareClient<Operations> extends EventEmitter {
143
153
 
144
154
  const query = options?.query ? `?${createQueryParams(options.query)}` : ''
145
155
 
146
- response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}${query}`, {
156
+ response = await fetch(`${this.baseURL}/store-api${interpolatedUrl}${query}`, {
147
157
  ...options,
148
158
  method,
149
159
  headers: {
150
160
  'Content-Type': 'application/json',
151
- 'sw-access-key': this.options.apiKey,
152
- ...(this.options.contextToken && { 'sw-context-token': this.options.contextToken }),
153
- ...(this.options.includeSeoUrls && { 'sw-include-seo-urls': 'true' }),
154
- ...(this.options.language && { 'sw-language-id': this.options.language }),
161
+ 'sw-access-key': this.apiKey,
162
+ ...(this.contextToken && { 'sw-context-token': this.contextToken }),
163
+ ...(this.includeSeoUrls && { 'sw-include-seo-urls': 'true' }),
164
+ ...(this.language && { 'sw-language-id': this.language }),
165
+ ...this.headers,
155
166
  ...options?.headers,
156
167
  },
157
168
  body: options?.body ? JSON.stringify(options.body) : undefined,
package/src/types.ts CHANGED
@@ -42,6 +42,7 @@ export interface ShopwareClientOptions {
42
42
  apiKey: string
43
43
  includeSeoUrls?: boolean
44
44
  language?: string
45
+ headers?: HeadersInit
45
46
 
46
47
  /**
47
48
  * If true, the context token will be reflected in the response headers.
@@ -56,4 +57,5 @@ export interface ShopwareClientOptions {
56
57
  * @default undefined
57
58
  */
58
59
  contextToken?: string
60
+
59
61
  }