Skip to content

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

Open
Ayoub-Mabrouk wants to merge 1 commit intoexpressjs:masterfrom
Ayoub-Mabrouk:chore/req-accepts
Open

Refactor req.accepts to utilize rest parameters for better argument handling#6140
Ayoub-Mabrouk wants to merge 1 commit intoexpressjs:masterfrom
Ayoub-Mabrouk:chore/req-accepts

Conversation

@Ayoub-Mabrouk
Copy link
Copy Markdown
Contributor

  • 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
Copy Markdown
Contributor

/**
 * 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
Copy Markdown
Contributor

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.

3 participants