Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion for improvement of script file #801

Open
SophiaLi20 opened this issue Feb 9, 2025 · 0 comments
Open

suggestion for improvement of script file #801

SophiaLi20 opened this issue Feb 9, 2025 · 0 comments

Comments

@SophiaLi20
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant