Skip to content

fix: type rawBody as optional since it can be undefined - #176

Open
marceli1404 wants to merge 1 commit into
koajs:masterfrom
marceli1404:fix/rawbody-type
Open

fix: type rawBody as optional since it can be undefined#176
marceli1404 wants to merge 1 commit into
koajs:masterfrom
marceli1404:fix/rawbody-type

Conversation

@marceli1404

@marceli1404 marceli1404 commented Aug 4, 2026

Copy link
Copy Markdown

Closes #174

The module augments koa.Request and http.IncomingMessage with rawBody: string, but at runtime the property is only assigned when a body is actually parsed — it remains undefined for unsupported content types, disabled parsers, closed requests, or when the request is skipped. With skipLibCheck: false (or in strict consumer projects) TypeScript reports rawBody as always a string, which is misleading and can hide runtime bugs.

Change

  • koa.Request.rawBodystring | undefined (optional)
  • http.IncomingMessage.rawBodystring | undefined (optional)

The existing test suite already asserts the undefined behavior (e.g. expect(ctx.request.rawBody).toEqual(undefined) in test/middleware.test.ts), so this only aligns the declared types with the implemented behavior.

Verification

  • npm test: 34 passed
  • npm run build (tsup + dts): success

Summary by Sourcery

Align type declarations of rawBody with its runtime behavior by making it optional on Koa and Node request objects.

Bug Fixes:

  • Mark koa.Request.rawBody as an optional string to reflect that it may be undefined at runtime.
  • Mark http.IncomingMessage.rawBody as an optional string to reflect that it may be undefined at runtime.

Summary by CodeRabbit

  • Bug Fixes
    • Improved type compatibility by allowing the raw request body to be unavailable when it hasn’t been populated.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 37eb25d9-69d0-43ad-92cc-c4c7961675f9

📥 Commits

Reviewing files that changed from the base of the PR and between b9b77aa and 277a495.

📒 Files selected for processing (1)
  • src/body-parser.ts

📝 Walkthrough

Walkthrough

The Koa Request and Node IncomingMessage module augmentations now declare rawBody as an optional string. This matches runtime behavior when no suitable body type is found.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states that rawBody is now optional, which matches the primary change.
Linked Issues check ✅ Passed The changes satisfy issue [#174] by declaring rawBody as optional where runtime behavior can produce undefined.
Out of Scope Changes check ✅ Passed The pull request only updates rawBody declarations and contains no unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai

sourcery-ai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This pull request updates the TypeScript declaration of the rawBody property on koa.Request and http.IncomingMessage to be optional (string | undefined), aligning the types with actual runtime behavior where rawBody is only set when a body is parsed.

File-Level Changes

Change Details Files
Make rawBody optional in type declarations to match runtime behavior.
  • Change koa.Request.rawBody type from string to optional string
  • Change http.IncomingMessage.rawBody type from string to optional string
src/body-parser.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#174 Update the TypeScript declaration for koa.Request.rawBody so that it reflects that rawBody can be undefined at runtime (i.e., make it optional or type it as string undefined).
#174 Update the TypeScript declaration for http.IncomingMessage.rawBody so that it reflects that rawBody can be undefined at runtime (i.e., make it optional or type it as string undefined).

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

rawBody can be undefined but it's typed as a string

1 participant