@teamnovu/kit-shopware-api-client 0.0.7 → 0.0.9
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 +13 -7
- package/dist/index.mjs +107 -86
- package/package.json +1 -1
- package/src/client.ts +65 -39
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
|
-
|
|
7
|
+
__brand: string;
|
|
8
8
|
};
|
|
9
9
|
export declare class ShopwareClient<Operations> extends EventEmitter {
|
|
10
10
|
private options;
|
|
11
11
|
constructor(options: ShopwareClientOptions);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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,73 +1,73 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { z as
|
|
5
|
-
const
|
|
6
|
-
status:
|
|
7
|
-
code:
|
|
8
|
-
title:
|
|
9
|
-
detail:
|
|
10
|
-
meta:
|
|
11
|
-
source:
|
|
12
|
-
pointer:
|
|
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);
|
|
4
|
+
import { z as i } from "zod";
|
|
5
|
+
const g = 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:
|
|
15
|
-
|
|
16
|
-
file:
|
|
17
|
-
line:
|
|
18
|
-
function:
|
|
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
|
-
}),
|
|
22
|
-
errors:
|
|
21
|
+
}), y = i.object({
|
|
22
|
+
errors: i.array(g)
|
|
23
23
|
});
|
|
24
|
-
class
|
|
25
|
-
constructor(e, t,
|
|
24
|
+
class h extends Error {
|
|
25
|
+
constructor(e, t, n) {
|
|
26
26
|
super(e);
|
|
27
|
-
|
|
28
|
-
this.status = t, this.response =
|
|
29
|
-
const { success:
|
|
30
|
-
this.errors =
|
|
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 : [];
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
class m {
|
|
34
34
|
constructor() {
|
|
35
|
-
|
|
35
|
+
l(this, "listeners", /* @__PURE__ */ new Map());
|
|
36
36
|
}
|
|
37
37
|
addEventListener(r, e, t) {
|
|
38
38
|
if (!e) return;
|
|
39
|
-
const
|
|
39
|
+
const n = this.normalizeOptions(t), s = {
|
|
40
40
|
listener: e,
|
|
41
|
-
options:
|
|
41
|
+
options: n
|
|
42
42
|
};
|
|
43
|
-
if (
|
|
44
|
-
if (
|
|
45
|
-
|
|
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(
|
|
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
|
|
54
|
-
if (!
|
|
55
|
-
const
|
|
56
|
-
a !== -1 && (
|
|
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
57
|
}
|
|
58
58
|
dispatchEvent(r) {
|
|
59
|
-
var
|
|
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
|
|
64
|
-
if (!((
|
|
63
|
+
for (const s of t)
|
|
64
|
+
if (!((n = s.signal) != null && n.aborted)) {
|
|
65
65
|
try {
|
|
66
|
-
typeof
|
|
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 (
|
|
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
|
|
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,
|
|
100
|
+
this.addEventListener(r, e, n);
|
|
101
101
|
}
|
|
102
102
|
emit(r, e) {
|
|
103
103
|
const t = new CustomEvent(r, { detail: e });
|
|
@@ -110,27 +110,27 @@ class m {
|
|
|
110
110
|
r ? this.listeners.delete(r) : this.listeners.clear();
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
const
|
|
114
|
-
function
|
|
115
|
-
const r = new URLSearchParams(), e = (t,
|
|
116
|
-
|
|
113
|
+
const u = (o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function";
|
|
114
|
+
function w(o) {
|
|
115
|
+
const r = new URLSearchParams(), e = (t, n) => {
|
|
116
|
+
u(n) || (typeof n == "object" && r.append(t, JSON.stringify(n)), r.append(t, String(n)));
|
|
117
117
|
};
|
|
118
|
-
for (const [t,
|
|
119
|
-
if (!
|
|
120
|
-
if (typeof
|
|
121
|
-
r.append(t, String(
|
|
122
|
-
else if (Array.isArray(
|
|
123
|
-
for (const
|
|
124
|
-
e(`${t}[]`,
|
|
118
|
+
for (const [t, n] of Object.entries(o))
|
|
119
|
+
if (!u(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,
|
|
126
|
+
e(t, n);
|
|
127
127
|
return r;
|
|
128
128
|
}
|
|
129
|
-
const
|
|
129
|
+
const b = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
|
|
130
130
|
class k extends m {
|
|
131
131
|
constructor(e) {
|
|
132
132
|
super();
|
|
133
|
-
|
|
133
|
+
l(this, "options", {
|
|
134
134
|
baseURL: "",
|
|
135
135
|
apiKey: "",
|
|
136
136
|
language: "",
|
|
@@ -138,36 +138,54 @@ class k extends m {
|
|
|
138
138
|
includeSeoUrls: !1,
|
|
139
139
|
reflectContextToken: !0
|
|
140
140
|
});
|
|
141
|
-
this.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
172
|
+
get contextToken() {
|
|
173
|
+
return this.options.contextToken;
|
|
174
|
+
}
|
|
175
|
+
set includeSeoUrls(e) {
|
|
164
176
|
this.options.includeSeoUrls = e;
|
|
165
177
|
}
|
|
166
|
-
|
|
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 =
|
|
188
|
+
const t = b.exec(e);
|
|
171
189
|
if (!(t != null && t.groups))
|
|
172
190
|
throw new Error("Invalid operation format");
|
|
173
191
|
return {
|
|
@@ -177,15 +195,17 @@ class k extends m {
|
|
|
177
195
|
};
|
|
178
196
|
}
|
|
179
197
|
interpolateUrl(e, t) {
|
|
180
|
-
return e.replace(/{([^}]+?)}/g, (
|
|
181
|
-
if (!(
|
|
182
|
-
throw new Error(`Missing required URL parameter: ${
|
|
183
|
-
return t[
|
|
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) {
|
|
205
|
+
let n;
|
|
187
206
|
try {
|
|
188
|
-
const { method: s, url:
|
|
207
|
+
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)}` : "";
|
|
208
|
+
n = await fetch(`${this.options.baseURL}/store-api${c}${p}`, {
|
|
189
209
|
...t,
|
|
190
210
|
method: s,
|
|
191
211
|
headers: {
|
|
@@ -198,27 +218,28 @@ class k extends m {
|
|
|
198
218
|
},
|
|
199
219
|
body: t != null && t.body ? JSON.stringify(t.body) : void 0
|
|
200
220
|
});
|
|
201
|
-
if (!c.ok) {
|
|
202
|
-
const l = new p(
|
|
203
|
-
`Shopware API request failed: ${c.statusText}`,
|
|
204
|
-
c.status,
|
|
205
|
-
await c.json().catch(() => {
|
|
206
|
-
})
|
|
207
|
-
);
|
|
208
|
-
throw this.emit("error", l), l;
|
|
209
|
-
}
|
|
210
|
-
if (this.options.reflectContextToken) {
|
|
211
|
-
const l = c.headers.get("sw-context-token");
|
|
212
|
-
l && this.setContextToken(l);
|
|
213
|
-
}
|
|
214
|
-
return c;
|
|
215
221
|
} catch (s) {
|
|
216
|
-
|
|
222
|
+
const a = new h(
|
|
217
223
|
"Failed to communicate with Shopware API",
|
|
218
224
|
500,
|
|
219
225
|
s instanceof Error ? s.message : void 0
|
|
220
226
|
);
|
|
227
|
+
throw this.emit("error", a), a;
|
|
228
|
+
}
|
|
229
|
+
if (!n.ok) {
|
|
230
|
+
const s = new h(
|
|
231
|
+
`Shopware API request failed: ${n.statusText}`,
|
|
232
|
+
n.status,
|
|
233
|
+
await n.json().catch(() => {
|
|
234
|
+
})
|
|
235
|
+
);
|
|
236
|
+
throw this.emit("error", s), s;
|
|
237
|
+
}
|
|
238
|
+
if (this.options.reflectContextToken) {
|
|
239
|
+
const s = n.headers.get("sw-context-token");
|
|
240
|
+
s && (this.contextToken = s);
|
|
221
241
|
}
|
|
242
|
+
return n;
|
|
222
243
|
}
|
|
223
244
|
async query(e, t) {
|
|
224
245
|
return (await this.queryRaw(e, t)).json().catch(() => {
|
|
@@ -226,6 +247,6 @@ class k extends m {
|
|
|
226
247
|
}
|
|
227
248
|
}
|
|
228
249
|
export {
|
|
229
|
-
|
|
250
|
+
h as ShopwareApiError,
|
|
230
251
|
k as ShopwareClient
|
|
231
252
|
};
|
package/package.json
CHANGED
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'> & {
|
|
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.
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -110,6 +134,8 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
110
134
|
operation: OperationKey,
|
|
111
135
|
options?: OperationOptions<Operations, OperationKey>,
|
|
112
136
|
): Promise<Response> {
|
|
137
|
+
let response: Response
|
|
138
|
+
|
|
113
139
|
try {
|
|
114
140
|
const { method, url } = this.parseOperation(operation)
|
|
115
141
|
|
|
@@ -117,7 +143,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
117
143
|
|
|
118
144
|
const query = options?.query ? `?${createQueryParams(options.query)}` : ''
|
|
119
145
|
|
|
120
|
-
|
|
146
|
+
response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}${query}`, {
|
|
121
147
|
...options,
|
|
122
148
|
method,
|
|
123
149
|
headers: {
|
|
@@ -130,38 +156,38 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
130
156
|
},
|
|
131
157
|
body: options?.body ? JSON.stringify(options.body) : undefined,
|
|
132
158
|
})
|
|
159
|
+
} catch (error) {
|
|
160
|
+
const shopwareError = new ShopwareApiError(
|
|
161
|
+
'Failed to communicate with Shopware API',
|
|
162
|
+
500,
|
|
163
|
+
error instanceof Error ? error.message : undefined,
|
|
164
|
+
)
|
|
133
165
|
|
|
134
|
-
|
|
135
|
-
const error = new ShopwareApiError(
|
|
136
|
-
`Shopware API request failed: ${response.statusText}`,
|
|
137
|
-
response.status,
|
|
138
|
-
await response.json().catch(() => undefined),
|
|
139
|
-
)
|
|
166
|
+
this.emit('error', shopwareError)
|
|
140
167
|
|
|
141
|
-
|
|
168
|
+
throw shopwareError
|
|
169
|
+
}
|
|
142
170
|
|
|
143
|
-
|
|
144
|
-
|
|
171
|
+
if (!response.ok) {
|
|
172
|
+
const error = new ShopwareApiError(
|
|
173
|
+
`Shopware API request failed: ${response.statusText}`,
|
|
174
|
+
response.status,
|
|
175
|
+
await response.json().catch(() => undefined),
|
|
176
|
+
)
|
|
145
177
|
|
|
146
|
-
|
|
147
|
-
const contextToken = response.headers.get('sw-context-token')
|
|
148
|
-
if (contextToken) {
|
|
149
|
-
this.setContextToken(contextToken)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
178
|
+
this.emit('error', error)
|
|
152
179
|
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
if (error instanceof ShopwareApiError) {
|
|
156
|
-
throw error
|
|
157
|
-
}
|
|
180
|
+
throw error
|
|
181
|
+
}
|
|
158
182
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
183
|
+
if (this.options.reflectContextToken) {
|
|
184
|
+
const contextToken = response.headers.get('sw-context-token')
|
|
185
|
+
if (contextToken) {
|
|
186
|
+
this.contextToken = contextToken
|
|
187
|
+
}
|
|
164
188
|
}
|
|
189
|
+
|
|
190
|
+
return response
|
|
165
191
|
}
|
|
166
192
|
|
|
167
193
|
async query<OperationKey extends (keyof Operations) & string>(
|