@teamnovu/kit-shopware-api-client 0.0.12 → 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 +4 -2
- package/dist/index.mjs +41 -32
- package/dist/types.d.ts +2 -1
- package/package.json +1 -1
- package/src/client.ts +12 -1
- package/src/types.ts +2 -1
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);
|
|
@@ -21,8 +23,8 @@ export declare class ShopwareClient<Operations> extends EventEmitter {
|
|
|
21
23
|
get includeSeoUrls(): boolean | undefined;
|
|
22
24
|
set reflectContextToken(reflectContextToken: boolean | undefined);
|
|
23
25
|
get reflectContextToken(): boolean | undefined;
|
|
24
|
-
set headers(headers: HeadersInit);
|
|
25
|
-
get headers(): HeadersInit;
|
|
26
|
+
set headers(headers: HeadersInit | undefined);
|
|
27
|
+
get headers(): HeadersInit | undefined;
|
|
26
28
|
private parseOperation;
|
|
27
29
|
private interpolateUrl;
|
|
28
30
|
queryRaw<OperationKey extends (keyof Operations) & string>(operation: OperationKey, options?: OperationOptions<Operations, OperationKey>): Promise<Response>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
var y = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var h = (o, 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
|
|
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
|
-
}),
|
|
23
|
-
errors: i.array(
|
|
24
|
-
}),
|
|
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 } =
|
|
29
|
+
const { success: n, data: a } = T.safeParse(r);
|
|
30
30
|
this.errors = n ? a.errors : [];
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
-
|
|
34
|
-
let u =
|
|
35
|
-
const
|
|
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((
|
|
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
|
-
|
|
110
|
-
let
|
|
111
|
-
const m = /* @__PURE__ */
|
|
112
|
-
function
|
|
113
|
-
const s = new URLSearchParams(), e = /* @__PURE__ */
|
|
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
|
-
|
|
128
|
-
const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i, g = class g extends
|
|
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:
|
|
213
|
-
r = await fetch(`${this.baseURL}/store-api${
|
|
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:
|
|
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 (
|
|
228
|
-
const
|
|
236
|
+
} catch (a) {
|
|
237
|
+
const c = new u(
|
|
229
238
|
"Failed to communicate with Shopware API",
|
|
230
239
|
500,
|
|
231
|
-
|
|
240
|
+
a instanceof Error ? a.message : void 0
|
|
232
241
|
);
|
|
233
|
-
throw this.emit("error",
|
|
242
|
+
throw this.emit("error", c), c;
|
|
234
243
|
}
|
|
235
244
|
if (!r.ok) {
|
|
236
|
-
const
|
|
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",
|
|
251
|
+
throw this.emit("error", a), a;
|
|
243
252
|
}
|
|
244
253
|
if (this.options.reflectContextToken) {
|
|
245
|
-
const
|
|
246
|
-
|
|
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
|
-
|
|
264
|
+
l(g, "ShopwareClient");
|
|
256
265
|
let w = g;
|
|
257
266
|
export {
|
|
258
267
|
u as ShopwareApiError,
|
package/dist/types.d.ts
CHANGED
|
@@ -24,7 +24,8 @@ export interface ShopwareClientOptions {
|
|
|
24
24
|
apiKey: string;
|
|
25
25
|
includeSeoUrls?: boolean;
|
|
26
26
|
language?: string;
|
|
27
|
-
headers
|
|
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
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.')
|
|
@@ -107,7 +116,7 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
107
116
|
return this.options.reflectContextToken
|
|
108
117
|
}
|
|
109
118
|
|
|
110
|
-
set headers(headers: HeadersInit) {
|
|
119
|
+
set headers(headers: HeadersInit | undefined) {
|
|
111
120
|
this.options.headers = headers
|
|
112
121
|
}
|
|
113
122
|
|
|
@@ -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
|
@@ -42,7 +42,8 @@ export interface ShopwareClientOptions {
|
|
|
42
42
|
apiKey: string
|
|
43
43
|
includeSeoUrls?: boolean
|
|
44
44
|
language?: string
|
|
45
|
-
headers
|
|
45
|
+
headers?: HeadersInit
|
|
46
|
+
requestInitDefaults?: RequestInit
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* If true, the context token will be reflected in the response headers.
|