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

fix: remove template on Application #16

Merged
merged 1 commit into from
Jan 2, 2025
Merged

fix: remove template on Application #16

merged 1 commit into from
Jan 2, 2025

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jan 2, 2025

Summary by CodeRabbit

  • Refactor

    • Simplified type handling in core application classes by removing generic type parameters
    • Updated Application, Request, and Response classes to use a standardized Context type
    • Streamlined context management across the application framework
  • Tests

    • Updated test cases to reflect new type handling in application context

Copy link

coderabbitai bot commented Jan 2, 2025

Walkthrough

The pull request focuses on simplifying the type system in the application by removing generic type parameters from the Application, Request, and Response classes. The changes standardize the context handling across these classes, replacing the generic <T extends Context> with a direct Context type. This modification affects type declarations, method signatures, and constructor parameters in multiple files, ensuring a more consistent and straightforward approach to context management.

Changes

File Change Summary
src/application.ts Removed generic type <T>, updated middleware, context storage, and class properties to use Context directly
src/request.ts Simplified class definition by removing generic type, updated ctx property and constructor to use Context
src/response.ts Removed generic type parameter, standardized ctx property to use Context type
test/application/context.test.ts Updated MyApp class to extend Application without generic type, modified app.use method type annotation

Possibly related PRs

Poem

🐰 Generics dance, now they're gone,
Context flows smooth as a dawn
Types simplified, code so clean
A rabbit's refactor, a coding dream!
Hop to simplicity, type-safe and bright 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f4b2c22 and 113fd92.

📒 Files selected for processing (4)
  • src/application.ts (7 hunks)
  • src/request.ts (1 hunks)
  • src/response.ts (1 hunks)
  • test/application/context.test.ts (1 hunks)
🔇 Additional comments (21)
test/application/context.test.ts (2)

50-50: Good alignment with removed generics in Application
By directly extending Application without a generic parameter, we maintain consistency with the new simplified type system. This change is aligned with the removal of generics throughout the codebase.


58-58: Explicit context type usage
Specifying ctx: MyContext clarifies that the middleware operates on the custom context class. This change preserves type safety even though the generic parameter on Application is removed.

src/application.ts (13)

30-30: Removal of generic type parameter
Dropping <T> from the Application class helps simplify the codebase and avoid type duplication. The new definition keeps the application's core functionality while streamlining type usage.


44-44: Revised middleware array type
Changing to MiddlewareFunc<Context>[] ensures consistency across all middleware, removing ambiguity introduced by a generic.


45-45: AsyncLocalStorage bound to Context
This aligns well with the simplified design. It ensures a consistent context object is stored for each request lifecycle.


47-47: ContextClass changed to ProtoImplClass
By removing <T>, the codebase adopts a single Context inheritance chain, making the context usage more uniform and predictable.


49-49: RequestClass updated
This matches the removal of generics in Request. References to Request<T> are replaced by Request, ensuring standard usage.


51-51: ResponseClass updated
Similarly, generics are removed in Response, simplifying the type references.


87-87: Anonymous subclass of Context
Creating an inline ApplicationContext class without generics maintains the same extensibility while avoiding type overhead.


89-89: Anonymous subclass of Request
Same approach for Request ensures the request object’s shape is consistent with the final Context type.


91-91: Anonymous subclass of Response
Following the same pattern, this maintains a single, uniform response type.


154-154: Parameterizing the middleware function
The generic <T extends Context = Context> in use<T> is a flexible approach. Despite removing generics from Application, it still offers compatibility for specialized contexts if needed.


163-163: Push cast to base Context
Casting fn to MiddlewareFunc<Context> ensures that the internal middleware pipeline remains correctly typed, while still allowing specialized contexts in external definitions.


199-199: handleRequest signature with Context
Changing from <T extends Context> to a single Context aligns with the new simplified architecture.


249-249: _respond method with a single Context
Ensures consistency across the request-response cycle with the updated type definitions.

src/response.ts (3)

19-19: Removing the generic type parameter
Simplifies Response and unifies all references to a single Context type.


24-24: ctx: Context
Directly assigning ctx as Context fosters a clear, uniform reference to the context.


27-27: Constructor alignment
Accepting Context directly ensures this class is consistent with the rest of the refactor.

src/request.ts (3)

19-19: Removing the generic type parameter
Streamlines Request, mirroring similar changes in Application and Response.


24-24: ctx: Context
Establishing a single context reference here improves maintainability and clarity.


28-28: Constructor type consistency
Accepting Context in the constructor ensures uniformity across the entire codebase.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Jan 2, 2025

Open in Stackblitz

npm i https://pkg.pr.new/eggjs/koa/@eggjs/koa@16

commit: 113fd92

@fengmk2 fengmk2 merged commit 0070bcf into master Jan 2, 2025
21 checks passed
@fengmk2 fengmk2 deleted the fix-App branch January 2, 2025 10:16
Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.44%. Comparing base (a3bdefe) to head (113fd92).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #16   +/-   ##
=======================================
  Coverage   98.44%   98.44%           
=======================================
  Files           6        6           
  Lines        1924     1924           
  Branches      366      366           
=======================================
  Hits         1894     1894           
  Misses         30       30           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

fengmk2 pushed a commit that referenced this pull request Jan 2, 2025
[skip ci]

## [2.20.4](v2.20.3...v2.20.4) (2025-01-02)

### Bug Fixes

* remove template on Application ([#16](#16)) ([0070bcf](0070bcf))
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.

1 participant