Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* * * This commit was sponsored by Neighbourhoodie You can hire Neighbourhoodie for all your Hoodie / CouchDB / Offline First needs http://go.hood.ie/thehoodiefirm
- Loading branch information
Showing
3 changed files
with
77 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module.exports = request | ||
|
||
var nets = require('nets') | ||
var Promise = require('lie') | ||
|
||
function request (options) { | ||
var requestState | ||
var reject | ||
var promise = new Promise(function (resolve, _reject) { | ||
reject = _reject | ||
requestState = nets({ | ||
method: options.method, | ||
url: options.url, | ||
timeout: options.timeout | ||
}, function (error, response, body) { | ||
if (error) { | ||
error.name = error.code === 'ETIMEDOUT' ? 'TimeoutError' : 'ConnectionError' | ||
error.code = undefined | ||
return reject(error) | ||
} | ||
|
||
if (response.statusCode >= 400) { | ||
error = new Error('Server error') | ||
error.name = 'ServerError' | ||
error.code = response.statusCode | ||
return reject(error) | ||
} | ||
|
||
resolve() | ||
}) | ||
}) | ||
|
||
promise.abort = function () { | ||
try { | ||
requestState.abort() | ||
} catch (error) {} | ||
var error = new Error('Aborted') | ||
error.name = 'AbortError' | ||
error.code = 0 | ||
reject(error) | ||
} | ||
|
||
return promise | ||
} |