@constructor-io/constructorio-client-javascript 2.29.12 → 2.29.13

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/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
6
+
7
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
8
+
9
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
10
+
11
+ /* eslint-disable object-curly-newline, no-underscore-dangle */
12
+ var qs = require('qs');
13
+
14
+ var fetchPonyfill = require('fetch-ponyfill');
15
+
16
+ var Promise = require('es6-promise');
17
+
18
+ var EventDispatcher = require('../utils/event-dispatcher');
19
+
20
+ var helpers = require('../utils/helpers'); // Create URL from supplied quizId and parameters
21
+
22
+
23
+ function createQuizUrl(quizId, parameters, options, path) {
24
+ var apiKey = options.apiKey,
25
+ clientId = options.clientId,
26
+ sessionId = options.sessionId,
27
+ segments = options.segments,
28
+ userId = options.userId,
29
+ version = options.version;
30
+ var serviceUrl = 'https://quizzes.cnstrc.com';
31
+ var queryParams = {
32
+ c: version
33
+ };
34
+ var answersParamString = '';
35
+ queryParams.key = apiKey;
36
+ queryParams.i = clientId;
37
+ queryParams.s = sessionId; // Pull user segments from options
38
+
39
+ if (segments && segments.length) {
40
+ queryParams.us = segments;
41
+ } // Pull user id from options
42
+
43
+
44
+ if (userId) {
45
+ queryParams.ui = userId;
46
+ } // Validate quiz id is provided
47
+
48
+
49
+ if (!quizId || typeof quizId !== 'string') {
50
+ throw new Error('quizId is a required parameter of type string');
51
+ }
52
+
53
+ if (path === 'finalize' && ((0, _typeof2["default"])(parameters.a) !== 'object' || !Array.isArray(parameters.a) || parameters.a.length === 0)) {
54
+ throw new Error('a is a required parameter of type array');
55
+ }
56
+
57
+ if (parameters) {
58
+ var section = parameters.section,
59
+ a = parameters.a,
60
+ versionId = parameters.versionId; // Pull section from parameters
61
+
62
+ if (section) {
63
+ queryParams.section = section;
64
+ } // Pull version_id from parameters
65
+
66
+
67
+ if (versionId) {
68
+ queryParams.version_id = versionId;
69
+ } // Pull a from parameters and transform
70
+
71
+
72
+ if (a) {
73
+ a.forEach(function (ans) {
74
+ answersParamString += "&".concat(qs.stringify({
75
+ a: ans
76
+ }, {
77
+ arrayFormat: 'comma'
78
+ }));
79
+ });
80
+ }
81
+ }
82
+
83
+ queryParams._dt = Date.now();
84
+ queryParams = helpers.cleanParams(queryParams);
85
+ var queryString = qs.stringify(queryParams, {
86
+ indices: false
87
+ });
88
+ return "".concat(serviceUrl, "/v1/quizzes/").concat(encodeURIComponent(quizId), "/").concat(encodeURIComponent(path), "/?").concat(queryString).concat(answersParamString);
89
+ }
90
+ /**
91
+ * Interface to quiz related API calls
92
+ *
93
+ * @module quizzes
94
+ * @inner
95
+ * @returns {object}
96
+ */
97
+
98
+
99
+ var Quizzes = /*#__PURE__*/function () {
100
+ function Quizzes(options) {
101
+ (0, _classCallCheck2["default"])(this, Quizzes);
102
+ this.options = options || {};
103
+ this.eventDispatcher = new EventDispatcher(options.eventDispatcher);
104
+ }
105
+ /**
106
+ * Retrieve next quiz from api
107
+ *
108
+ * @function getNextQuiz
109
+ * @description Retrieve next quiz from Constructor.io API
110
+ * @param {string} id - The id of the quiz
111
+ * @param {string} [parameters] - Additional parameters to refine result set
112
+ * @param {string} [parameters.section] - Section for customer's product catalog (optional)
113
+ * @param {array} [parameters.a] - An array for answers in the format [[1,2],[1]]
114
+ * @param {string} [parameters.version_id] - Specific version id for the quiz.
115
+ * @param {object} [networkParameters] - Parameters relevant to the network request
116
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
117
+ * @returns {Promise}
118
+ * @see https://quizzes.cnstrc.com/api/#/quizzes/QuizzesController_getNextQuestion
119
+ * @example
120
+ * constructorio.search.getNextQuiz('quizId', {
121
+ * a: [[1,2],[1]],
122
+ * section: "123",
123
+ * version_id: "123"
124
+ * });
125
+ */
126
+
127
+
128
+ (0, _createClass2["default"])(Quizzes, [{
129
+ key: "getNextQuiz",
130
+ value: function getNextQuiz(quizId, parameters) {
131
+ var _this = this;
132
+
133
+ var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
134
+ var requestUrl;
135
+ var fetch = this.options && this.options.fetch || fetchPonyfill({
136
+ Promise: Promise
137
+ }).fetch;
138
+ var controller = new AbortController();
139
+ var signal = controller.signal;
140
+
141
+ try {
142
+ requestUrl = createQuizUrl(quizId, parameters, this.options, 'next');
143
+ } catch (e) {
144
+ return Promise.reject(e);
145
+ } // Handle network timeout if specified
146
+
147
+
148
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
149
+ return fetch(requestUrl, {
150
+ signal: signal
151
+ }).then(function (response) {
152
+ if (response.ok) {
153
+ return response.json();
154
+ }
155
+
156
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
157
+ }).then(function (json) {
158
+ if (json.version_id) {
159
+ _this.eventDispatcher.queue('quizzes.getNextQuiz.completed', json);
160
+
161
+ return json;
162
+ }
163
+
164
+ throw new Error('getNextQuiz response data is malformed');
165
+ });
166
+ }
167
+ /**
168
+ * Retrieves filter expression and recommendation URL from given answers.
169
+ *
170
+ * @function getFinalizeQuiz
171
+ * @description Retrieve quiz recommendation and filter expression from Constructor.io API
172
+ * @param {string} id - The id of the quiz
173
+ * @param {string} [parameters] - Additional parameters to refine result set
174
+ * @param {string} [parameters.section] - Section for customer's product catalog (optional)
175
+ * @param {array} [parameters.a] - An array for answers in the format [[1,2],[1]]
176
+ * @param {string} [parameters.version_id] - Specific version id for the quiz.
177
+ * @param {object} [networkParameters] - Parameters relevant to the network request
178
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
179
+ * @returns {Promise}
180
+ * @see https://quizzes.cnstrc.com/api/#/quizzes/QuizzesController_getQuizResult
181
+ * @example
182
+ * constructorio.search.getFinalizeQuiz('quizId', {
183
+ * a: [[1,2],[1]],
184
+ * section: "123",
185
+ * version_id: "123"
186
+ * });
187
+ */
188
+
189
+ }, {
190
+ key: "getFinalizeQuiz",
191
+ value: function getFinalizeQuiz(quizId, parameters) {
192
+ var _this2 = this;
193
+
194
+ var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
195
+ var requestUrl;
196
+ var fetch = this.options && this.options.fetch || fetchPonyfill({
197
+ Promise: Promise
198
+ }).fetch;
199
+ var controller = new AbortController();
200
+ var signal = controller.signal;
201
+
202
+ try {
203
+ requestUrl = createQuizUrl(quizId, parameters, this.options, 'finalize');
204
+ } catch (e) {
205
+ return Promise.reject(e);
206
+ } // Handle network timeout if specified
207
+
208
+
209
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
210
+ return fetch(requestUrl, {
211
+ signal: signal
212
+ }).then(function (response) {
213
+ if (response.ok) {
214
+ return response.json();
215
+ }
216
+
217
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
218
+ }).then(function (json) {
219
+ if (json.version_id) {
220
+ _this2.eventDispatcher.queue('quizzes.getFinalizeQuiz.completed', json);
221
+
222
+ return json;
223
+ }
224
+
225
+ throw new Error('getFinalizeQuiz response data is malformed');
226
+ });
227
+ }
228
+ }]);
229
+ return Quizzes;
230
+ }();
231
+
232
+ module.exports = Quizzes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-client-javascript",
3
- "version": "2.29.12",
3
+ "version": "2.29.13",
4
4
  "description": "Constructor.io JavaScript client",
5
5
  "main": "lib/constructorio.js",
6
6
  "scripts": {