@teamnovu/kit-shopware-api-client 0.0.1 → 0.0.3
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 +23 -0
- package/dist/eventEmitter.d.ts +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +185 -0
- package/dist/types.d.ts +38 -0
- package/package.json +6 -7
- package/src/client.ts +33 -6
- package/src/eventEmitter.ts +162 -0
- package/src/types.ts +17 -1
- package/eslint.config.mjs +0 -84
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { EventEmitter } from './eventEmitter';
|
|
2
|
+
import { OperationOptions, OperationProp, ShopwareClientOptions } from './types';
|
|
3
|
+
export declare class ShopwareApiError extends Error {
|
|
4
|
+
readonly status: number;
|
|
5
|
+
readonly response?: unknown | undefined;
|
|
6
|
+
constructor(message: string, status: number, response?: unknown | undefined);
|
|
7
|
+
}
|
|
8
|
+
export declare class ShopwareClient<Operations extends Record<string, {
|
|
9
|
+
body?: unknown;
|
|
10
|
+
response?: unknown;
|
|
11
|
+
}>> extends EventEmitter {
|
|
12
|
+
private options;
|
|
13
|
+
constructor(options: ShopwareClientOptions);
|
|
14
|
+
setBaseURL(baseURL: string): void;
|
|
15
|
+
setApiKey(apiKey: string): void;
|
|
16
|
+
setLanguage(language: string | undefined): void;
|
|
17
|
+
setContextToken(contextToken: string | undefined): void;
|
|
18
|
+
setIncludeSeoUrls(includeSeoUrls?: boolean): void;
|
|
19
|
+
setReflectContextToken(reflectContextToken?: boolean): void;
|
|
20
|
+
private parseOperation;
|
|
21
|
+
private interpolateUrl;
|
|
22
|
+
query<OperationKey extends (keyof Operations) & string>(operation: OperationKey, options?: OperationOptions<Operations, OperationKey>): Promise<OperationProp<Operations, OperationKey, 'response'>>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class EventEmitter implements EventTarget {
|
|
2
|
+
private listeners;
|
|
3
|
+
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
|
|
4
|
+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
|
|
5
|
+
dispatchEvent(event: Event): boolean;
|
|
6
|
+
private normalizeOptions;
|
|
7
|
+
private optionsMatch;
|
|
8
|
+
on(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
|
|
9
|
+
off(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
|
|
10
|
+
once(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
|
|
11
|
+
emit(type: string, detail?: unknown): boolean;
|
|
12
|
+
getEventTypes(): string[];
|
|
13
|
+
removeAllListeners(type?: string): void;
|
|
14
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
var u = Object.defineProperty;
|
|
2
|
+
var p = (o, t, e) => t in o ? u(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
|
|
3
|
+
var c = (o, t, e) => p(o, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
class d {
|
|
5
|
+
constructor() {
|
|
6
|
+
c(this, "listeners", /* @__PURE__ */ new Map());
|
|
7
|
+
}
|
|
8
|
+
addEventListener(t, e, s) {
|
|
9
|
+
if (!e) return;
|
|
10
|
+
const n = this.normalizeOptions(s), r = {
|
|
11
|
+
listener: e,
|
|
12
|
+
options: n
|
|
13
|
+
};
|
|
14
|
+
if (n.signal) {
|
|
15
|
+
if (n.signal.aborted) return;
|
|
16
|
+
r.signal = n.signal, n.signal.addEventListener("abort", () => {
|
|
17
|
+
this.removeEventListener(t, e, s);
|
|
18
|
+
}, { once: !0 });
|
|
19
|
+
}
|
|
20
|
+
this.listeners.has(t) || this.listeners.set(t, []), this.listeners.get(t).push(r);
|
|
21
|
+
}
|
|
22
|
+
removeEventListener(t, e, s) {
|
|
23
|
+
if (!e) return;
|
|
24
|
+
const n = this.listeners.get(t);
|
|
25
|
+
if (!n) return;
|
|
26
|
+
const r = this.normalizeOptions(s), a = n.findIndex((i) => i.listener === e && this.optionsMatch(i.options, r));
|
|
27
|
+
a !== -1 && (n.splice(a, 1), n.length === 0 && this.listeners.delete(t));
|
|
28
|
+
}
|
|
29
|
+
dispatchEvent(t) {
|
|
30
|
+
var n;
|
|
31
|
+
const e = this.listeners.get(t.type);
|
|
32
|
+
if (!e || e.length === 0) return !0;
|
|
33
|
+
const s = [...e];
|
|
34
|
+
for (const r of s)
|
|
35
|
+
if (!((n = r.signal) != null && n.aborted)) {
|
|
36
|
+
try {
|
|
37
|
+
typeof r.listener == "function" ? r.listener.call(this, t) : r.listener.handleEvent(t);
|
|
38
|
+
} catch (a) {
|
|
39
|
+
console.error("Error in event listener:", a);
|
|
40
|
+
}
|
|
41
|
+
if (r.options.once && this.removeEventListener(t.type, r.listener, r.options), t.cancelBubble) break;
|
|
42
|
+
}
|
|
43
|
+
return !t.defaultPrevented;
|
|
44
|
+
}
|
|
45
|
+
normalizeOptions(t) {
|
|
46
|
+
return typeof t == "boolean" ? { capture: t } : {
|
|
47
|
+
capture: !1,
|
|
48
|
+
once: !1,
|
|
49
|
+
passive: !1,
|
|
50
|
+
...t
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
optionsMatch(t, e) {
|
|
54
|
+
return t.capture === e.capture;
|
|
55
|
+
}
|
|
56
|
+
// Utility methods for easier usage
|
|
57
|
+
on(t, e, s) {
|
|
58
|
+
this.addEventListener(t, e, s);
|
|
59
|
+
}
|
|
60
|
+
off(t, e, s) {
|
|
61
|
+
this.removeEventListener(t, e, s);
|
|
62
|
+
}
|
|
63
|
+
once(t, e, s) {
|
|
64
|
+
const n = typeof s == "boolean" ? {
|
|
65
|
+
capture: s,
|
|
66
|
+
once: !0
|
|
67
|
+
} : {
|
|
68
|
+
...s,
|
|
69
|
+
once: !0
|
|
70
|
+
};
|
|
71
|
+
this.addEventListener(t, e, n);
|
|
72
|
+
}
|
|
73
|
+
emit(t, e) {
|
|
74
|
+
const s = new CustomEvent(t, { detail: e });
|
|
75
|
+
return this.dispatchEvent(s);
|
|
76
|
+
}
|
|
77
|
+
getEventTypes() {
|
|
78
|
+
return Array.from(this.listeners.keys());
|
|
79
|
+
}
|
|
80
|
+
removeAllListeners(t) {
|
|
81
|
+
t ? this.listeners.delete(t) : this.listeners.clear();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
class h extends Error {
|
|
85
|
+
constructor(t, e, s) {
|
|
86
|
+
super(t), this.status = e, this.response = s, this.name = "ShopwareApiError";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const f = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
|
|
90
|
+
class v extends d {
|
|
91
|
+
constructor(e) {
|
|
92
|
+
super();
|
|
93
|
+
c(this, "options", {
|
|
94
|
+
baseURL: "",
|
|
95
|
+
apiKey: "",
|
|
96
|
+
language: "",
|
|
97
|
+
contextToken: "",
|
|
98
|
+
includeSeoUrls: !1,
|
|
99
|
+
reflectContextToken: !0
|
|
100
|
+
});
|
|
101
|
+
this.setBaseURL(e.baseURL ?? this.options.baseURL), this.setApiKey(e.apiKey ?? this.options.apiKey), this.setLanguage(e.language ?? this.options.language), this.setContextToken(e.contextToken ?? this.options.contextToken), this.setIncludeSeoUrls(e.includeSeoUrls ?? this.options.includeSeoUrls), this.setReflectContextToken(e.reflectContextToken ?? this.options.reflectContextToken);
|
|
102
|
+
}
|
|
103
|
+
setBaseURL(e) {
|
|
104
|
+
if (typeof e != "string")
|
|
105
|
+
throw new Error("Invalid baseURL: It must be a string.");
|
|
106
|
+
this.options.baseURL = e;
|
|
107
|
+
}
|
|
108
|
+
setApiKey(e) {
|
|
109
|
+
if (typeof e != "string")
|
|
110
|
+
throw new Error("Invalid apiKey: It must be a string.");
|
|
111
|
+
this.options.apiKey = e;
|
|
112
|
+
}
|
|
113
|
+
setLanguage(e) {
|
|
114
|
+
if (e && typeof e != "string")
|
|
115
|
+
throw new Error("Invalid language: If provided, it must be a string.");
|
|
116
|
+
this.options.language = e;
|
|
117
|
+
}
|
|
118
|
+
setContextToken(e) {
|
|
119
|
+
if (e && typeof e != "string")
|
|
120
|
+
throw new Error("Invalid contextToken: If provided, it must be a string.");
|
|
121
|
+
this.options.contextToken = e;
|
|
122
|
+
}
|
|
123
|
+
setIncludeSeoUrls(e = !0) {
|
|
124
|
+
this.options.includeSeoUrls = e;
|
|
125
|
+
}
|
|
126
|
+
setReflectContextToken(e = !0) {
|
|
127
|
+
this.options.reflectContextToken = e;
|
|
128
|
+
}
|
|
129
|
+
parseOperation(e) {
|
|
130
|
+
const s = f.exec(e);
|
|
131
|
+
if (!(s != null && s.groups))
|
|
132
|
+
throw new Error("Invalid operation format");
|
|
133
|
+
return {
|
|
134
|
+
name: s.groups.name,
|
|
135
|
+
method: s.groups.method.toUpperCase(),
|
|
136
|
+
url: s.groups.url
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
interpolateUrl(e, s) {
|
|
140
|
+
return e.replace(/{([^}]+?)}/g, (n, r) => {
|
|
141
|
+
if (!(r in s))
|
|
142
|
+
throw new Error(`Missing required URL parameter: ${r}`);
|
|
143
|
+
return s[r];
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async query(e, s) {
|
|
147
|
+
try {
|
|
148
|
+
const { method: n, url: r } = this.parseOperation(e), a = this.interpolateUrl(r, (s == null ? void 0 : s.params) ?? {}), i = await fetch(`${this.options.baseURL}/store-api${a}`, {
|
|
149
|
+
...s,
|
|
150
|
+
method: n,
|
|
151
|
+
headers: {
|
|
152
|
+
"Content-Type": "application/json",
|
|
153
|
+
"sw-access-key": this.options.apiKey,
|
|
154
|
+
...this.options.contextToken && { "sw-context-token": this.options.contextToken },
|
|
155
|
+
...this.options.includeSeoUrls && { "sw-include-seo-urls": "true" },
|
|
156
|
+
...this.options.language && { "sw-language-id": this.options.language },
|
|
157
|
+
...s == null ? void 0 : s.headers
|
|
158
|
+
},
|
|
159
|
+
body: s != null && s.body ? JSON.stringify(s.body) : void 0
|
|
160
|
+
});
|
|
161
|
+
if (!i.ok)
|
|
162
|
+
throw new h(
|
|
163
|
+
`Shopware API request failed: ${i.statusText}`,
|
|
164
|
+
i.status,
|
|
165
|
+
await i.json().catch(() => {
|
|
166
|
+
})
|
|
167
|
+
);
|
|
168
|
+
if (this.options.reflectContextToken) {
|
|
169
|
+
const l = i.headers.get("sw-context-token");
|
|
170
|
+
l && (this.setContextToken(l), this.emit("contextToken", l));
|
|
171
|
+
}
|
|
172
|
+
return await i.json();
|
|
173
|
+
} catch (n) {
|
|
174
|
+
throw n instanceof h ? n : new h(
|
|
175
|
+
"Failed to communicate with Shopware API",
|
|
176
|
+
500,
|
|
177
|
+
n instanceof Error ? n.message : void 0
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export {
|
|
183
|
+
h as ShopwareApiError,
|
|
184
|
+
v as ShopwareClient
|
|
185
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
|
2
|
+
type ExtractUrlParams<URL extends string> = URL extends `${infer _Start}/{${infer Param}}${infer Rest}` ? Param | ExtractUrlParams<Rest> : never;
|
|
3
|
+
type UrlParamsRecord<URL extends string> = {
|
|
4
|
+
[K in ExtractUrlParams<URL>]: string;
|
|
5
|
+
};
|
|
6
|
+
export type Operation<Operations, OperationKey extends keyof Operations> = Operations[OperationKey];
|
|
7
|
+
export type OperationProp<Operations, OperationKey extends keyof Operations, Prop extends string> = Operation<Operations, OperationKey> extends {
|
|
8
|
+
[K in Prop]?: unknown;
|
|
9
|
+
} ? Operation<Operations, OperationKey>[Prop] : never;
|
|
10
|
+
export type InferParameters<T extends string> = UrlParamsRecord<ExtractUrlFromOperation<T>>;
|
|
11
|
+
type OperationKey<Operations> = keyof Operations & string;
|
|
12
|
+
type ExtractUrlFromOperation<K extends string> = K extends `${string} ${string} ${infer U}` ? U : never;
|
|
13
|
+
export type OperationOptions<Operations, K extends OperationKey<Operations>> = Omit<RequestInit, 'body' | 'method'> & {
|
|
14
|
+
body?: OperationProp<Operations, K, 'body'>;
|
|
15
|
+
} & (UrlParamsRecord<ExtractUrlFromOperation<K>> extends infer T ? (keyof T extends never ? {
|
|
16
|
+
params?: void;
|
|
17
|
+
} : {
|
|
18
|
+
params: T;
|
|
19
|
+
}) : never);
|
|
20
|
+
export interface ShopwareClientOptions {
|
|
21
|
+
baseURL: string;
|
|
22
|
+
apiKey: string;
|
|
23
|
+
includeSeoUrls?: boolean;
|
|
24
|
+
language?: string;
|
|
25
|
+
/**
|
|
26
|
+
* If true, the context token will be reflected in the response headers.
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
reflectContextToken?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* If provided, it will be used as the context token.
|
|
32
|
+
* If reflectContextToken is true, this token will be used only until the first response
|
|
33
|
+
* sends a new context token.
|
|
34
|
+
* @default undefined
|
|
35
|
+
*/
|
|
36
|
+
contextToken?: string;
|
|
37
|
+
}
|
|
38
|
+
export {};
|
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.3",
|
|
4
4
|
"description": "A client for the Shopware API",
|
|
5
5
|
"main": "dist/index.mjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -14,11 +14,6 @@
|
|
|
14
14
|
"peerDependencies": {
|
|
15
15
|
"vue": "^3.0.0"
|
|
16
16
|
},
|
|
17
|
-
"scripts": {
|
|
18
|
-
"build": "vite build",
|
|
19
|
-
"watch": "NODE_ENV=development vite build --watch",
|
|
20
|
-
"lint": "eslint --fix --ignore-pattern 'dist/**' ."
|
|
21
|
-
},
|
|
22
17
|
"repository": {
|
|
23
18
|
"type": "git",
|
|
24
19
|
"url": "git+ssh://git@github.com:teamnovu/kit.git"
|
|
@@ -26,5 +21,9 @@
|
|
|
26
21
|
"keywords": [],
|
|
27
22
|
"author": "Elias Bernhaut",
|
|
28
23
|
"homepage": "https://kit.novu.ch/packages/shopware-api-client/",
|
|
29
|
-
"
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "vite build",
|
|
26
|
+
"watch": "NODE_ENV=development vite build --watch",
|
|
27
|
+
"lint": "eslint --fix --ignore-pattern 'dist/**' ."
|
|
28
|
+
}
|
|
30
29
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { EventEmitter } from './eventEmitter'
|
|
1
2
|
import type {
|
|
2
3
|
OperationOptions,
|
|
3
4
|
OperationProp,
|
|
@@ -20,20 +21,25 @@ const operationRegex = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(
|
|
|
20
21
|
export class ShopwareClient<Operations extends Record<string, {
|
|
21
22
|
body?: unknown
|
|
22
23
|
response?: unknown
|
|
23
|
-
}>> {
|
|
24
|
+
}>> extends EventEmitter {
|
|
24
25
|
private options: ShopwareClientOptions = {
|
|
25
26
|
baseURL: '',
|
|
26
27
|
apiKey: '',
|
|
27
28
|
language: '',
|
|
29
|
+
contextToken: '',
|
|
28
30
|
includeSeoUrls: false,
|
|
31
|
+
reflectContextToken: true,
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
constructor(options: ShopwareClientOptions) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
this.
|
|
36
|
-
this.options
|
|
35
|
+
super()
|
|
36
|
+
|
|
37
|
+
this.setBaseURL(options.baseURL ?? this.options.baseURL)
|
|
38
|
+
this.setApiKey(options.apiKey ?? this.options.apiKey)
|
|
39
|
+
this.setLanguage(options.language ?? this.options.language)
|
|
40
|
+
this.setContextToken(options.contextToken ?? this.options.contextToken)
|
|
41
|
+
this.setIncludeSeoUrls(options.includeSeoUrls ?? this.options.includeSeoUrls)
|
|
42
|
+
this.setReflectContextToken(options.reflectContextToken ?? this.options.reflectContextToken)
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
public setBaseURL(baseURL: string) {
|
|
@@ -60,10 +66,22 @@ export class ShopwareClient<Operations extends Record<string, {
|
|
|
60
66
|
this.options.language = language
|
|
61
67
|
}
|
|
62
68
|
|
|
69
|
+
public setContextToken(contextToken: string | undefined) {
|
|
70
|
+
if (contextToken && typeof contextToken !== 'string') {
|
|
71
|
+
throw new Error('Invalid contextToken: If provided, it must be a string.')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.options.contextToken = contextToken
|
|
75
|
+
}
|
|
76
|
+
|
|
63
77
|
public setIncludeSeoUrls(includeSeoUrls: boolean = true) {
|
|
64
78
|
this.options.includeSeoUrls = includeSeoUrls
|
|
65
79
|
}
|
|
66
80
|
|
|
81
|
+
public setReflectContextToken(reflectContextToken: boolean = true) {
|
|
82
|
+
this.options.reflectContextToken = reflectContextToken
|
|
83
|
+
}
|
|
84
|
+
|
|
67
85
|
private parseOperation(operation: keyof Operations & string): {
|
|
68
86
|
name: string
|
|
69
87
|
method: string
|
|
@@ -104,6 +122,7 @@ export class ShopwareClient<Operations extends Record<string, {
|
|
|
104
122
|
headers: {
|
|
105
123
|
'Content-Type': 'application/json',
|
|
106
124
|
'sw-access-key': this.options.apiKey,
|
|
125
|
+
...(this.options.contextToken && { 'sw-context-token': this.options.contextToken }),
|
|
107
126
|
...(this.options.includeSeoUrls && { 'sw-include-seo-urls': 'true' }),
|
|
108
127
|
...(this.options.language && { 'sw-language-id': this.options.language }),
|
|
109
128
|
...options?.headers,
|
|
@@ -119,6 +138,14 @@ export class ShopwareClient<Operations extends Record<string, {
|
|
|
119
138
|
)
|
|
120
139
|
}
|
|
121
140
|
|
|
141
|
+
if (this.options.reflectContextToken) {
|
|
142
|
+
const contextToken = response.headers.get('sw-context-token')
|
|
143
|
+
if (contextToken) {
|
|
144
|
+
this.setContextToken(contextToken)
|
|
145
|
+
this.emit('contextToken', contextToken)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
122
149
|
const data = await response.json()
|
|
123
150
|
return data
|
|
124
151
|
} catch (error) {
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
interface EventListenerEntry {
|
|
2
|
+
listener: EventListenerOrEventListenerObject
|
|
3
|
+
options: AddEventListenerOptions
|
|
4
|
+
signal?: AbortSignal
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class EventEmitter implements EventTarget {
|
|
8
|
+
private listeners = new Map<string, EventListenerEntry[]>()
|
|
9
|
+
|
|
10
|
+
addEventListener(
|
|
11
|
+
type: string,
|
|
12
|
+
listener: EventListenerOrEventListenerObject | null,
|
|
13
|
+
options?: boolean | AddEventListenerOptions,
|
|
14
|
+
): void {
|
|
15
|
+
if (!listener) return
|
|
16
|
+
|
|
17
|
+
const listenerOptions = this.normalizeOptions(options)
|
|
18
|
+
const entry: EventListenerEntry = {
|
|
19
|
+
listener,
|
|
20
|
+
options: listenerOptions,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (listenerOptions.signal) {
|
|
24
|
+
if (listenerOptions.signal.aborted) return
|
|
25
|
+
|
|
26
|
+
entry.signal = listenerOptions.signal
|
|
27
|
+
listenerOptions.signal.addEventListener('abort', () => {
|
|
28
|
+
this.removeEventListener(type, listener, options)
|
|
29
|
+
}, { once: true })
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!this.listeners.has(type)) {
|
|
33
|
+
this.listeners.set(type, [])
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.listeners.get(type)!.push(entry)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
removeEventListener(
|
|
40
|
+
type: string,
|
|
41
|
+
listener: EventListenerOrEventListenerObject | null,
|
|
42
|
+
options?: boolean | EventListenerOptions,
|
|
43
|
+
): void {
|
|
44
|
+
if (!listener) return
|
|
45
|
+
|
|
46
|
+
const entries = this.listeners.get(type)
|
|
47
|
+
if (!entries) return
|
|
48
|
+
|
|
49
|
+
const listenerOptions = this.normalizeOptions(options)
|
|
50
|
+
const index = entries.findIndex(entry =>
|
|
51
|
+
entry.listener === listener
|
|
52
|
+
&& this.optionsMatch(entry.options, listenerOptions))
|
|
53
|
+
|
|
54
|
+
if (index !== -1) {
|
|
55
|
+
entries.splice(index, 1)
|
|
56
|
+
if (entries.length === 0) {
|
|
57
|
+
this.listeners.delete(type)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
dispatchEvent(event: Event): boolean {
|
|
63
|
+
const entries = this.listeners.get(event.type)
|
|
64
|
+
if (!entries || entries.length === 0) return true
|
|
65
|
+
|
|
66
|
+
const entriesCopy = [...entries]
|
|
67
|
+
|
|
68
|
+
for (const entry of entriesCopy) {
|
|
69
|
+
if (entry.signal?.aborted) continue
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
if (typeof entry.listener === 'function') {
|
|
73
|
+
entry.listener.call(this, event)
|
|
74
|
+
} else {
|
|
75
|
+
entry.listener.handleEvent(event)
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
// In a real implementation, you might want to handle errors differently
|
|
79
|
+
console.error('Error in event listener:', error)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Remove if 'once' option was set
|
|
83
|
+
if (entry.options.once) {
|
|
84
|
+
this.removeEventListener(event.type, entry.listener, entry.options)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Stop if event propagation was stopped
|
|
88
|
+
if (event.cancelBubble) break
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return !event.defaultPrevented
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private normalizeOptions(
|
|
95
|
+
options?: boolean | AddEventListenerOptions | EventListenerOptions,
|
|
96
|
+
): AddEventListenerOptions {
|
|
97
|
+
if (typeof options === 'boolean') {
|
|
98
|
+
return { capture: options }
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
capture: false,
|
|
102
|
+
once: false,
|
|
103
|
+
passive: false,
|
|
104
|
+
...options,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private optionsMatch(a: AddEventListenerOptions, b: AddEventListenerOptions): boolean {
|
|
109
|
+
return a.capture === b.capture
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Utility methods for easier usage
|
|
113
|
+
on(
|
|
114
|
+
type: string,
|
|
115
|
+
listener: EventListenerOrEventListenerObject,
|
|
116
|
+
options?: boolean | AddEventListenerOptions,
|
|
117
|
+
): void {
|
|
118
|
+
this.addEventListener(type, listener, options)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
off(
|
|
122
|
+
type: string,
|
|
123
|
+
listener: EventListenerOrEventListenerObject,
|
|
124
|
+
options?: boolean | EventListenerOptions,
|
|
125
|
+
): void {
|
|
126
|
+
this.removeEventListener(type, listener, options)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
once(
|
|
130
|
+
type: string,
|
|
131
|
+
listener: EventListenerOrEventListenerObject,
|
|
132
|
+
options?: boolean | AddEventListenerOptions,
|
|
133
|
+
): void {
|
|
134
|
+
const onceOptions = typeof options === 'boolean'
|
|
135
|
+
? {
|
|
136
|
+
capture: options,
|
|
137
|
+
once: true,
|
|
138
|
+
}
|
|
139
|
+
: {
|
|
140
|
+
...options,
|
|
141
|
+
once: true,
|
|
142
|
+
}
|
|
143
|
+
this.addEventListener(type, listener, onceOptions)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
emit(type: string, detail?: unknown): boolean {
|
|
147
|
+
const event = new CustomEvent(type, { detail })
|
|
148
|
+
return this.dispatchEvent(event)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
getEventTypes(): string[] {
|
|
152
|
+
return Array.from(this.listeners.keys())
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
removeAllListeners(type?: string): void {
|
|
156
|
+
if (type) {
|
|
157
|
+
this.listeners.delete(type)
|
|
158
|
+
} else {
|
|
159
|
+
this.listeners.clear()
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -15,7 +15,9 @@ export type OperationProp<
|
|
|
15
15
|
Operations,
|
|
16
16
|
OperationKey extends keyof Operations,
|
|
17
17
|
Prop extends string,
|
|
18
|
-
> = Operation<Operations, OperationKey> extends { [K in Prop]?: unknown }
|
|
18
|
+
> = Operation<Operations, OperationKey> extends { [K in Prop]?: unknown }
|
|
19
|
+
? Operation<Operations, OperationKey>[Prop]
|
|
20
|
+
: never
|
|
19
21
|
|
|
20
22
|
export type InferParameters<T extends string> = UrlParamsRecord<ExtractUrlFromOperation<T>>
|
|
21
23
|
|
|
@@ -37,4 +39,18 @@ export interface ShopwareClientOptions {
|
|
|
37
39
|
apiKey: string
|
|
38
40
|
includeSeoUrls?: boolean
|
|
39
41
|
language?: string
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* If true, the context token will be reflected in the response headers.
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
reflectContextToken?: boolean
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* If provided, it will be used as the context token.
|
|
51
|
+
* If reflectContextToken is true, this token will be used only until the first response
|
|
52
|
+
* sends a new context token.
|
|
53
|
+
* @default undefined
|
|
54
|
+
*/
|
|
55
|
+
contextToken?: string
|
|
40
56
|
}
|
package/eslint.config.mjs
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import js from '@eslint/js'
|
|
2
|
-
import pluginStylistic from '@stylistic/eslint-plugin'
|
|
3
|
-
import tseslint from 'typescript-eslint'
|
|
4
|
-
|
|
5
|
-
export default tseslint.config(
|
|
6
|
-
js.configs.recommended,
|
|
7
|
-
tseslint.configs.recommended,
|
|
8
|
-
pluginStylistic.configs['recommended'],
|
|
9
|
-
{
|
|
10
|
-
rules: {
|
|
11
|
-
// stylistic
|
|
12
|
-
// ********************
|
|
13
|
-
'@stylistic/newline-per-chained-call': [
|
|
14
|
-
'error',
|
|
15
|
-
{ ignoreChainWithDepth: 2 },
|
|
16
|
-
],
|
|
17
|
-
'@stylistic/nonblock-statement-body-position': 'error',
|
|
18
|
-
'@stylistic/one-var-declaration-per-line': 'error',
|
|
19
|
-
'@stylistic/operator-linebreak': [
|
|
20
|
-
'error',
|
|
21
|
-
'before',
|
|
22
|
-
{ overrides: { '=': 'none' } },
|
|
23
|
-
],
|
|
24
|
-
'@stylistic/function-paren-newline': ['error', 'multiline-arguments'],
|
|
25
|
-
'@stylistic/dot-location': ['error', 'property'],
|
|
26
|
-
'@stylistic/no-mixed-operators': ['error', { allowSamePrecedence: true }],
|
|
27
|
-
'@stylistic/brace-style': ['error', '1tbs', { allowSingleLine: true }],
|
|
28
|
-
'@stylistic/array-bracket-newline': ['error', { multiline: true }],
|
|
29
|
-
'@stylistic/array-element-newline': [
|
|
30
|
-
'error',
|
|
31
|
-
{
|
|
32
|
-
multiline: true,
|
|
33
|
-
consistent: true,
|
|
34
|
-
},
|
|
35
|
-
],
|
|
36
|
-
'@stylistic/max-len': [
|
|
37
|
-
'warn',
|
|
38
|
-
{
|
|
39
|
-
code: 150,
|
|
40
|
-
tabWidth: 2,
|
|
41
|
-
},
|
|
42
|
-
],
|
|
43
|
-
'@stylistic/object-curly-newline': [
|
|
44
|
-
'error',
|
|
45
|
-
{
|
|
46
|
-
multiline: true,
|
|
47
|
-
consistent: true,
|
|
48
|
-
},
|
|
49
|
-
],
|
|
50
|
-
'@stylistic/object-property-newline': [
|
|
51
|
-
'error',
|
|
52
|
-
{ allowAllPropertiesOnSameLine: false },
|
|
53
|
-
],
|
|
54
|
-
|
|
55
|
-
// common
|
|
56
|
-
// ********************
|
|
57
|
-
'no-console': ['error', { allow: ['warn', 'error'] }],
|
|
58
|
-
'prefer-destructuring': [
|
|
59
|
-
'error',
|
|
60
|
-
{
|
|
61
|
-
array: false,
|
|
62
|
-
object: true,
|
|
63
|
-
},
|
|
64
|
-
],
|
|
65
|
-
'no-unused-vars': 'off',
|
|
66
|
-
'eqeqeq': ['error', 'smart'],
|
|
67
|
-
'no-undef': 'off', // done by typescript
|
|
68
|
-
'@typescript-eslint/consistent-type-imports': 'error',
|
|
69
|
-
'@typescript-eslint/no-explicit-any': 'warn',
|
|
70
|
-
'@typescript-eslint/no-unused-vars': [
|
|
71
|
-
'error',
|
|
72
|
-
{
|
|
73
|
-
args: 'all',
|
|
74
|
-
argsIgnorePattern: '^_',
|
|
75
|
-
caughtErrors: 'all',
|
|
76
|
-
caughtErrorsIgnorePattern: '^_',
|
|
77
|
-
destructuredArrayIgnorePattern: '^_',
|
|
78
|
-
varsIgnorePattern: '^_',
|
|
79
|
-
ignoreRestSiblings: true,
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
)
|