diff --git a/adapters.json b/adapters.json index 73f6441ed8f..b0e5e41d1a9 100644 --- a/adapters.json +++ b/adapters.json @@ -6,7 +6,6 @@ "aol", "appnexus", "appnexusAst", - "fan", "indexExchange", "kruxlink", "openx", diff --git a/src/ajax.js b/src/ajax.js index e8e4cd29f1d..0e74772a81a 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -1,3 +1,5 @@ +import {parse as parseURL, format as formatURL} from './url'; + /** * Simple cross-browser ajax request function * https://gist.github.com/Xeoncross/7663273 @@ -7,10 +9,12 @@ * @param url string url * @param callback object callback * @param data mixed data - * @param x null Ajax request + * @param options object */ -export const ajax = function ajax(url, callback, data, x = null) { +export const ajax = function ajax(url, callback, data, options = {}) { + let x; + try { if (window.XMLHttpRequest) { x = new window.XMLHttpRequest('MSXML2.XMLHTTP.3.0'); @@ -20,17 +24,22 @@ export const ajax = function ajax(url, callback, data, x = null) { x = new window.ActiveXObject('MSXML2.XMLHTTP.3.0'); } + const method = options.method || (data ? 'POST' : 'GET'); + + if (method === 'GET' && data) { + let urlInfo = parseURL(url); + Object.assign(urlInfo.search, data); + url = formatURL(urlInfo); + } + //x = new (window.XMLHttpRequest || window.ActiveXObject)('MSXML2.XMLHTTP.3.0'); x.open(method, url, 1); if (options.withCredentials) { x.withCredentials = true; } else { - if (options.preflight !== false) { - x.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - } - x.setRequestHeader('Content-Type', - options.contentType || 'application/json;charset=UTF-8'); + x.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + x.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); } //x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); @@ -40,7 +49,7 @@ export const ajax = function ajax(url, callback, data, x = null) { } }; - x.send(data); + x.send(method === 'POST' && data); } catch (e) { console.log(e); } diff --git a/src/url.js b/src/url.js new file mode 100644 index 00000000000..3588d44fad5 --- /dev/null +++ b/src/url.js @@ -0,0 +1,48 @@ +export function parseQS(query) { + return !query ? {} : query + .replace(/^\?/, '') + .split('&') + .reduce((acc, criteria) => { + let [k, v] = criteria.split('='); + if (/\[\]$/.test(k)) { + k = k.replace('[]', ''); + acc[k] = acc[k] || []; + acc[k].push(v); + } else { + acc[k] = v || ''; + } + return acc; + }, {}); +} + +export function formatQS(query) { + return Object + .keys(query) + .map(k => Array.isArray(query[k]) ? + query[k].map(v => `${k}[]=${v}`).join('&') : + `${k}=${query[k]}`) + .join('&'); +} + +export function parse(url) { + let parsed = document.createElement('a'); + parsed.href = decodeURIComponent(url); + return { + protocol: (parsed.protocol || '').replace(/:$/, ''), + hostname: parsed.hostname, + port: +parsed.port, + pathname: parsed.pathname, + search: parseQS(parsed.search || ''), + hash: (parsed.hash || '').replace(/^#/, ''), + host: parsed.host + }; +} + +export function format(obj) { + return (obj.protocol || 'http') + '://' + + (obj.host || + obj.hostname + (obj.port ? `:${obj.port}` : '')) + + (obj.pathname || '') + + (obj.search ? `?${formatQS(obj.search || '')}` : '') + + (obj.hash ? `#${obj.hash}` : ''); +} diff --git a/test/spec/url_spec.js b/test/spec/url_spec.js new file mode 100644 index 00000000000..c2421e20c49 --- /dev/null +++ b/test/spec/url_spec.js @@ -0,0 +1,68 @@ +import {format, parse} from '../../src/url'; + +describe('helpers.url', () => { + + describe('parse()', () => { + + let parsed; + + beforeEach(() => { + parsed = parse('http://example.com:3000/pathname/?search=test&foo=bar#hash'); + }); + + it('extracts the protocol', () => { + expect(parsed).to.have.property('protocol', 'http'); + }); + + it('extracts the hostname', () => { + expect(parsed).to.have.property('hostname', 'example.com'); + }); + + it('extracts the port', () => { + expect(parsed).to.have.property('port', 3000); + }); + + it('extracts the pathname', () => { + expect(parsed).to.have.property('pathname', '/pathname/'); + }); + + it('extracts the search query', () => { + expect(parsed).to.have.property('search'); + expect(parsed.search).to.eql({ + foo: 'bar', + search: 'test' + }); + }); + + it('extracts the hash', () => { + expect(parsed).to.have.property('hash', 'hash'); + }); + + it('extracts the host', () => { + expect(parsed).to.have.property('host', 'example.com:3000'); + }); + + }); + + describe('format()', () => { + + it('formats an object in to a URL', () => { + expect(format({ + protocol: 'http', + hostname: 'example.com', + port: 3000, + pathname: '/pathname/', + search: {foo: 'bar', search: 'test'}, + hash: 'hash' + })).to.equal('http://example.com:3000/pathname/?foo=bar&search=test#hash'); + }); + + it('will use defaults for missing properties', () => { + expect(format({ + hostname: 'example.com' + })).to.equal('http://example.com'); + }); + + }); + +});