open-chat-studio-widget 0.4.4 → 0.4.5

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.
@@ -4267,6 +4267,180 @@ function renderMarkdownSync(content) {
4267
4267
  }
4268
4268
  }
4269
4269
 
4270
+ var js_cookie = {exports: {}};
4271
+
4272
+ /*! js-cookie v3.0.5 | MIT */
4273
+
4274
+ (function (module, exports) {
4275
+ (function (global, factory) {
4276
+ module.exports = factory() ;
4277
+ })(commonjsGlobal, (function () {
4278
+ /* eslint-disable no-var */
4279
+ function assign (target) {
4280
+ for (var i = 1; i < arguments.length; i++) {
4281
+ var source = arguments[i];
4282
+ for (var key in source) {
4283
+ target[key] = source[key];
4284
+ }
4285
+ }
4286
+ return target
4287
+ }
4288
+ /* eslint-enable no-var */
4289
+
4290
+ /* eslint-disable no-var */
4291
+ var defaultConverter = {
4292
+ read: function (value) {
4293
+ if (value[0] === '"') {
4294
+ value = value.slice(1, -1);
4295
+ }
4296
+ return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
4297
+ },
4298
+ write: function (value) {
4299
+ return encodeURIComponent(value).replace(
4300
+ /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
4301
+ decodeURIComponent
4302
+ )
4303
+ }
4304
+ };
4305
+ /* eslint-enable no-var */
4306
+
4307
+ /* eslint-disable no-var */
4308
+
4309
+ function init (converter, defaultAttributes) {
4310
+ function set (name, value, attributes) {
4311
+ if (typeof document === 'undefined') {
4312
+ return
4313
+ }
4314
+
4315
+ attributes = assign({}, defaultAttributes, attributes);
4316
+
4317
+ if (typeof attributes.expires === 'number') {
4318
+ attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
4319
+ }
4320
+ if (attributes.expires) {
4321
+ attributes.expires = attributes.expires.toUTCString();
4322
+ }
4323
+
4324
+ name = encodeURIComponent(name)
4325
+ .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
4326
+ .replace(/[()]/g, escape);
4327
+
4328
+ var stringifiedAttributes = '';
4329
+ for (var attributeName in attributes) {
4330
+ if (!attributes[attributeName]) {
4331
+ continue
4332
+ }
4333
+
4334
+ stringifiedAttributes += '; ' + attributeName;
4335
+
4336
+ if (attributes[attributeName] === true) {
4337
+ continue
4338
+ }
4339
+
4340
+ // Considers RFC 6265 section 5.2:
4341
+ // ...
4342
+ // 3. If the remaining unparsed-attributes contains a %x3B (";")
4343
+ // character:
4344
+ // Consume the characters of the unparsed-attributes up to,
4345
+ // not including, the first %x3B (";") character.
4346
+ // ...
4347
+ stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
4348
+ }
4349
+
4350
+ return (document.cookie =
4351
+ name + '=' + converter.write(value, name) + stringifiedAttributes)
4352
+ }
4353
+
4354
+ function get (name) {
4355
+ if (typeof document === 'undefined' || (arguments.length && !name)) {
4356
+ return
4357
+ }
4358
+
4359
+ // To prevent the for loop in the first place assign an empty array
4360
+ // in case there are no cookies at all.
4361
+ var cookies = document.cookie ? document.cookie.split('; ') : [];
4362
+ var jar = {};
4363
+ for (var i = 0; i < cookies.length; i++) {
4364
+ var parts = cookies[i].split('=');
4365
+ var value = parts.slice(1).join('=');
4366
+
4367
+ try {
4368
+ var found = decodeURIComponent(parts[0]);
4369
+ jar[found] = converter.read(value, found);
4370
+
4371
+ if (name === found) {
4372
+ break
4373
+ }
4374
+ } catch (e) {}
4375
+ }
4376
+
4377
+ return name ? jar[name] : jar
4378
+ }
4379
+
4380
+ return Object.create(
4381
+ {
4382
+ set,
4383
+ get,
4384
+ remove: function (name, attributes) {
4385
+ set(
4386
+ name,
4387
+ '',
4388
+ assign({}, attributes, {
4389
+ expires: -1
4390
+ })
4391
+ );
4392
+ },
4393
+ withAttributes: function (attributes) {
4394
+ return init(this.converter, assign({}, this.attributes, attributes))
4395
+ },
4396
+ withConverter: function (converter) {
4397
+ return init(assign({}, this.converter, converter), this.attributes)
4398
+ }
4399
+ },
4400
+ {
4401
+ attributes: { value: Object.freeze(defaultAttributes) },
4402
+ converter: { value: Object.freeze(converter) }
4403
+ }
4404
+ )
4405
+ }
4406
+
4407
+ var api = init(defaultConverter, { path: '/' });
4408
+ /* eslint-enable no-var */
4409
+
4410
+ return api;
4411
+
4412
+ }));
4413
+ }(js_cookie));
4414
+
4415
+ const Cookies = js_cookie.exports;
4416
+
4417
+ /**
4418
+ * Get CSRF token from cookies if the current domain matches the API base URL
4419
+ */
4420
+ function getCSRFToken(apiBaseUrl) {
4421
+ if (!currentDomainMatchesApiBaseUrl(apiBaseUrl)) {
4422
+ return undefined;
4423
+ }
4424
+ return Cookies.get('csrftoken');
4425
+ }
4426
+ function currentDomainMatchesApiBaseUrl(apiBaseUrl) {
4427
+ const currentDomain = window.location.hostname;
4428
+ const apiDomain = getDomainFromUrl(apiBaseUrl);
4429
+ if (!apiDomain) {
4430
+ return false;
4431
+ }
4432
+ return currentDomain === apiDomain;
4433
+ }
4434
+ function getDomainFromUrl(url) {
4435
+ try {
4436
+ const urlObj = new URL(url);
4437
+ return urlObj.hostname;
4438
+ }
4439
+ catch (error) {
4440
+ return null;
4441
+ }
4442
+ }
4443
+
4270
4444
  /**
4271
4445
  * Convert a CSS percentage (60%) or pixel (10px) value to pixels.
4272
4446
  * @param value The CSS string value
@@ -4472,6 +4646,16 @@ const OcsChat = class {
4472
4646
  getApiBaseUrl() {
4473
4647
  return this.apiBaseUrl || window.location.origin;
4474
4648
  }
4649
+ getApiHeaders() {
4650
+ const headers = {
4651
+ 'Content-Type': 'application/json',
4652
+ };
4653
+ const csrfToken = getCSRFToken(this.getApiBaseUrl());
4654
+ if (csrfToken) {
4655
+ headers['X-CSRFToken'] = csrfToken;
4656
+ }
4657
+ return headers;
4658
+ }
4475
4659
  async startSession() {
4476
4660
  try {
4477
4661
  this.isLoading = true;
@@ -4490,9 +4674,7 @@ const OcsChat = class {
4490
4674
  }
4491
4675
  const response = await fetch(`${this.getApiBaseUrl()}/api/chat/start/`, {
4492
4676
  method: 'POST',
4493
- headers: {
4494
- 'Content-Type': 'application/json',
4495
- },
4677
+ headers: this.getApiHeaders(),
4496
4678
  body: JSON.stringify(requestBody)
4497
4679
  });
4498
4680
  if (!response.ok) {
@@ -4549,9 +4731,7 @@ const OcsChat = class {
4549
4731
  this.isTyping = true;
4550
4732
  const response = await fetch(`${this.getApiBaseUrl()}/api/chat/${this.sessionId}/message/`, {
4551
4733
  method: 'POST',
4552
- headers: {
4553
- 'Content-Type': 'application/json',
4554
- },
4734
+ headers: this.getApiHeaders(),
4555
4735
  body: JSON.stringify({ message: message.trim() })
4556
4736
  });
4557
4737
  if (!response.ok) {