@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
|
@@ -34,7 +34,6 @@ import { createCorrelationIdMiddleware as createCorrelationIdMiddleware$1 } from
|
|
|
34
34
|
import { createHttpMiddleware } from '@commercetools/sdk-middleware-http';
|
|
35
35
|
|
|
36
36
|
function ownKeys$3(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
37
|
-
|
|
38
37
|
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(_context2 = ownKeys$3(Object(source), !0)).call(_context2, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context3 = ownKeys$3(Object(source))).call(_context3, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
39
38
|
function get(payload) {
|
|
40
39
|
return {
|
|
@@ -43,9 +42,10 @@ function get(payload) {
|
|
|
43
42
|
method: 'GET'
|
|
44
43
|
})
|
|
45
44
|
};
|
|
46
|
-
}
|
|
47
|
-
// because `delete` is a reserved keyword in ECMAScript
|
|
45
|
+
}
|
|
48
46
|
|
|
47
|
+
// contrary to the other methods this does not bear the exact name of the HTTP-verb
|
|
48
|
+
// because `delete` is a reserved keyword in ECMAScript
|
|
49
49
|
function del(payload) {
|
|
50
50
|
return {
|
|
51
51
|
type: 'SDK',
|
|
@@ -70,25 +70,20 @@ function post(payload) {
|
|
|
70
70
|
})
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
|
-
|
|
74
73
|
const enhancePayloadForForwardToProxy = payload => {
|
|
75
74
|
var _payload$headers, _context;
|
|
76
|
-
|
|
77
75
|
const headers = (_payload$headers = payload.headers) !== null && _payload$headers !== void 0 ? _payload$headers : {};
|
|
78
76
|
const exchangeTokenClaims = [];
|
|
79
|
-
|
|
80
77
|
if (payload.includeUserPermissions) {
|
|
81
78
|
exchangeTokenClaims.push('permissions');
|
|
82
79
|
}
|
|
83
|
-
|
|
84
80
|
return {
|
|
85
81
|
uri: '/proxy/forward-to',
|
|
86
82
|
mcApiProxyTarget: undefined,
|
|
87
83
|
headers: omitEmpty(_objectSpread$3(_objectSpread$3({}, _reduceInstanceProperty(_context = _Object$entries(headers)).call(_context, (customForwardHeaders, _ref) => {
|
|
88
84
|
let _ref2 = _slicedToArray(_ref, 2),
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
headerName = _ref2[0],
|
|
86
|
+
headerValue = _ref2[1];
|
|
92
87
|
return _objectSpread$3(_objectSpread$3({}, customForwardHeaders), {}, {
|
|
93
88
|
// Prefix headers so that the MC API can allow and forward them.
|
|
94
89
|
["x-forward-header-".concat(headerName)]: headerValue
|
|
@@ -101,7 +96,6 @@ const enhancePayloadForForwardToProxy = payload => {
|
|
|
101
96
|
}))
|
|
102
97
|
};
|
|
103
98
|
};
|
|
104
|
-
|
|
105
99
|
const forwardTo = {
|
|
106
100
|
get(payload) {
|
|
107
101
|
return {
|
|
@@ -111,7 +105,6 @@ const forwardTo = {
|
|
|
111
105
|
}, enhancePayloadForForwardToProxy(payload))
|
|
112
106
|
};
|
|
113
107
|
},
|
|
114
|
-
|
|
115
108
|
del(payload) {
|
|
116
109
|
return {
|
|
117
110
|
type: 'SDK',
|
|
@@ -120,7 +113,6 @@ const forwardTo = {
|
|
|
120
113
|
}, enhancePayloadForForwardToProxy(payload))
|
|
121
114
|
};
|
|
122
115
|
},
|
|
123
|
-
|
|
124
116
|
head(payload) {
|
|
125
117
|
return {
|
|
126
118
|
type: 'SDK',
|
|
@@ -129,7 +121,6 @@ const forwardTo = {
|
|
|
129
121
|
}, enhancePayloadForForwardToProxy(payload))
|
|
130
122
|
};
|
|
131
123
|
},
|
|
132
|
-
|
|
133
124
|
post(payload) {
|
|
134
125
|
return {
|
|
135
126
|
type: 'SDK',
|
|
@@ -138,7 +129,6 @@ const forwardTo = {
|
|
|
138
129
|
}, enhancePayloadForForwardToProxy(payload))
|
|
139
130
|
};
|
|
140
131
|
}
|
|
141
|
-
|
|
142
132
|
};
|
|
143
133
|
|
|
144
134
|
var index = /*#__PURE__*/Object.freeze({
|
|
@@ -151,22 +141,16 @@ var index = /*#__PURE__*/Object.freeze({
|
|
|
151
141
|
});
|
|
152
142
|
|
|
153
143
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
154
|
-
|
|
155
144
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
156
145
|
let SdkGet = /*#__PURE__*/function (_Component) {
|
|
157
146
|
_inherits(SdkGet, _Component);
|
|
158
|
-
|
|
159
147
|
var _super = _createSuper(SdkGet);
|
|
160
|
-
|
|
161
148
|
function SdkGet() {
|
|
162
149
|
var _this;
|
|
163
|
-
|
|
164
150
|
_classCallCheck(this, SdkGet);
|
|
165
|
-
|
|
166
151
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
167
152
|
args[_key] = arguments[_key];
|
|
168
153
|
}
|
|
169
|
-
|
|
170
154
|
_this = _super.call(this, ...args);
|
|
171
155
|
_this.state = {
|
|
172
156
|
// We want the component to have a loading state by default, so we
|
|
@@ -179,25 +163,20 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
179
163
|
error: undefined
|
|
180
164
|
};
|
|
181
165
|
_this.isComponentMounted = false;
|
|
182
|
-
|
|
183
166
|
_this.changeRequestsInFlight = delta => {
|
|
184
167
|
if (_this.isComponentMounted) _this.setState(prevState => ({
|
|
185
168
|
requestsInFlight: prevState.requestsInFlight + delta
|
|
186
169
|
}));
|
|
187
170
|
};
|
|
188
|
-
|
|
189
171
|
_this.fetch = _ref => {
|
|
190
172
|
let dispatch = _ref.dispatch,
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
173
|
+
actionCreator = _ref.actionCreator,
|
|
174
|
+
actionCreatorArgs = _ref.actionCreatorArgs,
|
|
175
|
+
onSuccess = _ref.onSuccess,
|
|
176
|
+
onError = _ref.onError;
|
|
196
177
|
_this.changeRequestsInFlight(1);
|
|
197
|
-
|
|
198
178
|
return dispatch(actionCreator(...actionCreatorArgs)).then(result => {
|
|
199
179
|
_this.changeRequestsInFlight(-1);
|
|
200
|
-
|
|
201
180
|
if (_this.isComponentMounted) _this.setState({
|
|
202
181
|
error: undefined,
|
|
203
182
|
result
|
|
@@ -206,7 +185,6 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
206
185
|
return result;
|
|
207
186
|
}, error => {
|
|
208
187
|
_this.changeRequestsInFlight(-1);
|
|
209
|
-
|
|
210
188
|
if (_this.isComponentMounted) _this.setState({
|
|
211
189
|
error,
|
|
212
190
|
result: undefined
|
|
@@ -214,7 +192,6 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
214
192
|
if (onError) onError(error);else SdkGet.errorHandler(error);
|
|
215
193
|
});
|
|
216
194
|
};
|
|
217
|
-
|
|
218
195
|
_this.refresh = () => _this.fetch({
|
|
219
196
|
dispatch: _this.props.dispatch,
|
|
220
197
|
actionCreator: _this.props.actionCreator,
|
|
@@ -222,10 +199,8 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
222
199
|
onSuccess: _this.props.onSuccess,
|
|
223
200
|
onError: _this.props.onError
|
|
224
201
|
});
|
|
225
|
-
|
|
226
202
|
return _this;
|
|
227
203
|
}
|
|
228
|
-
|
|
229
204
|
_createClass(SdkGet, [{
|
|
230
205
|
key: "componentDidMount",
|
|
231
206
|
value: function componentDidMount() {
|
|
@@ -275,15 +250,12 @@ let SdkGet = /*#__PURE__*/function (_Component) {
|
|
|
275
250
|
});
|
|
276
251
|
}
|
|
277
252
|
}]);
|
|
278
|
-
|
|
279
253
|
return SdkGet;
|
|
280
254
|
}(Component);
|
|
281
255
|
SdkGet.displayName = 'SdkGet';
|
|
282
|
-
|
|
283
256
|
SdkGet.errorHandler = error => {
|
|
284
257
|
throw error;
|
|
285
258
|
};
|
|
286
|
-
|
|
287
259
|
SdkGet.defaultProps = {
|
|
288
260
|
actionCreatorArgs: [],
|
|
289
261
|
shouldRefetch: (prevArgs, nextArgs) => !deepEqual(prevArgs, nextArgs)
|
|
@@ -297,17 +269,16 @@ SdkGet.propTypes = process.env.NODE_ENV !== "production" ? {
|
|
|
297
269
|
onError: _pt.func,
|
|
298
270
|
render: _pt.func.isRequired
|
|
299
271
|
} : {};
|
|
300
|
-
|
|
301
272
|
const mapDispatchToProps = dispatch => ({
|
|
302
273
|
dispatch
|
|
303
274
|
});
|
|
304
|
-
|
|
305
275
|
var SdkGet$1 = connect(null, mapDispatchToProps)(SdkGet);
|
|
306
276
|
|
|
307
277
|
// NOTE: This string will be replaced on build time with the package version.
|
|
308
|
-
var version = "22.
|
|
278
|
+
var version = "22.3.0";
|
|
309
279
|
|
|
310
280
|
/* eslint-disable no-console */
|
|
281
|
+
|
|
311
282
|
const isLoggerEnabled = () => {
|
|
312
283
|
if (process.env.DEBUG === 'true') return true;
|
|
313
284
|
if (process.env.NODE_ENV === 'development') return true;
|
|
@@ -315,7 +286,6 @@ const isLoggerEnabled = () => {
|
|
|
315
286
|
if (process.env.NODE_ENV === 'production' && queryParams.searchParams.get('debug') === 'true') return true;
|
|
316
287
|
return false;
|
|
317
288
|
};
|
|
318
|
-
|
|
319
289
|
const logger = {
|
|
320
290
|
groupCollapsed: function () {
|
|
321
291
|
return isLoggerEnabled() && console.groupCollapsed(...arguments);
|
|
@@ -336,7 +306,6 @@ const logger = {
|
|
|
336
306
|
};
|
|
337
307
|
|
|
338
308
|
function ownKeys$2(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
339
|
-
|
|
340
309
|
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(_context3 = ownKeys$2(Object(source), !0)).call(_context3, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context4 = ownKeys$2(Object(source))).call(_context4, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
341
310
|
const parseUri = uri => {
|
|
342
311
|
const parser = document.createElement('a');
|
|
@@ -346,21 +315,16 @@ const parseUri = uri => {
|
|
|
346
315
|
search: decode(parser.search.substring(1))
|
|
347
316
|
};
|
|
348
317
|
};
|
|
349
|
-
|
|
350
318
|
const isPostAction = action => action.payload.method === 'POST';
|
|
351
|
-
|
|
352
319
|
const logRequest = _ref => {
|
|
353
320
|
var _context;
|
|
354
|
-
|
|
355
321
|
let method = _ref.method,
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
322
|
+
request = _ref.request,
|
|
323
|
+
response = _ref.response,
|
|
324
|
+
error = _ref.error,
|
|
325
|
+
action = _ref.action;
|
|
360
326
|
const uriParts = parseUri(request.uri);
|
|
361
|
-
|
|
362
327
|
const groupName = _concatInstanceProperty(_context = "%c".concat(method, " %c")).call(_context, uriParts.pathname);
|
|
363
|
-
|
|
364
328
|
logger.groupCollapsed(groupName, "color: ".concat(error ? 'red' : 'black', "; font-weight: bold;"), 'color: gray; font-weight: lighter;');
|
|
365
329
|
logger.log('%caction', 'color: cadetblue; font-weight: bold;', action);
|
|
366
330
|
logger.log('%crequest', "color: cornflowerblue; font-weight: bold;", _objectSpread$2({
|
|
@@ -376,46 +340,39 @@ const logRequest = _ref => {
|
|
|
376
340
|
};
|
|
377
341
|
const mcHostnameRegex = /^mc(-(\d){4,})?\.(.*)$/;
|
|
378
342
|
const mcPreviewHostnameRegex = /^.*\.mc-preview\.(.*)$/;
|
|
379
|
-
|
|
380
343
|
const getMcOriginTld = host => {
|
|
381
344
|
if (host.match(mcPreviewHostnameRegex)) {
|
|
382
345
|
return host.replace(mcPreviewHostnameRegex, '$1');
|
|
383
346
|
}
|
|
384
|
-
|
|
385
347
|
return host.replace(mcHostnameRegex, '$3');
|
|
386
348
|
};
|
|
387
|
-
|
|
388
349
|
const getMcApiUrlFromOrigin = actualWindow => {
|
|
389
350
|
var _context2;
|
|
390
|
-
|
|
391
351
|
const url = new _URL(actualWindow.origin);
|
|
392
352
|
const originTld = getMcOriginTld(url.host);
|
|
393
353
|
return _concatInstanceProperty(_context2 = "".concat(url.protocol, "//mc-api.")).call(_context2, originTld);
|
|
394
354
|
};
|
|
395
|
-
|
|
396
355
|
const parseAsBoolean = value => value === true || value === 'true';
|
|
397
|
-
|
|
398
356
|
function getMcApiUrl() {
|
|
399
357
|
let actualWindow = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
|
|
400
358
|
const isServedByProxy = parseAsBoolean(actualWindow.app.servedByProxy);
|
|
359
|
+
|
|
401
360
|
/**
|
|
402
361
|
* Prefer using the origin URL for the MC API based on the origin value
|
|
403
362
|
* of the browser's `window.location`.
|
|
404
363
|
* This ensures that the application always uses the correct URL associated
|
|
405
364
|
* with that environment, instead of relying on the config value.
|
|
406
365
|
*/
|
|
407
|
-
|
|
408
366
|
if (isServedByProxy) {
|
|
409
367
|
return getMcApiUrlFromOrigin(actualWindow);
|
|
410
368
|
}
|
|
411
|
-
|
|
412
369
|
return actualWindow.app.mcApiUrl;
|
|
413
370
|
}
|
|
414
371
|
|
|
415
372
|
function ownKeys$1(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
416
|
-
|
|
417
373
|
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(_context = ownKeys$1(Object(source), !0)).call(_context, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context2 = ownKeys$1(Object(source))).call(_context2, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
418
374
|
|
|
375
|
+
// This is currently required by @commercetools/sdk-middleware-http package
|
|
419
376
|
_globalThis.Buffer = Buffer;
|
|
420
377
|
const userAgent = createHttpUserAgent({
|
|
421
378
|
name: '@commercetools/sdk-client',
|
|
@@ -425,33 +382,29 @@ const userAgent = createHttpUserAgent({
|
|
|
425
382
|
// points to the appkit repo issues
|
|
426
383
|
contactEmail: 'mc@commercetools.com'
|
|
427
384
|
});
|
|
428
|
-
|
|
429
385
|
const customUserAgentMiddleware = next => (request, response) => {
|
|
430
386
|
const requestWithCustomUserAgent = _objectSpread$1(_objectSpread$1({}, request), {}, {
|
|
431
387
|
headers: _objectSpread$1(_objectSpread$1({}, request.headers), {}, {
|
|
432
388
|
'X-User-Agent': userAgent
|
|
433
389
|
})
|
|
434
390
|
});
|
|
435
|
-
|
|
436
391
|
next(requestWithCustomUserAgent, response);
|
|
437
|
-
};
|
|
438
|
-
// the application
|
|
439
|
-
|
|
392
|
+
};
|
|
440
393
|
|
|
394
|
+
// NOTE we should not use these directly but rather have them passed in from
|
|
395
|
+
// the application
|
|
441
396
|
const httpMiddleware = createHttpMiddleware({
|
|
442
397
|
host: getMcApiUrl(),
|
|
443
398
|
includeResponseHeaders: true,
|
|
444
399
|
credentialsMode: 'include',
|
|
445
400
|
fetch
|
|
446
401
|
});
|
|
447
|
-
|
|
448
402
|
const createCorrelationIdMiddleware = _ref => {
|
|
449
403
|
let getCorrelationId = _ref.getCorrelationId;
|
|
450
404
|
return createCorrelationIdMiddleware$1({
|
|
451
405
|
generate: getCorrelationId
|
|
452
406
|
});
|
|
453
407
|
};
|
|
454
|
-
|
|
455
408
|
const createClient = _ref2 => {
|
|
456
409
|
let getCorrelationId = _ref2.getCorrelationId;
|
|
457
410
|
return createClient$1({
|
|
@@ -462,31 +415,26 @@ const createClient = _ref2 => {
|
|
|
462
415
|
};
|
|
463
416
|
|
|
464
417
|
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
465
|
-
|
|
466
418
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context4, _context5; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context4 = ownKeys(Object(source), !0)).call(_context4, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context5 = ownKeys(Object(source))).call(_context5, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
419
|
+
const isSdkActionForUri = actionPayload => actionPayload.uri !== undefined;
|
|
467
420
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
421
|
+
// https://github.com/commercetools/nodejs/blob/master/packages/api-request-builder/src/default-services.js#L200:L200
|
|
471
422
|
const ORDER_EDIT_SERVICE = 'orderEdits';
|
|
472
|
-
|
|
473
423
|
const actionToUri = (action, projectKey) => {
|
|
474
424
|
var _action$payload$optio;
|
|
425
|
+
if (isSdkActionForUri(action.payload)) return action.payload.uri;
|
|
475
426
|
|
|
476
|
-
|
|
477
|
-
|
|
427
|
+
// Validate that `projectKey` exists
|
|
478
428
|
if (!projectKey) {
|
|
479
429
|
var _context;
|
|
480
|
-
|
|
481
430
|
throw new Error(_concatInstanceProperty(_context = "Expected projectKey to be defined for action service \"".concat(action.payload.service, "\" (method \"")).call(_context, action.payload.method, "\")"));
|
|
482
431
|
}
|
|
483
|
-
|
|
484
432
|
const requestBuilder = createRequestBuilder({
|
|
485
433
|
projectKey
|
|
486
|
-
});
|
|
434
|
+
});
|
|
435
|
+
// NOTE it's weird that we have to access this from the request builder.
|
|
487
436
|
// Shouldn't it just be a part of the object we parse?
|
|
488
437
|
// NOTE shouldn't requestBuilder be called requestUriBuilder instead?
|
|
489
|
-
|
|
490
438
|
const service = requestBuilder[action.payload.service];
|
|
491
439
|
if (action.payload.options) service.parse(action.payload.options);
|
|
492
440
|
return service.build({
|
|
@@ -497,33 +445,29 @@ const actionToUri = (action, projectKey) => {
|
|
|
497
445
|
// at this stage, the `projectKey` should be available already.
|
|
498
446
|
withProjectKey: true
|
|
499
447
|
});
|
|
500
|
-
};
|
|
501
|
-
|
|
448
|
+
};
|
|
502
449
|
|
|
450
|
+
// Force TS cast of generic action to TNotificationAction
|
|
503
451
|
const isSdkAction = action => action.type === 'SDK';
|
|
504
|
-
|
|
505
452
|
const isSdkError = error => error.statusCode !== undefined;
|
|
506
|
-
|
|
507
453
|
function createSdkMiddleware(_ref) {
|
|
508
454
|
let getCorrelationId = _ref.getCorrelationId,
|
|
509
|
-
|
|
510
|
-
|
|
455
|
+
getProjectKey = _ref.getProjectKey,
|
|
456
|
+
getAdditionalHeaders = _ref.getAdditionalHeaders;
|
|
511
457
|
const client = createClient({
|
|
512
458
|
getCorrelationId
|
|
513
459
|
});
|
|
514
|
-
|
|
515
460
|
const middleware = _ref2 => {
|
|
516
461
|
let dispatch = _ref2.dispatch;
|
|
517
462
|
return next => action => {
|
|
518
463
|
var _context2, _context3;
|
|
519
|
-
|
|
520
464
|
if (!isSdkAction(action)) {
|
|
521
465
|
return next(action);
|
|
522
466
|
}
|
|
523
|
-
|
|
524
467
|
const projectKey = getProjectKey();
|
|
468
|
+
const uri = _filterInstanceProperty(_context2 = [action.payload.mcApiProxyTarget && "/proxy/".concat(action.payload.mcApiProxyTarget), actionToUri(action, projectKey)]).call(_context2, Boolean).join('');
|
|
525
469
|
|
|
526
|
-
|
|
470
|
+
// This `requestName` is never really used.
|
|
527
471
|
//
|
|
528
472
|
// We keep track of requests which are in progress in the `loading` state of
|
|
529
473
|
// the application. The `loading` state is an array of strings
|
|
@@ -534,29 +478,26 @@ function createSdkMiddleware(_ref) {
|
|
|
534
478
|
// loading: ['PRODUCTS_FETCHED', 'sdk.fetch(/product-projection-search)']
|
|
535
479
|
// than to debug
|
|
536
480
|
// loading: 2
|
|
481
|
+
const requestName = _concatInstanceProperty(_context3 = "sdk.".concat(action.payload.method.toLowerCase(), "(")).call(_context3, uri, ")");
|
|
537
482
|
|
|
538
|
-
|
|
539
|
-
const requestName = _concatInstanceProperty(_context3 = "sdk.".concat(action.payload.method.toLowerCase(), "(")).call(_context3, uri, ")"); // NOTE here the middleware is aware of the application
|
|
483
|
+
// NOTE here the middleware is aware of the application
|
|
540
484
|
// Instead we should probably convert to a middleware factory
|
|
541
485
|
// and provide hooks for `onFetch`, `onResult` and `onError
|
|
542
|
-
|
|
543
|
-
|
|
544
486
|
dispatch({
|
|
545
487
|
type: SHOW_LOADING,
|
|
546
488
|
payload: requestName
|
|
547
|
-
});
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
// NOTE the promise returned by the client resolves to a custom format
|
|
548
492
|
// it will contain { statusCode, headers, body }
|
|
549
493
|
// NOTE This retry logic could be moved to an sdk client middleware,
|
|
550
494
|
// but the client's middleware system is not capable of that right now
|
|
551
495
|
// https://github.com/commercetools/merchant-center-frontend/pull/3304
|
|
552
496
|
// https://github.com/commercetools/nodejs/issues/390
|
|
553
|
-
|
|
554
497
|
const sendRequest = function () {
|
|
555
498
|
let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
556
|
-
|
|
557
|
-
|
|
499
|
+
shouldRenewToken = _ref3.shouldRenewToken;
|
|
558
500
|
const additionalHeaders = getAdditionalHeaders();
|
|
559
|
-
|
|
560
501
|
const headers = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
|
|
561
502
|
Accept: 'application/json'
|
|
562
503
|
}, action.payload.headers || {}), shouldRenewToken ? {
|
|
@@ -564,7 +505,6 @@ function createSdkMiddleware(_ref) {
|
|
|
564
505
|
} : {}), projectKey && {
|
|
565
506
|
'X-Project-Key': projectKey
|
|
566
507
|
}), additionalHeaders !== null && additionalHeaders !== void 0 ? additionalHeaders : {});
|
|
567
|
-
|
|
568
508
|
const body = action.payload.method === 'POST' ? action.payload.payload : undefined;
|
|
569
509
|
return client.execute({
|
|
570
510
|
uri,
|
|
@@ -595,7 +535,6 @@ function createSdkMiddleware(_ref) {
|
|
|
595
535
|
throw error;
|
|
596
536
|
});
|
|
597
537
|
};
|
|
598
|
-
|
|
599
538
|
return sendRequest().catch(error => {
|
|
600
539
|
// in case of 401 error, try again with a new token
|
|
601
540
|
// https://github.com/commercetools/merchant-center-backend/blob/master/docs/AUTHENTICATION.md#problems-due-to-oauth-token-caching
|
|
@@ -604,13 +543,13 @@ function createSdkMiddleware(_ref) {
|
|
|
604
543
|
shouldRenewToken: true
|
|
605
544
|
});
|
|
606
545
|
}
|
|
607
|
-
|
|
608
546
|
throw error;
|
|
609
547
|
}).then(result => {
|
|
610
548
|
dispatch({
|
|
611
549
|
type: HIDE_LOADING,
|
|
612
550
|
payload: requestName
|
|
613
|
-
});
|
|
551
|
+
});
|
|
552
|
+
// The promise returned by "fetch" will reject when the request fails,
|
|
614
553
|
// but only in certain cases. See "Checking that the fetch was successful"
|
|
615
554
|
// in https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
|
616
555
|
// The SDK already handles this case for us.
|
|
@@ -625,7 +564,6 @@ function createSdkMiddleware(_ref) {
|
|
|
625
564
|
});
|
|
626
565
|
};
|
|
627
566
|
};
|
|
628
|
-
|
|
629
567
|
return middleware;
|
|
630
568
|
}
|
|
631
569
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commercetools-frontend/sdk",
|
|
3
|
-
"version": "22.
|
|
3
|
+
"version": "22.3.0",
|
|
4
4
|
"description": "Tools for declarative fetching",
|
|
5
5
|
"bugs": "https://github.com/commercetools/merchant-center-application-kit/issues",
|
|
6
6
|
"repository": {
|
|
@@ -9,21 +9,35 @@
|
|
|
9
9
|
"directory": "packages/sdk"
|
|
10
10
|
},
|
|
11
11
|
"homepage": "https://docs.commercetools.com/custom-applications",
|
|
12
|
-
"keywords": [
|
|
12
|
+
"keywords": [
|
|
13
|
+
"javascript",
|
|
14
|
+
"frontend",
|
|
15
|
+
"react",
|
|
16
|
+
"toolkit"
|
|
17
|
+
],
|
|
13
18
|
"license": "MIT",
|
|
14
19
|
"publishConfig": {
|
|
15
20
|
"access": "public"
|
|
16
21
|
},
|
|
17
22
|
"preconstruct": {
|
|
18
|
-
"entrypoints": [
|
|
23
|
+
"entrypoints": [
|
|
24
|
+
"./index.ts",
|
|
25
|
+
"./test-utils/index.ts"
|
|
26
|
+
]
|
|
19
27
|
},
|
|
20
28
|
"main": "dist/commercetools-frontend-sdk.cjs.js",
|
|
21
29
|
"module": "dist/commercetools-frontend-sdk.esm.js",
|
|
22
|
-
"files": [
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"test-utils",
|
|
33
|
+
"package.json",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
23
37
|
"dependencies": {
|
|
24
38
|
"@babel/runtime": "^7.20.13",
|
|
25
39
|
"@babel/runtime-corejs3": "^7.20.13",
|
|
26
|
-
"@commercetools-frontend/constants": "22.
|
|
40
|
+
"@commercetools-frontend/constants": "22.3.0",
|
|
27
41
|
"@commercetools/api-request-builder": "6.0.0",
|
|
28
42
|
"@commercetools/http-user-agent": "3.0.0",
|
|
29
43
|
"@commercetools/sdk-client": "3.0.0",
|
|
@@ -42,9 +56,12 @@
|
|
|
42
56
|
"uuid": "9.0.0"
|
|
43
57
|
},
|
|
44
58
|
"devDependencies": {
|
|
59
|
+
"@emotion/react": "11.11.0",
|
|
45
60
|
"@testing-library/react": "12.1.5",
|
|
46
61
|
"@types/uuid": "^9.0.1",
|
|
62
|
+
"jest-mock": "29.5.0",
|
|
47
63
|
"react": "17.0.2",
|
|
64
|
+
"react-dom": "17.0.2",
|
|
48
65
|
"react-redux": "7.2.9",
|
|
49
66
|
"redux": "4.2.1",
|
|
50
67
|
"redux-thunk": "2.4.2"
|
|
@@ -54,4 +71,4 @@
|
|
|
54
71
|
"react-redux": "7.x",
|
|
55
72
|
"redux": "4.x"
|
|
56
73
|
}
|
|
57
|
-
}
|
|
74
|
+
}
|
|
@@ -20,49 +20,47 @@ var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInsta
|
|
|
20
20
|
var _spliceInstanceProperty__default = /*#__PURE__*/_interopDefault(_spliceInstanceProperty);
|
|
21
21
|
var _Promise__default = /*#__PURE__*/_interopDefault(_Promise);
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* START -->
|
|
25
|
+
* Remove once `@commercetools/sdk-client` exposes proper types.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/* <-- END */
|
|
29
|
+
|
|
23
30
|
const serialize = data => {
|
|
24
31
|
const undefinedPlaceholder = uuid.v4();
|
|
25
32
|
const placeholderRegexp = new RegExp("\"".concat(undefinedPlaceholder, "\""), 'g');
|
|
26
|
-
|
|
27
33
|
const mapUndefinedValues = (_k, v) => v === undefined ? undefinedPlaceholder : v;
|
|
28
|
-
|
|
29
34
|
const withPlaceholders = _JSON$stringify__default["default"](data, mapUndefinedValues, 2);
|
|
30
|
-
|
|
31
35
|
return withPlaceholders.replace(placeholderRegexp, 'undefined');
|
|
32
36
|
};
|
|
33
|
-
|
|
34
37
|
const throwIfNoMocksArePassed = mocks => {
|
|
35
38
|
if (!mocks || !_Array$isArray__default["default"](mocks) || mocks.length === 0) {
|
|
36
39
|
throw new Error('Missing or invalid argument for `mocks`. Expected an array of mocked actions.');
|
|
37
40
|
}
|
|
38
41
|
};
|
|
39
|
-
|
|
40
42
|
const isSdkAction = action => action.type === 'SDK';
|
|
41
|
-
|
|
42
43
|
const isSdkMockSuccess = mock => mock.response !== undefined;
|
|
43
|
-
|
|
44
44
|
const createTestMiddleware = mocks => {
|
|
45
|
-
throwIfNoMocksArePassed(mocks);
|
|
45
|
+
throwIfNoMocksArePassed(mocks);
|
|
46
|
+
|
|
47
|
+
// We clone the mocks so we can keep the user-provided mocks around for
|
|
46
48
|
// the debugging message. The mocksStack gets mutated, while mocks
|
|
47
49
|
// should never be mutated.
|
|
48
|
-
|
|
49
50
|
const mocksStack = [...mocks];
|
|
50
51
|
return () => next => action => {
|
|
51
52
|
var _context;
|
|
52
|
-
|
|
53
53
|
if (!isSdkAction(action)) {
|
|
54
54
|
return next(action);
|
|
55
55
|
}
|
|
56
|
-
|
|
57
56
|
const index = _findIndexInstanceProperty__default["default"](mocksStack).call(mocksStack, item => fastEquals.deepEqual(item.action, action));
|
|
58
|
-
|
|
59
57
|
if (index === -1) throw new Error(_concatInstanceProperty__default["default"](_context = "Could not find any more mocks for action ".concat(serialize(action), " in ")).call(_context, serialize(mocks)));
|
|
60
|
-
const mock = mocksStack[index];
|
|
58
|
+
const mock = mocksStack[index];
|
|
61
59
|
|
|
60
|
+
// Mocks should only be used once, so we remove it from the stack.
|
|
62
61
|
_spliceInstanceProperty__default["default"](mocksStack).call(mocksStack, index, 1);
|
|
63
|
-
|
|
64
62
|
return isSdkMockSuccess(mock) ? _Promise__default["default"].resolve(mock.response) : _Promise__default["default"].reject(mock.error);
|
|
65
63
|
};
|
|
66
|
-
};
|
|
64
|
+
};
|
|
67
65
|
|
|
68
66
|
exports.createTestMiddleware = createTestMiddleware;
|
|
@@ -20,49 +20,47 @@ var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInsta
|
|
|
20
20
|
var _spliceInstanceProperty__default = /*#__PURE__*/_interopDefault(_spliceInstanceProperty);
|
|
21
21
|
var _Promise__default = /*#__PURE__*/_interopDefault(_Promise);
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* START -->
|
|
25
|
+
* Remove once `@commercetools/sdk-client` exposes proper types.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/* <-- END */
|
|
29
|
+
|
|
23
30
|
const serialize = data => {
|
|
24
31
|
const undefinedPlaceholder = uuid.v4();
|
|
25
32
|
const placeholderRegexp = new RegExp("\"".concat(undefinedPlaceholder, "\""), 'g');
|
|
26
|
-
|
|
27
33
|
const mapUndefinedValues = (_k, v) => v === undefined ? undefinedPlaceholder : v;
|
|
28
|
-
|
|
29
34
|
const withPlaceholders = _JSON$stringify__default["default"](data, mapUndefinedValues, 2);
|
|
30
|
-
|
|
31
35
|
return withPlaceholders.replace(placeholderRegexp, 'undefined');
|
|
32
36
|
};
|
|
33
|
-
|
|
34
37
|
const throwIfNoMocksArePassed = mocks => {
|
|
35
38
|
if (!mocks || !_Array$isArray__default["default"](mocks) || mocks.length === 0) {
|
|
36
39
|
throw new Error('Missing or invalid argument for `mocks`. Expected an array of mocked actions.');
|
|
37
40
|
}
|
|
38
41
|
};
|
|
39
|
-
|
|
40
42
|
const isSdkAction = action => action.type === 'SDK';
|
|
41
|
-
|
|
42
43
|
const isSdkMockSuccess = mock => mock.response !== undefined;
|
|
43
|
-
|
|
44
44
|
const createTestMiddleware = mocks => {
|
|
45
|
-
throwIfNoMocksArePassed(mocks);
|
|
45
|
+
throwIfNoMocksArePassed(mocks);
|
|
46
|
+
|
|
47
|
+
// We clone the mocks so we can keep the user-provided mocks around for
|
|
46
48
|
// the debugging message. The mocksStack gets mutated, while mocks
|
|
47
49
|
// should never be mutated.
|
|
48
|
-
|
|
49
50
|
const mocksStack = [...mocks];
|
|
50
51
|
return () => next => action => {
|
|
51
52
|
var _context;
|
|
52
|
-
|
|
53
53
|
if (!isSdkAction(action)) {
|
|
54
54
|
return next(action);
|
|
55
55
|
}
|
|
56
|
-
|
|
57
56
|
const index = _findIndexInstanceProperty__default["default"](mocksStack).call(mocksStack, item => fastEquals.deepEqual(item.action, action));
|
|
58
|
-
|
|
59
57
|
if (index === -1) throw new Error(_concatInstanceProperty__default["default"](_context = "Could not find any more mocks for action ".concat(serialize(action), " in ")).call(_context, serialize(mocks)));
|
|
60
|
-
const mock = mocksStack[index];
|
|
58
|
+
const mock = mocksStack[index];
|
|
61
59
|
|
|
60
|
+
// Mocks should only be used once, so we remove it from the stack.
|
|
62
61
|
_spliceInstanceProperty__default["default"](mocksStack).call(mocksStack, index, 1);
|
|
63
|
-
|
|
64
62
|
return isSdkMockSuccess(mock) ? _Promise__default["default"].resolve(mock.response) : _Promise__default["default"].reject(mock.error);
|
|
65
63
|
};
|
|
66
|
-
};
|
|
64
|
+
};
|
|
67
65
|
|
|
68
66
|
exports.createTestMiddleware = createTestMiddleware;
|