@constructor-io/constructorio-node 5.1.1 → 5.2.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/package.json +1 -1
- package/src/modules/quizzes.js +2 -2
- package/src/modules/tracker.js +16 -16
- package/src/utils/helpers.js +6 -0
package/package.json
CHANGED
package/src/modules/quizzes.js
CHANGED
|
@@ -108,7 +108,7 @@ class Quizzes {
|
|
|
108
108
|
* @description Retrieve quiz question from Constructor.io API
|
|
109
109
|
* @param {string} quizId - The identifier of the quiz
|
|
110
110
|
* @param {string} parameters - Additional parameters to refine result set
|
|
111
|
-
* @param {array} parameters.answers - An array of answers in the format [[1,2],[1], ["true"], ["seen"], [""]]. Based on the question type, answers should either be an integer, "true", "false", "seen" or empty string ("") if
|
|
111
|
+
* @param {array} parameters.answers - An array of answers in the format [[1,2],[1], ["true"], ["seen"], [""]]. Based on the question type, answers should either be an integer, "true", "false", "seen" or empty string ("") if the question is skippable: i.e., configured as `is_skippable:true`.
|
|
112
112
|
* @param {string} [parameters.section] - Product catalog section
|
|
113
113
|
* @param {string} [parameters.quizVersionId] - Version identifier for the quiz. Version ID will be returned with the first request and it should be passed with subsequent requests. More information can be found [here]{@link https://docs.constructor.com/reference/configuration-quizzes}
|
|
114
114
|
* @param {string} [parameters.quizSessionId] - Session identifier for the quiz. Session ID will be returned with the first request and it should be passed with subsequent requests. More information can be found [here]{@link https://docs.constructor.com/reference/configuration-quizzes}
|
|
@@ -186,7 +186,7 @@ class Quizzes {
|
|
|
186
186
|
* @description Retrieve quiz recommendation and filter expression from Constructor.io API
|
|
187
187
|
* @param {string} quizId - The identifier of the quiz
|
|
188
188
|
* @param {string} parameters - Additional parameters to refine result set
|
|
189
|
-
* @param {array} parameters.answers - An array of answers in the format [[1,2],[1], ["true"], ["seen"], [""]]. Based on the question type, answers should either be an integer, "true", "false", "seen" or empty string ("") if
|
|
189
|
+
* @param {array} parameters.answers - An array of answers in the format [[1,2],[1], ["true"], ["seen"], [""]]. Based on the question type, answers should either be an integer, "true", "false", "seen" or empty string ("") if the question is skippable: i.e., configured as `is_skippable:true`.
|
|
190
190
|
* @param {string} [parameters.section] - Product catalog section
|
|
191
191
|
* @param {string} [parameters.quizVersionId] - Version identifier for the quiz. Version ID will be returned with the first request and it should be passed with subsequent requests. More information can be found [here]{@link https://docs.constructor.com/reference/configuration-quizzes}
|
|
192
192
|
* @param {string} [parameters.quizSessionId] - Session identifier for the quiz. Session ID will be returned with the first request and it should be passed with subsequent requests. More information can be found [here]{@link https://docs.constructor.com/reference/configuration-quizzes}
|
package/src/modules/tracker.js
CHANGED
|
@@ -139,6 +139,7 @@ function send(url, userParameters, networkParameters, method = 'GET', body = {})
|
|
|
139
139
|
|
|
140
140
|
if (request) {
|
|
141
141
|
const instance = this;
|
|
142
|
+
const emitError = helpers.getEmitError(instance, { url, method });
|
|
142
143
|
|
|
143
144
|
request.then((response) => {
|
|
144
145
|
// Request was successful, and returned a 2XX status code
|
|
@@ -152,26 +153,25 @@ function send(url, userParameters, networkParameters, method = 'GET', body = {})
|
|
|
152
153
|
|
|
153
154
|
// Request was successful, but returned a non-2XX status code
|
|
154
155
|
else {
|
|
155
|
-
response.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
const contentType = response.headers.get('Content-Type') || '';
|
|
157
|
+
|
|
158
|
+
if (contentType.includes('application/json')) {
|
|
159
|
+
response.json().then((json) => {
|
|
160
|
+
emitError(json && json.message);
|
|
161
|
+
}).catch((error) => {
|
|
162
|
+
emitError(error.type);
|
|
160
163
|
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
164
|
+
} else {
|
|
165
|
+
// If not JSON, fallback to text
|
|
166
|
+
response.text().then((text) => {
|
|
167
|
+
emitError(text || 'Unknown error message');
|
|
168
|
+
}).catch((error) => {
|
|
169
|
+
emitError(`Error reading text: ${error.message}`);
|
|
166
170
|
});
|
|
167
|
-
}
|
|
171
|
+
}
|
|
168
172
|
}
|
|
169
173
|
}).catch((error) => {
|
|
170
|
-
|
|
171
|
-
url,
|
|
172
|
-
method,
|
|
173
|
-
message: error.toString(),
|
|
174
|
-
});
|
|
174
|
+
emitError(error.toString());
|
|
175
175
|
});
|
|
176
176
|
}
|
|
177
177
|
}
|
package/src/utils/helpers.js
CHANGED
|
@@ -134,6 +134,12 @@ const utils = {
|
|
|
134
134
|
|
|
135
135
|
return url;
|
|
136
136
|
},
|
|
137
|
+
|
|
138
|
+
getEmitError(instance, { url, method }) {
|
|
139
|
+
return function emitError(message) {
|
|
140
|
+
instance.eventemitter.emit('error', { url, method, message });
|
|
141
|
+
};
|
|
142
|
+
},
|
|
137
143
|
};
|
|
138
144
|
|
|
139
145
|
module.exports = utils;
|