Skip to content

Commit f67f1ff

Browse files
committed
added logger function and calling of logger
1 parent d7ca0ec commit f67f1ff

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/core/lib/request.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 30
8989
})
9090
}
9191
}
92+
fetchOptions.logHandler('info', { url: url, option: option});
9293
fetch(url, option)
9394
.then(function(response) {
95+
fetchOptions.logHandler('info', data);
9496
let data = response.json();
9597
if (response.ok && response.status === 200) {
9698
resolve(data);
9799
} else {
98100
data.then((json) => {
99101
if (fetchOptions.retryCondition && fetchOptions.retryCondition(response)) {
100-
onError(json)
102+
onError(json)
101103
} else {
102104
reject(json)
103105
}
@@ -106,6 +108,7 @@ function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 30
106108
});
107109
}
108110
}).catch((error) => {
111+
fetchOptions.logHandler('error', error);
109112
reject(error)
110113
});
111114
}

src/core/modules/assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class Assets {
119119
var options = Utils.mergeDeep(this.fetchOptions, fetchOptions);
120120
return Utils.sendRequest(this, options);
121121
} else {
122-
console.error("Kindly provide an asset uid. e.g. .Assets('asset_uid')");
122+
fetchOptions.logHandler('error', "Kindly provide an asset uid. e.g. .Assets('asset_uid')");
123123
}
124124
}
125125
}

src/core/stack.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ let errorRetry = [408, 429]
2525
* @param param.fetchOptions.retryCondition - A function to determine if the error can be retried. Default retry is on status codes 408, 429.
2626
* @param param.fetchOptions.retryDelayOptions.base - The base number of milliseconds to use in the exponential backoff for operation retries.
2727
* @param param.fetchOptions.retryDelayOptions.customBackoff - A custom function that accepts a retry count and error and returns the amount of time to delay in milliseconds.
28+
* @param param.fetchOptions.logHandler - A function for logging of requests, responses and errors
2829
*
2930
* @example
3031
* var Stack = Contentstack.Stack({
@@ -33,7 +34,7 @@ let errorRetry = [408, 429]
3334
* 'environment':'environment_name',
3435
* 'region': 'us',
3536
* 'fetchOptions': {
36-
*
37+
*
3738
* }
3839
* });
3940
*
@@ -49,6 +50,18 @@ export default class Stack {
4950
return true;
5051
}
5152
return false
53+
},
54+
logHandler: (level, data) => {
55+
if (level === 'error' && data) {
56+
console.error(`[error] ${data}`)
57+
return
58+
} else if (level === 'warning' && data) {
59+
console.warn(`[warning] ${data}`)
60+
return
61+
} else if (level === 'info' && data) {
62+
console.info(`[info] ${data}`)
63+
return
64+
}
5265
}
5366
};
5467
this.config = Utils.mergeDeep({}, config)
@@ -80,10 +93,10 @@ export default class Stack {
8093
this.environment = stack_arguments[0].environment;
8194
return this;
8295
} else {
83-
console.error("Kindly provide valid object parameters. The specified API Key, Delivery Token, or Environment Name is invalid.");
96+
this.fetchOptions.logHandler('error', "Kindly provide valid object parameters. The specified API Key, Delivery Token, or Environment Name is invalid.");
8497
}
8598
case 3:
86-
console.warn("WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!");
99+
this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!");
87100
if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") {
88101
this.headers = {
89102
api_key: stack_arguments[0],
@@ -92,18 +105,18 @@ export default class Stack {
92105
this.environment = stack_arguments[2];
93106
return this;
94107
} else {
95-
console.error("Kindly provide valid string parameters.");
108+
this.fetchOptions.logHandler('error', "Kindly provide valid string parameters.");
96109
}
97110
case 4:
98-
console.warn("WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!");
111+
this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!");
99112
if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") {
100113
this.headers = {
101114
api_key: stack_arguments[0],
102115
access_token: stack_arguments[1]
103116
};
104117
this.environment = stack_arguments[2];
105118
} else {
106-
console.error("Kindly provide valid string parameters.");
119+
this.fetchOptions.logHandler('error', "Kindly provide valid string parameters.");
107120
}
108121
if (stack_arguments[3]) {
109122
if(typeof stack_arguments[3] === "string" && stack_arguments[3] !== undefined && stack_arguments[3] !== "us") {
@@ -114,15 +127,15 @@ export default class Stack {
114127
}
115128
return this;
116129
case 5:
117-
console.warn("WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!");
130+
this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!");
118131
if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") {
119132
this.headers = {
120133
api_key: stack_arguments[0],
121134
access_token: stack_arguments[1]
122135
};
123136
this.environment = stack_arguments[2];
124137
} else {
125-
console.error("Kindly provide valid string parameters.");
138+
this.fetchOptions.logHandler('error', "Kindly provide valid string parameters.");
126139
}
127140

128141
if (stack_arguments[3]) {
@@ -137,7 +150,7 @@ export default class Stack {
137150
}
138151
return this;
139152
default:
140-
console.error("Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack.");
153+
this.fetchOptions.logHandler('error', "Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack.");
141154
}
142155

143156
}
@@ -203,7 +216,7 @@ export default class Stack {
203216
this.queryCachePolicy = policy;
204217
}
205218
} else {
206-
console.error("Kindly provide the valid policy");
219+
this.fetchOptions.logHandler('error', "Kindly provide the valid policy");
207220
}
208221
return this;
209222
}

0 commit comments

Comments
 (0)