@lacagnottedesproches/lcdp-js-lite 1.6.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.
@@ -0,0 +1,597 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.lcdpjs = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ function _arrayLikeToArray(r, a) {
8
+ (null == a || a > r.length) && (a = r.length);
9
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
10
+ return n;
11
+ }
12
+ function _arrayWithoutHoles(r) {
13
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
14
+ }
15
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
16
+ try {
17
+ var i = n[a](c),
18
+ u = i.value;
19
+ } catch (n) {
20
+ return void e(n);
21
+ }
22
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
23
+ }
24
+ function _asyncToGenerator(n) {
25
+ return function () {
26
+ var t = this,
27
+ e = arguments;
28
+ return new Promise(function (r, o) {
29
+ var a = n.apply(t, e);
30
+ function _next(n) {
31
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
32
+ }
33
+ function _throw(n) {
34
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
35
+ }
36
+ _next(void 0);
37
+ });
38
+ };
39
+ }
40
+ function _classCallCheck(a, n) {
41
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
42
+ }
43
+ function _construct(t, e, r) {
44
+ if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
45
+ var o = [null];
46
+ o.push.apply(o, e);
47
+ var p = new (t.bind.apply(t, o))();
48
+ return p;
49
+ }
50
+ function _createClass(e, r, t) {
51
+ return Object.defineProperty(e, "prototype", {
52
+ writable: false
53
+ }), e;
54
+ }
55
+ function _defineProperty(e, r, t) {
56
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
57
+ value: t,
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true
61
+ }) : e[r] = t, e;
62
+ }
63
+ function _isNativeReflectConstruct() {
64
+ try {
65
+ var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
66
+ } catch (t) {}
67
+ return (_isNativeReflectConstruct = function () {
68
+ return !!t;
69
+ })();
70
+ }
71
+ function _iterableToArray(r) {
72
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
73
+ }
74
+ function _nonIterableSpread() {
75
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
76
+ }
77
+ function _regenerator() {
78
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
79
+ var e,
80
+ t,
81
+ r = "function" == typeof Symbol ? Symbol : {},
82
+ n = r.iterator || "@@iterator",
83
+ o = r.toStringTag || "@@toStringTag";
84
+ function i(r, n, o, i) {
85
+ var c = n && n.prototype instanceof Generator ? n : Generator,
86
+ u = Object.create(c.prototype);
87
+ return _regeneratorDefine(u, "_invoke", function (r, n, o) {
88
+ var i,
89
+ c,
90
+ u,
91
+ f = 0,
92
+ p = o || [],
93
+ y = false,
94
+ G = {
95
+ p: 0,
96
+ n: 0,
97
+ v: e,
98
+ a: d,
99
+ f: d.bind(e, 4),
100
+ d: function (t, r) {
101
+ return i = t, c = 0, u = e, G.n = r, a;
102
+ }
103
+ };
104
+ function d(r, n) {
105
+ for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
106
+ var o,
107
+ i = p[t],
108
+ d = G.p,
109
+ l = i[2];
110
+ r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
111
+ }
112
+ if (o || r > 1) return a;
113
+ throw y = true, n;
114
+ }
115
+ return function (o, p, l) {
116
+ if (f > 1) throw TypeError("Generator is already running");
117
+ for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
118
+ i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
119
+ try {
120
+ if (f = 2, i) {
121
+ if (c || (o = "next"), t = i[o]) {
122
+ if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
123
+ if (!t.done) return t;
124
+ u = t.value, c < 2 && (c = 0);
125
+ } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
126
+ i = e;
127
+ } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
128
+ } catch (t) {
129
+ i = e, c = 1, u = t;
130
+ } finally {
131
+ f = 1;
132
+ }
133
+ }
134
+ return {
135
+ value: t,
136
+ done: y
137
+ };
138
+ };
139
+ }(r, o, i), true), u;
140
+ }
141
+ var a = {};
142
+ function Generator() {}
143
+ function GeneratorFunction() {}
144
+ function GeneratorFunctionPrototype() {}
145
+ t = Object.getPrototypeOf;
146
+ var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
147
+ return this;
148
+ }), t),
149
+ u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
150
+ function f(e) {
151
+ return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
152
+ }
153
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
154
+ return this;
155
+ }), _regeneratorDefine(u, "toString", function () {
156
+ return "[object Generator]";
157
+ }), (_regenerator = function () {
158
+ return {
159
+ w: i,
160
+ m: f
161
+ };
162
+ })();
163
+ }
164
+ function _regeneratorDefine(e, r, n, t) {
165
+ var i = Object.defineProperty;
166
+ try {
167
+ i({}, "", {});
168
+ } catch (e) {
169
+ i = 0;
170
+ }
171
+ _regeneratorDefine = function (e, r, n, t) {
172
+ function o(r, n) {
173
+ _regeneratorDefine(e, r, function (e) {
174
+ return this._invoke(r, n, e);
175
+ });
176
+ }
177
+ r ? i ? i(e, r, {
178
+ value: n,
179
+ enumerable: !t,
180
+ configurable: !t,
181
+ writable: !t
182
+ }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
183
+ }, _regeneratorDefine(e, r, n, t);
184
+ }
185
+ function _toConsumableArray(r) {
186
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
187
+ }
188
+ function _toPrimitive(t, r) {
189
+ if ("object" != typeof t || !t) return t;
190
+ var e = t[Symbol.toPrimitive];
191
+ if (void 0 !== e) {
192
+ var i = e.call(t, r);
193
+ if ("object" != typeof i) return i;
194
+ throw new TypeError("@@toPrimitive must return a primitive value.");
195
+ }
196
+ return ("string" === r ? String : Number)(t);
197
+ }
198
+ function _toPropertyKey(t) {
199
+ var i = _toPrimitive(t, "string");
200
+ return "symbol" == typeof i ? i : i + "";
201
+ }
202
+ function _unsupportedIterableToArray(r, a) {
203
+ if (r) {
204
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
205
+ var t = {}.toString.call(r).slice(8, -1);
206
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
207
+ }
208
+ }
209
+
210
+ var LCDPRequest = /*#__PURE__*/_createClass(function LCDPRequest(secret, apiUrl) {
211
+ var _this = this;
212
+ _classCallCheck(this, LCDPRequest);
213
+ _defineProperty(this, "_apiToken", void 0);
214
+ _defineProperty(this, "_apiUrl", void 0);
215
+ _defineProperty(this, "requestLCDPApi", function (endpoint, payload, authToken) {
216
+ var customMethod = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "POST";
217
+ if (typeof window !== "undefined") {
218
+ throw new Error("Must be used server side");
219
+ }
220
+ return new Promise(/*#__PURE__*/function () {
221
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(resolve, reject) {
222
+ var url, response, status, _yield$response$json, detail, data, _t;
223
+ return _regenerator().w(function (_context) {
224
+ while (1) switch (_context.p = _context.n) {
225
+ case 0:
226
+ url = "".concat(_this._apiUrl, "/").concat(endpoint);
227
+ _context.p = 1;
228
+ _context.n = 2;
229
+ return fetch(url, {
230
+ method: customMethod,
231
+ headers: {
232
+ "Content-Type": "application/json",
233
+ "User-Agent": "lcdp/v1",
234
+ Authorization: "Bearer ".concat(authToken)
235
+ },
236
+ body: JSON.stringify(payload)
237
+ });
238
+ case 2:
239
+ response = _context.v;
240
+ status = response.status;
241
+ if (!(status >= 400)) {
242
+ _context.n = 4;
243
+ break;
244
+ }
245
+ _context.n = 3;
246
+ return response.json();
247
+ case 3:
248
+ _yield$response$json = _context.v;
249
+ detail = _yield$response$json.detail;
250
+ reject(detail);
251
+ _context.n = 7;
252
+ break;
253
+ case 4:
254
+ if (!(status === 204)) {
255
+ _context.n = 5;
256
+ break;
257
+ }
258
+ resolve();
259
+ _context.n = 7;
260
+ break;
261
+ case 5:
262
+ _context.n = 6;
263
+ return response.json();
264
+ case 6:
265
+ data = _context.v;
266
+ resolve(data);
267
+ case 7:
268
+ _context.n = 9;
269
+ break;
270
+ case 8:
271
+ _context.p = 8;
272
+ _t = _context.v;
273
+ reject(_t);
274
+ case 9:
275
+ return _context.a(2);
276
+ }
277
+ }, _callee, null, [[1, 8]]);
278
+ }));
279
+ return function (_x, _x2) {
280
+ return _ref.apply(this, arguments);
281
+ };
282
+ }());
283
+ });
284
+ _defineProperty(this, "createAccount", /*#__PURE__*/function () {
285
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(payload) {
286
+ return _regenerator().w(function (_context3) {
287
+ while (1) switch (_context3.n) {
288
+ case 0:
289
+ return _context3.a(2, new Promise(/*#__PURE__*/function () {
290
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(resolve, reject) {
291
+ var responsePromise;
292
+ return _regenerator().w(function (_context2) {
293
+ while (1) switch (_context2.n) {
294
+ case 0:
295
+ responsePromise = _this.requestLCDPApi("accounts", payload, _this._apiToken);
296
+ return _context2.a(2, responsePromise.then(function (data) {
297
+ try {
298
+ // TODO: may be improved
299
+ resolve(data);
300
+ } catch (error) {
301
+ reject(error);
302
+ }
303
+ })["catch"](function (reason) {
304
+ return reject(reason);
305
+ }));
306
+ }
307
+ }, _callee2);
308
+ }));
309
+ return function (_x4, _x5) {
310
+ return _ref3.apply(this, arguments);
311
+ };
312
+ }()));
313
+ }
314
+ }, _callee3);
315
+ }));
316
+ return function (_x3) {
317
+ return _ref2.apply(this, arguments);
318
+ };
319
+ }());
320
+ _defineProperty(this, "createPot", /*#__PURE__*/function () {
321
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(payload) {
322
+ return _regenerator().w(function (_context5) {
323
+ while (1) switch (_context5.n) {
324
+ case 0:
325
+ return _context5.a(2, new Promise(/*#__PURE__*/function () {
326
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(resolve, reject) {
327
+ var responsePromise;
328
+ return _regenerator().w(function (_context4) {
329
+ while (1) switch (_context4.n) {
330
+ case 0:
331
+ responsePromise = _this.requestLCDPApi("pots", payload, _this._apiToken);
332
+ return _context4.a(2, responsePromise.then(function (data) {
333
+ try {
334
+ // TODO: may be improved
335
+ resolve(data);
336
+ } catch (error) {
337
+ reject(error);
338
+ }
339
+ })["catch"](function (reason) {
340
+ return reject(reason);
341
+ }));
342
+ }
343
+ }, _callee4);
344
+ }));
345
+ return function (_x7, _x8) {
346
+ return _ref5.apply(this, arguments);
347
+ };
348
+ }()));
349
+ }
350
+ }, _callee5);
351
+ }));
352
+ return function (_x6) {
353
+ return _ref4.apply(this, arguments);
354
+ };
355
+ }());
356
+ _defineProperty(this, "createAccountAndPot", /*#__PURE__*/function () {
357
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(payload) {
358
+ return _regenerator().w(function (_context7) {
359
+ while (1) switch (_context7.n) {
360
+ case 0:
361
+ return _context7.a(2, new Promise(/*#__PURE__*/function () {
362
+ var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(resolve, reject) {
363
+ var responsePromise;
364
+ return _regenerator().w(function (_context6) {
365
+ while (1) switch (_context6.n) {
366
+ case 0:
367
+ responsePromise = _this.requestLCDPApi("wizards", payload, _this._apiToken);
368
+ return _context6.a(2, responsePromise.then(function (data) {
369
+ try {
370
+ // TODO: may be improved
371
+ resolve(data);
372
+ } catch (error) {
373
+ reject(error);
374
+ }
375
+ })["catch"](function (reason) {
376
+ return reject(reason);
377
+ }));
378
+ }
379
+ }, _callee6);
380
+ }));
381
+ return function (_x0, _x1) {
382
+ return _ref7.apply(this, arguments);
383
+ };
384
+ }()));
385
+ }
386
+ }, _callee7);
387
+ }));
388
+ return function (_x9) {
389
+ return _ref6.apply(this, arguments);
390
+ };
391
+ }());
392
+ _defineProperty(this, "updatePot", /*#__PURE__*/function () {
393
+ var _ref8 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(payload) {
394
+ return _regenerator().w(function (_context9) {
395
+ while (1) switch (_context9.n) {
396
+ case 0:
397
+ return _context9.a(2, new Promise(/*#__PURE__*/function () {
398
+ var _ref9 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(resolve, reject) {
399
+ var responsePromise;
400
+ return _regenerator().w(function (_context8) {
401
+ while (1) switch (_context8.n) {
402
+ case 0:
403
+ responsePromise = _this.requestLCDPApi("pots", payload, _this._apiToken, "PATCH");
404
+ return _context8.a(2, responsePromise.then(function (_) {
405
+ try {
406
+ resolve();
407
+ } catch (error) {
408
+ reject(error);
409
+ }
410
+ })["catch"](function (reason) {
411
+ return reject(reason);
412
+ }));
413
+ }
414
+ }, _callee8);
415
+ }));
416
+ return function (_x11, _x12) {
417
+ return _ref9.apply(this, arguments);
418
+ };
419
+ }()));
420
+ }
421
+ }, _callee9);
422
+ }));
423
+ return function (_x10) {
424
+ return _ref8.apply(this, arguments);
425
+ };
426
+ }());
427
+ if (typeof window !== "undefined") {
428
+ throw new Error("Must be used server side");
429
+ }
430
+ this._apiToken = secret;
431
+ this._apiUrl = apiUrl;
432
+ });
433
+
434
+ exports.AfinaError = void 0;
435
+ (function (AfinaError) {
436
+ AfinaError["Verify"] = "Fail to verify origin request";
437
+ AfinaError["Stripe"] = "Stripe is not available";
438
+ AfinaError["ClientSide"] = "Should be on Client Side";
439
+ AfinaError["Key"] = "A publishable key is mandatory";
440
+ })(exports.AfinaError || (exports.AfinaError = {}));
441
+ exports.TokenObjectType = void 0;
442
+ (function (TokenObjectType) {
443
+ TokenObjectType["Account"] = "account";
444
+ TokenObjectType["Pot"] = "pot";
445
+ TokenObjectType["Wizard"] = "wizard";
446
+ })(exports.TokenObjectType || (exports.TokenObjectType = {}));
447
+
448
+ var lcdpPromise = null;
449
+ var API_URL = "https://api.lacagnottedesproches.fr";
450
+ var API_URL_REGEX = function API_URL_REGEX(url) {
451
+ return new RegExp("/^".concat(url, "?(.*)$/"));
452
+ };
453
+ var onErrorListener = null;
454
+ var onLoadListener = null;
455
+ var onError = function onError(reject) {
456
+ return function () {
457
+ reject(new Error("Failed to load lcdp.js"));
458
+ };
459
+ };
460
+ var onLoad = function onLoad(resolve, reject) {
461
+ return function () {
462
+ var _window$lcdp;
463
+ if ((_window$lcdp = window.lcdp) !== null && _window$lcdp !== void 0 && _window$lcdp.Afina) {
464
+ resolve(window.lcdp.Afina);
465
+ } else {
466
+ reject(new Error("lcdp.js not available"));
467
+ }
468
+ };
469
+ };
470
+ var findScript = function findScript(url, url_regex) {
471
+ var scripts = document.querySelectorAll("script[src^=\"".concat(url, "\"]"));
472
+ for (var i = 0; i < scripts.length; i++) {
473
+ var script = scripts[i];
474
+ if (!url_regex.test(script.src)) {
475
+ continue;
476
+ }
477
+ return script;
478
+ }
479
+ return null;
480
+ };
481
+ var injectScript = function injectScript(url) {
482
+ var script = document.createElement("script");
483
+ script.src = "".concat(url, "/js");
484
+ var headOrBody = document.head || document.body;
485
+ if (!headOrBody) {
486
+ throw new Error("Expected document.body not to be null. Stripe.js requires a <body> element.");
487
+ }
488
+ headOrBody.appendChild(script);
489
+ var divModalRoot = document.createElement("div");
490
+ divModalRoot.id = "lcdp-modal-root";
491
+ document.body.appendChild(divModalRoot);
492
+ return script;
493
+ };
494
+ var getLCDPPromise = function getLCDPPromise(args) {
495
+ if (lcdpPromise) {
496
+ return lcdpPromise;
497
+ }
498
+ lcdpPromise = new Promise(function (resolve, reject) {
499
+ var _window$lcdp2;
500
+ if (typeof window === "undefined" || typeof document === "undefined") {
501
+ resolve(null);
502
+ return;
503
+ }
504
+ if ((_window$lcdp2 = window.lcdp) !== null && _window$lcdp2 !== void 0 && _window$lcdp2.Afina) {
505
+ resolve(window.lcdp.Afina);
506
+ return;
507
+ }
508
+ var url = API_URL;
509
+ var url_regex = API_URL_REGEX(url);
510
+ if (args && args.length >= 2) {
511
+ var opts = args[1];
512
+ if (opts !== null && opts !== void 0 && opts.api_url) {
513
+ url = opts.api_url;
514
+ url_regex = API_URL_REGEX(url);
515
+ }
516
+ }
517
+ try {
518
+ var script = findScript(url, url_regex);
519
+ if (script) {
520
+ console.warn("it seems a lcdp script already exists.");
521
+ } else if (!script) {
522
+ script = injectScript(url);
523
+ } else if (script && onLoadListener !== null && onErrorListener !== null) {
524
+ var _script$parentNode;
525
+ // remove event listeners
526
+ script.removeEventListener("load", onLoadListener);
527
+ script.removeEventListener("error", onErrorListener);
528
+ // if script exists, but we are reloading due to an error,
529
+ // reload script to trigger 'load' event
530
+ (_script$parentNode = script.parentNode) === null || _script$parentNode === void 0 || _script$parentNode.removeChild(script);
531
+ script = injectScript(url);
532
+ }
533
+ onLoadListener = onLoad(resolve, reject);
534
+ onErrorListener = onError(reject);
535
+ script.addEventListener("load", onLoadListener);
536
+ script.addEventListener("error", onErrorListener);
537
+ } catch (error) {
538
+ reject(error);
539
+ return;
540
+ }
541
+ });
542
+ return lcdpPromise;
543
+ };
544
+ var initLCDP = /*#__PURE__*/function () {
545
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(maybeLCDP, args) {
546
+ var lcdp;
547
+ return _regenerator().w(function (_context) {
548
+ while (1) switch (_context.n) {
549
+ case 0:
550
+ if (!(maybeLCDP === null)) {
551
+ _context.n = 1;
552
+ break;
553
+ }
554
+ return _context.a(2, Promise.resolve(null));
555
+ case 1:
556
+ lcdp = _construct(maybeLCDP, _toConsumableArray(args));
557
+ return _context.a(2, lcdp.init().then(function (lcdpReady) {
558
+ return lcdpReady;
559
+ }));
560
+ }
561
+ }, _callee);
562
+ }));
563
+ return function initLCDP(_x, _x2) {
564
+ return _ref.apply(this, arguments);
565
+ };
566
+ }();
567
+ var loadLCDP = /*#__PURE__*/function () {
568
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
569
+ var _len,
570
+ args,
571
+ _key,
572
+ _args2 = arguments;
573
+ return _regenerator().w(function (_context2) {
574
+ while (1) switch (_context2.n) {
575
+ case 0:
576
+ for (_len = _args2.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
577
+ args[_key] = _args2[_key];
578
+ }
579
+ return _context2.a(2, getLCDPPromise(args).then(function (lcdp) {
580
+ return initLCDP(lcdp, args);
581
+ }));
582
+ }
583
+ }, _callee2);
584
+ }));
585
+ return function loadLCDP() {
586
+ return _ref2.apply(this, arguments);
587
+ };
588
+ }();
589
+ var requestLCDP = function requestLCDP(secret) {
590
+ var apiUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : API_URL;
591
+ return new LCDPRequest(secret, apiUrl);
592
+ };
593
+
594
+ exports.loadLCDP = loadLCDP;
595
+ exports.requestLCDP = requestLCDP;
596
+
597
+ }));
@@ -0,0 +1,3 @@
1
+ !function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).lcdpjs={})}(this,function(n){"use strict";function t(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=Array(t);r<t;r++)e[r]=n[r];return e}function r(n,t,r,e,o,i,u){try{var c=n[i](u),a=c.value}catch(n){return void r(n)}c.done?t(a):Promise.resolve(a).then(e,o)}function e(n){return function(){var t=this,e=arguments;return new Promise(function(o,i){var u=n.apply(t,e);function c(n){r(u,o,i,c,a,"next",n)}function a(n){r(u,o,i,c,a,"throw",n)}c(void 0)})}}function o(n,t,r){if(c())return Reflect.construct.apply(null,arguments);var e=[null];return e.push.apply(e,t),new(n.bind.apply(n,e))}function i(n,t,r){return Object.defineProperty(n,"prototype",{writable:!1}),n}function u(n,t,r){return(t=function(n){var t=function(n,t){if("object"!=typeof n||!n)return n;var r=n[Symbol.toPrimitive];if(void 0!==r){var e=r.call(n,t);if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(n)}(n,"string");return"symbol"==typeof t?t:t+""}(t))in n?Object.defineProperty(n,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):n[t]=r,n}function c(){try{var n=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(n){}return(c=function(){return!!n})()}function a(){
2
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
3
+ var n,t,r="function"==typeof Symbol?Symbol:{},e=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,e,o,i){var a=e&&e.prototype instanceof c?e:c,l=Object.create(a.prototype);return f(l,"_invoke",function(r,e,o){var i,c,a,f=0,l=o||[],s=!1,p={p:0,n:0,v:n,a:d,f:d.bind(n,4),d:function(t,r){return i=t,c=0,a=n,p.n=r,u}};function d(r,e){for(c=r,a=e,t=0;!s&&f&&!o&&t<l.length;t++){var o,i=l[t],d=p.p,v=i[2];r>3?(o=v===e)&&(a=i[(c=i[4])?5:(c=3,3)],i[4]=i[5]=n):i[0]<=d&&((o=r<2&&d<i[1])?(c=0,p.v=e,p.n=i[1]):d<v&&(o=r<3||i[0]>e||e>v)&&(i[4]=r,i[5]=e,p.n=v,c=0))}if(o||r>1)return u;throw s=!0,e}return function(o,l,v){if(f>1)throw TypeError("Generator is already running");for(s&&1===l&&d(l,v),c=l,a=v;(t=c<2?n:a)||!s;){i||(c?c<3?(c>1&&(p.n=-1),d(c,a)):p.n=a:p.v=a);try{if(f=2,i){if(c||(o="next"),t=i[o]){if(!(t=t.call(i,a)))throw TypeError("iterator result is not an object");if(!t.done)return t;a=t.value,c<2&&(c=0)}else 1===c&&(t=i.return)&&t.call(i),c<2&&(a=TypeError("The iterator does not provide a '"+o+"' method"),c=1);i=n}else if((t=(s=p.n<0)?a:r.call(e,p))!==u)break}catch(t){i=n,c=1,a=t}finally{f=1}}return{value:t,done:s}}}(r,o,i),!0),l}var u={};function c(){}function l(){}function s(){}t=Object.getPrototypeOf;var p=[][e]?t(t([][e]())):(f(t={},e,function(){return this}),t),d=s.prototype=c.prototype=Object.create(p);function v(n){return Object.setPrototypeOf?Object.setPrototypeOf(n,s):(n.__proto__=s,f(n,o,"GeneratorFunction")),n.prototype=Object.create(d),n}return l.prototype=s,f(d,"constructor",s),f(s,"constructor",l),l.displayName="GeneratorFunction",f(s,o,"GeneratorFunction"),f(d),f(d,o,"Generator"),f(d,e,function(){return this}),f(d,"toString",function(){return"[object Generator]"}),(a=function(){return{w:i,m:v}})()}function f(n,t,r,e){var o=Object.defineProperty;try{o({},"",{})}catch(n){o=0}f=function(n,t,r,e){function i(t,r){f(n,t,function(n){return this._invoke(t,r,n)})}t?o?o(n,t,{value:r,enumerable:!e,configurable:!e,writable:!e}):n[t]=r:(i("next",0),i("throw",1),i("return",2))},f(n,t,r,e)}function l(n){return function(n){if(Array.isArray(n))return t(n)}(n)||function(n){if("undefined"!=typeof Symbol&&null!=n[Symbol.iterator]||null!=n["@@iterator"])return Array.from(n)}(n)||function(n,r){if(n){if("string"==typeof n)return t(n,r);var e={}.toString.call(n).slice(8,-1);return"Object"===e&&n.constructor&&(e=n.constructor.name),"Map"===e||"Set"===e?Array.from(n):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?t(n,r):void 0}}(n)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var s,p,d=i(function n(t,r){var o=this;if(function(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function")}(this,n),u(this,"_apiToken",void 0),u(this,"_apiUrl",void 0),u(this,"requestLCDPApi",function(n,t,r){var i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"POST";if("undefined"!=typeof window)throw new Error("Must be used server side");return new Promise(function(){var u=e(a().m(function e(u,c){var f,l,s,p,d,v,y;return a().w(function(e){for(;;)switch(e.p=e.n){case 0:return f="".concat(o._apiUrl,"/").concat(n),e.p=1,e.n=2,fetch(f,{method:i,headers:{"Content-Type":"application/json","User-Agent":"lcdp/v1",Authorization:"Bearer ".concat(r)},body:JSON.stringify(t)});case 2:if(l=e.v,!((s=l.status)>=400)){e.n=4;break}return e.n=3,l.json();case 3:p=e.v,d=p.detail,c(d),e.n=7;break;case 4:if(204!==s){e.n=5;break}u(),e.n=7;break;case 5:return e.n=6,l.json();case 6:v=e.v,u(v);case 7:e.n=9;break;case 8:e.p=8,y=e.v,c(y);case 9:return e.a(2)}},e,null,[[1,8]])}));return function(n,t){return u.apply(this,arguments)}}())}),u(this,"createAccount",function(){var n=e(a().m(function n(t){return a().w(function(n){for(;;)if(0===n.n)return n.a(2,new Promise(function(){var n=e(a().m(function n(r,e){var i;return a().w(function(n){for(;;)if(0===n.n)return i=o.requestLCDPApi("accounts",t,o._apiToken),n.a(2,i.then(function(n){try{r(n)}catch(n){e(n)}}).catch(function(n){return e(n)}))},n)}));return function(t,r){return n.apply(this,arguments)}}()))},n)}));return function(t){return n.apply(this,arguments)}}()),u(this,"createPot",function(){var n=e(a().m(function n(t){return a().w(function(n){for(;;)if(0===n.n)return n.a(2,new Promise(function(){var n=e(a().m(function n(r,e){var i;return a().w(function(n){for(;;)if(0===n.n)return i=o.requestLCDPApi("pots",t,o._apiToken),n.a(2,i.then(function(n){try{r(n)}catch(n){e(n)}}).catch(function(n){return e(n)}))},n)}));return function(t,r){return n.apply(this,arguments)}}()))},n)}));return function(t){return n.apply(this,arguments)}}()),u(this,"createAccountAndPot",function(){var n=e(a().m(function n(t){return a().w(function(n){for(;;)if(0===n.n)return n.a(2,new Promise(function(){var n=e(a().m(function n(r,e){var i;return a().w(function(n){for(;;)if(0===n.n)return i=o.requestLCDPApi("wizards",t,o._apiToken),n.a(2,i.then(function(n){try{r(n)}catch(n){e(n)}}).catch(function(n){return e(n)}))},n)}));return function(t,r){return n.apply(this,arguments)}}()))},n)}));return function(t){return n.apply(this,arguments)}}()),u(this,"updatePot",function(){var n=e(a().m(function n(t){return a().w(function(n){for(;;)if(0===n.n)return n.a(2,new Promise(function(){var n=e(a().m(function n(r,e){var i;return a().w(function(n){for(;;)if(0===n.n)return i=o.requestLCDPApi("pots",t,o._apiToken,"PATCH"),n.a(2,i.then(function(n){try{r()}catch(n){e(n)}}).catch(function(n){return e(n)}))},n)}));return function(t,r){return n.apply(this,arguments)}}()))},n)}));return function(t){return n.apply(this,arguments)}}()),"undefined"!=typeof window)throw new Error("Must be used server side");this._apiToken=t,this._apiUrl=r});n.AfinaError=void 0,(s=n.AfinaError||(n.AfinaError={})).Verify="Fail to verify origin request",s.Stripe="Stripe is not available",s.ClientSide="Should be on Client Side",s.Key="A publishable key is mandatory",n.TokenObjectType=void 0,(p=n.TokenObjectType||(n.TokenObjectType={})).Account="account",p.Pot="pot",p.Wizard="wizard";var v=null,y="https://api.lacagnottedesproches.fr",h=function(n){return new RegExp("/^".concat(n,"?(.*)$/"))},m=null,w=null,b=function(n){var t=document.createElement("script");t.src="".concat(n,"/js");var r=document.head||document.body;if(!r)throw new Error("Expected document.body not to be null. Stripe.js requires a <body> element.");r.appendChild(t);var e=document.createElement("div");return e.id="lcdp-modal-root",document.body.appendChild(e),t},g=function(n){return v||(v=new Promise(function(t,r){var e;if("undefined"!=typeof window&&"undefined"!=typeof document)if(null!==(e=window.lcdp)&&void 0!==e&&e.Afina)t(window.lcdp.Afina);else{var o=y,i=h(o);if(n&&n.length>=2){var u=n[1];null!=u&&u.api_url&&(o=u.api_url,i=h(o))}try{var c=function(n,t){for(var r=document.querySelectorAll('script[src^="'.concat(n,'"]')),e=0;e<r.length;e++){var o=r[e];if(t.test(o.src))return o}return null}(o,i);if(c)console.warn("it seems a lcdp script already exists.");else if(c){if(c&&null!==w&&null!==m){var a;c.removeEventListener("load",w),c.removeEventListener("error",m),null===(a=c.parentNode)||void 0===a||a.removeChild(c),c=b(o)}}else c=b(o);w=function(n,t){return function(){var r;null!==(r=window.lcdp)&&void 0!==r&&r.Afina?n(window.lcdp.Afina):t(new Error("lcdp.js not available"))}}(t,r),m=function(n){return function(){n(new Error("Failed to load lcdp.js"))}}(r),c.addEventListener("load",w),c.addEventListener("error",m)}catch(n){return void r(n)}}else t(null)}))},P=function(){var n=e(a().m(function n(t,r){var e;return a().w(function(n){for(;;)switch(n.n){case 0:if(null!==t){n.n=1;break}return n.a(2,Promise.resolve(null));case 1:return e=o(t,l(r)),n.a(2,e.init().then(function(n){return n}))}},n)}));return function(t,r){return n.apply(this,arguments)}}(),A=function(){var n=e(a().m(function n(){var t,r,e,o=arguments;return a().w(function(n){for(;;)if(0===n.n){for(t=o.length,r=new Array(t),e=0;e<t;e++)r[e]=o[e];return n.a(2,g(r).then(function(n){return P(n,r)}))}},n)}));return function(){return n.apply(this,arguments)}}();n.loadLCDP=A,n.requestLCDP=function(n){return new d(n,arguments.length>1&&void 0!==arguments[1]?arguments[1]:y)}});