@teamnovu/kit-shopware-api-client 0.0.13 → 0.0.15
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 -0
- package/dist/index.mjs +45 -28
- package/dist/types.d.ts +7 -0
- package/package.json +1 -1
- package/src/client.ts +24 -0
- package/src/types.ts +8 -0
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);
|
|
@@ -23,6 +25,8 @@ export declare class ShopwareClient<Operations> extends EventEmitter {
|
|
|
23
25
|
get reflectContextToken(): boolean | undefined;
|
|
24
26
|
set headers(headers: HeadersInit | undefined);
|
|
25
27
|
get headers(): HeadersInit | undefined;
|
|
28
|
+
set queryClientId(queryClientId: string | undefined);
|
|
29
|
+
get queryClientId(): string | undefined;
|
|
26
30
|
private parseOperation;
|
|
27
31
|
private interpolateUrl;
|
|
28
32
|
queryRaw<OperationKey extends (keyof Operations) & string>(operation: OperationKey, options?: OperationOptions<Operations, OperationKey>): Promise<Response>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var y = Object.defineProperty;
|
|
2
|
-
var
|
|
2
|
+
var I = (o, s, e) => s in o ? y(o, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[s] = e;
|
|
3
3
|
var c = (o, s) => y(o, "name", { value: s, configurable: !0 });
|
|
4
|
-
var
|
|
4
|
+
var u = (o, s, e) => I(o, typeof s != "symbol" ? s + "" : s, e);
|
|
5
5
|
import { z as i } from "zod";
|
|
6
6
|
const k = i.object({
|
|
7
7
|
status: i.string(),
|
|
@@ -21,20 +21,20 @@ const k = i.object({
|
|
|
21
21
|
).optional()
|
|
22
22
|
}), U = i.object({
|
|
23
23
|
errors: i.array(k)
|
|
24
|
-
}),
|
|
24
|
+
}), f = class f extends Error {
|
|
25
25
|
constructor(e, t, r) {
|
|
26
26
|
super(e);
|
|
27
|
-
|
|
27
|
+
u(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(f, "ShopwareApiError");
|
|
34
|
+
let h = f;
|
|
35
|
+
const p = class p {
|
|
36
36
|
constructor() {
|
|
37
|
-
|
|
37
|
+
u(this, "listeners", /* @__PURE__ */ new Map());
|
|
38
38
|
}
|
|
39
39
|
addEventListener(s, e, t) {
|
|
40
40
|
if (!e) return;
|
|
@@ -106,10 +106,10 @@ const f = class f {
|
|
|
106
106
|
s ? this.listeners.delete(s) : this.listeners.clear();
|
|
107
107
|
}
|
|
108
108
|
};
|
|
109
|
-
c(
|
|
110
|
-
let d =
|
|
109
|
+
c(p, "EventEmitter");
|
|
110
|
+
let d = p;
|
|
111
111
|
const m = /* @__PURE__ */ c((o) => o == null || typeof o == "number" && isNaN(o) || typeof o == "function", "isIgnored");
|
|
112
|
-
function
|
|
112
|
+
function q(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");
|
|
@@ -124,11 +124,11 @@ function T(o) {
|
|
|
124
124
|
e(t, r);
|
|
125
125
|
return s;
|
|
126
126
|
}
|
|
127
|
-
c(
|
|
128
|
-
const
|
|
127
|
+
c(q, "createQueryParams");
|
|
128
|
+
const T = /^(?<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
|
+
u(this, "options", {
|
|
132
132
|
baseURL: "",
|
|
133
133
|
apiKey: "",
|
|
134
134
|
language: "",
|
|
@@ -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, this.queryClientId = e.queryClientId ?? this.options.queryClientId;
|
|
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")
|
|
@@ -189,8 +195,16 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
|
|
|
189
195
|
get headers() {
|
|
190
196
|
return this.options.headers;
|
|
191
197
|
}
|
|
198
|
+
set queryClientId(e) {
|
|
199
|
+
if (e && typeof e != "string")
|
|
200
|
+
throw new Error("Invalid queryClientId: If provided, it must be a string.");
|
|
201
|
+
this.options.queryClientId = e;
|
|
202
|
+
}
|
|
203
|
+
get queryClientId() {
|
|
204
|
+
return this.options.queryClientId;
|
|
205
|
+
}
|
|
192
206
|
parseOperation(e) {
|
|
193
|
-
const t =
|
|
207
|
+
const t = T.exec(e);
|
|
194
208
|
if (!(t != null && t.groups))
|
|
195
209
|
throw new Error("Invalid operation format");
|
|
196
210
|
return {
|
|
@@ -207,15 +221,18 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
|
|
|
207
221
|
});
|
|
208
222
|
}
|
|
209
223
|
async queryRaw(e, t) {
|
|
224
|
+
var n;
|
|
210
225
|
let r;
|
|
211
226
|
try {
|
|
212
|
-
const { method:
|
|
213
|
-
r = await fetch(`${this.baseURL}/store-api${
|
|
227
|
+
const { method: a, url: l } = this.parseOperation(e), b = this.interpolateUrl(l, (t == null ? void 0 : t.params) ?? {}), x = t != null && t.query ? `?${q(t.query)}` : "";
|
|
228
|
+
r = await fetch(`${this.baseURL}/store-api${b}${x}`, {
|
|
229
|
+
...this.requestInitDefaults,
|
|
214
230
|
...t,
|
|
215
|
-
method:
|
|
231
|
+
method: a,
|
|
216
232
|
headers: {
|
|
217
233
|
"Content-Type": "application/json",
|
|
218
234
|
"sw-access-key": this.apiKey,
|
|
235
|
+
...(n = this.requestInitDefaults) == null ? void 0 : n.headers,
|
|
219
236
|
...this.contextToken && { "sw-context-token": this.contextToken },
|
|
220
237
|
...this.includeSeoUrls && { "sw-include-seo-urls": "true" },
|
|
221
238
|
...this.language && { "sw-language-id": this.language },
|
|
@@ -224,26 +241,26 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
|
|
|
224
241
|
},
|
|
225
242
|
body: t != null && t.body ? JSON.stringify(t.body) : void 0
|
|
226
243
|
});
|
|
227
|
-
} catch (
|
|
228
|
-
const
|
|
244
|
+
} catch (a) {
|
|
245
|
+
const l = new h(
|
|
229
246
|
"Failed to communicate with Shopware API",
|
|
230
247
|
500,
|
|
231
|
-
|
|
248
|
+
a instanceof Error ? a.message : void 0
|
|
232
249
|
);
|
|
233
|
-
throw this.emit("error",
|
|
250
|
+
throw this.emit("error", l), l;
|
|
234
251
|
}
|
|
235
252
|
if (!r.ok) {
|
|
236
|
-
const
|
|
253
|
+
const a = new h(
|
|
237
254
|
`Shopware API request failed: ${r.statusText}`,
|
|
238
255
|
r.status,
|
|
239
256
|
await r.json().catch(() => {
|
|
240
257
|
})
|
|
241
258
|
);
|
|
242
|
-
throw this.emit("error",
|
|
259
|
+
throw this.emit("error", a), a;
|
|
243
260
|
}
|
|
244
261
|
if (this.options.reflectContextToken) {
|
|
245
|
-
const
|
|
246
|
-
|
|
262
|
+
const a = r.headers.get("sw-context-token");
|
|
263
|
+
a && (this.contextToken = a);
|
|
247
264
|
}
|
|
248
265
|
return r;
|
|
249
266
|
}
|
|
@@ -255,6 +272,6 @@ const v = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
|
|
|
255
272
|
c(g, "ShopwareClient");
|
|
256
273
|
let w = g;
|
|
257
274
|
export {
|
|
258
|
-
|
|
275
|
+
h as ShopwareApiError,
|
|
259
276
|
w as ShopwareClient
|
|
260
277
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export interface ShopwareClientOptions {
|
|
|
25
25
|
includeSeoUrls?: boolean;
|
|
26
26
|
language?: string;
|
|
27
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
|
|
@@ -37,5 +38,11 @@ export interface ShopwareClientOptions {
|
|
|
37
38
|
* @default undefined
|
|
38
39
|
*/
|
|
39
40
|
contextToken?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The vue-query queryClientKey of the QueryClient that holds all queries
|
|
43
|
+
* made through this client. If not set, the default QueryClient is used.
|
|
44
|
+
* @default undefined
|
|
45
|
+
*/
|
|
46
|
+
queryClientId?: string;
|
|
40
47
|
}
|
|
41
48
|
export {};
|
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
|
|
@@ -40,6 +41,15 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
40
41
|
this.includeSeoUrls = options.includeSeoUrls ?? this.options.includeSeoUrls
|
|
41
42
|
this.reflectContextToken = options.reflectContextToken ?? this.options.reflectContextToken
|
|
42
43
|
this.headers = options.headers ?? this.options.headers
|
|
44
|
+
this.queryClientId = options.queryClientId ?? this.options.queryClientId
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
set requestInitDefaults(defaults: RequestInit | undefined) {
|
|
48
|
+
this.options.requestInitDefaults = defaults
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get requestInitDefaults() {
|
|
52
|
+
return this.options.requestInitDefaults
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
set baseURL(baseURL: string) {
|
|
@@ -115,6 +125,18 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
115
125
|
return this.options.headers
|
|
116
126
|
}
|
|
117
127
|
|
|
128
|
+
set queryClientId(queryClientId: string | undefined) {
|
|
129
|
+
if (queryClientId && typeof queryClientId !== 'string') {
|
|
130
|
+
throw new Error('Invalid queryClientId: If provided, it must be a string.')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.options.queryClientId = queryClientId
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get queryClientId() {
|
|
137
|
+
return this.options.queryClientId
|
|
138
|
+
}
|
|
139
|
+
|
|
118
140
|
private parseOperation(operation: keyof Operations & string): {
|
|
119
141
|
name: string
|
|
120
142
|
method: string
|
|
@@ -154,11 +176,13 @@ export class ShopwareClient<Operations> extends EventEmitter {
|
|
|
154
176
|
const query = options?.query ? `?${createQueryParams(options.query)}` : ''
|
|
155
177
|
|
|
156
178
|
response = await fetch(`${this.baseURL}/store-api${interpolatedUrl}${query}`, {
|
|
179
|
+
...this.requestInitDefaults,
|
|
157
180
|
...options,
|
|
158
181
|
method,
|
|
159
182
|
headers: {
|
|
160
183
|
'Content-Type': 'application/json',
|
|
161
184
|
'sw-access-key': this.apiKey,
|
|
185
|
+
...this.requestInitDefaults?.headers,
|
|
162
186
|
...(this.contextToken && { 'sw-context-token': this.contextToken }),
|
|
163
187
|
...(this.includeSeoUrls && { 'sw-include-seo-urls': 'true' }),
|
|
164
188
|
...(this.language && { 'sw-language-id': this.language }),
|
package/src/types.ts
CHANGED
|
@@ -43,6 +43,7 @@ export interface ShopwareClientOptions {
|
|
|
43
43
|
includeSeoUrls?: boolean
|
|
44
44
|
language?: string
|
|
45
45
|
headers?: HeadersInit
|
|
46
|
+
requestInitDefaults?: RequestInit
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* If true, the context token will be reflected in the response headers.
|
|
@@ -58,4 +59,11 @@ export interface ShopwareClientOptions {
|
|
|
58
59
|
*/
|
|
59
60
|
contextToken?: string
|
|
60
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The vue-query queryClientKey of the QueryClient that holds all queries
|
|
64
|
+
* made through this client. If not set, the default QueryClient is used.
|
|
65
|
+
* @default undefined
|
|
66
|
+
*/
|
|
67
|
+
queryClientId?: string
|
|
68
|
+
|
|
61
69
|
}
|