@constructor-io/constructorio-client-javascript 2.30.0-beta.0 → 2.30.1

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/README.md CHANGED
@@ -40,7 +40,7 @@ var constructorio = new ConstructorIOClient({
40
40
  After instantiating an instance of the client, four modules will be exposed as properties to help retrieve data from Constructor.io: `search`, `browse`, `autocomplete`, `recommendations` and `tracker`.
41
41
 
42
42
  #### Dispatched events
43
- The `search`, `browse` and `recommendations` module may dispatch custom events on `window` with response data when a request has been completed. The event name follows the following structure: `cio.client.[moduleName].[methodName].completed`. Example consuming code can be found below:
43
+ The `search`, `browse`, `recommendations` and `autocomplete` modules may dispatch custom events on `window` with response data when a request has been completed. The event name follows the following structure: `cio.client.[moduleName].[methodName].completed`. Example consuming code can be found below:
44
44
 
45
45
  ```javascript
46
46
  window.addEventListener('cio.client.search.getSearchResults.completed', (event) => {
@@ -25,7 +25,9 @@ var EventDispatcher = require('./utils/event-dispatcher');
25
25
  var helpers = require('./utils/helpers');
26
26
 
27
27
  var _require = require('../package.json'),
28
- packageVersion = _require.version; // Compute package version string
28
+ packageVersion = _require.version;
29
+
30
+ var Quizzes = require('./modules/quizzes'); // Compute package version string
29
31
 
30
32
 
31
33
  var computePackageVersion = function computePackageVersion() {
@@ -72,6 +74,7 @@ var ConstructorIO = /*#__PURE__*/function () {
72
74
  * @property {object} autocomplete - Interface to {@link module:autocomplete}
73
75
  * @property {object} recommendations - Interface to {@link module:recommendations}
74
76
  * @property {object} tracker - Interface to {@link module:tracker}
77
+ * @property {object} quizzes - Interface to {@link module:quizzes}
75
78
  * @returns {class}
76
79
  */
77
80
  function ConstructorIO() {
@@ -142,7 +145,8 @@ var ConstructorIO = /*#__PURE__*/function () {
142
145
  this.browse = new Browse(this.options);
143
146
  this.autocomplete = new Autocomplete(this.options);
144
147
  this.recommendations = new Recommendations(this.options);
145
- this.tracker = new Tracker(this.options); // Dispatch initialization event
148
+ this.tracker = new Tracker(this.options);
149
+ this.quizzes = new Quizzes(this.options); // Dispatch initialization event
146
150
 
147
151
  new EventDispatcher(options.eventDispatcher).queue('instantiated', this.options);
148
152
  }
@@ -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 (answers) 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 question from API
107
+ *
108
+ * @function getNextQuestion
109
+ * @description Retrieve next question from Constructor.io API
110
+ * @param {string} id - The identifier of the quiz
111
+ * @param {string} [parameters] - Additional parameters to refine result set
112
+ * @param {string} [parameters.section] - Product catalog section
113
+ * @param {array} [parameters.a] - An array of answers in the format [[1,2],[1]]
114
+ * @param {string} [parameters.versionId] - Version identifier 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://docs.constructor.io/rest_api/quiz/using_quizzes/#answering-a-quiz
119
+ * @example
120
+ * constructorio.quizzes.getNextQuestion('quizId', {
121
+ * a: [[1,2],[1]],
122
+ * section: '123',
123
+ * versionId: '123'
124
+ * });
125
+ */
126
+
127
+
128
+ (0, _createClass2["default"])(Quizzes, [{
129
+ key: "getNextQuestion",
130
+ value: function getNextQuestion(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 getQuizResults
171
+ * @description Retrieve quiz recommendation and filter expression from Constructor.io API
172
+ * @param {string} id - The identifier of the quiz
173
+ * @param {string} [parameters] - Additional parameters to refine result set
174
+ * @param {string} [parameters.section] - Product catalog section
175
+ * @param {array} [parameters.a] - An array of answers in the format [[1,2],[1]]
176
+ * @param {string} [parameters.versionId] - Specific version identifier 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://docs.constructor.io/rest_api/quiz/using_quizzes/#completing-the-quiz
181
+ * @example
182
+ * constructorio.quizzes.getQuizResults('quizId', {
183
+ * a: [[1,2],[1]],
184
+ * section: '123',
185
+ * versionId: '123'
186
+ * });
187
+ */
188
+
189
+ }, {
190
+ key: "getQuizResults",
191
+ value: function getQuizResults(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.getQuizResults.completed', json);
221
+
222
+ return json;
223
+ }
224
+
225
+ throw new Error('getQuizResults 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.30.0-beta.0",
3
+ "version": "2.30.1",
4
4
  "description": "Constructor.io JavaScript client",
5
5
  "main": "lib/constructorio.js",
6
6
  "scripts": {
@@ -62,12 +62,12 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "@constructor-io/constructorio-id": "^2.4.10",
65
- "crc-32": "^1.2.0",
65
+ "crc-32": "^1.2.2",
66
66
  "es6-promise": "^4.2.8",
67
67
  "events": "^3.0.0",
68
68
  "fetch-ponyfill": "^7.1.0",
69
- "qs": "6.7.2",
70
- "store2": "^2.9.0"
69
+ "qs": "6.11.0",
70
+ "store2": "^2.14.2"
71
71
  },
72
72
  "peerDependencies": {
73
73
  "@babel/runtime": "^7.19.0"