Skip to content

Commit

Permalink
v.1.0.0 contd
Browse files Browse the repository at this point in the history
  • Loading branch information
foo123 committed Jun 10, 2022
1 parent aba14e0 commit 6b37abf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Version: **1.0.0** (5 kB minified)
function mylistener(req, res)
{
console.log('REQUEST', 'API', req.getAPI(), 'Method', req.getMethod(), 'URL', req.getURL(), 'Headers', req.getHeaders(), 'Body', req.getBody());
console.log('RESPONSE', 'API', res.getAPI(), 'Status', res.getStatus(), 'Headers', res.getHeaders(), 'Body', res.getBody());
console.log('RESPONSE', 'API', res.getAPI(), 'Status', res.getStatus(), 'URL', res.getURL(), 'Headers', res.getHeaders(), 'Body', res.getBody());
}

AjaxListener.install().onRequest(mylistener);
Expand Down
33 changes: 28 additions & 5 deletions src/AjaxListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,16 @@ Request.prototype = {
};
AjaxListener.Request = Request;

function Response(api, status, _headersFactory, _body)
function Response(api, status, _url, _headersFactory, _body)
{
var self = this, headers = null, body = null;
var self = this, url = null, headers = null, body = null;

self.dispose = function() {
status = null;
_url = null;
_headersFactory = null;
_body = null;
url = null;
headers = null;
body = null;
return self;
Expand All @@ -231,6 +233,26 @@ function Response(api, status, _headersFactory, _body)
self.getStatus = function() {
return status;
};
self.getURL = function(raw) {
if (true === raw) return _url;
if (null == url && null != _url)
{
var u = new window.URL(_url, get_base_url());
url = {
href: u.href,
origin: u.origin,
hostname: u.hostname,
protocol: u.protocol,
host: u.host,
port: u.port,
path: u.pathname,
query: u.search,
hash: u.hash,
queryParams: u.search && u.search.length ? parse_url_params(u.search.slice(1)) : {}
};
}
return url;
};
self.getHeaders = function() {
if (null == headers && null != _headersFactory)
{
Expand Down Expand Up @@ -267,6 +289,7 @@ Response.prototype = {
,dispose: null
,getAPI: null
,getStatus: null
,getURL: null
,getHeaders: null
,getBody: null
};
Expand All @@ -284,15 +307,15 @@ function listenerFetch(request)
request.text().then(function(requestText) {
notify(
new Request('fetch', request.method, request.url, factory(extract_headers, request.headers), requestText),
new Response('fetch', response.status, factory(extract_headers, response.headers), responseText)
new Response('fetch', response.status, response.url || request.url, factory(extract_headers, response.headers), responseText)
);
});
}
else
{
notify(
new Request('fetch', 'GET', request, factory(null, {}), ''),
new Response('fetch', response.status, factory(extract_headers, response.headers), responseText)
new Response('fetch', response.status, response. url || request, factory(extract_headers, response.headers), responseText)
);
}
})/*.catch(function(err) {})*/;
Expand Down Expand Up @@ -323,7 +346,7 @@ function listenerOpen(method, url)
{
notify(
new Request('xhr', method, url, factory(null, headers), null == body ? '' : body),
new Response('xhr', self.status, factory(parse_headers, self.getAllResponseHeaders()), self.responseText)
new Response('xhr', self.status, self.responseURL && self.responseURL.length ? self.responseURL : url, factory(parse_headers, self.getAllResponseHeaders()), self.responseText)
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/AjaxListener.min.js

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

2 changes: 1 addition & 1 deletion test/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function mylistener(req, res)
{
console.log('REQUEST', 'API', req.getAPI(), 'Method', req.getMethod(), 'URL', req.getURL(), 'Headers', req.getHeaders(), 'Body', req.getBody());
console.log('RESPONSE', 'API', res.getAPI(), 'Status', res.getStatus(), 'Headers', res.getHeaders(), 'Body', res.getBody());
console.log('RESPONSE', 'API', res.getAPI(), 'Status', res.getStatus(), 'URL', res.getURL(), 'Headers', res.getHeaders(), 'Body', res.getBody());
}

AjaxListener.install().onRequest(mylistener);
Expand Down

0 comments on commit 6b37abf

Please sign in to comment.