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

Refactor req.accepts to utilize rest parameters for better argument handling #6140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ayoub-Mabrouk
Copy link

  • The function signature has been updated to accept a variable number of type arguments using the spread operator (...types).
  • The internal logic now directly invokes accepts(this).types(...types), simplifying the code and improving readability.

@Abdel-Monaam-Aouini
Copy link

/**
 * Determines if the specified MIME type(s) are acceptable based on the request's `Accept` header.
 * Returns the best match if a type is acceptable; otherwise, returns `undefined`.
 * If no acceptable type is found, the server should respond with a 406 "Not Acceptable".
 *
 * The `type` argument can be a:
 * - Single MIME type string (e.g., "application/json")
 * - Extension name (e.g., "json")
 * - Comma-delimited list of types (e.g., "json, html, text/plain")
 * - Argument list (e.g., `"json", "html", "text/plain"`)
 * - Array of types (e.g., `["json", "html", "text/plain"]`)
 *
 * The function returns the best match if any is found, otherwise `undefined`.
 *
 * Examples:
 *
 *     // Accept: text/html
 *     req.accepts('html');
 *     // => "html"
 *
 *     // Accept: text/*, application/json
 *     req.accepts('html');
 *     // => "html"
 *     req.accepts('text/html');
 *     // => "text/html"
 *     req.accepts('json, text');
 *     // => "json"
 *     req.accepts('application/json');
 *     // => "application/json"
 *
 *     // Accept: text/*, application/json
 *     req.accepts('image/png');
 *     req.accepts('png');
 *     // => undefined
 *
 *     // Accept: text/*;q=.5, application/json
 *     req.accepts(['html', 'json']);
 *     req.accepts('html', 'json');
 *     req.accepts('html, json');
 *     // => "json"
 *
 * @param {String|Array} types - A single type, list of types, or array of types to check against the `Accept` header.
 * @return {String|undefined} - The best matching type, or `undefined` if no match is found.
 * @public
 */

add this docs please

@Abdel-Monaam-Aouini
Copy link

write some tests please :)

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

Successfully merging this pull request may close these issues.

2 participants