@shopware/api-client 1.0.2 → 1.1.0

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/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { ofetch } from 'ofetch';
2
2
  import { createHooks } from 'hookable';
3
3
  import defu from 'defu';
4
4
 
5
- function createHeaders(init) {
5
+ function createHeaders(init, hookCallback) {
6
6
  const _headers = {
7
7
  "Content-Type": "application/json"
8
8
  };
@@ -17,7 +17,12 @@ function createHeaders(init) {
17
17
  if (prop === "apply") {
18
18
  throw new Error("Cannot override apply method");
19
19
  }
20
+ hookCallback?.(prop, value);
20
21
  return Reflect.set(target, prop, value);
22
+ },
23
+ deleteProperty: (target, prop) => {
24
+ hookCallback?.(prop);
25
+ return Reflect.deleteProperty(target, prop);
21
26
  }
22
27
  };
23
28
  const headersProxy = new Proxy(
@@ -109,13 +114,21 @@ function createPathWithParams(requestPath, pathParams) {
109
114
  }
110
115
 
111
116
  function createAPIClient(params) {
112
- const defaultHeaders = createHeaders({
113
- "sw-access-key": params.accessToken,
114
- Accept: "application/json",
115
- "sw-context-token": params.contextToken,
116
- ...params.defaultHeaders
117
- });
118
117
  const apiClientHooks = createHooks();
118
+ const defaultHeaders = createHeaders(
119
+ {
120
+ "sw-access-key": params.accessToken,
121
+ accept: "application/json",
122
+ "sw-context-token": params.contextToken,
123
+ ...params.defaultHeaders
124
+ },
125
+ (key, value) => {
126
+ apiClientHooks.callHook("onDefaultHeaderChanged", key, value);
127
+ if (key === "sw-context-token") {
128
+ apiClientHooks.callHook("onContextChanged", value);
129
+ }
130
+ }
131
+ );
119
132
  const apiFetch = ofetch.create({
120
133
  baseURL: params.baseURL,
121
134
  // async onRequest({ request, options }) {},
@@ -127,7 +140,6 @@ function createAPIClient(params) {
127
140
  "sw-context-token"
128
141
  );
129
142
  defaultHeaders["sw-context-token"] = newContextToken;
130
- apiClientHooks.callHook("onContextChanged", newContextToken);
131
143
  }
132
144
  },
133
145
  async onResponseError({ response }) {
@@ -145,11 +157,15 @@ function createAPIClient(params) {
145
157
  const fetchOptions = {
146
158
  ...currentParams.fetchOptions || {}
147
159
  };
160
+ let mergedHeaders = defu(currentParams.headers, defaultHeaders);
161
+ if (mergedHeaders?.["Content-Type"]?.includes("multipart/form-data") && typeof window !== "undefined") {
162
+ delete mergedHeaders["Content-Type"];
163
+ }
148
164
  const resp = await apiFetch.raw(requestPathWithParams, {
149
165
  ...fetchOptions,
150
166
  method,
151
167
  body: currentParams.body,
152
- headers: defu(defaultHeaders, currentParams.headers),
168
+ headers: mergedHeaders,
153
169
  query: currentParams.query
154
170
  });
155
171
  return {
@@ -176,16 +192,21 @@ function createAuthorizationHeader(token) {
176
192
  }
177
193
  function createAdminAPIClient(params) {
178
194
  const isTokenBasedAuth = params.credentials?.grant_type === "client_credentials";
195
+ const apiClientHooks = createHooks();
179
196
  const sessionData = {
180
197
  accessToken: params.sessionData?.accessToken || "",
181
198
  refreshToken: params.sessionData?.refreshToken || "",
182
199
  expirationTime: Number(params.sessionData?.expirationTime || 0)
183
200
  };
184
- const defaultHeaders = createHeaders({
185
- Authorization: createAuthorizationHeader(sessionData.accessToken),
186
- Accept: "application/json"
187
- });
188
- const apiClientHooks = createHooks();
201
+ const defaultHeaders = createHeaders(
202
+ {
203
+ Authorization: createAuthorizationHeader(sessionData.accessToken),
204
+ Accept: "application/json"
205
+ },
206
+ (key, value) => {
207
+ apiClientHooks.callHook("onDefaultHeaderChanged", key, value);
208
+ }
209
+ );
189
210
  function getSessionData() {
190
211
  return { ...sessionData };
191
212
  }
@@ -235,12 +256,10 @@ function createAdminAPIClient(params) {
235
256
  if (!context.response._data)
236
257
  return;
237
258
  updateSessionData(context.response._data);
238
- options.headers = {
239
- ...options.headers,
240
- Authorization: createAuthorizationHeader(
241
- context.response._data.access_token
242
- )
243
- };
259
+ options.headers.append(
260
+ "Authorization",
261
+ createAuthorizationHeader(sessionData.accessToken)
262
+ );
244
263
  }
245
264
  });
246
265
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopware/api-client",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Shopware client for API connection.",
5
5
  "author": "Shopware",
6
6
  "type": "module",
@@ -44,17 +44,18 @@
44
44
  "devDependencies": {
45
45
  "@codspeed/vitest-plugin": "3.1.1",
46
46
  "@types/prettier": "3.0.0",
47
- "@vitest/coverage-v8": "2.0.4",
47
+ "@vitest/coverage-v8": "2.1.4",
48
+ "jsdom": "^25.0.1",
48
49
  "prettier": "3.3.3",
49
50
  "unbuild": "2.0.0",
50
- "vitest": "2.0.4",
51
+ "vitest": "2.1.4",
51
52
  "eslint-config-shopware": "1.0.0",
52
53
  "tsconfig": "0.0.0"
53
54
  },
54
55
  "dependencies": {
55
56
  "defu": "6.1.4",
56
57
  "hookable": "5.5.3",
57
- "ofetch": "1.3.4"
58
+ "ofetch": "1.4.1"
58
59
  },
59
60
  "scripts": {
60
61
  "build": "export NODE_ENV=production && unbuild && pnpm build:types",