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 ContextDelegation #283

Merged
merged 2 commits into from
Jan 2, 2025
Merged

fix: remove ContextDelegation #283

merged 2 commits into from
Jan 2, 2025

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jan 2, 2025

Summary by CodeRabbit

  • Dependencies

    • Updated @eggjs/koa to version 2.20.3
    • Updated @eggjs/bin to version 7
    • Updated @types/node to version 22
    • Updated pedding to version 2
    • Added new dependency @eggjs/supertest with version 8.1.1
  • Type Improvements

    • Enhanced type flexibility for BaseContextClass
    • Streamlined context-related type definitions
    • Improved type safety across multiple components
  • Testing

    • Added new assertions to verify EggCore module properties
    • Updated import statements for request and mm modules across multiple test files

Copy link

coderabbitai bot commented Jan 2, 2025

Walkthrough

The pull request introduces type-related changes across multiple files in the @eggjs/core package. The modifications primarily focus on refactoring context-related types, replacing ContextDelegation with more generic Context types, and updating dependency versions. The changes span the package.json, base context class, egg core types, and loader implementations, enhancing type safety and flexibility in the codebase.

Changes

File Change Summary
package.json - Updated @eggjs/koa to ^2.20.3
- Changed @eggjs/bin from ^7.0.0 to 7
- Updated @types/node from 20 to 22
- Modified mm from 3 to ^4.0.2
- Changed pedding from ^2.0.0 to 2
- Added @eggjs/supertest with version ^8.1.1
src/base_context_class.ts - Added generics to BaseContextClass
- Updated ctx and service property types
- Modified constructor parameter type
src/egg.ts - Removed ContextDelegation type
- Updated Response and MiddlewareFunc type definitions
src/loader/context_loader.ts - Changed ctx type from ContextDelegation to Context
- Updated ClassLoaderOptions and ClassLoader types
src/loader/egg_loader.ts - Updated context-related type imports
- Modified prototype reference to KoaContext
test/index.test.ts - Added new assertion to verify EggCore properties
test/asyncLocalStorage.test.ts - Changed import from supertest to @eggjs/supertest
test/egg-ts.test.ts - Updated import for mm and request from supertest to @eggjs/supertest
test/egg.test.ts - Updated import for mm and request from supertest to @eggjs/supertest
test/loader/context_loader.test.ts - Changed import from supertest to @eggjs/supertest
test/loader/egg_loader.test.ts - Updated import for mm from default to named import
test/loader/get_app_info.test.ts - Changed import for mm from default to named import
test/loader/get_framework_paths.test.ts - Changed import for mm from default to named import
test/loader/get_load_units.test.ts - Changed import for mm from default to named import
test/loader/get_server_env.test.ts - Changed import for mm from default to named import
test/loader/load_file.test.ts - Changed import for mm from default to named import
test/loader/mixin/load_config.test.ts - Changed import for mm from default to named import
test/loader/mixin/load_controller.test.ts - Changed import from supertest to @eggjs/supertest
test/loader/mixin/load_custom_loader.test.ts - Changed import from supertest to @eggjs/supertest
test/loader/mixin/load_extend.test.ts - Changed import for request from supertest to @eggjs/supertest
- Changed import for mm from default to named import
test/loader/mixin/load_extend_class.test.ts - Changed import for request from supertest to @eggjs/supertest
- Changed import for mm from default to named import
test/loader/mixin/load_helper_extend.test.ts - Changed import from supertest to @eggjs/supertest
test/loader/mixin/load_middleware.test.ts - Changed import from supertest to @eggjs/supertest
test/loader/mixin/load_plugin.test.ts - Changed import for mm from default to named import
test/loader/mixin/load_service.test.ts - Changed import from supertest to @eggjs/supertest
- Changed import for mm from default to named import
test/support-typescript.test.ts - Changed import from supertest to @eggjs/supertest
test/utils/index.test.ts - Changed import for mm from default to named import
test/utils/router.test.ts - Changed import from supertest to @eggjs/supertest

Sequence Diagram

sequenceDiagram
    participant Dev as Developer
    participant Core as EggCore
    participant Context as Context
    participant Loader as ClassLoader

    Dev->>Core: Initialize Application
    Core->>Context: Create Context
    Context->>Loader: Pass Context
    Loader->>Loader: Instantiate with Generic Context
    Loader-->>Dev: Return Configured Instance
Loading

Poem

🐰 Hop, hop, through types so bright,
Generics dancing with pure delight!
Context flows like a river free,
Egg's core now typed with harmony
A rabbit's code, both swift and light! 🚀


📜 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 8b0765e and c03ea59.

📒 Files selected for processing (23)
  • package.json (2 hunks)
  • test/asyncLocalStorage.test.ts (1 hunks)
  • test/egg-ts.test.ts (1 hunks)
  • test/egg.test.ts (1 hunks)
  • test/loader/context_loader.test.ts (1 hunks)
  • test/loader/egg_loader.test.ts (1 hunks)
  • test/loader/get_app_info.test.ts (1 hunks)
  • test/loader/get_framework_paths.test.ts (1 hunks)
  • test/loader/get_load_units.test.ts (1 hunks)
  • test/loader/get_server_env.test.ts (1 hunks)
  • test/loader/load_file.test.ts (1 hunks)
  • test/loader/mixin/load_config.test.ts (1 hunks)
  • test/loader/mixin/load_controller.test.ts (1 hunks)
  • test/loader/mixin/load_custom_loader.test.ts (1 hunks)
  • test/loader/mixin/load_extend.test.ts (1 hunks)
  • test/loader/mixin/load_extend_class.test.ts (1 hunks)
  • test/loader/mixin/load_helper_extend.test.ts (1 hunks)
  • test/loader/mixin/load_middleware.test.ts (1 hunks)
  • test/loader/mixin/load_plugin.test.ts (1 hunks)
  • test/loader/mixin/load_service.test.ts (1 hunks)
  • test/support-typescript.test.ts (1 hunks)
  • test/utils/index.test.ts (1 hunks)
  • test/utils/router.test.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • test/loader/mixin/load_config.test.ts
  • test/loader/mixin/load_plugin.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • package.json
🔇 Additional comments (21)
test/support-typescript.test.ts (1)

2-2: Adopt @eggjs/supertest for consistency across the test suite
The switch from the supertest package to @eggjs/supertest aligns with the broader dependency updates in this PR, ensuring all tests remain consistent with Egg's testing approach.

test/loader/get_load_units.test.ts (1)

2-2: Switch to named import for mm
Migrating from default to named imports provides better clarity and consistency now that mm is versioned differently. Confirm that all references within the file indeed align with the named import usage.

test/loader/mixin/load_extend_class.test.ts (2)

2-2: Migrate to @eggjs/supertest
Using @eggjs/supertest instead of the original supertest package ensures alignment with Egg's customized test environment and fosters consistent mocking capabilities.


3-3: Use named import for mm
Importing mm as a named symbol matches the updates elsewhere in this PR. Verify that any default import references in the related files have been updated as well.

test/loader/get_app_info.test.ts (1)

2-2: Switch to named import for mm
The new named import style for mm is consistent with updates in other test files and should prevent confusion around default vs. named imports going forward.

test/asyncLocalStorage.test.ts (1)

3-3: Switch to @eggjs/supertest import looks good.
This aligns testing utilities with the Egg.js ecosystem. Ensure the @eggjs/supertest package is installed and that all tests have been updated accordingly.

test/loader/mixin/load_helper_extend.test.ts (1)

1-1: Consistent change to @eggjs/supertest.
This import update is consistent with other files in the PR. No further issues spotted.

test/loader/get_server_env.test.ts (1)

2-2: Switch to named import for mm.
Ensure that the new named import mm is compatible with usage in this file, especially if only named exports are provided by the mm library.

test/loader/context_loader.test.ts (1)

1-1: Importing request from @eggjs/supertest
This aligns with the Egg.js test framework updates. Confirm that any advanced features of supertest you rely on are still supported in @eggjs/supertest.

test/loader/get_framework_paths.test.ts (1)

2-2: Use named import for mm with updated version.

Switching from default to named import is a valid approach if mm now exports a named member. Please confirm that all references to mm throughout the code are updated accordingly and no default import usage remains.

test/loader/load_file.test.ts (1)

2-2: Confirm mm usage is consistent.

This transition from default to named import suggests that mm has changed its export format. Verify that the call sites in this module and beyond use the named import correctly.

test/loader/egg_loader.test.ts (1)

4-4: Validate mm usage across the test suite.

Since this test file also replaces the default import with a named import, ensure the entire test suite aligns with mm v4’s API. Any leftover references to the default import might cause runtime issues.

test/loader/mixin/load_custom_loader.test.ts (1)

2-2: Confirm new request library behavior.

Switching from supertest to @eggjs/supertest might introduce subtle differences in request handling. Please verify the request API usage and confirm that tests behave as expected.

test/loader/mixin/load_service.test.ts (1)

3-4: Import updates are consistent and correct
These import modifications (switching to @eggjs/supertest and named import for mm) look appropriate and align with the changes in other files.

test/loader/mixin/load_extend.test.ts (1)

2-3: Imports have been successfully standardized
Moving to @eggjs/supertest for requests and switching to a named import for mm is consistent with the pull request’s objective to unify module imports.

test/utils/index.test.ts (1)

3-3: Named import for mm
This change is part of the overall consistency improvements. No functional issues detected.

test/loader/mixin/load_middleware.test.ts (1)

3-3: Use of @eggjs/supertest for request
This update aligns with the standardized request import across the test suite.

test/egg-ts.test.ts (1)

2-3: Use named imports consistently for mm and request.

These updated import statements appear correct. Just ensure all references to mm and request throughout this file are aligned with the new named import syntax and that no references to the default import remain.

test/utils/router.test.ts (1)

2-2: Switching to @eggjs/supertest.

This appears to be a straightforward import change that aligns with the rest of the test suite. Make sure all references to request comply with the new named import and there are no leftover references to supertest.

test/loader/mixin/load_controller.test.ts (1)

3-3: Use of @eggjs/supertest is consistent.

Migrating from supertest to @eggjs/supertest looks correct and maintains consistency with other tests. Confirm that the rest of the file references the named import properly.

test/egg.test.ts (1)

6-7: Adopting named imports for mm and request.

The import changes for mm and request are valid. Verify that all legacy references to default imports are removed or updated.


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/core/@eggjs/core@283

commit: c03ea59

Copy link

socket-security bot commented Jan 2, 2025

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher

🚮 Removed packages: npm/@types/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected]

View full report↗︎

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.74%. Comparing base (1d3ab39) to head (c03ea59).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #283      +/-   ##
==========================================
- Coverage   97.75%   97.74%   -0.01%     
==========================================
  Files          10       10              
  Lines        3378     3376       -2     
  Branches      595      595              
==========================================
- Hits         3302     3300       -2     
  Misses         76       76              

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/index.test.ts (1)

10-10: Consider using a logger instead of console.log
While console.log works, a dedicated logger would provide more structure and flexibility for debugging.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e5204a5 and 8b0765e.

📒 Files selected for processing (6)
  • package.json (3 hunks)
  • src/base_context_class.ts (2 hunks)
  • src/egg.ts (2 hunks)
  • src/loader/context_loader.ts (2 hunks)
  • src/loader/egg_loader.ts (3 hunks)
  • test/index.test.ts (1 hunks)
🔇 Additional comments (20)
test/index.test.ts (1)

11-32: Good addition verifying module keys
This assertion ensures that all expected exports are present on EggCore.

src/base_context_class.ts (3)

1-1: Import statements look good
The new types being imported reflect the updated removal of ContextDelegation, aligning well with the broader refactor.


8-12: Generics enhance flexibility and type safety
Using T extends Context broadens the functionality for classes extending BaseContextClass. This is a good step toward more generic usage patterns.


Line range hint 17-37: Consistency in constructor
The constructor properly sets ctx as well as service. The removal of ContextDelegation references here is consistent with the new approach.

src/loader/context_loader.ts (4)

4-4: Switched to type import from 'egg.js'
Replacing ContextDelegation with Context is consistent with the goal of removing ContextDelegation references throughout.


9-9: ClassLoaderOptions interface properly updated
Ensures that ctx in ClassLoaderOptions is the Context type, maintaining type consistency in loading contexts.


15-15: Updating _ctx to Context
This aligns with the removal of ContextDelegation everywhere. Good to see uniform usage of Context.


101-101: getInstance function aligned with the new Context
This ensures that instance creation leverages the updated context type.

src/egg.ts (3)

44-44: Re-exporting KoaMiddlewareFunc under its new type
The type re-export is appropriate and preserves backward compatibility for downstream code.


53-53: Extending KoaResponse with generics
Replacing KoaResponse param with Context is consistent with removing ContextDelegation.


66-66: MiddlewareFunc updated to use the new Context
This simplification aligns with the overall refactor. Great job maintaining consistency across the codebase.

src/loader/egg_loader.ts (5)

10-10: Use of KoaContext is consistent with removal of ContextDelegation.

Good job replacing the direct Context import with KoaContext from @eggjs/koa. This aligns with the stated PR objective to remove ContextDelegation, ensuring all references now point to the updated Koa context type.


28-28: Prototype change seems aligned with the Koa-based approach.

Switching context to KoaContext.prototype is a necessary step to ensure the updated Koa context is being used. This is a straightforward replacement and should not introduce functional issues, assuming the rest of the code is updated consistently.


1696-1696: Context typing in class controller middleware.

The this: Context type annotation for the class controller middleware clarifies that this is bound to the request context, improving type safety for controller methods. Ensure the calling convention across your code does pass the correct Context here to avoid runtime issues in certain edge cases.


1732-1732: Object controller middleware typed with this: Context.

Likewise, using this: Context in this asynchronous object controller middleware helps ensure correct references to the contextual properties. You might consider adding unit tests verifying these handlers to ensure future code refactors remain type-compliant.


18-20: Verify external references to ContextDelegation.

Since ContextDelegation is now replaced by Context here, please confirm no remaining references to ContextDelegation exist in the codebase or in dependent modules (e.g., egg.js).

Run the following script to detect references outside of the provided file:

✅ Verification successful

Based on the search results, I can see that there are no references to ContextDelegation in the codebase. The file src/loader/egg_loader.ts shows that it's using Context type from the imports, and there are no other files containing any references to ContextDelegation.

No remaining references to ContextDelegation found in the codebase.

The change from ContextDelegation to Context appears to be a clean replacement with no lingering references that need attention.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any lingering references to `ContextDelegation`
rg 'ContextDelegation'

Length of output: 22


Script:

#!/bin/bash
# Let's try a case-insensitive search and also look for potential variations
rg -i "contextdeleg"

# Also, let's check the egg.js file specifically since it was mentioned in the imports
fd -e js -e ts "egg" --exec cat {}

Length of output: 108417

package.json (4)

41-41: Version bump for @eggjs/koa.

Upgrading to ^2.20.3 maintains compatibility with the new KoaContext usage. Confirm that no breaking changes exist in the minor update.
[approve]


58-58: Pinned version for @eggjs/bin.

Switching from ^7.0.0 to a pinned "7" ensures a stable, predictable environment. This is helpful to avoid unplanned minor updates, but stay cautious about missing out on patches.


62-62: Updated @types/node to version 22.

Using a higher version can unlock better type definitions, but verify local environment compatibility.


71-71: Pinned version for pedding.

Pinning from ^2.0.0 to "2" prevents unintentional version increments. Good for stability; just keep an eye out for any needed security patches.

@fengmk2 fengmk2 merged commit c56cf7b into master Jan 2, 2025
23 checks passed
@fengmk2 fengmk2 deleted the remove-ContextDelegation branch January 2, 2025 09:14
fengmk2 pushed a commit that referenced this pull request Jan 2, 2025
[skip ci]

## [6.2.9](v6.2.8...v6.2.9) (2025-01-02)

### Bug Fixes

* remove ContextDelegation ([#283](#283)) ([c56cf7b](c56cf7b))
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