Skip to content

Commit 439244b

Browse files
author
Ujjwal Mishra
committed
more generalised code
1 parent 03a37e9 commit 439244b

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

dist/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
class ApiLogger {
2+
23
constructor(limit) {
34
if (window.apiLogger) {
45
return window.apiLogger;
56
}
67
const errorQueue = this.getErrorQueueFromStorage();
7-
if (limit && errorQueue && errorQueue.limit) {
8-
console.log(
9-
`ApiLogger has already a limit. please use changeLimit function from Logger Instance.`
10-
)
11-
}
128
errorQueue.limit = errorQueue.limit || limit || 50;
13-
this.limit = errorQueue.limit || limit || 50;
149
this.count = errorQueue.errors.length || 0;
1510
this.errorQueue = errorQueue;
1611
window.apiLogger = this;
1712
this.wrappedFetch = this.wrappedFetch.bind(this);
1813
this.originalFetch = window.fetch;
14+
if (errorQueue.limit !== limit) this.changeLimit(limit);
1915
}
2016

2117
push(value) {
22-
if (this.count === this.limit) this.pop();
18+
if (this.count === this.errorQueue.limit) this.pop();
2319
this.errorQueue.errors.push(value);
2420
this.count++;
2521
this.updateErrorQueueInStorage();
@@ -30,7 +26,12 @@ class ApiLogger {
3026
}
3127

3228
updateErrorQueueInStorage() {
33-
localStorage.setItem('errorQueue', JSON.stringify(this.errorQueue));
29+
try {
30+
localStorage.setItem('errorQueue', JSON.stringify(this.errorQueue));
31+
} catch (e){
32+
console.error(e);
33+
console.log('Error while updating localstorage.');
34+
}
3435
}
3536

3637
pop() {
@@ -39,31 +40,48 @@ class ApiLogger {
3940
this.count--;
4041
}
4142

43+
getBody(body) {
44+
let newBody = null;
45+
try {
46+
newBody = JSON.parse(body);
47+
} catch (err) {
48+
newBody = body;
49+
}
50+
return newBody;
51+
}
52+
4253
wrappedFetch () {
4354
const store = this;
4455
return new Promise((resolve, reject) => {
45-
const [url, { method, body }] = arguments;
56+
let [url, options] = arguments;
57+
let { body, method } = options || {};
58+
4659
this.originalFetch.apply(window, arguments)
4760
.then((response) => {
4861
if (response && response.status >= 400) {
49-
let reqData = null;
50-
if (typeof body === 'object') {
51-
reqData = body && this.truncateString(JSON.stringify(body), 50);
52-
} else if (typeof body === 'string') {
53-
reqData = this.truncateString(body, 50);
62+
body = body && this.getBody(body);
63+
const contentType = response.headers.get("content-type");
64+
const logData = {
65+
id: new Date().getTime(),
66+
url,
67+
method,
68+
body,
69+
status: response.status,
70+
contentType: response.headers.get("content-type")
5471
}
55-
response.text().then((text) => {
56-
store.push({
57-
id: new Date().getTime(),
58-
url,
59-
method,
60-
body: reqData,
61-
response: this.truncateString(text, 50),
62-
status: response.status,
63-
contentType: response.headers.get("content-type")
72+
if (contentType && contentType.includes("application/json")) {
73+
return response.json().then((json) => {
74+
logData.response = json
75+
store.push(logData);
76+
resolve(response);
77+
});
78+
} else {
79+
response.text().then((text) => {
80+
logData.response = text;
81+
store.push(logData);
82+
resolve(response);
6483
});
65-
resolve(response);
66-
});
84+
}
6785
} else {
6886
resolve(response);
6987
}
@@ -84,8 +102,10 @@ class ApiLogger {
84102
}
85103

86104
changeLimit(limit) {
105+
if (this.errorQueue.errors.length > limit) {
106+
this.errorQueue.errors = this.errorQueue.errors.slice(this.errorQueue.errors.length - limit)
107+
}
87108
this.errorQueue.limit = limit;
88-
this.limit = limit;
89109
this.updateErrorQueueInStorage();
90110
}
91111
}

test/test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Http Failures', function() {
1111
global.localStorage.clear();
1212
});
1313
it('should store 400 response', function(done) {
14-
apiLogger.wrappedFetch('http://httpbin.org/status/400', { method: 'get' }).then(() => {
14+
apiLogger.wrappedFetch('http://httpbin.org/status/400').then(() => {
1515
let errorQueue = JSON.parse(global.localStorage.getItem('errorQueue'));
1616
if (errorQueue && errorQueue.errors[0].status === 400) done();
1717
else done({ message: 'Did not log in localstoarge'});
@@ -20,12 +20,11 @@ describe('Http Failures', function() {
2020
it('should store any response data in text format with turncated value', function(done) {
2121
apiLogger.wrappedFetch('http://httpbin.org/html/status/401', { method: 'get' }).then(() => {
2222
const errorQueue = JSON.parse(global.localStorage.getItem('errorQueue'));
23-
const error = errorQueue.errors[1]
23+
const error = errorQueue.errors[1];
2424
if (
2525
errorQueue &&
2626
error.status === 404 &&
27-
error.response.indexOf(`<!DOCTYPE HTML PUBLIC`) === 0 &&
28-
error.response.length === 53
27+
error.response.indexOf(`<!DOCTYPE HTML PUBLIC`) === 0
2928
) done();
3029
else done({ message: 'Did not log in localstoarge'});
3130
});

0 commit comments

Comments
 (0)