Readarr/frontend/src/jQuery/jquery.ajax.js
Qstick f460f630c3 Target to .NET 4.6.1, Update SignalR, Owin, Nancy (#84)
* Target .net 4.6.1

* Update to SignalR 2.2.2

* Fix Socks Reference in NZBDrone.Common

* UI Fixes, Build Fixes

* Update Nancy to 1.4.4

* Upgrade Microsoft Owin to 3.1.0

* Delete npm-shrinkwrap.json

* Fix SignalR Messages
2017-09-20 22:05:00 -04:00

47 lines
983 B
JavaScript

import $ from 'jquery';
const absUrlRegex = /^(https?:)?\/\//i;
const apiRoot = window.Sonarr.apiRoot;
const urlBase = window.Sonarr.urlBase;
function isRelative(xhr) {
return !absUrlRegex.test(xhr.url);
}
function moveBodyToQuery(xhr) {
if (xhr.data && xhr.type === 'DELETE') {
if (xhr.url.contains('?')) {
xhr.url += '&';
} else {
xhr.url += '?';
}
xhr.url += $.param(xhr.data);
delete xhr.data;
}
}
function addRootUrl(xhr) {
const url = xhr.url;
if (url.startsWith('/signalr')) {
xhr.url = urlBase + xhr.url;
} else {
xhr.url = apiRoot + xhr.url;
}
}
function addApiKey(xhr) {
xhr.headers = xhr.headers || {};
xhr.headers['X-Api-Key'] = window.Sonarr.apiKey;
}
export default function() {
const originalAjax = $.ajax;
$.ajax = function(xhr) {
if (xhr && isRelative(xhr)) {
moveBodyToQuery(xhr);
addRootUrl(xhr);
addApiKey(xhr);
}
return originalAjax.apply(this, arguments);
};
}