The current implementation of Boston.js provides several helper functions but lacks proper documentation and some necessary enhancements. This issue aims to:
- Improve Code Readability – Adding detailed comments to explain function purposes and parameters.
- Enhance Error Handling – Prevent duplicate error messages in invalidateField.
- Better Data Handling in request – Ensure JSON data is handled properly.
- Refactor for Maintainability – Improve variable naming and function robustness
Proposed Changes:
- Add comments for each function to explain its usage and parameters.
- Modify invalidateField to remove existing errors before appending a new one.
- Update request to automatically stringify object data when sending a request.
- Ensure better error handling to prevent function execution on undefined values.
`Code examples for fixes
/**
* Displays an error message below an input field, ensuring no duplicates.
* @param {HTMLElement} field - The input field.
* @param {string} message - The error message to display.
*/
function invalidateField(field, message) {
var existingErrors = field.parentElement.querySelector(".t--err");
if (existingErrors) existingErrors.remove();
var errors = document.createElement("div");
errors.className = "t--subinfo t--err m-t100";
errors.innerHTML = message;
field.parentElement.appendChild(errors);
}`
/**
* Sends an HTTP request with proper error handling and JSON data support.
*/
function request(obj, token) {
var request = new XMLHttpRequest();
request.open(obj.method, obj.url, true);
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
if (typeof obj.success === "function") obj.success(request);
} else {
if (typeof obj.error === "function") obj.error(request);
}
};
if (token) {
request.setRequestHeader("Authorization", "Token " + token);
}
request.onerror = function () {
if (typeof obj.error === "function") obj.error(request);
};
if (obj.data) {
if (typeof obj.data === "object") {
request.setRequestHeader("Content-Type", "application/json");
request.send(JSON.stringify(obj.data));
} else {
request.send(obj.data);
}
} else {
request.send();
}
}
Impact of the Changes:
- Better Maintainability – Makes it easier for new contributors to understand the code.
- Improved Error Handling – Reduces redundant error messages and unexpected behavior.
- More Robust HTTP Requests – Ensures correct data handling in request.
Would love to hear feedback from maintainers on this proposal!
The current implementation of Boston.js provides several helper functions but lacks proper documentation and some necessary enhancements. This issue aims to:
Proposed Changes:
Impact of the Changes:
Would love to hear feedback from maintainers on this proposal!