Skip to content

Conversation

Phillip9587
Copy link
Contributor

This PR moves the common request validation logic, previously duplicated across all parsers, into the read function. This improves maintainability, reduces package size, and brings us closer to our goal of a generic body parser (see #22).

It also adds support for the defaultCharset option in normalizeOptions(). This option was already supported in the qs and text parsers, and I’ve now added support in the json parser as well. While we’ll likely want to validate these options more thoroughly in a future major release, the current approach should suffice for now.

There’s not much left before we can move the full middleware creation logic into its own function, but I wanted to keep this PR focused and manageable. Small steps toward our goals.

Copy link
Member

@UlisesGascon UlisesGascon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this work @Phillip9587 ❤️

@Phillip9587
Copy link
Contributor Author

Hey @jonchurch @wesleytodd, I’d appreciate your feedback on this PR. Planning to merge by the end of the month if no blockers come up.

@Phillip9587 Phillip9587 requested a review from Copilot August 28, 2025 20:37
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Refactors common request validation logic by moving it from individual parser functions into the centralized read function. This reduces code duplication and improves maintainability while adding consistent defaultCharset support across all parsers.

  • Consolidates request validation logic (body parsing checks, content-type validation, charset handling) into the read function
  • Adds defaultCharset option support to normalizeOptions() and the JSON parser
  • Removes duplicated validation code from all parser types (json, text, raw, urlencoded)

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/read.js Adds common request validation logic including body existence checks, content-type validation, and charset handling
lib/utils.js Adds defaultCharset option support to normalizeOptions() function
lib/types/*.js Removes duplicated validation code and updates to use centralized read function with normalized options
test/utils.js Adds test coverage for the new defaultCharset option in normalizeOptions()
README.md Documents the new defaultCharset option for the JSON parser
HISTORY.md Documents the refactoring changes

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

encoding = getCharset(req) || options.defaultCharset

// validate charset
if (!!options?.isValidCharset && !options.isValidCharset(encoding)) {
Copy link
Preview

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double negation !!options?.isValidCharset is unnecessary. Since isValidCharset is a function, you can check it directly: if (options?.isValidCharset && !options.isValidCharset(encoding))

Suggested change
if (!!options?.isValidCharset && !options.isValidCharset(encoding)) {
if (options?.isValidCharset && !options.isValidCharset(encoding)) {

Copilot uses AI. Check for mistakes.

@@ -16,6 +16,8 @@ var getBody = require('raw-body')
var iconv = require('iconv-lite')
var onFinished = require('on-finished')
var zlib = require('node:zlib')
var hasBody = require('type-is').hasBody
Copy link
Preview

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using destructuring assignment for consistency with other imports: var { hasBody } = require('type-is')

Suggested change
var hasBody = require('type-is').hasBody
var { hasBody } = require('type-is')

Copilot uses AI. Check for mistakes.

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