@shopware/api-client 1.2.0 → 1.3.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.
@@ -43,72 +43,6 @@ export type Schemas = {
43
43
  entity?: "product";
44
44
  sorting?: string;
45
45
  };
46
- SwagPaypalVaultToken: {
47
- // TODO: [OpenAPI][SwagPaypalVaultToken] - add SwagPaypalVaultToken definition to schema
48
- /** Format: date-time */
49
- createdAt: string;
50
- customer?: components["schemas"]["Customer"];
51
- customerId: string;
52
- id?: string;
53
- identifier: string;
54
- mainMapping?: components["schemas"]["SwagPaypalVaultTokenMapping"];
55
- paymentMethod?: components["schemas"]["PaymentMethod"];
56
- paymentMethodId: string;
57
- /** Format: date-time */
58
- updatedAt?: string;
59
- };
60
46
  };
61
47
 
62
- export type operations = {
63
- "updateLineItem patch /checkout/cart/line-item": {
64
- contentType?: "application/json";
65
- accept?: "application/json";
66
- headers?: {
67
- /** Instructs Shopware to return the response in the given language. */
68
- "sw-language-id"?: string;
69
- };
70
- body: {
71
- // TODO: [OpenAPI][updateLineItem] - add proper request body type with required fields
72
- items: Array<{
73
- id: string;
74
- quantity: number;
75
- }>;
76
- };
77
- response: components["schemas"]["Cart"];
78
- responseCode: 200;
79
- };
80
- "readProduct post /product": {
81
- contentType?: "application/json";
82
- accept?: "application/json";
83
- headers?: {
84
- /** Instructs Shopware to return the response in the given language. */
85
- "sw-language-id"?: string;
86
- };
87
- body?: components["schemas"]["Criteria"];
88
- response: {
89
- elements: components["schemas"]["Product"][]; // TODO: [OpenAPI][readProduct]: add elements property as required
90
- } & components["schemas"]["EntitySearchResult"];
91
- responseCode: 200;
92
- };
93
- "readShippingMethod post /shipping-method": {
94
- contentType?: "application/json";
95
- accept?: "application/json";
96
- headers?: {
97
- /** Instructs Shopware to return the response in the given language. */
98
- "sw-language-id"?: string;
99
- };
100
- query?: {
101
- /** List only available shipping methods. This filters shipping methods methods which can not be used in the actual context because of their availability rule. */
102
- onlyAvailable?: boolean;
103
- };
104
- body?: components["schemas"]["Criteria"];
105
- response: {
106
- /** aggregation result */
107
- aggregations?: Record<string, never>;
108
- elements: components["schemas"]["ShippingMethod"][]; // TODO: [OpenAPI][readShippingMethod]: response should be `EntitySearchResult` and elements should be required
109
- /** Total amount */
110
- total?: number;
111
- };
112
- responseCode: 200;
113
- };
114
- };
48
+ // export type operations = {};
package/dist/index.cjs CHANGED
@@ -50,10 +50,7 @@ function createHeaders(init, hookCallback) {
50
50
 
51
51
  var __defProp = Object.defineProperty;
52
52
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
53
- var __publicField = (obj, key, value) => {
54
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
55
- return value;
56
- };
53
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
57
54
  class ApiClientError extends Error {
58
55
  constructor(response) {
59
56
  let message = "Failed request";
@@ -116,7 +113,7 @@ function errorInterceptor(response) {
116
113
  function createPathWithParams(requestPath, pathParams) {
117
114
  return Object.keys(pathParams || {}).reduce((acc, paramName) => {
118
115
  return acc.replace(`{${paramName}}`, pathParams[paramName]);
119
- }, requestPath);
116
+ }, requestPath || "");
120
117
  }
121
118
 
122
119
  function createAPIClient(params) {
@@ -135,25 +132,31 @@ function createAPIClient(params) {
135
132
  }
136
133
  }
137
134
  );
138
- const apiFetch = ofetch.ofetch.create({
139
- baseURL: params.baseURL,
140
- ...params.fetchOptions,
141
- // async onRequest({ request, options }) {},
142
- // async onRequestError({ request, options, error }) {},
143
- async onResponse(context) {
144
- apiClientHooks.callHook("onSuccessResponse", context.response);
145
- if (context.response.headers.has("sw-context-token") && defaultHeaders["sw-context-token"] !== context.response.headers.get("sw-context-token")) {
146
- const newContextToken = context.response.headers.get(
147
- "sw-context-token"
148
- );
149
- defaultHeaders["sw-context-token"] = newContextToken;
135
+ let currentBaseURL = params.baseURL;
136
+ let currentAccessToken = params.accessToken;
137
+ function createFetchClient(baseURL) {
138
+ return ofetch.ofetch.create({
139
+ baseURL,
140
+ ...params.fetchOptions,
141
+ async onRequest(context) {
142
+ apiClientHooks.callHook("onRequest", context);
143
+ },
144
+ async onResponse(context) {
145
+ apiClientHooks.callHook("onSuccessResponse", context.response);
146
+ if (context.response.headers.has("sw-context-token") && defaultHeaders["sw-context-token"] !== context.response.headers.get("sw-context-token")) {
147
+ const newContextToken = context.response.headers.get(
148
+ "sw-context-token"
149
+ );
150
+ defaultHeaders["sw-context-token"] = newContextToken;
151
+ }
152
+ },
153
+ async onResponseError({ response }) {
154
+ apiClientHooks.callHook("onResponseError", response);
155
+ errorInterceptor(response);
150
156
  }
151
- },
152
- async onResponseError({ response }) {
153
- apiClientHooks.callHook("onResponseError", response);
154
- errorInterceptor(response);
155
- }
156
- });
157
+ });
158
+ }
159
+ let apiFetch = createFetchClient(currentBaseURL);
157
160
  async function invoke(pathParam, ...params2) {
158
161
  const [, method, requestPath] = pathParam.split(" ");
159
162
  const currentParams = params2[0] || {};
@@ -164,9 +167,10 @@ function createAPIClient(params) {
164
167
  const fetchOptions = {
165
168
  ...currentParams.fetchOptions || {}
166
169
  };
167
- const mergedHeaders = defu__default(currentParams.headers, defaultHeaders);
170
+ let mergedHeaders = defu__default(currentParams.headers, defaultHeaders);
168
171
  if (mergedHeaders?.["Content-Type"]?.includes("multipart/form-data") && typeof window !== "undefined") {
169
- mergedHeaders["Content-Type"] = void 0;
172
+ const { "Content-Type": _, ...headersWithoutContentType } = mergedHeaders;
173
+ mergedHeaders = headersWithoutContentType;
170
174
  }
171
175
  const resp = await apiFetch.raw(requestPathWithParams, {
172
176
  ...fetchOptions,
@@ -186,15 +190,37 @@ function createAPIClient(params) {
186
190
  * Default headers used in every client request (if not overriden in specific request).
187
191
  */
188
192
  defaultHeaders,
189
- hook: apiClientHooks.hook
193
+ hook: apiClientHooks.hook,
194
+ /**
195
+ * Update the base configuration for API client
196
+ */
197
+ updateBaseConfig: (config) => {
198
+ let shouldRecreateClient = false;
199
+ if (config.baseURL !== void 0 && config.baseURL !== currentBaseURL) {
200
+ currentBaseURL = config.baseURL;
201
+ shouldRecreateClient = true;
202
+ }
203
+ if (config.accessToken !== void 0 && config.accessToken !== currentAccessToken) {
204
+ currentAccessToken = config.accessToken;
205
+ defaultHeaders["sw-access-key"] = config.accessToken;
206
+ }
207
+ if (shouldRecreateClient) {
208
+ apiFetch = createFetchClient(currentBaseURL);
209
+ }
210
+ },
211
+ /**
212
+ * Get the current base configuration
213
+ */
214
+ getBaseConfig: () => ({
215
+ baseURL: currentBaseURL,
216
+ accessToken: currentAccessToken
217
+ })
190
218
  };
191
219
  }
192
220
 
193
221
  function createAuthorizationHeader(token) {
194
- if (!token)
195
- return "";
196
- if (token.startsWith("Bearer "))
197
- return token;
222
+ if (!token) return "";
223
+ if (token.startsWith("Bearer ")) return token;
198
224
  return `Bearer ${token}`;
199
225
  }
200
226
  function createAdminAPIClient(params) {
@@ -261,8 +287,7 @@ function createAdminAPIClient(params) {
261
287
  errorInterceptor(response);
262
288
  },
263
289
  onResponse(context) {
264
- if (!context.response._data)
265
- return;
290
+ if (!context.response._data) return;
266
291
  updateSessionData(context.response._data);
267
292
  options.headers.set(
268
293
  "Authorization",