Skip to content

Commit

Permalink
restore url.js and modifcations to ajax.js (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Guisinger committed Aug 22, 2016
1 parent 1cd2d64 commit b2c617a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 9 deletions.
1 change: 0 additions & 1 deletion adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"aol",
"appnexus",
"appnexusAst",
"fan",
"indexExchange",
"kruxlink",
"openx",
Expand Down
25 changes: 17 additions & 8 deletions src/ajax.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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);
}
Expand Down
48 changes: 48 additions & 0 deletions src/url.js
Original file line number Diff line number Diff line change
@@ -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}` : '');
}
68 changes: 68 additions & 0 deletions test/spec/url_spec.js
Original file line number Diff line number Diff line change
@@ -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');
});

});

});

0 comments on commit b2c617a

Please sign in to comment.