@teamnovu/kit-shopware-api-client 0.0.11 → 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 +2 -0
- package/dist/index.mjs +50 -42
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/client.ts +16 -5
- package/src/types.ts +2 -0
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,40 +1,40 @@
|
|
|
1
1
|
var y = Object.defineProperty;
|
|
2
|
-
var x = (
|
|
3
|
-
var c = (
|
|
4
|
-
var
|
|
5
|
-
import { z as
|
|
6
|
-
const k =
|
|
7
|
-
status:
|
|
8
|
-
code:
|
|
9
|
-
title:
|
|
10
|
-
detail:
|
|
11
|
-
meta:
|
|
12
|
-
source:
|
|
13
|
-
pointer:
|
|
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:
|
|
16
|
-
|
|
17
|
-
file:
|
|
18
|
-
line:
|
|
19
|
-
function:
|
|
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 =
|
|
23
|
-
errors:
|
|
24
|
-
}),
|
|
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
|
-
|
|
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(
|
|
34
|
-
let
|
|
35
|
-
const
|
|
33
|
+
c(p, "ShopwareApiError");
|
|
34
|
+
let u = p;
|
|
35
|
+
const f = class f {
|
|
36
36
|
constructor() {
|
|
37
|
-
|
|
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(
|
|
110
|
-
let
|
|
111
|
-
const m = /* @__PURE__ */ c((
|
|
112
|
-
function T(
|
|
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(
|
|
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
|
|
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
|
-
|
|
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.
|
|
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.
|
|
212
|
-
...this.
|
|
213
|
-
...this.
|
|
214
|
-
...this.
|
|
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
|
|
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
|
|
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
|
-
|
|
258
|
+
u as ShopwareApiError,
|
|
251
259
|
w as ShopwareClient
|
|
252
260
|
};
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
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.
|
|
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.
|
|
152
|
-
...(this.
|
|
153
|
-
...(this.
|
|
154
|
-
...(this.
|
|
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
|
}
|