@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 +2 -0
- package/dist/index.mjs +114 -100
- package/dist/types.d.ts +1 -0
- package/package.json +1 -4
- package/src/client.ts +16 -5
- package/src/types.ts +2 -0
- package/vite.config.js +4 -9
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
|
|
2
|
-
var
|
|
3
|
-
var
|
|
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
|
|
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
|
-
}),
|
|
22
|
-
errors: i.array(
|
|
23
|
-
})
|
|
24
|
-
|
|
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
|
-
|
|
28
|
-
this.status = t, this.response =
|
|
29
|
-
const { success:
|
|
30
|
-
this.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
|
-
|
|
32
|
+
};
|
|
33
|
+
c(p, "ShopwareApiError");
|
|
34
|
+
let u = p;
|
|
35
|
+
const f = class f {
|
|
34
36
|
constructor() {
|
|
35
|
-
|
|
37
|
+
h(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
|
+
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 !
|
|
68
|
+
return !s.defaultPrevented;
|
|
67
69
|
}
|
|
68
|
-
normalizeOptions(
|
|
69
|
-
return typeof
|
|
70
|
+
normalizeOptions(s) {
|
|
71
|
+
return typeof s == "boolean" ? { capture: s } : {
|
|
70
72
|
capture: !1,
|
|
71
73
|
once: !1,
|
|
72
74
|
passive: !1,
|
|
73
|
-
...
|
|
75
|
+
...s
|
|
74
76
|
};
|
|
75
77
|
}
|
|
76
|
-
optionsMatch(
|
|
77
|
-
return
|
|
78
|
+
optionsMatch(s, e) {
|
|
79
|
+
return s.capture === e.capture;
|
|
78
80
|
}
|
|
79
81
|
// Utility methods for easier usage
|
|
80
|
-
on(
|
|
81
|
-
this.addEventListener(
|
|
82
|
+
on(s, e, t) {
|
|
83
|
+
this.addEventListener(s, e, t);
|
|
82
84
|
}
|
|
83
|
-
off(
|
|
84
|
-
this.removeEventListener(
|
|
85
|
+
off(s, e, t) {
|
|
86
|
+
this.removeEventListener(s, e, t);
|
|
85
87
|
}
|
|
86
|
-
once(
|
|
87
|
-
const
|
|
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(
|
|
96
|
+
this.addEventListener(s, e, r);
|
|
95
97
|
}
|
|
96
|
-
emit(
|
|
97
|
-
const t = new CustomEvent(
|
|
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(
|
|
104
|
-
|
|
105
|
+
removeAllListeners(s) {
|
|
106
|
+
s ? this.listeners.delete(s) : this.listeners.clear();
|
|
105
107
|
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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,
|
|
121
|
-
return
|
|
124
|
+
e(t, r);
|
|
125
|
+
return s;
|
|
122
126
|
}
|
|
123
|
-
|
|
124
|
-
class
|
|
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
|
-
|
|
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 =
|
|
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, (
|
|
193
|
-
if (!(
|
|
194
|
-
throw new Error(`Missing required URL parameter: ${
|
|
195
|
-
return t[
|
|
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
|
|
210
|
+
let r;
|
|
200
211
|
try {
|
|
201
|
-
const { method:
|
|
202
|
-
|
|
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:
|
|
215
|
+
method: n,
|
|
205
216
|
headers: {
|
|
206
217
|
"Content-Type": "application/json",
|
|
207
|
-
"sw-access-key": this.
|
|
208
|
-
...this.
|
|
209
|
-
...this.
|
|
210
|
-
...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,
|
|
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 (
|
|
216
|
-
const a = new
|
|
227
|
+
} catch (n) {
|
|
228
|
+
const a = new u(
|
|
217
229
|
"Failed to communicate with Shopware API",
|
|
218
230
|
500,
|
|
219
|
-
|
|
231
|
+
n instanceof Error ? n.message : void 0
|
|
220
232
|
);
|
|
221
233
|
throw this.emit("error", a), a;
|
|
222
234
|
}
|
|
223
|
-
if (!
|
|
224
|
-
const
|
|
225
|
-
`Shopware API request failed: ${
|
|
226
|
-
|
|
227
|
-
await
|
|
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",
|
|
242
|
+
throw this.emit("error", n), n;
|
|
231
243
|
}
|
|
232
244
|
if (this.options.reflectContextToken) {
|
|
233
|
-
const
|
|
234
|
-
|
|
245
|
+
const n = r.headers.get("sw-context-token");
|
|
246
|
+
n && (this.contextToken = n);
|
|
235
247
|
}
|
|
236
|
-
return
|
|
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
|
-
|
|
245
|
-
|
|
258
|
+
u as ShopwareApiError,
|
|
259
|
+
w as ShopwareClient
|
|
246
260
|
};
|
package/dist/types.d.ts
CHANGED
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.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.
|
|
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
|
}
|
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
|
})
|