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