@shopware/api-client 1.0.2 → 1.1.1

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.cjs CHANGED
@@ -8,7 +8,7 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
8
8
 
9
9
  const defu__default = /*#__PURE__*/_interopDefaultCompat(defu);
10
10
 
11
- function createHeaders(init) {
11
+ function createHeaders(init, hookCallback) {
12
12
  const _headers = {
13
13
  "Content-Type": "application/json"
14
14
  };
@@ -23,7 +23,12 @@ function createHeaders(init) {
23
23
  if (prop === "apply") {
24
24
  throw new Error("Cannot override apply method");
25
25
  }
26
+ hookCallback?.(prop, value);
26
27
  return Reflect.set(target, prop, value);
28
+ },
29
+ deleteProperty: (target, prop) => {
30
+ hookCallback?.(prop);
31
+ return Reflect.deleteProperty(target, prop);
27
32
  }
28
33
  };
29
34
  const headersProxy = new Proxy(
@@ -115,13 +120,21 @@ function createPathWithParams(requestPath, pathParams) {
115
120
  }
116
121
 
117
122
  function createAPIClient(params) {
118
- const defaultHeaders = createHeaders({
119
- "sw-access-key": params.accessToken,
120
- Accept: "application/json",
121
- "sw-context-token": params.contextToken,
122
- ...params.defaultHeaders
123
- });
124
123
  const apiClientHooks = hookable.createHooks();
124
+ const defaultHeaders = createHeaders(
125
+ {
126
+ "sw-access-key": params.accessToken,
127
+ accept: "application/json",
128
+ "sw-context-token": params.contextToken,
129
+ ...params.defaultHeaders
130
+ },
131
+ (key, value) => {
132
+ apiClientHooks.callHook("onDefaultHeaderChanged", key, value);
133
+ if (key === "sw-context-token") {
134
+ apiClientHooks.callHook("onContextChanged", value);
135
+ }
136
+ }
137
+ );
125
138
  const apiFetch = ofetch.ofetch.create({
126
139
  baseURL: params.baseURL,
127
140
  // async onRequest({ request, options }) {},
@@ -133,7 +146,6 @@ function createAPIClient(params) {
133
146
  "sw-context-token"
134
147
  );
135
148
  defaultHeaders["sw-context-token"] = newContextToken;
136
- apiClientHooks.callHook("onContextChanged", newContextToken);
137
149
  }
138
150
  },
139
151
  async onResponseError({ response }) {
@@ -151,11 +163,15 @@ function createAPIClient(params) {
151
163
  const fetchOptions = {
152
164
  ...currentParams.fetchOptions || {}
153
165
  };
166
+ let mergedHeaders = defu__default(currentParams.headers, defaultHeaders);
167
+ if (mergedHeaders?.["Content-Type"]?.includes("multipart/form-data") && typeof window !== "undefined") {
168
+ delete mergedHeaders["Content-Type"];
169
+ }
154
170
  const resp = await apiFetch.raw(requestPathWithParams, {
155
171
  ...fetchOptions,
156
172
  method,
157
173
  body: currentParams.body,
158
- headers: defu__default(defaultHeaders, currentParams.headers),
174
+ headers: mergedHeaders,
159
175
  query: currentParams.query
160
176
  });
161
177
  return {
@@ -182,16 +198,21 @@ function createAuthorizationHeader(token) {
182
198
  }
183
199
  function createAdminAPIClient(params) {
184
200
  const isTokenBasedAuth = params.credentials?.grant_type === "client_credentials";
201
+ const apiClientHooks = hookable.createHooks();
185
202
  const sessionData = {
186
203
  accessToken: params.sessionData?.accessToken || "",
187
204
  refreshToken: params.sessionData?.refreshToken || "",
188
205
  expirationTime: Number(params.sessionData?.expirationTime || 0)
189
206
  };
190
- const defaultHeaders = createHeaders({
191
- Authorization: createAuthorizationHeader(sessionData.accessToken),
192
- Accept: "application/json"
193
- });
194
- const apiClientHooks = hookable.createHooks();
207
+ const defaultHeaders = createHeaders(
208
+ {
209
+ Authorization: createAuthorizationHeader(sessionData.accessToken),
210
+ Accept: "application/json"
211
+ },
212
+ (key, value) => {
213
+ apiClientHooks.callHook("onDefaultHeaderChanged", key, value);
214
+ }
215
+ );
195
216
  function getSessionData() {
196
217
  return { ...sessionData };
197
218
  }
@@ -241,12 +262,10 @@ function createAdminAPIClient(params) {
241
262
  if (!context.response._data)
242
263
  return;
243
264
  updateSessionData(context.response._data);
244
- options.headers = {
245
- ...options.headers,
246
- Authorization: createAuthorizationHeader(
247
- context.response._data.access_token
248
- )
249
- };
265
+ options.headers.append(
266
+ "Authorization",
267
+ createAuthorizationHeader(sessionData.accessToken)
268
+ );
250
269
  }
251
270
  });
252
271
  }
@@ -262,19 +281,20 @@ function createAdminAPIClient(params) {
262
281
  });
263
282
  async function invoke(pathParam, ...params2) {
264
283
  const [, method, requestPath] = pathParam.split(" ");
284
+ const currentParams = params2[0] || {};
265
285
  const requestPathWithParams = createPathWithParams(
266
286
  requestPath,
267
- params2[0]?.pathParams
287
+ currentParams.pathParams
268
288
  );
269
289
  const fetchOptions = {
270
- ...params2[0]?.fetchOptions || {}
290
+ ...currentParams.fetchOptions || {}
271
291
  };
272
292
  const resp = await apiFetch.raw(requestPathWithParams, {
273
293
  ...fetchOptions,
274
294
  method,
275
- body: params2[0]?.body,
276
- headers: defu__default(defaultHeaders, params2[0]?.headers),
277
- query: params2[0]?.query
295
+ body: currentParams.body,
296
+ headers: defu__default(currentParams.headers, defaultHeaders),
297
+ query: currentParams.query
278
298
  });
279
299
  return {
280
300
  data: resp._data,