@shopgate/pwa-core 7.30.0-alpha.6 → 7.30.0-alpha.8

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.
Files changed (85) hide show
  1. package/classes/AppCommand/index.js +115 -11
  2. package/classes/AppCommand/spec.js +260 -6
  3. package/classes/AppCommandRequest/index.js +129 -20
  4. package/classes/AppPermissionsRequest/AppPermissionsRequest.js +45 -7
  5. package/classes/AppPermissionsRequest/GetAppPermissionsRequest.js +48 -9
  6. package/classes/AppPermissionsRequest/RequestAppPermissionsRequest.js +54 -9
  7. package/classes/Bridge/index.js +34 -4
  8. package/classes/Bridge/spec.js +24 -1
  9. package/classes/BrightnessRequest/index.js +59 -10
  10. package/classes/BrightnessRequest/spec.js +111 -6
  11. package/classes/BrowserConnector/index.js +180 -26
  12. package/classes/Conditioner/index.js +74 -8
  13. package/classes/Conditioner/spec.js +75 -1
  14. package/classes/DataRequest/index.js +116 -13
  15. package/classes/DevServerBridge/index.js +86 -9
  16. package/classes/DevServerBridge/spec.js +231 -14
  17. package/classes/ErrorManager/index.js +144 -20
  18. package/classes/ErrorManager/spec.js +244 -2
  19. package/classes/Event/index.js +101 -15
  20. package/classes/HttpRequest/index.js +182 -21
  21. package/classes/PipelineDependencies/index.js +42 -6
  22. package/classes/PipelineDependencies/spec.js +46 -3
  23. package/classes/PipelineManager/index.js +517 -71
  24. package/classes/PipelineManager/spec.js +733 -15
  25. package/classes/PipelineRequest/index.js +167 -19
  26. package/classes/PipelineRequest/mock.js +118 -21
  27. package/classes/PipelineRequest/spec.js +333 -2
  28. package/classes/PipelineSequence/index.js +34 -6
  29. package/classes/Request/index.js +61 -13
  30. package/classes/RequestBuffer/index.js +43 -6
  31. package/classes/RequestManager/index.js +216 -33
  32. package/classes/RequestManager/spec.js +188 -1
  33. package/classes/Scanner/index.js +246 -67
  34. package/classes/ScannerEvent/index.js +23 -9
  35. package/classes/ScannerEventHandler/index.js +39 -16
  36. package/classes/ScannerEventListener/index.js +84 -24
  37. package/classes/ScannerManager/ScanProcessingError.js +11 -3
  38. package/classes/ScannerManager/index.js +133 -21
  39. package/classes/WebStorageRequest/index.js +76 -9
  40. package/commands/analyticsSetCustomValues.js +8 -2
  41. package/commands/appPermissions.js +10 -3
  42. package/commands/brightness.js +33 -5
  43. package/commands/broadcastEvent.js +8 -2
  44. package/commands/cleanTab.js +11 -3
  45. package/commands/closeInAppBrowser.js +22 -2
  46. package/commands/flushTab.js +8 -2
  47. package/commands/getWebStorageEntry.js +11 -2
  48. package/commands/hideMenuBar.js +8 -2
  49. package/commands/hideNavigationBar.js +8 -2
  50. package/commands/hideSplashScreen.js +8 -2
  51. package/commands/onload.js +13 -3
  52. package/commands/openAppSettings.js +8 -2
  53. package/commands/openPage.js +8 -2
  54. package/commands/openPageExtern.js +8 -2
  55. package/commands/performCommandsAfterDelay.js +11 -3
  56. package/commands/plotProjects.js +65 -7
  57. package/commands/popTabToRoot.js +11 -3
  58. package/commands/registerEvents.js +10 -2
  59. package/commands/scanner.js +76 -7
  60. package/commands/setCookie.js +8 -2
  61. package/commands/setDebugLoggingEnabled.js +8 -2
  62. package/commands/setScrollingEnabled.js +7 -2
  63. package/commands/setWebStorageEntry.js +8 -2
  64. package/commands/shareItem.js +18 -2
  65. package/commands/showNavigationBar.js +8 -2
  66. package/commands/showTab.js +13 -2
  67. package/commands/unifiedTracking.js +128 -30
  68. package/constants/AppCommands.js +6 -1
  69. package/constants/AppEvents.js +9 -1
  70. package/constants/AppPermissions.js +57 -13
  71. package/constants/Command.js +1 -1
  72. package/constants/ErrorHandleTypes.js +2 -1
  73. package/constants/ErrorManager.js +15 -1
  74. package/constants/Pipeline.js +52 -17
  75. package/constants/ProcessTypes.js +3 -1
  76. package/constants/RequestManagerModes.js +19 -7
  77. package/constants/RequestTypes.js +2 -1
  78. package/constants/Scanner.js +39 -10
  79. package/constants/Trilean.js +6 -1
  80. package/emitters/ui.js +2 -1
  81. package/helpers/index.js +66 -8
  82. package/helpers/logGroup.js +56 -8
  83. package/helpers/version.js +216 -22
  84. package/index.js +60 -5
  85. package/package.json +1 -1
@@ -1,44 +1,198 @@
1
- function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import{logger}from"../../helpers";import event from"../Event";import{TYPE_TRUSTED}from"../../constants/Pipeline";import*as requestTypes from"../../constants/RequestTypes";import*as appCommands from"../../constants/AppCommands";var appConfig=process.env.APP_CONFIG||{};/**
1
+ import { logger } from "../../helpers";
2
+ import event from "../Event";
3
+ import { TYPE_TRUSTED } from "../../constants/Pipeline";
4
+ import * as requestTypes from "../../constants/RequestTypes";
5
+ import * as appCommands from "../../constants/AppCommands";
6
+ const appConfig = process.env.APP_CONFIG || {};
7
+
8
+ /**
2
9
  * The BrowserConnector emulates the SGJavaScriptBridge within browser environments.
3
10
  * It routes supported app commands to Shopgate Connect which can mimic the behavior of the app.
4
- */var BrowserConnector=/*#__PURE__*/function(){/**
11
+ */
12
+ class BrowserConnector {
13
+ /**
5
14
  * The constructor.
6
15
  * @param {string} [ip=process.env.IP] The IP of the dev server.
7
16
  * @param {number} [port=process.env.PORT] The port of the dev server.
8
- */function BrowserConnector(){var _this=this;var ip=arguments.length>0&&arguments[0]!==undefined?arguments[0]:process.env.IP;var port=arguments.length>1&&arguments[1]!==undefined?arguments[1]:process.env.PORT;_classCallCheck(this,BrowserConnector);/**
9
- * Builds the GET query.
10
- * @param {Object} input The input parameters.
17
+ */
18
+ constructor(ip = process.env.IP, port = process.env.PORT) {
19
+ /**
20
+ * Builds the GET query.
21
+ * @param {Object} input The input parameters.
22
+ * @return {string}
23
+ */
24
+ this.buildQueryString = input => {
25
+ if (Object.keys(input).length === 0 || this.isPOST) {
26
+ return '';
27
+ }
28
+ const query = Object.keys(input).map(key => `${key}=${input[key]}`).join('&');
29
+ return `?${query}`;
30
+ };
31
+ /**
32
+ * Handles a response of the server.
33
+ * @param {Object} response The server response.
34
+ * @return {BrowserConnector}
35
+ */
36
+ this.processResponse = response => {
37
+ const {
38
+ cmds = []
39
+ } = response || {};
40
+ if (cmds.length === 0) {
41
+ if (this.command.c === appCommands.COMMAND_SEND_PIPELINE_REQUEST) {
42
+ event.call(`pipelineResponse:${this.command.p.serial}`, [response.error || null, this.command.p.serial, !response.error ? response : undefined]);
43
+ }
44
+ }
45
+
46
+ // Process the response commands.
47
+ cmds.forEach(command => {
48
+ const name = command.c;
49
+ const params = command.p;
50
+ let args = [];
51
+
52
+ /**
53
+ * The server returns a response command for a request command.
54
+ * If the native app receives such a command, it calls a related event within the
55
+ * webviews. Here the response parameters are sorted in the specified order for
56
+ * the different response events.
57
+ */
58
+ if (name === 'pipelineResponse') {
59
+ args = [params.error, params.serial, params.output];
60
+ } else if (name === 'httpResponse') {
61
+ args = [params.error, params.serial, params.response];
62
+ } else if (name === 'dataResponse') {
63
+ args = [params.serial, params.status, params.body, params.bodyContentType];
64
+ } else if (name === 'webStorageResponse') {
65
+ args = [params.serial, params.age, params.value];
66
+ }
67
+ event.call(name, args);
68
+ });
69
+ return this;
70
+ };
71
+ this.ip = ip;
72
+ this.port = port;
73
+ this.supportedCommands = [appCommands.COMMAND_SEND_PIPELINE_REQUEST, appCommands.COMMAND_SEND_HTTP_REQUEST, appCommands.COMMAND_SEND_DATA_REQUEST, appCommands.COMMAND_GET_WEBSTORAGE_ENTRY];
74
+ this.appConfig = appConfig;
75
+ }
76
+ /**
11
77
  * @return {string}
12
- */_defineProperty(this,"buildQueryString",function(input){if(Object.keys(input).length===0||_this.isPOST){return'';}var query=Object.keys(input).map(function(key){return"".concat(key,"=").concat(input[key]);}).join('&');return"?".concat(query);});/**
13
- * Handles a response of the server.
14
- * @param {Object} response The server response.
15
- * @return {BrowserConnector}
16
- */_defineProperty(this,"processResponse",function(response){var _ref=response||{},_ref$cmds=_ref.cmds,cmds=_ref$cmds===void 0?[]:_ref$cmds;if(cmds.length===0){if(_this.command.c===appCommands.COMMAND_SEND_PIPELINE_REQUEST){event.call("pipelineResponse:".concat(_this.command.p.serial),[response.error||null,_this.command.p.serial,!response.error?response:undefined]);}}// Process the response commands.
17
- cmds.forEach(function(command){var name=command.c;var params=command.p;var args=[];/**
18
- * The server returns a response command for a request command.
19
- * If the native app receives such a command, it calls a related event within the
20
- * webviews. Here the response parameters are sorted in the specified order for
21
- * the different response events.
22
- */if(name==='pipelineResponse'){args=[params.error,params.serial,params.output];}else if(name==='httpResponse'){args=[params.error,params.serial,params.response];}else if(name==='dataResponse'){args=[params.serial,params.status,params.body,params.bodyContentType];}else if(name==='webStorageResponse'){args=[params.serial,params.age,params.value];}event.call(name,args);});return _this;});this.ip=ip;this.port=port;this.supportedCommands=[appCommands.COMMAND_SEND_PIPELINE_REQUEST,appCommands.COMMAND_SEND_HTTP_REQUEST,appCommands.COMMAND_SEND_DATA_REQUEST,appCommands.COMMAND_GET_WEBSTORAGE_ENTRY];this.appConfig=appConfig;}return _createClass(BrowserConnector,[{key:"requestType",get:/**
23
- * @return {string}
24
- */function get(){if(!this.isPipelineRequest){return requestTypes.REQUEST_TYPE_POST;}var segments=this.command.p.name.split('.');if(segments[2].startsWith('get')){return requestTypes.REQUEST_TYPE_GET;}return requestTypes.REQUEST_TYPE_POST;}/**
78
+ */
79
+ get requestType() {
80
+ if (!this.isPipelineRequest) {
81
+ return requestTypes.REQUEST_TYPE_POST;
82
+ }
83
+ const segments = this.command.p.name.split('.');
84
+ if (segments[2].startsWith('get')) {
85
+ return requestTypes.REQUEST_TYPE_GET;
86
+ }
87
+ return requestTypes.REQUEST_TYPE_POST;
88
+ }
89
+
90
+ /**
25
91
  * @return {boolean}
26
- */},{key:"isGET",get:function get(){return this.requestType===requestTypes.REQUEST_TYPE_GET;}/**
92
+ */
93
+ get isGET() {
94
+ return this.requestType === requestTypes.REQUEST_TYPE_GET;
95
+ }
96
+
97
+ /**
27
98
  * @return {boolean}
28
- */},{key:"isPOST",get:function get(){return this.requestType===requestTypes.REQUEST_TYPE_POST;}/**
99
+ */
100
+ get isPOST() {
101
+ return this.requestType === requestTypes.REQUEST_TYPE_POST;
102
+ }
103
+
104
+ /**
29
105
  * @return {string}
30
- */},{key:"connectUrl",get:function get(){var _this$appConfig$apiUr=this.appConfig.apiUrl,apiUrl=_this$appConfig$apiUr===void 0?'':_this$appConfig$apiUr;var _this$command$p=this.command.p,name=_this$command$p.name,type=_this$command$p.type,input=_this$command$p.input;var queryString=this.buildQueryString(input);if(type&&type===TYPE_TRUSTED){return"".concat(apiUrl,"app/trustedPipelines/").concat(name).concat(queryString);}return"".concat(apiUrl,"app/pipelines/").concat(name).concat(queryString);}/**
106
+ */
107
+ get connectUrl() {
108
+ const {
109
+ apiUrl = ''
110
+ } = this.appConfig;
111
+ const {
112
+ name,
113
+ type,
114
+ input
115
+ } = this.command.p;
116
+ const queryString = this.buildQueryString(input);
117
+ if (type && type === TYPE_TRUSTED) {
118
+ return `${apiUrl}app/trustedPipelines/${name}${queryString}`;
119
+ }
120
+ return `${apiUrl}app/pipelines/${name}${queryString}`;
121
+ }
122
+
123
+ /**
31
124
  * @return {boolean}
32
- */},{key:"isPipelineRequest",get:function get(){return this.command.c===appCommands.COMMAND_SEND_PIPELINE_REQUEST;}/**
125
+ */
126
+ get isPipelineRequest() {
127
+ return this.command.c === appCommands.COMMAND_SEND_PIPELINE_REQUEST;
128
+ }
129
+
130
+ /**
33
131
  * @return {string}
34
- */},{key:"suffix",get:function get(){if(this.command.c===appCommands.COMMAND_GET_WEBSTORAGE_ENTRY){return'web_storage';}if(this.command.c===appCommands.COMMAND_SEND_HTTP_REQUEST){return'http_request';}return'';}/**
132
+ */
133
+ get suffix() {
134
+ if (this.command.c === appCommands.COMMAND_GET_WEBSTORAGE_ENTRY) {
135
+ return 'web_storage';
136
+ }
137
+ if (this.command.c === appCommands.COMMAND_SEND_HTTP_REQUEST) {
138
+ return 'http_request';
139
+ }
140
+ return '';
141
+ }
142
+
143
+ /**
35
144
  * @return {string}
36
- */},{key:"localURL",get:function get(){return"http://".concat(this.ip,":").concat(this.port,"/").concat(this.suffix);}/**
145
+ */
146
+ get localURL() {
147
+ return `http://${this.ip}:${this.port}/${this.suffix}`;
148
+ }
149
+
150
+ /**
37
151
  * @param {string} libVersion The library version.
38
152
  * @returns {string}
39
- */},{key:"getRequestBody",value:function getRequestBody(libVersion){if(this.isPipelineRequest){var p=this.command.p;var input=p.input;return JSON.stringify(input);}return JSON.stringify({cmds:[this.command],ver:libVersion});}/**
153
+ */
154
+ getRequestBody(libVersion) {
155
+ if (this.isPipelineRequest) {
156
+ const {
157
+ p
158
+ } = this.command;
159
+ const {
160
+ input
161
+ } = p;
162
+ return JSON.stringify(input);
163
+ }
164
+ return JSON.stringify({
165
+ cmds: [this.command],
166
+ ver: libVersion
167
+ });
168
+ }
169
+
170
+ /**
40
171
  * Dispatches a single command to the dev server.
41
172
  * @param {Object} command The command to dispatch.
42
173
  * @param {string} libVersion The lib version for the command.
43
174
  * @return {BrowserConnector}
44
- */},{key:"dispatchCommandForVersion",value:function dispatchCommandForVersion(command,libVersion){this.command=command;var _ref2=this.command||{},name=_ref2.c;if(this.supportedCommands.includes(name)){var URL=this.isPipelineRequest?this.connectUrl:this.localURL;var options=_extends({method:this.requestType,credentials:'include',headers:new Headers({'Content-Type':'application/json'})},this.isPOST&&{body:this.getRequestBody(libVersion)});fetch(URL,options).then(function(response){return response.json();}).then(this.processResponse)["catch"](function(err){return err&&logger.error(err);});}return this;}}]);}();export default BrowserConnector;
175
+ */
176
+ dispatchCommandForVersion(command, libVersion) {
177
+ this.command = command;
178
+ const {
179
+ c: name
180
+ } = this.command || {};
181
+ if (this.supportedCommands.includes(name)) {
182
+ const URL = this.isPipelineRequest ? this.connectUrl : this.localURL;
183
+ const options = {
184
+ method: this.requestType,
185
+ credentials: 'include',
186
+ headers: new Headers({
187
+ 'Content-Type': 'application/json'
188
+ }),
189
+ ...(this.isPOST && {
190
+ body: this.getRequestBody(libVersion)
191
+ })
192
+ };
193
+ fetch(URL, options).then(response => response.json()).then(this.processResponse).catch(err => err && logger.error(err));
194
+ }
195
+ return this;
196
+ }
197
+ }
198
+ export default BrowserConnector;
@@ -1,20 +1,86 @@
1
- import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}import{logger}from"../../helpers";/**
1
+ import { logger } from "../../helpers";
2
+
3
+ /**
2
4
  * Creates an action handler API.
3
- */var Conditioner=/*#__PURE__*/function(){/**
5
+ */
6
+ class Conditioner {
7
+ /**
4
8
  * @param {Map} conditions conditions
5
- */function Conditioner(conditions){_classCallCheck(this,Conditioner);this.conditions=conditions||new Map();}/**
9
+ */
10
+ constructor(conditions) {
11
+ this.conditions = conditions || new Map();
12
+ }
13
+
14
+ /**
6
15
  * @param {string} name The name of the registered conditioner.
7
16
  * @param {Function} conditioner The registered conditioner.
8
17
  * @param {number} priority conditioner priority
9
18
  * @return {Conditioner}
10
- */return _createClass(Conditioner,[{key:"addConditioner",value:function addConditioner(name,conditioner){var priority=arguments.length>2&&arguments[2]!==undefined?arguments[2]:1;if(typeof conditioner!=='function'){throw new Error("Conditioners need to be of type function. Received: '".concat(_typeof(conditioner),"'"));}this.conditions.set(name,{priority:priority,conditioner:conditioner});return this;}/**
19
+ */
20
+ addConditioner(name, conditioner, priority = 1) {
21
+ if (typeof conditioner !== 'function') {
22
+ throw new Error(`Conditioners need to be of type function. Received: '${typeof conditioner}'`);
23
+ }
24
+ this.conditions.set(name, {
25
+ priority,
26
+ conditioner
27
+ });
28
+ return this;
29
+ }
30
+
31
+ /**
11
32
  * @param {string} name The name of the registered conditioner.
12
33
  * @return {Conditioner}
13
- */},{key:"removeConditioner",value:function removeConditioner(name){if(!this.conditions.has(name)){logger.warn("Couldn't remove conditioner. '".concat(name,"' no found."));return this;}this.conditions["delete"](name);return this;}/**
34
+ */
35
+ removeConditioner(name) {
36
+ if (!this.conditions.has(name)) {
37
+ logger.warn(`Couldn't remove conditioner. '${name}' no found.`);
38
+ return this;
39
+ }
40
+ this.conditions.delete(name);
41
+ return this;
42
+ }
43
+
44
+ /**
14
45
  * @param {string} name The name of the conditioner to eliminate.
15
46
  * @return {Conditioner} new instance with cloned conditions
16
- */},{key:"without",value:function without(name){var clonedConditions=new Map(this.conditions);if(clonedConditions.has(name)){clonedConditions["delete"](name);}return new Conditioner(clonedConditions);}/**
47
+ */
48
+ without(name) {
49
+ const clonedConditions = new Map(this.conditions);
50
+ if (clonedConditions.has(name)) {
51
+ clonedConditions.delete(name);
52
+ }
53
+ return new Conditioner(clonedConditions);
54
+ }
55
+
56
+ /**
17
57
  * Resolves if all conditions are fulfilled.
18
58
  * @return {Promise}
19
- */},{key:"check",value:function check(){var _this=this;return new Promise(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(resolve,reject){var sorted,i,condition,conditionResult;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:sorted=Array.from(_this.conditions.values()).sort(function(a,b){if(a.priority===b.priority){return 0;}return a.priority<b.priority?-1:1;});_context.prev=1;i=0;case 3:if(!(i<sorted.length)){_context.next=14;break;}condition=sorted[i];// eslint-disable-next-line no-await-in-loop
20
- _context.next=7;return condition.conditioner();case 7:conditionResult=_context.sent;if(!(conditionResult===false)){_context.next=11;break;}logger.warn('conditioner failed',i,condition,_this.conditions);return _context.abrupt("return",resolve(false));case 11:i+=1;_context.next=3;break;case 14:return _context.abrupt("return",resolve(true));case 17:_context.prev=17;_context.t0=_context["catch"](1);return _context.abrupt("return",reject(_context.t0));case 20:case"end":return _context.stop();}},_callee,null,[[1,17]]);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());}}]);}();export default Conditioner;
59
+ */
60
+ check() {
61
+ return new Promise(async (resolve, reject) => {
62
+ const sorted = Array.from(this.conditions.values()).sort((a, b) => {
63
+ if (a.priority === b.priority) {
64
+ return 0;
65
+ }
66
+ return a.priority < b.priority ? -1 : 1;
67
+ });
68
+ try {
69
+ for (let i = 0; i < sorted.length; i += 1) {
70
+ const condition = sorted[i];
71
+
72
+ // eslint-disable-next-line no-await-in-loop
73
+ const conditionResult = await condition.conditioner();
74
+ if (conditionResult === false) {
75
+ logger.warn('conditioner failed', i, condition, this.conditions);
76
+ return resolve(false);
77
+ }
78
+ }
79
+ return resolve(true);
80
+ } catch (error) {
81
+ return reject(error);
82
+ }
83
+ });
84
+ }
85
+ }
86
+ export default Conditioner;
@@ -1 +1,75 @@
1
- import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import Conditioner from"./index";import{logger}from"../../helpers";jest.mock("../../helpers",function(){return{logger:{warn:jest.fn()}};});describe('Conditioner',function(){var conditioner;var cond1;var cond2;var cond3;beforeEach(function(){jest.clearAllMocks();conditioner=new Conditioner();cond1=jest.fn().mockReturnValue(true);cond2=jest.fn().mockReturnValue(true);cond3=jest.fn().mockReturnValue(true);});it('should fail on first sorted condition',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(){return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:cond3.mockReturnValue(false);conditioner.addConditioner(1,cond1);conditioner.addConditioner(2,cond2,-1);conditioner.addConditioner(3,cond3,-2);_context.t0=expect;_context.next=7;return conditioner.check();case 7:_context.t1=_context.sent;(0,_context.t0)(_context.t1).toBeFalsy();expect(cond3).toBeCalledTimes(1);expect(cond1).toBeCalledTimes(0);expect(cond2).toBeCalledTimes(0);expect(logger.warn).toBeCalledTimes(1);case 13:case"end":return _context.stop();}},_callee);})));it('should fail on first promisified sorted condition',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(){return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:expect.assertions(5);cond3.mockResolvedValue(false);conditioner.addConditioner(1,cond1);conditioner.addConditioner(2,cond2,-1);conditioner.addConditioner(2,cond3,-2);_context2.t0=expect;_context2.next=8;return conditioner.check();case 8:_context2.t1=_context2.sent;(0,_context2.t0)(_context2.t1).toBeFalsy();expect(cond3).toBeCalledTimes(1);expect(cond1).toBeCalledTimes(0);expect(cond2).toBeCalledTimes(0);expect(logger.warn).toBeCalledTimes(1);case 14:case"end":return _context2.stop();}},_callee2);})));it('should fail on second promisified sorted condition',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(){return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:expect.assertions(5);cond3.mockResolvedValue(false);conditioner.addConditioner(1,cond1);conditioner.addConditioner(2,cond2,-10);conditioner.addConditioner(2,cond3,-2);_context3.t0=expect;_context3.next=8;return conditioner.check();case 8:_context3.t1=_context3.sent;(0,_context3.t0)(_context3.t1).toBeFalsy();expect(cond3).toBeCalledTimes(1);expect(cond1).toBeCalledTimes(0);expect(cond2).toBeCalledTimes(0);expect(logger.warn).toBeCalledTimes(1);case 14:case"end":return _context3.stop();}},_callee3);})));it('should resolve true',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(){return _regeneratorRuntime.wrap(function _callee4$(_context4){while(1)switch(_context4.prev=_context4.next){case 0:conditioner.addConditioner(1,cond1);conditioner.addConditioner(2,cond2);conditioner.addConditioner(3,cond3);_context4.t0=expect;_context4.next=6;return conditioner.check();case 6:_context4.t1=_context4.sent;(0,_context4.t0)(_context4.t1).toBeTruthy();expect(cond1).toBeCalledTimes(1);expect(cond2).toBeCalledTimes(1);expect(cond3).toBeCalledTimes(1);expect(logger.warn).toBeCalledTimes(0);case 12:case"end":return _context4.stop();}},_callee4);})));it('should return cloned conditioner',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5(){var newConditioner;return _regeneratorRuntime.wrap(function _callee5$(_context5){while(1)switch(_context5.prev=_context5.next){case 0:conditioner.addConditioner('one',cond1);conditioner.addConditioner('two',cond2);newConditioner=conditioner.without('one');expect(newConditioner).not.toBe(conditioner);expect(newConditioner.conditions).not.toBe(conditioner.conditions);expect(newConditioner.conditions.has('one')).toBeFalsy();expect(newConditioner.conditions.has('two')).toBeTruthy();expect(logger.warn).toBeCalledTimes(0);case 8:case"end":return _context5.stop();}},_callee5);})));});
1
+ import Conditioner from "./index";
2
+ import { logger } from "../../helpers";
3
+ jest.mock("../../helpers", () => ({
4
+ logger: {
5
+ warn: jest.fn()
6
+ }
7
+ }));
8
+ describe('Conditioner', () => {
9
+ let conditioner;
10
+ let cond1;
11
+ let cond2;
12
+ let cond3;
13
+ beforeEach(() => {
14
+ jest.clearAllMocks();
15
+ conditioner = new Conditioner();
16
+ cond1 = jest.fn().mockReturnValue(true);
17
+ cond2 = jest.fn().mockReturnValue(true);
18
+ cond3 = jest.fn().mockReturnValue(true);
19
+ });
20
+ it('should fail on first sorted condition', async () => {
21
+ cond3.mockReturnValue(false);
22
+ conditioner.addConditioner(1, cond1);
23
+ conditioner.addConditioner(2, cond2, -1);
24
+ conditioner.addConditioner(3, cond3, -2);
25
+ expect(await conditioner.check()).toBeFalsy();
26
+ expect(cond3).toBeCalledTimes(1);
27
+ expect(cond1).toBeCalledTimes(0);
28
+ expect(cond2).toBeCalledTimes(0);
29
+ expect(logger.warn).toBeCalledTimes(1);
30
+ });
31
+ it('should fail on first promisified sorted condition', async () => {
32
+ expect.assertions(5);
33
+ cond3.mockResolvedValue(false);
34
+ conditioner.addConditioner(1, cond1);
35
+ conditioner.addConditioner(2, cond2, -1);
36
+ conditioner.addConditioner(2, cond3, -2);
37
+ expect(await conditioner.check()).toBeFalsy();
38
+ expect(cond3).toBeCalledTimes(1);
39
+ expect(cond1).toBeCalledTimes(0);
40
+ expect(cond2).toBeCalledTimes(0);
41
+ expect(logger.warn).toBeCalledTimes(1);
42
+ });
43
+ it('should fail on second promisified sorted condition', async () => {
44
+ expect.assertions(5);
45
+ cond3.mockResolvedValue(false);
46
+ conditioner.addConditioner(1, cond1);
47
+ conditioner.addConditioner(2, cond2, -10);
48
+ conditioner.addConditioner(2, cond3, -2);
49
+ expect(await conditioner.check()).toBeFalsy();
50
+ expect(cond3).toBeCalledTimes(1);
51
+ expect(cond1).toBeCalledTimes(0);
52
+ expect(cond2).toBeCalledTimes(0);
53
+ expect(logger.warn).toBeCalledTimes(1);
54
+ });
55
+ it('should resolve true', async () => {
56
+ conditioner.addConditioner(1, cond1);
57
+ conditioner.addConditioner(2, cond2);
58
+ conditioner.addConditioner(3, cond3);
59
+ expect(await conditioner.check()).toBeTruthy();
60
+ expect(cond1).toBeCalledTimes(1);
61
+ expect(cond2).toBeCalledTimes(1);
62
+ expect(cond3).toBeCalledTimes(1);
63
+ expect(logger.warn).toBeCalledTimes(0);
64
+ });
65
+ it('should return cloned conditioner', async () => {
66
+ conditioner.addConditioner('one', cond1);
67
+ conditioner.addConditioner('two', cond2);
68
+ const newConditioner = conditioner.without('one');
69
+ expect(newConditioner).not.toBe(conditioner);
70
+ expect(newConditioner.conditions).not.toBe(conditioner.conditions);
71
+ expect(newConditioner.conditions.has('one')).toBeFalsy();
72
+ expect(newConditioner.conditions.has('two')).toBeTruthy();
73
+ expect(logger.warn).toBeCalledTimes(0);
74
+ });
75
+ });
@@ -1,39 +1,142 @@
1
- function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _callSuper(_this,derived,args){function isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));}catch(e){return false;}}derived=_getPrototypeOf(derived);return _possibleConstructorReturn(_this,isNativeReflectConstruct()?Reflect.construct(derived,args||[],_getPrototypeOf(_this).constructor):derived.apply(_this,args));}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call;}return _assertThisInitialized(self);}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return self;}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o);};return _getPrototypeOf(o);}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function");}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass);}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o;};return _setPrototypeOf(o,p);}import event from"../Event";import AppCommand from"../AppCommand";import Request from"../Request";import requestBuffer from"../RequestBuffer";import{logger,ajaxUrl}from"../../helpers";/**
1
+ import event from "../Event";
2
+ import AppCommand from "../AppCommand";
3
+ import Request from "../Request";
4
+ import requestBuffer from "../RequestBuffer";
5
+ import { logger, ajaxUrl } from "../../helpers";
6
+
7
+ /**
2
8
  * The DataRequest class. It is the interface to the legacy system.
3
- */var DataRequest=/*#__PURE__*/function(_Request){/**
9
+ */
10
+ class DataRequest extends Request {
11
+ /**
4
12
  * Initializes the DataRequest object
5
13
  * @param {string} src The source url
6
- */function DataRequest(src){var _this2;_classCallCheck(this,DataRequest);_this2=_callSuper(this,DataRequest);_this2.src=src;_this2.payload={};_this2.noCache=true;_this2.createSerial(_this2.src);_this2.createEventCallbackName('dataResponse');return _this2;}/**
14
+ */
15
+ constructor(src) {
16
+ super();
17
+ this.src = src;
18
+ this.payload = {};
19
+ this.noCache = true;
20
+ this.createSerial(this.src);
21
+ this.createEventCallbackName('dataResponse');
22
+ }
23
+
24
+ /**
7
25
  * Sets the payload for the DataRequest
8
26
  * @param {Object|string} [payload={}] The payload to send with the request
9
27
  * @returns {DataRequest}
10
- */_inherits(DataRequest,_Request);return _createClass(DataRequest,[{key:"setPayload",value:function setPayload(){var payload=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};this.payload=payload;return this;}/**
28
+ */
29
+ setPayload(payload = {}) {
30
+ this.payload = payload;
31
+ return this;
32
+ }
33
+
34
+ /**
11
35
  * Decides if the response of the DataRequest will be cached
12
36
  * @param {boolean} [noCache=true] If set to `true`, then the DataResponse will not be cached
13
37
  * @returns {DataRequest} The DataRequest
14
- */},{key:"setNoCache",value:function setNoCache(){var noCache=arguments.length>0&&arguments[0]!==undefined?arguments[0]:true;this.noCache=noCache;return this;}/**
38
+ */
39
+ setNoCache(noCache = true) {
40
+ this.noCache = noCache;
41
+ return this;
42
+ }
43
+
44
+ /**
15
45
  * Determines the correct content type for the request payload.
16
46
  * @return {string} The content type
17
- */},{key:"getContentType",value:function getContentType(){var contentType='text/plain';if(_typeof(this.payload)==='object'||typeof this.payload==='string'&&this.payload.search(/_method=POST/)===0){contentType='application/x-www-form-urlencoded; charset=UTF-8';}return contentType;}/**
47
+ */
48
+ getContentType() {
49
+ let contentType = 'text/plain';
50
+ if (typeof this.payload === 'object' || typeof this.payload === 'string' && this.payload.search(/_method=POST/) === 0) {
51
+ contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
52
+ }
53
+ return contentType;
54
+ }
55
+
56
+ /**
18
57
  * Creates the data request body from the payload.
19
58
  * @return {string} The request body
20
- */},{key:"getRequestBody",value:function getRequestBody(){/**
59
+ */
60
+ getRequestBody() {
61
+ /**
21
62
  * Serializes a JavaScript object for a data request body.
22
63
  * @param {Object} obj The object that shall be serialized.
23
64
  * @param {string} [prefix] An optional prefix for the object keys.
24
65
  * @return {string} The serialized object.
25
- */var serializeObject=function serializeObject(obj,prefix){var result=[];Object.keys(obj).forEach(function(propName){var key=prefix?"".concat(prefix,"[").concat(propName,"]"):propName;var value=obj[propName];var tmp;if(_typeof(value)==='object'){tmp=serializeObject(value,key);}else{tmp="".concat(window.encodeURIComponent(key),"=").concat(window.encodeURIComponent(value));}result.push(tmp);});return result.join('&');};var body='';if(this.payload){if(_typeof(this.payload)==='object'){body=serializeObject(this.payload);}else if(typeof this.payload==='string'){body=this.payload;}}return body;}/**
66
+ */
67
+ const serializeObject = (obj, prefix) => {
68
+ const result = [];
69
+ Object.keys(obj).forEach(propName => {
70
+ const key = prefix ? `${prefix}[${propName}]` : propName;
71
+ const value = obj[propName];
72
+ let tmp;
73
+ if (typeof value === 'object') {
74
+ tmp = serializeObject(value, key);
75
+ } else {
76
+ tmp = `${window.encodeURIComponent(key)}=${window.encodeURIComponent(value)}`;
77
+ }
78
+ result.push(tmp);
79
+ });
80
+ return result.join('&');
81
+ };
82
+ let body = '';
83
+ if (this.payload) {
84
+ if (typeof this.payload === 'object') {
85
+ body = serializeObject(this.payload);
86
+ } else if (typeof this.payload === 'string') {
87
+ body = this.payload;
88
+ }
89
+ }
90
+ return body;
91
+ }
92
+
93
+ /**
26
94
  * Dispatches the data request.
27
95
  * @param {Function} resolve The resolve() callback of the request promise.
28
96
  * @param {Function} reject The reject() callback of the request promise.
29
- */},{key:"onDispatch",value:function onDispatch(resolve,reject){var _this3=this;// Add the request to the buffer.
30
- requestBuffer.add(this,this.serial);var requestCallbackName=this.getEventCallbackName();/**
97
+ */
98
+ onDispatch(resolve, reject) {
99
+ // Add the request to the buffer.
100
+ requestBuffer.add(this, this.serial);
101
+ const requestCallbackName = this.getEventCallbackName();
102
+
103
+ /**
31
104
  * The request event callback for the response call.
32
105
  * @param {string} serial The serial that was used to identify the DataRequest callback..
33
106
  * @param {number} status The request status.
34
107
  * @param {string} body The response body.
35
108
  * @param {string} bodyContentType The type of data within the response body.
36
109
  * @return {Object}
37
- */var requestCallback=function requestCallback(serial,status,body,bodyContentType){event.removeCallback(requestCallbackName,requestCallback);requestBuffer.remove(serial);if(status!==200){return reject(status);}var responsePayload=body;if(bodyContentType==='application/json; charset=UTF-8'){responsePayload=JSON.parse(responsePayload);}logger.log("dataResponse: ".concat(_this3.src),{status:status,responsePayload:responsePayload});return resolve(responsePayload);};// Apply the event callback.
38
- event.addCallback(requestCallbackName,requestCallback);// Send the DataRequest.
39
- var command=new AppCommand();command.setCommandName('sendDataRequest').dispatch({src:ajaxUrl(this.src),serial:this.serial,body:this.getRequestBody(),bodyContentType:this.getContentType(),noCache:this.noCache});}}]);}(Request);export default DataRequest;
110
+ */
111
+ const requestCallback = (serial, status, body, bodyContentType) => {
112
+ event.removeCallback(requestCallbackName, requestCallback);
113
+ requestBuffer.remove(serial);
114
+ if (status !== 200) {
115
+ return reject(status);
116
+ }
117
+ let responsePayload = body;
118
+ if (bodyContentType === 'application/json; charset=UTF-8') {
119
+ responsePayload = JSON.parse(responsePayload);
120
+ }
121
+ logger.log(`dataResponse: ${this.src}`, {
122
+ status,
123
+ responsePayload
124
+ });
125
+ return resolve(responsePayload);
126
+ };
127
+
128
+ // Apply the event callback.
129
+ event.addCallback(requestCallbackName, requestCallback);
130
+
131
+ // Send the DataRequest.
132
+ const command = new AppCommand();
133
+ command.setCommandName('sendDataRequest').dispatch({
134
+ src: ajaxUrl(this.src),
135
+ serial: this.serial,
136
+ body: this.getRequestBody(),
137
+ bodyContentType: this.getContentType(),
138
+ noCache: this.noCache
139
+ });
140
+ }
141
+ }
142
+ export default DataRequest;