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

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);
25
+ get headers(): HeadersInit;
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,8 +1,9 @@
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);
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);
4
5
  import { z as i } from "zod";
5
- const g = i.object({
6
+ const k = i.object({
6
7
  status: i.string(),
7
8
  code: i.string(),
8
9
  title: i.string().optional(),
@@ -18,121 +19,125 @@ const g = i.object({
18
19
  function: i.string()
19
20
  })
20
21
  ).optional()
21
- }), y = i.object({
22
- errors: i.array(g)
23
- });
24
- class h extends Error {
25
- constructor(e, t, n) {
22
+ }), U = i.object({
23
+ errors: i.array(k)
24
+ }), p = class p extends Error {
25
+ constructor(e, t, r) {
26
26
  super(e);
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 : [];
27
+ h(this, "errors");
28
+ this.status = t, this.response = r, this.name = "ShopwareApiError";
29
+ const { success: n, data: a } = U.safeParse(r);
30
+ this.errors = n ? a.errors : [];
31
31
  }
32
- }
33
- class m {
32
+ };
33
+ c(p, "ShopwareApiError");
34
+ let u = p;
35
+ const f = class f {
34
36
  constructor() {
35
- l(this, "listeners", /* @__PURE__ */ new Map());
37
+ h(this, "listeners", /* @__PURE__ */ new Map());
36
38
  }
37
- addEventListener(r, e, t) {
39
+ addEventListener(s, e, t) {
38
40
  if (!e) return;
39
- const n = this.normalizeOptions(t), s = {
41
+ const r = this.normalizeOptions(t), n = {
40
42
  listener: e,
41
- options: n
43
+ options: r
42
44
  };
43
- if (n.signal) {
44
- if (n.signal.aborted) return;
45
- s.signal = n.signal, n.signal.addEventListener("abort", () => {
46
- this.removeEventListener(r, e, t);
45
+ if (r.signal) {
46
+ if (r.signal.aborted) return;
47
+ n.signal = r.signal, r.signal.addEventListener("abort", () => {
48
+ this.removeEventListener(s, e, t);
47
49
  }, { once: !0 });
48
50
  }
49
- this.listeners.has(r) || this.listeners.set(r, []), this.listeners.get(r).push(s);
51
+ this.listeners.has(s) || this.listeners.set(s, []), this.listeners.get(s).push(n);
50
52
  }
51
- removeEventListener(r, e, t) {
53
+ removeEventListener(s, e, t) {
52
54
  if (!e) return;
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
- }
58
- dispatchEvent(r) {
59
- var n;
60
- const e = this.listeners.get(r.type);
55
+ const r = this.listeners.get(s);
56
+ if (!r) return;
57
+ const n = this.normalizeOptions(t), a = r.findIndex((l) => l.listener === e && this.optionsMatch(l.options, n));
58
+ a !== -1 && (r.splice(a, 1), r.length === 0 && this.listeners.delete(s));
59
+ }
60
+ dispatchEvent(s) {
61
+ var r;
62
+ const e = this.listeners.get(s.type);
61
63
  if (!e || e.length === 0) return !0;
62
64
  const t = [...e];
63
- for (const s of t)
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
+ for (const n of t)
66
+ if (!((r = n.signal) != null && r.aborted) && (typeof n.listener == "function" ? n.listener.call(this, s) : n.listener.handleEvent(s), n.options.once && this.removeEventListener(s.type, n.listener, n.options), s.cancelBubble))
65
67
  break;
66
- return !r.defaultPrevented;
68
+ return !s.defaultPrevented;
67
69
  }
68
- normalizeOptions(r) {
69
- return typeof r == "boolean" ? { capture: r } : {
70
+ normalizeOptions(s) {
71
+ return typeof s == "boolean" ? { capture: s } : {
70
72
  capture: !1,
71
73
  once: !1,
72
74
  passive: !1,
73
- ...r
75
+ ...s
74
76
  };
75
77
  }
76
- optionsMatch(r, e) {
77
- return r.capture === e.capture;
78
+ optionsMatch(s, e) {
79
+ return s.capture === e.capture;
78
80
  }
79
81
  // Utility methods for easier usage
80
- on(r, e, t) {
81
- this.addEventListener(r, e, t);
82
+ on(s, e, t) {
83
+ this.addEventListener(s, e, t);
82
84
  }
83
- off(r, e, t) {
84
- this.removeEventListener(r, e, t);
85
+ off(s, e, t) {
86
+ this.removeEventListener(s, e, t);
85
87
  }
86
- once(r, e, t) {
87
- const n = typeof t == "boolean" ? {
88
+ once(s, e, t) {
89
+ const r = typeof t == "boolean" ? {
88
90
  capture: t,
89
91
  once: !0
90
92
  } : {
91
93
  ...t,
92
94
  once: !0
93
95
  };
94
- this.addEventListener(r, e, n);
96
+ this.addEventListener(s, e, r);
95
97
  }
96
- emit(r, e) {
97
- const t = new CustomEvent(r, { detail: e });
98
+ emit(s, e) {
99
+ const t = new CustomEvent(s, { detail: e });
98
100
  return this.dispatchEvent(t);
99
101
  }
100
102
  getEventTypes() {
101
103
  return Array.from(this.listeners.keys());
102
104
  }
103
- removeAllListeners(r) {
104
- r ? this.listeners.delete(r) : this.listeners.clear();
105
+ removeAllListeners(s) {
106
+ s ? this.listeners.delete(s) : this.listeners.clear();
105
107
  }
106
- }
107
- const u = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
108
- function w(o) {
109
- const r = new URLSearchParams(), e = (t, n) => {
110
- u(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
111
- };
112
- for (const [t, n] of Object.entries(o))
113
- if (!u(n))
114
- if (typeof n == "string")
115
- r.append(t, String(n));
116
- else if (Array.isArray(n))
117
- for (const s of n)
118
- e(`${t}[]`, s);
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) => {
114
+ m(r) || (typeof r == "object" && s.append(t, JSON.stringify(r)), s.append(t, String(r)));
115
+ }, "appendNested");
116
+ for (const [t, r] of Object.entries(o))
117
+ if (!m(r))
118
+ if (typeof r == "string")
119
+ s.append(t, String(r));
120
+ else if (Array.isArray(r))
121
+ for (const n of r)
122
+ e(`${t}[]`, n);
119
123
  else
120
- e(t, n);
121
- return r;
124
+ e(t, r);
125
+ return s;
122
126
  }
123
- const b = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
124
- class k extends m {
127
+ c(T, "createQueryParams");
128
+ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends d {
125
129
  constructor(e) {
126
130
  super();
127
- l(this, "options", {
131
+ h(this, "options", {
128
132
  baseURL: "",
129
133
  apiKey: "",
130
134
  language: "",
131
135
  contextToken: "",
132
136
  includeSeoUrls: !1,
133
- reflectContextToken: !0
137
+ reflectContextToken: !0,
138
+ headers: {}
134
139
  });
135
- 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;
136
141
  }
137
142
  set baseURL(e) {
138
143
  if (typeof e != "string")
@@ -178,8 +183,14 @@ class k extends m {
178
183
  get reflectContextToken() {
179
184
  return this.options.reflectContextToken;
180
185
  }
186
+ set headers(e) {
187
+ this.options.headers = e;
188
+ }
189
+ get headers() {
190
+ return this.options.headers;
191
+ }
181
192
  parseOperation(e) {
182
- const t = b.exec(e);
193
+ const t = v.exec(e);
183
194
  if (!(t != null && t.groups))
184
195
  throw new Error("Invalid operation format");
185
196
  return {
@@ -189,58 +200,61 @@ class k extends m {
189
200
  };
190
201
  }
191
202
  interpolateUrl(e, t) {
192
- return e.replace(/{([^}]+?)}/g, (n, s) => {
193
- if (!(s in t))
194
- throw new Error(`Missing required URL parameter: ${s}`);
195
- return t[s];
203
+ return e.replace(/{([^}]+?)}/g, (r, n) => {
204
+ if (!(n in t))
205
+ throw new Error(`Missing required URL parameter: ${n}`);
206
+ return t[n];
196
207
  });
197
208
  }
198
209
  async queryRaw(e, t) {
199
- let n;
210
+ let r;
200
211
  try {
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}`, {
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}`, {
203
214
  ...t,
204
- method: s,
215
+ method: n,
205
216
  headers: {
206
217
  "Content-Type": "application/json",
207
- "sw-access-key": this.options.apiKey,
208
- ...this.options.contextToken && { "sw-context-token": this.options.contextToken },
209
- ...this.options.includeSeoUrls && { "sw-include-seo-urls": "true" },
210
- ...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,
211
223
  ...t == null ? void 0 : t.headers
212
224
  },
213
225
  body: t != null && t.body ? JSON.stringify(t.body) : void 0
214
226
  });
215
- } catch (s) {
216
- const a = new h(
227
+ } catch (n) {
228
+ const a = new u(
217
229
  "Failed to communicate with Shopware API",
218
230
  500,
219
- s instanceof Error ? s.message : void 0
231
+ n instanceof Error ? n.message : void 0
220
232
  );
221
233
  throw this.emit("error", a), a;
222
234
  }
223
- if (!n.ok) {
224
- const s = new h(
225
- `Shopware API request failed: ${n.statusText}`,
226
- n.status,
227
- await n.json().catch(() => {
235
+ if (!r.ok) {
236
+ const n = new u(
237
+ `Shopware API request failed: ${r.statusText}`,
238
+ r.status,
239
+ await r.json().catch(() => {
228
240
  })
229
241
  );
230
- throw this.emit("error", s), s;
242
+ throw this.emit("error", n), n;
231
243
  }
232
244
  if (this.options.reflectContextToken) {
233
- const s = n.headers.get("sw-context-token");
234
- s && (this.contextToken = s);
245
+ const n = r.headers.get("sw-context-token");
246
+ n && (this.contextToken = n);
235
247
  }
236
- return n;
248
+ return r;
237
249
  }
238
250
  async query(e, t) {
239
251
  return (await this.queryRaw(e, t)).json().catch(() => {
240
252
  });
241
253
  }
242
- }
254
+ };
255
+ c(g, "ShopwareClient");
256
+ let w = g;
243
257
  export {
244
- h as ShopwareApiError,
245
- k as ShopwareClient
258
+ u as ShopwareApiError,
259
+ w as ShopwareClient
246
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.10",
3
+ "version": "0.0.12",
4
4
  "description": "A client for the Shopware API",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "dist/index.mjs",
@@ -11,9 +11,6 @@
11
11
  "import": "./dist/index.mjs"
12
12
  }
13
13
  },
14
- "peerDependencies": {
15
- "vue": "^3.0.0"
16
- },
17
14
  "repository": {
18
15
  "type": "git",
19
16
  "url": "git+ssh://git@github.com:teamnovu/kit.git"
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) {
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
  }
package/vite.config.js CHANGED
@@ -1,12 +1,14 @@
1
- import vue from '@vitejs/plugin-vue'
2
1
  import { resolve } from 'path'
3
2
  import { defineConfig } from 'vite'
4
3
  import dts from 'vite-plugin-dts'
5
4
  import pkg from './package.json'
6
5
 
7
6
  export default defineConfig({
8
- plugins: [vue(), dts()],
7
+ plugins: [dts()],
9
8
  root: resolve(__dirname),
9
+ esbuild: {
10
+ keepNames: true,
11
+ },
10
12
  build: {
11
13
  emptyOutDir: false,
12
14
  lib: {
@@ -20,13 +22,6 @@ export default defineConfig({
20
22
  // make sure to externalize deps that shouldn't be bundled
21
23
  // into your library
22
24
  external: ['vue', ...Object.keys(pkg.dependencies ?? {})],
23
- output: {
24
- // Provide global variables to use in the UMD build
25
- // for externalized deps
26
- globals: {
27
- vue: 'Vue',
28
- },
29
- },
30
25
  },
31
26
  },
32
27
  })