Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isomorphism #185

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"scripts": {
"boosh": "smoosh make ./build.json",
"test": "node ./test.js"
},
"dependencies": {
"xhr2": "0.1.0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to add the library to the client. if you build this with ender or browserify i'm not so sure this library will work anymore in the browser (which is its original intention)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked for me in Webpack. I've since moved on to https://github.com/matthew-andrews/isomorphic-fetch

Feel free to make any changes you see fit to make it more generic.

}
}
37 changes: 24 additions & 13 deletions reqwest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
else if (typeof define == 'function' && define.amd) define(definition)
else context[name] = definition()
}('reqwest', this, function () {
var context = this

if (context.hasOwnProperty('window')) {
var win = window
, doc = document
, byTag = 'getElementsByTagName'
, head = doc[byTag]('head')[0]
} else {
var XHR2 = require('xhr2');
}

var win = window
, doc = document
, httpsRe = /^http/
, protocolRe = /(^\w+):\/\//
var httpsRe = /^http/
, twoHundo = /^(20\d|1223)$/ //http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
, byTag = 'getElementsByTagName'
, protocolRe = /(^\w+):\/\//
, readyState = 'readyState'
, contentType = 'Content-Type'
, requestedWith = 'X-Requested-With'
, head = doc[byTag]('head')[0]
, uniqid = 0
, callbackPrefix = 'reqwest_' + (+new Date())
, lastValue // data stored by the most recent JSONP callback
Expand Down Expand Up @@ -49,16 +55,18 @@
, xhr = function(o) {
// is it x-domain
if (o['crossOrigin'] === true) {
var xhr = win[xmlHttpRequest] ? new XMLHttpRequest() : null
var xhr = context[xmlHttpRequest] ? new XMLHttpRequest() : null
if (xhr && 'withCredentials' in xhr) {
return xhr
} else if (win[xDomainRequest]) {
} else if (context[xDomainRequest]) {
return new XDomainRequest()
} else {
throw new Error('Browser does not support cross-origin requests')
}
} else if (win[xmlHttpRequest]) {
} else if (context[xmlHttpRequest]) {
return new XMLHttpRequest()
} else if (XHR2) {
return new XHR2()
} else {
return new ActiveXObject('Microsoft.XMLHTTP')
}
Expand All @@ -71,8 +79,11 @@

function succeed(r) {
var protocol = protocolRe.exec(r.url);
protocol = (protocol && protocol[1]) || window.location.protocol;
return httpsRe.test(protocol) ? twoHundo.test(r.request.status) : !!r.request.response;
protocol = (protocol && protocol[1]) || (context.location && context.location.protocol);

return httpsRe.test(protocol)
? twoHundo.test(r.request.status)
: !!r.request.response;
}

function handleReadyState(r, success, error) {
Expand Down Expand Up @@ -206,7 +217,7 @@
http.open(method, url, o['async'] === false ? false : true)
setHeaders(http, o)
setCredentials(http, o)
if (win[xDomainRequest] && http instanceof win[xDomainRequest]) {
if (context[xDomainRequest] && http instanceof context[xDomainRequest]) {
http.onload = fn
http.onerror = err
// NOTE: see
Expand Down Expand Up @@ -311,7 +322,7 @@
switch (type) {
case 'json':
try {
resp = win.JSON ? win.JSON.parse(r) : eval('(' + r + ')')
resp = context.JSON ? context.JSON.parse(r) : eval('(' + r + ')')
} catch (err) {
return error(resp, 'Could not parse JSON in response', err)
}
Expand Down
Loading