Skip to content
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
9 changes: 6 additions & 3 deletions dist/xhook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// XHook - v1.4.9 - https://github.com/jpillora/xhook
// Jaime Pillora <dev@jpillora.com> - MIT Copyright 2018
(function(undefined) {
(function(window,undefined) {
var AFTER, BEFORE, COMMON_EVENTS, EventEmitter, FETCH, FIRE, FormData, NativeFetch, NativeFormData, NativeXMLHttp, OFF, ON, READY_STATE, UPLOAD_EVENTS, WINDOW, XHookFetchRequest, XHookFormData, XHookHttpRequest, XMLHTTP, convertHeaders, depricatedProp, document, fakeEvent, mergeObjects, msie, nullify, proxyEvents, slice, useragent, xhook, _base,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

Expand Down Expand Up @@ -606,8 +606,11 @@ if (typeof WINDOW[FETCH] === "function") {
headers: {}
};
}
options.url = url;
request = null;
if (url instanceof Request) {
request = url;
}
options.url = url;
beforeHooks = xhook.listeners(BEFORE);
afterHooks = xhook.listeners(AFTER);
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -697,4 +700,4 @@ if (typeof define === "function" && define.amd) {
WINDOW.xhook = xhook;
}

}.call(this));
}.call(this,window));
2 changes: 1 addition & 1 deletion dist/xhook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/xhook.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,11 @@ if typeof WINDOW[FETCH] is "function"
NativeFetch = WINDOW[FETCH]
xhook[FETCH] = NativeFetch
XHookFetchRequest = WINDOW[FETCH] = (url, options = { headers: {} }) ->
options.url = url
request = null
if url instanceof Request
request = url;
options.url = url


beforeHooks = xhook.listeners BEFORE
afterHooks = xhook.listeners AFTER
Expand Down
19 changes: 19 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ describe('xhook', function() {
});

describe('fetch', function() {
it('should handle request objects', function(done) {
const request = new Request('../example/example1.txt', { headers: {
'x-blah': 'blah',
}});
xhook.before(function(req, callback) {
const value = req.headers.get('x-blah');
expect(value).to.equal('blah');
callback();
});
fetch(request)
.then(function(response) {
return response.text()
})
.then(function(text) {
expect(text).to.contain('the first text file');
done();
});
});

it('should not modify url', function(done) {
fetch('../example/example1.txt')
Expand Down Expand Up @@ -170,4 +188,5 @@ describe('xhook', function() {
});



});