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