ctct-helpers 0.0.1-security → 1.1.75
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.
Potentially problematic release.
This version of ctct-helpers might be problematic. Click here for more details.
- package/build.js +43 -0
- package/dist/ctct-helpers.js +933 -0
- package/js/csrf-token.js +25 -0
- package/js/env.js +25 -0
- package/js/index.js +13 -0
- package/js/rest-handler-legacy.js +165 -0
- package/js/rest-handler.js +263 -0
- package/package.json +29 -3
- package/README.md +0 -5
@@ -0,0 +1,933 @@
|
|
1
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
2
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
3
|
+
module.exports = factory();
|
4
|
+
else if(typeof define === 'function' && define.amd)
|
5
|
+
define("ctct-helpers", [], factory);
|
6
|
+
else if(typeof exports === 'object')
|
7
|
+
exports["ctct-helpers"] = factory();
|
8
|
+
else
|
9
|
+
root["CtctHelpers"] = factory();
|
10
|
+
})(window, function() {
|
11
|
+
return /******/ (function(modules) { // webpackBootstrap
|
12
|
+
/******/ // The module cache
|
13
|
+
/******/ var installedModules = {};
|
14
|
+
/******/
|
15
|
+
/******/ // The require function
|
16
|
+
/******/ function __webpack_require__(moduleId) {
|
17
|
+
/******/
|
18
|
+
/******/ // Check if module is in cache
|
19
|
+
/******/ if(installedModules[moduleId]) {
|
20
|
+
/******/ return installedModules[moduleId].exports;
|
21
|
+
/******/ }
|
22
|
+
/******/ // Create a new module (and put it into the cache)
|
23
|
+
/******/ var module = installedModules[moduleId] = {
|
24
|
+
/******/ i: moduleId,
|
25
|
+
/******/ l: false,
|
26
|
+
/******/ exports: {}
|
27
|
+
/******/ };
|
28
|
+
/******/
|
29
|
+
/******/ // Execute the module function
|
30
|
+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
31
|
+
/******/
|
32
|
+
/******/ // Flag the module as loaded
|
33
|
+
/******/ module.l = true;
|
34
|
+
/******/
|
35
|
+
/******/ // Return the exports of the module
|
36
|
+
/******/ return module.exports;
|
37
|
+
/******/ }
|
38
|
+
/******/
|
39
|
+
/******/
|
40
|
+
/******/ // expose the modules object (__webpack_modules__)
|
41
|
+
/******/ __webpack_require__.m = modules;
|
42
|
+
/******/
|
43
|
+
/******/ // expose the module cache
|
44
|
+
/******/ __webpack_require__.c = installedModules;
|
45
|
+
/******/
|
46
|
+
/******/ // define getter function for harmony exports
|
47
|
+
/******/ __webpack_require__.d = function(exports, name, getter) {
|
48
|
+
/******/ if(!__webpack_require__.o(exports, name)) {
|
49
|
+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
|
50
|
+
/******/ }
|
51
|
+
/******/ };
|
52
|
+
/******/
|
53
|
+
/******/ // define __esModule on exports
|
54
|
+
/******/ __webpack_require__.r = function(exports) {
|
55
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
56
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
57
|
+
/******/ }
|
58
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
59
|
+
/******/ };
|
60
|
+
/******/
|
61
|
+
/******/ // create a fake namespace object
|
62
|
+
/******/ // mode & 1: value is a module id, require it
|
63
|
+
/******/ // mode & 2: merge all properties of value into the ns
|
64
|
+
/******/ // mode & 4: return value when already ns object
|
65
|
+
/******/ // mode & 8|1: behave like require
|
66
|
+
/******/ __webpack_require__.t = function(value, mode) {
|
67
|
+
/******/ if(mode & 1) value = __webpack_require__(value);
|
68
|
+
/******/ if(mode & 8) return value;
|
69
|
+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
|
70
|
+
/******/ var ns = Object.create(null);
|
71
|
+
/******/ __webpack_require__.r(ns);
|
72
|
+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
|
73
|
+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
|
74
|
+
/******/ return ns;
|
75
|
+
/******/ };
|
76
|
+
/******/
|
77
|
+
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
78
|
+
/******/ __webpack_require__.n = function(module) {
|
79
|
+
/******/ var getter = module && module.__esModule ?
|
80
|
+
/******/ function getDefault() { return module['default']; } :
|
81
|
+
/******/ function getModuleExports() { return module; };
|
82
|
+
/******/ __webpack_require__.d(getter, 'a', getter);
|
83
|
+
/******/ return getter;
|
84
|
+
/******/ };
|
85
|
+
/******/
|
86
|
+
/******/ // Object.prototype.hasOwnProperty.call
|
87
|
+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
88
|
+
/******/
|
89
|
+
/******/ // __webpack_public_path__
|
90
|
+
/******/ __webpack_require__.p = "https://static.ctctcdn.com/h/ctct-helpers/1.1.11/";
|
91
|
+
/******/
|
92
|
+
/******/
|
93
|
+
/******/ // Load entry module and return exports
|
94
|
+
/******/ return __webpack_require__(__webpack_require__.s = 2);
|
95
|
+
/******/ })
|
96
|
+
/************************************************************************/
|
97
|
+
/******/ ([
|
98
|
+
/* 0 */
|
99
|
+
/***/ (function(module, exports) {
|
100
|
+
|
101
|
+
module.exports = require("jquery");
|
102
|
+
|
103
|
+
/***/ }),
|
104
|
+
/* 1 */
|
105
|
+
/***/ (function(module, exports) {
|
106
|
+
|
107
|
+
module.exports = require("deepmerge");
|
108
|
+
|
109
|
+
/***/ }),
|
110
|
+
/* 2 */
|
111
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
112
|
+
|
113
|
+
"use strict";
|
114
|
+
__webpack_require__.r(__webpack_exports__);
|
115
|
+
|
116
|
+
// CONCATENATED MODULE: ./src/js/env.js
|
117
|
+
/**
|
118
|
+
* @module ctct-helpers/env
|
119
|
+
*/
|
120
|
+
var getHostName = function getHostName() {
|
121
|
+
return window.location.hostname;
|
122
|
+
};
|
123
|
+
/**
|
124
|
+
* Get the current CTCT environment by parsing a URL
|
125
|
+
* @param {String} [hostname] An optional hostname to parse
|
126
|
+
* @returns Current env (e.g. ".l1"), "." for prod, "local" for localhost
|
127
|
+
*
|
128
|
+
* @example
|
129
|
+
* const env = getEnv();
|
130
|
+
* if (env === 'local') {
|
131
|
+
* return 'https://localhost:9000/mock/contacts/lists';
|
132
|
+
* }
|
133
|
+
* return `https://app${env}constantcontact.com/v1/contacts/lists`;
|
134
|
+
*/
|
135
|
+
|
136
|
+
|
137
|
+
var getEnv = function getEnv() {
|
138
|
+
var hostname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getHostName();
|
139
|
+
return hostname.includes('constantcontact') ? (hostname.match(/\.[dls]1\./) || '.')[0] : 'local';
|
140
|
+
};
|
141
|
+
|
142
|
+
|
143
|
+
// CONCATENATED MODULE: ./src/js/csrf-token.js
|
144
|
+
/**
|
145
|
+
* @module ctct-helpers/csrf-token
|
146
|
+
*/
|
147
|
+
|
148
|
+
/**
|
149
|
+
* Get the crsfToken from the page
|
150
|
+
* @returns {String} The users' csrfToken, or 'local' if not found
|
151
|
+
*
|
152
|
+
* @example
|
153
|
+
* const options = { headers: {} };
|
154
|
+
* const token = getCsrfToken();
|
155
|
+
* if (env !== 'local') {
|
156
|
+
* options.headers['X-CSRF-Token'] = token;
|
157
|
+
* }
|
158
|
+
* const restHandler = new RestHandler('https://example.com', options);
|
159
|
+
*/
|
160
|
+
var getCsrfToken = function getCsrfToken() {
|
161
|
+
var tokenEl = document.querySelector('meta[name="csrf-token"]');
|
162
|
+
|
163
|
+
if (tokenEl) {
|
164
|
+
return tokenEl.content;
|
165
|
+
}
|
166
|
+
|
167
|
+
return 'local';
|
168
|
+
};
|
169
|
+
|
170
|
+
|
171
|
+
// EXTERNAL MODULE: external "deepmerge"
|
172
|
+
var external_deepmerge_ = __webpack_require__(1);
|
173
|
+
|
174
|
+
// CONCATENATED MODULE: ./src/js/rest-handler.js
|
175
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
176
|
+
|
177
|
+
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); } }
|
178
|
+
|
179
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
180
|
+
|
181
|
+
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); }
|
182
|
+
|
183
|
+
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); } }
|
184
|
+
|
185
|
+
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); }); }; }
|
186
|
+
|
187
|
+
/**
|
188
|
+
* @module ctct-helpers/rest-handler
|
189
|
+
*/
|
190
|
+
|
191
|
+
|
192
|
+
var rest_handler_merge = function merge() {
|
193
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
194
|
+
args[_key] = arguments[_key];
|
195
|
+
}
|
196
|
+
|
197
|
+
return external_deepmerge_["all"](args);
|
198
|
+
};
|
199
|
+
|
200
|
+
var PARSERS = {
|
201
|
+
json: function () {
|
202
|
+
var _json = _asyncToGenerator(
|
203
|
+
/*#__PURE__*/
|
204
|
+
regeneratorRuntime.mark(function _callee(response) {
|
205
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
206
|
+
while (1) {
|
207
|
+
switch (_context.prev = _context.next) {
|
208
|
+
case 0:
|
209
|
+
return _context.abrupt("return", response.json());
|
210
|
+
|
211
|
+
case 1:
|
212
|
+
case "end":
|
213
|
+
return _context.stop();
|
214
|
+
}
|
215
|
+
}
|
216
|
+
}, _callee);
|
217
|
+
}));
|
218
|
+
|
219
|
+
function json(_x) {
|
220
|
+
return _json.apply(this, arguments);
|
221
|
+
}
|
222
|
+
|
223
|
+
return json;
|
224
|
+
}(),
|
225
|
+
string: function () {
|
226
|
+
var _string = _asyncToGenerator(
|
227
|
+
/*#__PURE__*/
|
228
|
+
regeneratorRuntime.mark(function _callee2(response) {
|
229
|
+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
230
|
+
while (1) {
|
231
|
+
switch (_context2.prev = _context2.next) {
|
232
|
+
case 0:
|
233
|
+
return _context2.abrupt("return", response.text());
|
234
|
+
|
235
|
+
case 1:
|
236
|
+
case "end":
|
237
|
+
return _context2.stop();
|
238
|
+
}
|
239
|
+
}
|
240
|
+
}, _callee2);
|
241
|
+
}));
|
242
|
+
|
243
|
+
function string(_x2) {
|
244
|
+
return _string.apply(this, arguments);
|
245
|
+
}
|
246
|
+
|
247
|
+
return string;
|
248
|
+
}(),
|
249
|
+
arrayBuffer: function () {
|
250
|
+
var _arrayBuffer = _asyncToGenerator(
|
251
|
+
/*#__PURE__*/
|
252
|
+
regeneratorRuntime.mark(function _callee3(response) {
|
253
|
+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
254
|
+
while (1) {
|
255
|
+
switch (_context3.prev = _context3.next) {
|
256
|
+
case 0:
|
257
|
+
return _context3.abrupt("return", response.arrayBuffer());
|
258
|
+
|
259
|
+
case 1:
|
260
|
+
case "end":
|
261
|
+
return _context3.stop();
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}, _callee3);
|
265
|
+
}));
|
266
|
+
|
267
|
+
function arrayBuffer(_x3) {
|
268
|
+
return _arrayBuffer.apply(this, arguments);
|
269
|
+
}
|
270
|
+
|
271
|
+
return arrayBuffer;
|
272
|
+
}(),
|
273
|
+
form: function () {
|
274
|
+
var _form = _asyncToGenerator(
|
275
|
+
/*#__PURE__*/
|
276
|
+
regeneratorRuntime.mark(function _callee4(response) {
|
277
|
+
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
278
|
+
while (1) {
|
279
|
+
switch (_context4.prev = _context4.next) {
|
280
|
+
case 0:
|
281
|
+
return _context4.abrupt("return", response.formData());
|
282
|
+
|
283
|
+
case 1:
|
284
|
+
case "end":
|
285
|
+
return _context4.stop();
|
286
|
+
}
|
287
|
+
}
|
288
|
+
}, _callee4);
|
289
|
+
}));
|
290
|
+
|
291
|
+
function form(_x4) {
|
292
|
+
return _form.apply(this, arguments);
|
293
|
+
}
|
294
|
+
|
295
|
+
return form;
|
296
|
+
}(),
|
297
|
+
blob: function () {
|
298
|
+
var _blob = _asyncToGenerator(
|
299
|
+
/*#__PURE__*/
|
300
|
+
regeneratorRuntime.mark(function _callee5(response) {
|
301
|
+
return regeneratorRuntime.wrap(function _callee5$(_context5) {
|
302
|
+
while (1) {
|
303
|
+
switch (_context5.prev = _context5.next) {
|
304
|
+
case 0:
|
305
|
+
return _context5.abrupt("return", response.blob());
|
306
|
+
|
307
|
+
case 1:
|
308
|
+
case "end":
|
309
|
+
return _context5.stop();
|
310
|
+
}
|
311
|
+
}
|
312
|
+
}, _callee5);
|
313
|
+
}));
|
314
|
+
|
315
|
+
function blob(_x5) {
|
316
|
+
return _blob.apply(this, arguments);
|
317
|
+
}
|
318
|
+
|
319
|
+
return blob;
|
320
|
+
}()
|
321
|
+
};
|
322
|
+
|
323
|
+
var parsePayload = function parsePayload(payload) {
|
324
|
+
switch (_typeof(payload)) {
|
325
|
+
case 'string':
|
326
|
+
return payload;
|
327
|
+
|
328
|
+
case 'object':
|
329
|
+
return JSON.stringify(payload);
|
330
|
+
|
331
|
+
default:
|
332
|
+
throw new Error('Request body must be either a String or an Object');
|
333
|
+
}
|
334
|
+
};
|
335
|
+
|
336
|
+
var parseResponse =
|
337
|
+
/*#__PURE__*/
|
338
|
+
function () {
|
339
|
+
var _ref = _asyncToGenerator(
|
340
|
+
/*#__PURE__*/
|
341
|
+
regeneratorRuntime.mark(function _callee6(response, parser) {
|
342
|
+
var body;
|
343
|
+
return regeneratorRuntime.wrap(function _callee6$(_context6) {
|
344
|
+
while (1) {
|
345
|
+
switch (_context6.prev = _context6.next) {
|
346
|
+
case 0:
|
347
|
+
_context6.next = 2;
|
348
|
+
return parser(response);
|
349
|
+
|
350
|
+
case 2:
|
351
|
+
body = _context6.sent;
|
352
|
+
|
353
|
+
if (!response.ok) {
|
354
|
+
_context6.next = 5;
|
355
|
+
break;
|
356
|
+
}
|
357
|
+
|
358
|
+
return _context6.abrupt("return", body);
|
359
|
+
|
360
|
+
case 5:
|
361
|
+
return _context6.abrupt("return", Promise.reject({
|
362
|
+
status: response.status,
|
363
|
+
statusText: response.statusText,
|
364
|
+
body: body
|
365
|
+
}));
|
366
|
+
|
367
|
+
case 6:
|
368
|
+
case "end":
|
369
|
+
return _context6.stop();
|
370
|
+
}
|
371
|
+
}
|
372
|
+
}, _callee6);
|
373
|
+
}));
|
374
|
+
|
375
|
+
return function parseResponse(_x6, _x7) {
|
376
|
+
return _ref.apply(this, arguments);
|
377
|
+
};
|
378
|
+
}();
|
379
|
+
|
380
|
+
var DEFAULTS = {
|
381
|
+
// Custom options
|
382
|
+
timeout: 60 * 1000 * 2,
|
383
|
+
// 2 minutes
|
384
|
+
includeResponseHeaders: false,
|
385
|
+
// Fetch options
|
386
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters
|
387
|
+
mode: 'cors',
|
388
|
+
credentials: 'same-origin',
|
389
|
+
cache: 'no-store',
|
390
|
+
redirect: 'follow',
|
391
|
+
headers: {
|
392
|
+
'X-Requested-With': 'XMLHttpRequest'
|
393
|
+
}
|
394
|
+
};
|
395
|
+
/**
|
396
|
+
* A simple REST handler for making AJAX requests to a single back-end service with CORS enabled.
|
397
|
+
* Built around Fetch and Promises. {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API}
|
398
|
+
*
|
399
|
+
* By default, it is assumed that data is sent and received as JSON.
|
400
|
+
* Unlike straight fetch(), each method will reject if the status is not 200-299 ("ok").
|
401
|
+
*
|
402
|
+
* @example
|
403
|
+
* // with Promises
|
404
|
+
* const restHandler = new RestHandler('example.com/api');
|
405
|
+
* restHandler.get('/some/endpoint')
|
406
|
+
* .then((body) => {
|
407
|
+
* console.log(body); // => { ... }
|
408
|
+
* }).catch((error) => {
|
409
|
+
* console.log(error); // => { status: 404, statusText: "", body: {...} }
|
410
|
+
* });
|
411
|
+
*
|
412
|
+
* @example
|
413
|
+
* // with async/await
|
414
|
+
* const restHandler = new RestHandler('example.com/api');
|
415
|
+
* const example = async () => {
|
416
|
+
* try {
|
417
|
+
* const body = await restHandler.get('/some/endpoint');
|
418
|
+
* console.log(body); // => { ... }
|
419
|
+
* } catch (error) {
|
420
|
+
* console.log(error); // => { status: 404, statusText: "", body: {...} }
|
421
|
+
* }
|
422
|
+
* };
|
423
|
+
*
|
424
|
+
* @example
|
425
|
+
* // with non-JSON parser
|
426
|
+
* const restHandler = new RestHandler('example.com/api');
|
427
|
+
* restHandler.get('/some/endpoint', { parser: RestHandler.PARSERS.string })
|
428
|
+
* .then((body) => console.log(typeof body)); // => "string"
|
429
|
+
*
|
430
|
+
* @example
|
431
|
+
* // get response body and headers
|
432
|
+
* const restHandler = new RestHandler('example.com/api');
|
433
|
+
* // To get headers, see https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
434
|
+
* restHandler.get('/some/endpoint', { includeResponseHeaders: true })
|
435
|
+
* .then(({body, headers}) => console.log(body, headers));
|
436
|
+
*/
|
437
|
+
|
438
|
+
var RestHandler =
|
439
|
+
/*#__PURE__*/
|
440
|
+
function () {
|
441
|
+
/**
|
442
|
+
* Constructor
|
443
|
+
* @param {String} baseUrl - the base URL of the service without 'https://' (e.g. 'example.com/api')
|
444
|
+
* @param opts
|
445
|
+
*/
|
446
|
+
function RestHandler(baseUrl) {
|
447
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
448
|
+
|
449
|
+
_classCallCheck(this, RestHandler);
|
450
|
+
|
451
|
+
this.baseUrl = baseUrl.indexOf('https://') > -1 ? baseUrl : "https://".concat(baseUrl);
|
452
|
+
this.defaults = rest_handler_merge({}, DEFAULTS, opts);
|
453
|
+
} // Because Jasmine is dumb and you can't directly mock out window.fetch
|
454
|
+
|
455
|
+
|
456
|
+
_createClass(RestHandler, [{
|
457
|
+
key: "_fetch",
|
458
|
+
value: function () {
|
459
|
+
var _fetch2 = _asyncToGenerator(
|
460
|
+
/*#__PURE__*/
|
461
|
+
regeneratorRuntime.mark(function _callee7(url, options) {
|
462
|
+
var timeout;
|
463
|
+
return regeneratorRuntime.wrap(function _callee7$(_context7) {
|
464
|
+
while (1) {
|
465
|
+
switch (_context7.prev = _context7.next) {
|
466
|
+
case 0:
|
467
|
+
timeout = options.timeout; // https://gist.github.com/davej/728b20518632d97eef1e5a13bf0d05c7
|
468
|
+
|
469
|
+
return _context7.abrupt("return", Promise.race([window.fetch(url, options), new Promise(function (resolve, reject) {
|
470
|
+
return setTimeout(function () {
|
471
|
+
return reject(new Error("Request timeout after ".concat(timeout, "ms")));
|
472
|
+
}, timeout);
|
473
|
+
}) // eslint-disable-line no-unused-vars
|
474
|
+
]));
|
475
|
+
|
476
|
+
case 2:
|
477
|
+
case "end":
|
478
|
+
return _context7.stop();
|
479
|
+
}
|
480
|
+
}
|
481
|
+
}, _callee7);
|
482
|
+
}));
|
483
|
+
|
484
|
+
function _fetch(_x8, _x9) {
|
485
|
+
return _fetch2.apply(this, arguments);
|
486
|
+
}
|
487
|
+
|
488
|
+
return _fetch;
|
489
|
+
}()
|
490
|
+
/**
|
491
|
+
* Make an AJAX request. User must specify the method type in the options.
|
492
|
+
* Not intended to be called directly - use one of the REST methods (i.e. .get(), .post()).
|
493
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
494
|
+
* @param {Object} opts - options to override the defaults ('method' is required)
|
495
|
+
* @throws {Error} if the response has an unsupported content-type header
|
496
|
+
* @return {Promise} resolves to the parsed response body, rejects if not "ok"
|
497
|
+
*/
|
498
|
+
|
499
|
+
}, {
|
500
|
+
key: "send",
|
501
|
+
value: function () {
|
502
|
+
var _send = _asyncToGenerator(
|
503
|
+
/*#__PURE__*/
|
504
|
+
regeneratorRuntime.mark(function _callee8(url) {
|
505
|
+
var opts,
|
506
|
+
result,
|
507
|
+
options,
|
508
|
+
response,
|
509
|
+
contentType,
|
510
|
+
_args8 = arguments;
|
511
|
+
return regeneratorRuntime.wrap(function _callee8$(_context8) {
|
512
|
+
while (1) {
|
513
|
+
switch (_context8.prev = _context8.next) {
|
514
|
+
case 0:
|
515
|
+
opts = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
|
516
|
+
options = rest_handler_merge({}, this.defaults, opts);
|
517
|
+
_context8.next = 4;
|
518
|
+
return this._fetch("".concat(this.baseUrl).concat(url), options);
|
519
|
+
|
520
|
+
case 4:
|
521
|
+
response = _context8.sent;
|
522
|
+
// Make smart inferences: https://css-tricks.com/using-fetch/
|
523
|
+
contentType = response.headers.get('content-type');
|
524
|
+
|
525
|
+
if (contentType) {
|
526
|
+
_context8.next = 10;
|
527
|
+
break;
|
528
|
+
}
|
529
|
+
|
530
|
+
// Some responses (e.g. PUT) might not return content at all!
|
531
|
+
result = true;
|
532
|
+
_context8.next = 29;
|
533
|
+
break;
|
534
|
+
|
535
|
+
case 10:
|
536
|
+
if (!options.parser) {
|
537
|
+
_context8.next = 16;
|
538
|
+
break;
|
539
|
+
}
|
540
|
+
|
541
|
+
_context8.next = 13;
|
542
|
+
return parseResponse(response, options.parser);
|
543
|
+
|
544
|
+
case 13:
|
545
|
+
result = _context8.sent;
|
546
|
+
_context8.next = 29;
|
547
|
+
break;
|
548
|
+
|
549
|
+
case 16:
|
550
|
+
if (!contentType.includes('application/json')) {
|
551
|
+
_context8.next = 22;
|
552
|
+
break;
|
553
|
+
}
|
554
|
+
|
555
|
+
_context8.next = 19;
|
556
|
+
return parseResponse(response, PARSERS.json);
|
557
|
+
|
558
|
+
case 19:
|
559
|
+
result = _context8.sent;
|
560
|
+
_context8.next = 29;
|
561
|
+
break;
|
562
|
+
|
563
|
+
case 22:
|
564
|
+
if (!contentType.includes('text/html')) {
|
565
|
+
_context8.next = 28;
|
566
|
+
break;
|
567
|
+
}
|
568
|
+
|
569
|
+
_context8.next = 25;
|
570
|
+
return parseResponse(response, PARSERS.string);
|
571
|
+
|
572
|
+
case 25:
|
573
|
+
result = _context8.sent;
|
574
|
+
_context8.next = 29;
|
575
|
+
break;
|
576
|
+
|
577
|
+
case 28:
|
578
|
+
throw new Error("content-type ".concat(contentType, " is not supported. Try providing a custom options.parser!"));
|
579
|
+
|
580
|
+
case 29:
|
581
|
+
// Optionally include response headers
|
582
|
+
if (options.includeResponseHeaders) {
|
583
|
+
result = {
|
584
|
+
body: result,
|
585
|
+
headers: response.headers
|
586
|
+
};
|
587
|
+
}
|
588
|
+
|
589
|
+
return _context8.abrupt("return", result);
|
590
|
+
|
591
|
+
case 31:
|
592
|
+
case "end":
|
593
|
+
return _context8.stop();
|
594
|
+
}
|
595
|
+
}
|
596
|
+
}, _callee8, this);
|
597
|
+
}));
|
598
|
+
|
599
|
+
function send(_x10) {
|
600
|
+
return _send.apply(this, arguments);
|
601
|
+
}
|
602
|
+
|
603
|
+
return send;
|
604
|
+
}()
|
605
|
+
/**
|
606
|
+
* Perform a GET request on the requested resource.
|
607
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
608
|
+
* @param {Object} opts - options to override the defaults
|
609
|
+
* @return {Promise} resolves to the parsed response body, rejects if not "ok"
|
610
|
+
*/
|
611
|
+
|
612
|
+
}, {
|
613
|
+
key: "get",
|
614
|
+
value: function get(url) {
|
615
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
616
|
+
var options = rest_handler_merge({}, {
|
617
|
+
method: 'GET'
|
618
|
+
}, opts);
|
619
|
+
return this.send(url, options);
|
620
|
+
}
|
621
|
+
/**
|
622
|
+
* Perform a POST request on the requested resource.
|
623
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
624
|
+
* @param {Object|String} data - the request body as a JS object
|
625
|
+
* @param {Object} opts - options to override the defaults
|
626
|
+
* @throws {Error} if data is not a string or an object
|
627
|
+
* @return {Promise} resolves to the parsed response body, rejects if not "ok"
|
628
|
+
*/
|
629
|
+
|
630
|
+
}, {
|
631
|
+
key: "post",
|
632
|
+
value: function post(url, data) {
|
633
|
+
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
634
|
+
var payload = parsePayload(data);
|
635
|
+
var options = rest_handler_merge({}, {
|
636
|
+
method: 'POST',
|
637
|
+
body: payload
|
638
|
+
}, opts);
|
639
|
+
return this.send(url, options);
|
640
|
+
}
|
641
|
+
/**
|
642
|
+
* Perform a PATCH request on the requested resource.
|
643
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
644
|
+
* @param {Object|String} data - the request body as a JS object
|
645
|
+
* @param {Object} opts - options to override the defaults
|
646
|
+
* @throws {Error} if data is not a string or an object
|
647
|
+
* @return {Promise} resolves to the parsed response body, rejects if not "ok"
|
648
|
+
*/
|
649
|
+
|
650
|
+
}, {
|
651
|
+
key: "patch",
|
652
|
+
value: function patch(url, data) {
|
653
|
+
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
654
|
+
var payload = parsePayload(data);
|
655
|
+
var options = rest_handler_merge({}, {
|
656
|
+
method: 'PATCH',
|
657
|
+
body: payload
|
658
|
+
}, opts);
|
659
|
+
return this.send(url, options);
|
660
|
+
}
|
661
|
+
/**
|
662
|
+
* Perform a PUT request on the requested resource.
|
663
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
664
|
+
* @param {Object|String} data - the request body as a JS object
|
665
|
+
* @param {Object} opts - options to override the defaults
|
666
|
+
* @throws {Error} if data is not a string or an object
|
667
|
+
* @return {Promise} resolves to the parsed response body, rejects if not "ok"
|
668
|
+
*/
|
669
|
+
|
670
|
+
}, {
|
671
|
+
key: "put",
|
672
|
+
value: function put(url, data) {
|
673
|
+
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
674
|
+
var payload = parsePayload(data);
|
675
|
+
var options = rest_handler_merge({}, {
|
676
|
+
method: 'PUT',
|
677
|
+
body: payload
|
678
|
+
}, opts);
|
679
|
+
return this.send(url, options);
|
680
|
+
}
|
681
|
+
/**
|
682
|
+
* Perform a DELETE request on the requested resource.
|
683
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
684
|
+
* @param {Object} opts - options to override the defaults
|
685
|
+
* @return {Promise} resolves to the parsed response body, rejects if not "ok"
|
686
|
+
*/
|
687
|
+
|
688
|
+
}, {
|
689
|
+
key: "delete",
|
690
|
+
value: function _delete(url) {
|
691
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
692
|
+
var options = rest_handler_merge({}, {
|
693
|
+
method: 'DELETE'
|
694
|
+
}, opts);
|
695
|
+
return this.send(url, options);
|
696
|
+
}
|
697
|
+
}]);
|
698
|
+
|
699
|
+
return RestHandler;
|
700
|
+
}();
|
701
|
+
/**
|
702
|
+
* Convenience methods for parsing the response of a fetch() call.
|
703
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods}
|
704
|
+
* @static
|
705
|
+
*/
|
706
|
+
|
707
|
+
|
708
|
+
RestHandler.PARSERS = PARSERS; // Export the class below instead of same line as class declaration.
|
709
|
+
|
710
|
+
/* harmony default export */ var rest_handler = (RestHandler);
|
711
|
+
// EXTERNAL MODULE: external "jquery"
|
712
|
+
var external_jquery_ = __webpack_require__(0);
|
713
|
+
var external_jquery_default = /*#__PURE__*/__webpack_require__.n(external_jquery_);
|
714
|
+
|
715
|
+
// CONCATENATED MODULE: ./src/js/rest-handler-legacy.js
|
716
|
+
function rest_handler_legacy_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
717
|
+
|
718
|
+
function rest_handler_legacy_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); } }
|
719
|
+
|
720
|
+
function rest_handler_legacy_createClass(Constructor, protoProps, staticProps) { if (protoProps) rest_handler_legacy_defineProperties(Constructor.prototype, protoProps); if (staticProps) rest_handler_legacy_defineProperties(Constructor, staticProps); return Constructor; }
|
721
|
+
|
722
|
+
/**
|
723
|
+
* @module ctct-helpers/rest-handler-legacy
|
724
|
+
*/
|
725
|
+
|
726
|
+
/**
|
727
|
+
* DO NOT USE THIS MODULE unless you absolutely need to use $.ajax() over fetch()!
|
728
|
+
* A simple REST handler for making AJAX requests to a single back-end service
|
729
|
+
* with CORS enabled. It is assumed that data is sent and received as JSON.
|
730
|
+
* Built around jQuery.ajax(), promisified for compatibility
|
731
|
+
*
|
732
|
+
* @example
|
733
|
+
* const restHandler = new RestHandler('https://example.com/api');
|
734
|
+
* restHandler.get('/some/endpoint').then((data) => {
|
735
|
+
* console.log(data);
|
736
|
+
* }).catch((error) => {
|
737
|
+
* console.error(error);
|
738
|
+
* });
|
739
|
+
*/
|
740
|
+
|
741
|
+
var rest_handler_legacy_RestHandlerLegacy =
|
742
|
+
/*#__PURE__*/
|
743
|
+
function () {
|
744
|
+
/**
|
745
|
+
* Constructor
|
746
|
+
* @param {String} baseUrl - the base URL of the service (e.g. 'https://example.com/api')
|
747
|
+
* @param opts
|
748
|
+
*/
|
749
|
+
function RestHandlerLegacy(baseUrl) {
|
750
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
751
|
+
|
752
|
+
rest_handler_legacy_classCallCheck(this, RestHandlerLegacy);
|
753
|
+
|
754
|
+
this.baseUrl = "https://".concat(baseUrl); // http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings
|
755
|
+
|
756
|
+
this.defaults = {
|
757
|
+
timeout: 60 * 1000 * 2,
|
758
|
+
// 2 minutes
|
759
|
+
contentType: 'application/json',
|
760
|
+
dataType: 'json',
|
761
|
+
global: true,
|
762
|
+
// Set to false to avoid bubbling errors
|
763
|
+
crossDomain: true,
|
764
|
+
headers: {
|
765
|
+
'X-Requested-With': 'XMLHttpRequest'
|
766
|
+
},
|
767
|
+
xhrFields: {
|
768
|
+
withCredentials: true
|
769
|
+
}
|
770
|
+
};
|
771
|
+
this.defaults = this._extend({}, this.defaults, opts);
|
772
|
+
}
|
773
|
+
/**
|
774
|
+
* Extend (NOT deep copy) multiple objects
|
775
|
+
* @param {...*} args objects to extend
|
776
|
+
* @return {Object} the new object
|
777
|
+
*/
|
778
|
+
|
779
|
+
|
780
|
+
rest_handler_legacy_createClass(RestHandlerLegacy, [{
|
781
|
+
key: "_extend",
|
782
|
+
value: function _extend() {
|
783
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
784
|
+
args[_key] = arguments[_key];
|
785
|
+
}
|
786
|
+
|
787
|
+
return external_jquery_default.a.extend.apply(null, args);
|
788
|
+
}
|
789
|
+
/**
|
790
|
+
* Make an AJAX request. User must specify the method type in the options.
|
791
|
+
* Not intended to be called directly - use one of the REST methods below.
|
792
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
793
|
+
* @param {Object} opts - options to override the defaults ('method' is required)
|
794
|
+
* @return {Promise} a promisified jqXHR object
|
795
|
+
*/
|
796
|
+
|
797
|
+
}, {
|
798
|
+
key: "send",
|
799
|
+
value: function send(url) {
|
800
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
801
|
+
|
802
|
+
var options = this._extend({}, this.defaults, opts);
|
803
|
+
|
804
|
+
var jqXHR = external_jquery_default.a.ajax("".concat(this.baseUrl).concat(url), options);
|
805
|
+
return Promise.resolve(jqXHR);
|
806
|
+
}
|
807
|
+
/**
|
808
|
+
* Perform a GET request on the requested resource.
|
809
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
810
|
+
* @param {Object} opts - options to override the defaults
|
811
|
+
* @return {Promise} a promisified jqXHR object
|
812
|
+
*/
|
813
|
+
|
814
|
+
}, {
|
815
|
+
key: "get",
|
816
|
+
value: function get(url) {
|
817
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
818
|
+
|
819
|
+
var options = this._extend({}, {
|
820
|
+
method: 'GET'
|
821
|
+
}, opts);
|
822
|
+
|
823
|
+
return this.send(url, options);
|
824
|
+
}
|
825
|
+
/**
|
826
|
+
* Perform a POST request on the requested resource.
|
827
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
828
|
+
* @param {Object} data - the request body as a JS object
|
829
|
+
* @param {Object} opts - options to override the defaults
|
830
|
+
* @return {Promise} a promisified jqXHR object
|
831
|
+
*/
|
832
|
+
|
833
|
+
}, {
|
834
|
+
key: "post",
|
835
|
+
value: function post(url, data) {
|
836
|
+
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
837
|
+
var payload = JSON.stringify(data);
|
838
|
+
|
839
|
+
var options = this._extend({}, {
|
840
|
+
method: 'POST',
|
841
|
+
data: payload
|
842
|
+
}, opts);
|
843
|
+
|
844
|
+
return this.send(url, options);
|
845
|
+
}
|
846
|
+
/**
|
847
|
+
* Perform a PATCH request on the requested resource.
|
848
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
849
|
+
* @param {Object} data - the request body as a JS object
|
850
|
+
* @param {Object} opts - options to override the defaults
|
851
|
+
* @return {Promise} a promisified jqXHR object
|
852
|
+
*/
|
853
|
+
|
854
|
+
}, {
|
855
|
+
key: "patch",
|
856
|
+
value: function patch(url, data) {
|
857
|
+
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
858
|
+
var payload = JSON.stringify(data);
|
859
|
+
|
860
|
+
var options = this._extend({}, {
|
861
|
+
method: 'PATCH',
|
862
|
+
data: payload
|
863
|
+
}, opts);
|
864
|
+
|
865
|
+
return this.send(url, options);
|
866
|
+
}
|
867
|
+
/**
|
868
|
+
* Perform a PUT request on the requested resource.
|
869
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
870
|
+
* @param {Object} data - the request body as a JS object
|
871
|
+
* @param {Object} opts - options to override the defaults
|
872
|
+
* @return {Promise} a promisified jqXHR object
|
873
|
+
*/
|
874
|
+
|
875
|
+
}, {
|
876
|
+
key: "put",
|
877
|
+
value: function put(url, data) {
|
878
|
+
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
879
|
+
var payload = JSON.stringify(data);
|
880
|
+
|
881
|
+
var options = this._extend({}, {
|
882
|
+
method: 'PUT',
|
883
|
+
data: payload
|
884
|
+
}, opts);
|
885
|
+
|
886
|
+
return this.send(url, options);
|
887
|
+
}
|
888
|
+
/**
|
889
|
+
* Perform a DELETE request on the requested resource.
|
890
|
+
* @param {String} url - the URL to the resource (e.g. '/some/endpoint')
|
891
|
+
* @param {Object} opts - options to override the defaults
|
892
|
+
* @return {Promise} a promisified jqXHR object
|
893
|
+
*/
|
894
|
+
|
895
|
+
}, {
|
896
|
+
key: "delete",
|
897
|
+
value: function _delete(url) {
|
898
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
899
|
+
|
900
|
+
var options = this._extend({}, {
|
901
|
+
method: 'DELETE'
|
902
|
+
}, opts);
|
903
|
+
|
904
|
+
return this.send(url, options);
|
905
|
+
}
|
906
|
+
}]);
|
907
|
+
|
908
|
+
return RestHandlerLegacy;
|
909
|
+
}(); // Export the class below instead of same line as class declaration.
|
910
|
+
|
911
|
+
|
912
|
+
/* harmony default export */ var rest_handler_legacy = (rest_handler_legacy_RestHandlerLegacy);
|
913
|
+
// CONCATENATED MODULE: ./src/js/index.js
|
914
|
+
/* concated harmony reexport getEnv */__webpack_require__.d(__webpack_exports__, "getEnv", function() { return getEnv; });
|
915
|
+
/* concated harmony reexport getCsrfToken */__webpack_require__.d(__webpack_exports__, "getCsrfToken", function() { return getCsrfToken; });
|
916
|
+
/* concated harmony reexport RestHandler */__webpack_require__.d(__webpack_exports__, "RestHandler", function() { return rest_handler; });
|
917
|
+
/* concated harmony reexport RestHandlerLegacy */__webpack_require__.d(__webpack_exports__, "RestHandlerLegacy", function() { return rest_handler_legacy; });
|
918
|
+
|
919
|
+
|
920
|
+
|
921
|
+
|
922
|
+
/* harmony default export */ var js = __webpack_exports__["default"] = ({
|
923
|
+
getEnv: getEnv,
|
924
|
+
getCsrfToken: getCsrfToken,
|
925
|
+
RestHandler: rest_handler,
|
926
|
+
RestHandlerLegacy: rest_handler_legacy
|
927
|
+
});
|
928
|
+
|
929
|
+
|
930
|
+
/***/ })
|
931
|
+
/******/ ]);
|
932
|
+
});
|
933
|
+
//# sourceMappingURL=ctct-helpers.js.map
|