@teamnovu/kit-shopware-api-client 0.0.9 → 0.0.11
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/index.mjs +113 -113
- package/package.json +1 -4
- package/src/eventEmitter.ts +6 -9
- package/vite.config.js +4 -9
package/dist/index.mjs
CHANGED
|
@@ -1,136 +1,134 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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()
|
|
13
14
|
}).optional(),
|
|
14
|
-
trace:
|
|
15
|
-
|
|
16
|
-
file:
|
|
17
|
-
line:
|
|
18
|
-
function:
|
|
15
|
+
trace: o.array(
|
|
16
|
+
o.object({
|
|
17
|
+
file: o.string(),
|
|
18
|
+
line: o.number(),
|
|
19
|
+
function: o.string()
|
|
19
20
|
})
|
|
20
21
|
).optional()
|
|
21
|
-
}),
|
|
22
|
-
errors:
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
constructor(e, t, n) {
|
|
22
|
+
}), U = o.object({
|
|
23
|
+
errors: o.array(k)
|
|
24
|
+
}), f = class f extends Error {
|
|
25
|
+
constructor(e, t, r) {
|
|
26
26
|
super(e);
|
|
27
|
-
|
|
28
|
-
this.status = t, this.response =
|
|
29
|
-
const { success:
|
|
30
|
-
this.errors =
|
|
27
|
+
u(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
|
-
|
|
32
|
+
};
|
|
33
|
+
c(f, "ShopwareApiError");
|
|
34
|
+
let h = f;
|
|
35
|
+
const d = class d {
|
|
34
36
|
constructor() {
|
|
35
|
-
|
|
37
|
+
u(this, "listeners", /* @__PURE__ */ new Map());
|
|
36
38
|
}
|
|
37
|
-
addEventListener(
|
|
39
|
+
addEventListener(s, e, t) {
|
|
38
40
|
if (!e) return;
|
|
39
|
-
const
|
|
41
|
+
const r = this.normalizeOptions(t), n = {
|
|
40
42
|
listener: e,
|
|
41
|
-
options:
|
|
43
|
+
options: r
|
|
42
44
|
};
|
|
43
|
-
if (
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
this.removeEventListener(
|
|
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(
|
|
51
|
+
this.listeners.has(s) || this.listeners.set(s, []), this.listeners.get(s).push(n);
|
|
50
52
|
}
|
|
51
|
-
removeEventListener(
|
|
53
|
+
removeEventListener(s, e, t) {
|
|
52
54
|
if (!e) return;
|
|
53
|
-
const
|
|
54
|
-
if (!
|
|
55
|
-
const
|
|
56
|
-
a !== -1 && (
|
|
57
|
-
}
|
|
58
|
-
dispatchEvent(
|
|
59
|
-
var
|
|
60
|
-
const e = this.listeners.get(
|
|
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
|
|
64
|
-
if (!((
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (s.options.once && this.removeEventListener(r.type, s.listener, s.options), r.cancelBubble) break;
|
|
71
|
-
}
|
|
72
|
-
return !r.defaultPrevented;
|
|
73
|
-
}
|
|
74
|
-
normalizeOptions(r) {
|
|
75
|
-
return typeof r == "boolean" ? { capture: r } : {
|
|
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))
|
|
67
|
+
break;
|
|
68
|
+
return !s.defaultPrevented;
|
|
69
|
+
}
|
|
70
|
+
normalizeOptions(s) {
|
|
71
|
+
return typeof s == "boolean" ? { capture: s } : {
|
|
76
72
|
capture: !1,
|
|
77
73
|
once: !1,
|
|
78
74
|
passive: !1,
|
|
79
|
-
...
|
|
75
|
+
...s
|
|
80
76
|
};
|
|
81
77
|
}
|
|
82
|
-
optionsMatch(
|
|
83
|
-
return
|
|
78
|
+
optionsMatch(s, e) {
|
|
79
|
+
return s.capture === e.capture;
|
|
84
80
|
}
|
|
85
81
|
// Utility methods for easier usage
|
|
86
|
-
on(
|
|
87
|
-
this.addEventListener(
|
|
82
|
+
on(s, e, t) {
|
|
83
|
+
this.addEventListener(s, e, t);
|
|
88
84
|
}
|
|
89
|
-
off(
|
|
90
|
-
this.removeEventListener(
|
|
85
|
+
off(s, e, t) {
|
|
86
|
+
this.removeEventListener(s, e, t);
|
|
91
87
|
}
|
|
92
|
-
once(
|
|
93
|
-
const
|
|
88
|
+
once(s, e, t) {
|
|
89
|
+
const r = typeof t == "boolean" ? {
|
|
94
90
|
capture: t,
|
|
95
91
|
once: !0
|
|
96
92
|
} : {
|
|
97
93
|
...t,
|
|
98
94
|
once: !0
|
|
99
95
|
};
|
|
100
|
-
this.addEventListener(
|
|
96
|
+
this.addEventListener(s, e, r);
|
|
101
97
|
}
|
|
102
|
-
emit(
|
|
103
|
-
const t = new CustomEvent(
|
|
98
|
+
emit(s, e) {
|
|
99
|
+
const t = new CustomEvent(s, { detail: e });
|
|
104
100
|
return this.dispatchEvent(t);
|
|
105
101
|
}
|
|
106
102
|
getEventTypes() {
|
|
107
103
|
return Array.from(this.listeners.keys());
|
|
108
104
|
}
|
|
109
|
-
removeAllListeners(
|
|
110
|
-
|
|
105
|
+
removeAllListeners(s) {
|
|
106
|
+
s ? this.listeners.delete(s) : this.listeners.clear();
|
|
111
107
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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) {
|
|
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(i))
|
|
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);
|
|
125
123
|
else
|
|
126
|
-
e(t,
|
|
127
|
-
return
|
|
124
|
+
e(t, r);
|
|
125
|
+
return s;
|
|
128
126
|
}
|
|
129
|
-
|
|
130
|
-
class
|
|
127
|
+
c(T, "createQueryParams");
|
|
128
|
+
const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends p {
|
|
131
129
|
constructor(e) {
|
|
132
130
|
super();
|
|
133
|
-
|
|
131
|
+
u(this, "options", {
|
|
134
132
|
baseURL: "",
|
|
135
133
|
apiKey: "",
|
|
136
134
|
language: "",
|
|
@@ -185,7 +183,7 @@ class k extends m {
|
|
|
185
183
|
return this.options.reflectContextToken;
|
|
186
184
|
}
|
|
187
185
|
parseOperation(e) {
|
|
188
|
-
const t =
|
|
186
|
+
const t = v.exec(e);
|
|
189
187
|
if (!(t != null && t.groups))
|
|
190
188
|
throw new Error("Invalid operation format");
|
|
191
189
|
return {
|
|
@@ -195,19 +193,19 @@ class k extends m {
|
|
|
195
193
|
};
|
|
196
194
|
}
|
|
197
195
|
interpolateUrl(e, t) {
|
|
198
|
-
return e.replace(/{([^}]+?)}/g, (
|
|
199
|
-
if (!(
|
|
200
|
-
throw new Error(`Missing required URL parameter: ${
|
|
201
|
-
return t[
|
|
196
|
+
return e.replace(/{([^}]+?)}/g, (r, n) => {
|
|
197
|
+
if (!(n in t))
|
|
198
|
+
throw new Error(`Missing required URL parameter: ${n}`);
|
|
199
|
+
return t[n];
|
|
202
200
|
});
|
|
203
201
|
}
|
|
204
202
|
async queryRaw(e, t) {
|
|
205
|
-
let
|
|
203
|
+
let r;
|
|
206
204
|
try {
|
|
207
|
-
const { method:
|
|
208
|
-
|
|
205
|
+
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}`, {
|
|
209
207
|
...t,
|
|
210
|
-
method:
|
|
208
|
+
method: n,
|
|
211
209
|
headers: {
|
|
212
210
|
"Content-Type": "application/json",
|
|
213
211
|
"sw-access-key": this.options.apiKey,
|
|
@@ -218,35 +216,37 @@ class k extends m {
|
|
|
218
216
|
},
|
|
219
217
|
body: t != null && t.body ? JSON.stringify(t.body) : void 0
|
|
220
218
|
});
|
|
221
|
-
} catch (
|
|
219
|
+
} catch (n) {
|
|
222
220
|
const a = new h(
|
|
223
221
|
"Failed to communicate with Shopware API",
|
|
224
222
|
500,
|
|
225
|
-
|
|
223
|
+
n instanceof Error ? n.message : void 0
|
|
226
224
|
);
|
|
227
225
|
throw this.emit("error", a), a;
|
|
228
226
|
}
|
|
229
|
-
if (!
|
|
230
|
-
const
|
|
231
|
-
`Shopware API request failed: ${
|
|
232
|
-
|
|
233
|
-
await
|
|
227
|
+
if (!r.ok) {
|
|
228
|
+
const n = new h(
|
|
229
|
+
`Shopware API request failed: ${r.statusText}`,
|
|
230
|
+
r.status,
|
|
231
|
+
await r.json().catch(() => {
|
|
234
232
|
})
|
|
235
233
|
);
|
|
236
|
-
throw this.emit("error",
|
|
234
|
+
throw this.emit("error", n), n;
|
|
237
235
|
}
|
|
238
236
|
if (this.options.reflectContextToken) {
|
|
239
|
-
const
|
|
240
|
-
|
|
237
|
+
const n = r.headers.get("sw-context-token");
|
|
238
|
+
n && (this.contextToken = n);
|
|
241
239
|
}
|
|
242
|
-
return
|
|
240
|
+
return r;
|
|
243
241
|
}
|
|
244
242
|
async query(e, t) {
|
|
245
243
|
return (await this.queryRaw(e, t)).json().catch(() => {
|
|
246
244
|
});
|
|
247
245
|
}
|
|
248
|
-
}
|
|
246
|
+
};
|
|
247
|
+
c(g, "ShopwareClient");
|
|
248
|
+
let w = g;
|
|
249
249
|
export {
|
|
250
250
|
h as ShopwareApiError,
|
|
251
|
-
|
|
251
|
+
w as ShopwareClient
|
|
252
252
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teamnovu/kit-shopware-api-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
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/eventEmitter.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// TODO: Move naming to hooks - deprecate event emitter
|
|
2
|
+
|
|
1
3
|
interface EventListenerEntry {
|
|
2
4
|
listener: EventListenerOrEventListenerObject
|
|
3
5
|
options: AddEventListenerOptions
|
|
@@ -68,15 +70,10 @@ export class EventEmitter implements EventTarget {
|
|
|
68
70
|
for (const entry of entriesCopy) {
|
|
69
71
|
if (entry.signal?.aborted) continue
|
|
70
72
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
entry.listener.handleEvent(event)
|
|
76
|
-
}
|
|
77
|
-
} catch (error) {
|
|
78
|
-
// In a real implementation, you might want to handle errors differently
|
|
79
|
-
console.error('Error in event listener:', error)
|
|
73
|
+
if (typeof entry.listener === 'function') {
|
|
74
|
+
entry.listener.call(this, event)
|
|
75
|
+
} else {
|
|
76
|
+
entry.listener.handleEvent(event)
|
|
80
77
|
}
|
|
81
78
|
|
|
82
79
|
// Remove if 'once' option was set
|
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: [
|
|
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
|
})
|