@commercetools-frontend/sdk 22.2.1 → 22.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.
- package/dist/commercetools-frontend-sdk.cjs.dev.js +40 -102
- package/dist/commercetools-frontend-sdk.cjs.prod.js +35 -91
- package/dist/commercetools-frontend-sdk.esm.js +40 -102
- package/package.json +23 -6
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.cjs.dev.js +13 -15
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.cjs.prod.js +13 -15
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.esm.js +13 -15
|
@@ -57,7 +57,6 @@ var _globalThis__default = /*#__PURE__*/_interopDefault(_globalThis);
|
|
|
57
57
|
var createHttpUserAgent__default = /*#__PURE__*/_interopDefault(createHttpUserAgent);
|
|
58
58
|
|
|
59
59
|
function ownKeys$2(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); enumerableOnly && (symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
60
|
-
|
|
61
60
|
function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var _context2, _context3; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty__default["default"](_context2 = ownKeys$2(Object(source), !0)).call(_context2, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)) : _forEachInstanceProperty__default["default"](_context3 = ownKeys$2(Object(source))).call(_context3, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } return target; }
|
|
62
61
|
function get(payload) {
|
|
63
62
|
return {
|
|
@@ -66,9 +65,10 @@ function get(payload) {
|
|
|
66
65
|
method: 'GET'
|
|
67
66
|
})
|
|
68
67
|
};
|
|
69
|
-
}
|
|
70
|
-
// because `delete` is a reserved keyword in ECMAScript
|
|
68
|
+
}
|
|
71
69
|
|
|
70
|
+
// contrary to the other methods this does not bear the exact name of the HTTP-verb
|
|
71
|
+
// because `delete` is a reserved keyword in ECMAScript
|
|
72
72
|
function del(payload) {
|
|
73
73
|
return {
|
|
74
74
|
type: 'SDK',
|
|
@@ -93,25 +93,20 @@ function post(payload) {
|
|
|
93
93
|
})
|
|
94
94
|
};
|
|
95
95
|
}
|
|
96
|
-
|
|
97
96
|
const enhancePayloadForForwardToProxy = payload => {
|
|
98
97
|
var _payload$headers, _context;
|
|
99
|
-
|
|
100
98
|
const headers = (_payload$headers = payload.headers) !== null && _payload$headers !== void 0 ? _payload$headers : {};
|
|
101
99
|
const exchangeTokenClaims = [];
|
|
102
|
-
|
|
103
100
|
if (payload.includeUserPermissions) {
|
|
104
101
|
exchangeTokenClaims.push('permissions');
|
|
105
102
|
}
|
|
106
|
-
|
|
107
103
|
return {
|
|
108
104
|
uri: '/proxy/forward-to',
|
|
109
105
|
mcApiProxyTarget: undefined,
|
|
110
106
|
headers: omitEmpty__default["default"](_objectSpread$2(_objectSpread$2({}, _reduceInstanceProperty__default["default"](_context = _Object$entries__default["default"](headers)).call(_context, (customForwardHeaders, _ref) => {
|
|
111
107
|
let _ref2 = _slicedToArray(_ref, 2),
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
headerName = _ref2[0],
|
|
109
|
+
headerValue = _ref2[1];
|
|
115
110
|
return _objectSpread$2(_objectSpread$2({}, customForwardHeaders), {}, {
|
|
116
111
|
// Prefix headers so that the MC API can allow and forward them.
|
|
117
112
|
["x-forward-header-".concat(headerName)]: headerValue
|
|
@@ -124,7 +119,6 @@ const enhancePayloadForForwardToProxy = payload => {
|
|
|
124
119
|
}))
|
|
125
120
|
};
|
|
126
121
|
};
|
|
127
|
-
|
|
128
122
|
const forwardTo = {
|
|
129
123
|
get(payload) {
|
|
130
124
|
return {
|
|
@@ -134,7 +128,6 @@ const forwardTo = {
|
|
|
134
128
|
}, enhancePayloadForForwardToProxy(payload))
|
|
135
129
|
};
|
|
136
130
|
},
|
|
137
|
-
|
|
138
131
|
del(payload) {
|
|
139
132
|
return {
|
|
140
133
|
type: 'SDK',
|
|
@@ -143,7 +136,6 @@ const forwardTo = {
|
|
|
143
136
|
}, enhancePayloadForForwardToProxy(payload))
|
|
144
137
|
};
|
|
145
138
|
},
|
|
146
|
-
|
|
147
139
|
head(payload) {
|
|
148
140
|
return {
|
|
149
141
|
type: 'SDK',
|
|
@@ -152,7 +144,6 @@ const forwardTo = {
|
|
|
152
144
|
}, enhancePayloadForForwardToProxy(payload))
|
|
153
145
|
};
|
|
154
146
|
},
|
|
155
|
-
|
|
156
147
|
post(payload) {
|
|
157
148
|
return {
|
|
158
149
|
type: 'SDK',
|
|
@@ -161,7 +152,6 @@ const forwardTo = {
|
|
|
161
152
|
}, enhancePayloadForForwardToProxy(payload))
|
|
162
153
|
};
|
|
163
154
|
}
|
|
164
|
-
|
|
165
155
|
};
|
|
166
156
|
|
|
167
157
|
var index = /*#__PURE__*/Object.freeze({
|
|
@@ -174,22 +164,16 @@ var index = /*#__PURE__*/Object.freeze({
|
|
|
174
164
|
});
|
|
175
165
|
|
|
176
166
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = _Reflect$construct__default["default"](Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
177
|
-
|
|
178
167
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct__default["default"]) return false; if (_Reflect$construct__default["default"].sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct__default["default"](Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
179
168
|
let SdkGet = /*#__PURE__*/function (_Component) {
|
|
180
169
|
_inherits(SdkGet, _Component);
|
|
181
|
-
|
|
182
170
|
var _super = _createSuper(SdkGet);
|
|
183
|
-
|
|
184
171
|
function SdkGet() {
|
|
185
172
|
var _this;
|
|
186
|
-
|
|
187
173
|
_classCallCheck(this, SdkGet);
|
|
188
|
-
|
|
189
174
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
190
175
|
args[_key] = arguments[_key];
|
|
191
176
|
}
|
|
192
|
-
|
|
193
177
|
_this = _super.call(this, ...args);
|
|
194
178
|
_this.state = {
|
|
195
179
|
// We want the component to have a loading state by default, so we
|
|
@@ -202,25 +186,20 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
202
186
|
error: undefined
|
|
203
187
|
};
|
|
204
188
|
_this.isComponentMounted = false;
|
|
205
|
-
|
|
206
189
|
_this.changeRequestsInFlight = delta => {
|
|
207
190
|
if (_this.isComponentMounted) _this.setState(prevState => ({
|
|
208
191
|
requestsInFlight: prevState.requestsInFlight + delta
|
|
209
192
|
}));
|
|
210
193
|
};
|
|
211
|
-
|
|
212
194
|
_this.fetch = _ref => {
|
|
213
195
|
let dispatch = _ref.dispatch,
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
196
|
+
actionCreator = _ref.actionCreator,
|
|
197
|
+
actionCreatorArgs = _ref.actionCreatorArgs,
|
|
198
|
+
onSuccess = _ref.onSuccess,
|
|
199
|
+
onError = _ref.onError;
|
|
219
200
|
_this.changeRequestsInFlight(1);
|
|
220
|
-
|
|
221
201
|
return dispatch(actionCreator(...actionCreatorArgs)).then(result => {
|
|
222
202
|
_this.changeRequestsInFlight(-1);
|
|
223
|
-
|
|
224
203
|
if (_this.isComponentMounted) _this.setState({
|
|
225
204
|
error: undefined,
|
|
226
205
|
result
|
|
@@ -229,7 +208,6 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
229
208
|
return result;
|
|
230
209
|
}, error => {
|
|
231
210
|
_this.changeRequestsInFlight(-1);
|
|
232
|
-
|
|
233
211
|
if (_this.isComponentMounted) _this.setState({
|
|
234
212
|
error,
|
|
235
213
|
result: undefined
|
|
@@ -237,7 +215,6 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
237
215
|
if (onError) onError(error);else SdkGet.errorHandler(error);
|
|
238
216
|
});
|
|
239
217
|
};
|
|
240
|
-
|
|
241
218
|
_this.refresh = () => _this.fetch({
|
|
242
219
|
dispatch: _this.props.dispatch,
|
|
243
220
|
actionCreator: _this.props.actionCreator,
|
|
@@ -245,10 +222,8 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
245
222
|
onSuccess: _this.props.onSuccess,
|
|
246
223
|
onError: _this.props.onError
|
|
247
224
|
});
|
|
248
|
-
|
|
249
225
|
return _this;
|
|
250
226
|
}
|
|
251
|
-
|
|
252
227
|
_createClass(SdkGet, [{
|
|
253
228
|
key: "componentDidMount",
|
|
254
229
|
value: function componentDidMount() {
|
|
@@ -298,72 +273,60 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
298
273
|
});
|
|
299
274
|
}
|
|
300
275
|
}]);
|
|
301
|
-
|
|
302
276
|
return SdkGet;
|
|
303
277
|
}(react.Component);
|
|
304
278
|
SdkGet.displayName = 'SdkGet';
|
|
305
|
-
|
|
306
279
|
SdkGet.errorHandler = error => {
|
|
307
280
|
throw error;
|
|
308
281
|
};
|
|
309
|
-
|
|
310
282
|
SdkGet.defaultProps = {
|
|
311
283
|
actionCreatorArgs: [],
|
|
312
284
|
shouldRefetch: (prevArgs, nextArgs) => !fastEquals.deepEqual(prevArgs, nextArgs)
|
|
313
285
|
};
|
|
314
286
|
SdkGet.propTypes = {};
|
|
315
|
-
|
|
316
287
|
const mapDispatchToProps = dispatch => ({
|
|
317
288
|
dispatch
|
|
318
289
|
});
|
|
319
|
-
|
|
320
290
|
var SdkGet$1 = reactRedux.connect(null, mapDispatchToProps)(SdkGet);
|
|
321
291
|
|
|
322
292
|
// NOTE: This string will be replaced on build time with the package version.
|
|
323
|
-
var version = "22.
|
|
293
|
+
var version = "22.3.0";
|
|
324
294
|
|
|
325
295
|
const mcHostnameRegex = /^mc(-(\d){4,})?\.(.*)$/;
|
|
326
296
|
const mcPreviewHostnameRegex = /^.*\.mc-preview\.(.*)$/;
|
|
327
|
-
|
|
328
297
|
const getMcOriginTld = host => {
|
|
329
298
|
if (host.match(mcPreviewHostnameRegex)) {
|
|
330
299
|
return host.replace(mcPreviewHostnameRegex, '$1');
|
|
331
300
|
}
|
|
332
|
-
|
|
333
301
|
return host.replace(mcHostnameRegex, '$3');
|
|
334
302
|
};
|
|
335
|
-
|
|
336
303
|
const getMcApiUrlFromOrigin = actualWindow => {
|
|
337
304
|
var _context2;
|
|
338
|
-
|
|
339
305
|
const url = new _URL__default["default"](actualWindow.origin);
|
|
340
306
|
const originTld = getMcOriginTld(url.host);
|
|
341
307
|
return _concatInstanceProperty__default["default"](_context2 = "".concat(url.protocol, "//mc-api.")).call(_context2, originTld);
|
|
342
308
|
};
|
|
343
|
-
|
|
344
309
|
const parseAsBoolean = value => value === true || value === 'true';
|
|
345
|
-
|
|
346
310
|
function getMcApiUrl() {
|
|
347
311
|
let actualWindow = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
|
|
348
312
|
const isServedByProxy = parseAsBoolean(actualWindow.app.servedByProxy);
|
|
313
|
+
|
|
349
314
|
/**
|
|
350
315
|
* Prefer using the origin URL for the MC API based on the origin value
|
|
351
316
|
* of the browser's `window.location`.
|
|
352
317
|
* This ensures that the application always uses the correct URL associated
|
|
353
318
|
* with that environment, instead of relying on the config value.
|
|
354
319
|
*/
|
|
355
|
-
|
|
356
320
|
if (isServedByProxy) {
|
|
357
321
|
return getMcApiUrlFromOrigin(actualWindow);
|
|
358
322
|
}
|
|
359
|
-
|
|
360
323
|
return actualWindow.app.mcApiUrl;
|
|
361
324
|
}
|
|
362
325
|
|
|
363
326
|
function ownKeys$1(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); enumerableOnly && (symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
364
|
-
|
|
365
327
|
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var _context, _context2; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty__default["default"](_context = ownKeys$1(Object(source), !0)).call(_context, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)) : _forEachInstanceProperty__default["default"](_context2 = ownKeys$1(Object(source))).call(_context2, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } return target; }
|
|
366
328
|
|
|
329
|
+
// This is currently required by @commercetools/sdk-middleware-http package
|
|
367
330
|
_globalThis__default["default"].Buffer = buffer.Buffer;
|
|
368
331
|
const userAgent = createHttpUserAgent__default["default"]({
|
|
369
332
|
name: '@commercetools/sdk-client',
|
|
@@ -373,33 +336,29 @@ const userAgent = createHttpUserAgent__default["default"]({
|
|
|
373
336
|
// points to the appkit repo issues
|
|
374
337
|
contactEmail: 'mc@commercetools.com'
|
|
375
338
|
});
|
|
376
|
-
|
|
377
339
|
const customUserAgentMiddleware = next => (request, response) => {
|
|
378
340
|
const requestWithCustomUserAgent = _objectSpread$1(_objectSpread$1({}, request), {}, {
|
|
379
341
|
headers: _objectSpread$1(_objectSpread$1({}, request.headers), {}, {
|
|
380
342
|
'X-User-Agent': userAgent
|
|
381
343
|
})
|
|
382
344
|
});
|
|
383
|
-
|
|
384
345
|
next(requestWithCustomUserAgent, response);
|
|
385
|
-
};
|
|
386
|
-
// the application
|
|
387
|
-
|
|
346
|
+
};
|
|
388
347
|
|
|
348
|
+
// NOTE we should not use these directly but rather have them passed in from
|
|
349
|
+
// the application
|
|
389
350
|
const httpMiddleware = sdkMiddlewareHttp.createHttpMiddleware({
|
|
390
351
|
host: getMcApiUrl(),
|
|
391
352
|
includeResponseHeaders: true,
|
|
392
353
|
credentialsMode: 'include',
|
|
393
354
|
fetch
|
|
394
355
|
});
|
|
395
|
-
|
|
396
356
|
const createCorrelationIdMiddleware = _ref => {
|
|
397
357
|
let getCorrelationId = _ref.getCorrelationId;
|
|
398
358
|
return sdkMiddlewareCorrelationId.createCorrelationIdMiddleware({
|
|
399
359
|
generate: getCorrelationId
|
|
400
360
|
});
|
|
401
361
|
};
|
|
402
|
-
|
|
403
362
|
const createClient = _ref2 => {
|
|
404
363
|
let getCorrelationId = _ref2.getCorrelationId;
|
|
405
364
|
return sdkClient.createClient({
|
|
@@ -410,31 +369,26 @@ const createClient = _ref2 => {
|
|
|
410
369
|
};
|
|
411
370
|
|
|
412
371
|
function ownKeys(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); enumerableOnly && (symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
413
|
-
|
|
414
372
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context4, _context5; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty__default["default"](_context4 = ownKeys(Object(source), !0)).call(_context4, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)) : _forEachInstanceProperty__default["default"](_context5 = ownKeys(Object(source))).call(_context5, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } return target; }
|
|
373
|
+
const isSdkActionForUri = actionPayload => actionPayload.uri !== undefined;
|
|
415
374
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
375
|
+
// https://github.com/commercetools/nodejs/blob/master/packages/api-request-builder/src/default-services.js#L200:L200
|
|
419
376
|
const ORDER_EDIT_SERVICE = 'orderEdits';
|
|
420
|
-
|
|
421
377
|
const actionToUri = (action, projectKey) => {
|
|
422
378
|
var _action$payload$optio;
|
|
379
|
+
if (isSdkActionForUri(action.payload)) return action.payload.uri;
|
|
423
380
|
|
|
424
|
-
|
|
425
|
-
|
|
381
|
+
// Validate that `projectKey` exists
|
|
426
382
|
if (!projectKey) {
|
|
427
383
|
var _context;
|
|
428
|
-
|
|
429
384
|
throw new Error(_concatInstanceProperty__default["default"](_context = "Expected projectKey to be defined for action service \"".concat(action.payload.service, "\" (method \"")).call(_context, action.payload.method, "\")"));
|
|
430
385
|
}
|
|
431
|
-
|
|
432
386
|
const requestBuilder = apiRequestBuilder.createRequestBuilder({
|
|
433
387
|
projectKey
|
|
434
|
-
});
|
|
388
|
+
});
|
|
389
|
+
// NOTE it's weird that we have to access this from the request builder.
|
|
435
390
|
// Shouldn't it just be a part of the object we parse?
|
|
436
391
|
// NOTE shouldn't requestBuilder be called requestUriBuilder instead?
|
|
437
|
-
|
|
438
392
|
const service = requestBuilder[action.payload.service];
|
|
439
393
|
if (action.payload.options) service.parse(action.payload.options);
|
|
440
394
|
return service.build({
|
|
@@ -445,33 +399,29 @@ const actionToUri = (action, projectKey) => {
|
|
|
445
399
|
// at this stage, the `projectKey` should be available already.
|
|
446
400
|
withProjectKey: true
|
|
447
401
|
});
|
|
448
|
-
};
|
|
449
|
-
|
|
402
|
+
};
|
|
450
403
|
|
|
404
|
+
// Force TS cast of generic action to TNotificationAction
|
|
451
405
|
const isSdkAction = action => action.type === 'SDK';
|
|
452
|
-
|
|
453
406
|
const isSdkError = error => error.statusCode !== undefined;
|
|
454
|
-
|
|
455
407
|
function createSdkMiddleware(_ref) {
|
|
456
408
|
let getCorrelationId = _ref.getCorrelationId,
|
|
457
|
-
|
|
458
|
-
|
|
409
|
+
getProjectKey = _ref.getProjectKey,
|
|
410
|
+
getAdditionalHeaders = _ref.getAdditionalHeaders;
|
|
459
411
|
const client = createClient({
|
|
460
412
|
getCorrelationId
|
|
461
413
|
});
|
|
462
|
-
|
|
463
414
|
const middleware = _ref2 => {
|
|
464
415
|
let dispatch = _ref2.dispatch;
|
|
465
416
|
return next => action => {
|
|
466
417
|
var _context2, _context3;
|
|
467
|
-
|
|
468
418
|
if (!isSdkAction(action)) {
|
|
469
419
|
return next(action);
|
|
470
420
|
}
|
|
471
|
-
|
|
472
421
|
const projectKey = getProjectKey();
|
|
422
|
+
const uri = _filterInstanceProperty__default["default"](_context2 = [action.payload.mcApiProxyTarget && "/proxy/".concat(action.payload.mcApiProxyTarget), actionToUri(action, projectKey)]).call(_context2, Boolean).join('');
|
|
473
423
|
|
|
474
|
-
|
|
424
|
+
// This `requestName` is never really used.
|
|
475
425
|
//
|
|
476
426
|
// We keep track of requests which are in progress in the `loading` state of
|
|
477
427
|
// the application. The `loading` state is an array of strings
|
|
@@ -482,29 +432,26 @@ function createSdkMiddleware(_ref) {
|
|
|
482
432
|
// loading: ['PRODUCTS_FETCHED', 'sdk.fetch(/product-projection-search)']
|
|
483
433
|
// than to debug
|
|
484
434
|
// loading: 2
|
|
435
|
+
const requestName = _concatInstanceProperty__default["default"](_context3 = "sdk.".concat(action.payload.method.toLowerCase(), "(")).call(_context3, uri, ")");
|
|
485
436
|
|
|
486
|
-
|
|
487
|
-
const requestName = _concatInstanceProperty__default["default"](_context3 = "sdk.".concat(action.payload.method.toLowerCase(), "(")).call(_context3, uri, ")"); // NOTE here the middleware is aware of the application
|
|
437
|
+
// NOTE here the middleware is aware of the application
|
|
488
438
|
// Instead we should probably convert to a middleware factory
|
|
489
439
|
// and provide hooks for `onFetch`, `onResult` and `onError
|
|
490
|
-
|
|
491
|
-
|
|
492
440
|
dispatch({
|
|
493
441
|
type: constants.SHOW_LOADING,
|
|
494
442
|
payload: requestName
|
|
495
|
-
});
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
// NOTE the promise returned by the client resolves to a custom format
|
|
496
446
|
// it will contain { statusCode, headers, body }
|
|
497
447
|
// NOTE This retry logic could be moved to an sdk client middleware,
|
|
498
448
|
// but the client's middleware system is not capable of that right now
|
|
499
449
|
// https://github.com/commercetools/merchant-center-frontend/pull/3304
|
|
500
450
|
// https://github.com/commercetools/nodejs/issues/390
|
|
501
|
-
|
|
502
451
|
const sendRequest = function () {
|
|
503
452
|
let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
504
|
-
|
|
505
|
-
|
|
453
|
+
shouldRenewToken = _ref3.shouldRenewToken;
|
|
506
454
|
const additionalHeaders = getAdditionalHeaders();
|
|
507
|
-
|
|
508
455
|
const headers = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
|
|
509
456
|
Accept: 'application/json'
|
|
510
457
|
}, action.payload.headers || {}), shouldRenewToken ? {
|
|
@@ -512,7 +459,6 @@ function createSdkMiddleware(_ref) {
|
|
|
512
459
|
} : {}), projectKey && {
|
|
513
460
|
'X-Project-Key': projectKey
|
|
514
461
|
}), additionalHeaders !== null && additionalHeaders !== void 0 ? additionalHeaders : {});
|
|
515
|
-
|
|
516
462
|
const body = action.payload.method === 'POST' ? action.payload.payload : undefined;
|
|
517
463
|
return client.execute({
|
|
518
464
|
uri,
|
|
@@ -525,7 +471,6 @@ function createSdkMiddleware(_ref) {
|
|
|
525
471
|
throw error;
|
|
526
472
|
});
|
|
527
473
|
};
|
|
528
|
-
|
|
529
474
|
return sendRequest().catch(error => {
|
|
530
475
|
// in case of 401 error, try again with a new token
|
|
531
476
|
// https://github.com/commercetools/merchant-center-backend/blob/master/docs/AUTHENTICATION.md#problems-due-to-oauth-token-caching
|
|
@@ -534,13 +479,13 @@ function createSdkMiddleware(_ref) {
|
|
|
534
479
|
shouldRenewToken: true
|
|
535
480
|
});
|
|
536
481
|
}
|
|
537
|
-
|
|
538
482
|
throw error;
|
|
539
483
|
}).then(result => {
|
|
540
484
|
dispatch({
|
|
541
485
|
type: constants.HIDE_LOADING,
|
|
542
486
|
payload: requestName
|
|
543
|
-
});
|
|
487
|
+
});
|
|
488
|
+
// The promise returned by "fetch" will reject when the request fails,
|
|
544
489
|
// but only in certain cases. See "Checking that the fetch was successful"
|
|
545
490
|
// in https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
|
546
491
|
// The SDK already handles this case for us.
|
|
@@ -555,7 +500,6 @@ function createSdkMiddleware(_ref) {
|
|
|
555
500
|
});
|
|
556
501
|
};
|
|
557
502
|
};
|
|
558
|
-
|
|
559
503
|
return middleware;
|
|
560
504
|
}
|
|
561
505
|
|