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

feat: Param decorator to parse our app user agent strings #16472

Merged
merged 2 commits into from
Oct 21, 2024

Conversation

eirikurn
Copy link
Member

@eirikurn eirikurn commented Oct 18, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new decorator for parsing user agent strings in HTTP requests, enhancing request handling in the application.
    • Added functionality to automatically inject parsed user agent information into route handlers.
  • Tests

    • Implemented a suite of unit tests to validate the behavior of the new user agent parsing functionality.

@eirikurn eirikurn requested a review from a team as a code owner October 18, 2024 14:11
Copy link
Contributor

coderabbitai bot commented Oct 18, 2024

Walkthrough

The changes introduce a new decorator for parsing user agent strings in a NestJS application. This involves adding the ParsedUserAgent.decorator to the module's exports and creating two new files: ParsedUserAgent.decorator.ts for the decorator's functionality and ParsedUserAgent.decorator.spec.ts for unit testing its behavior. The existing exports remain unchanged, and the new functionality allows for automatic injection of parsed user agent information into request contexts.

Changes

File Path Change Summary
libs/nest/core/src/index.ts Added export for ParsedUserAgent.decorator from ./lib/decorators/.
libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.ts Introduced functionality for parsing user agent strings, including UserAgent interface, parseUserAgent function, and ParsedUserAgent decorator.
libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.spec.ts Added unit tests for the parseUserAgent function using Jest, covering various user agent string scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Decorator

    Client->>Server: HTTP Request
    Server->>Decorator: Apply ParsedUserAgent
    Decorator->>Server: Parse User Agent
    Server->>Client: Response with Parsed User Agent Info
Loading

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 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.

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
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (5)
libs/nest/core/src/index.ts (1)

2-2: LGTM! Consider named exports for better tree-shaking.

The new export for ParsedUserAgent.decorator is correctly added and aligns with the PR objectives. It follows the existing pattern and promotes reusability across NestJS apps.

For even better tree-shaking, consider using named exports instead of export *. This allows bundlers to more easily eliminate unused code. For example:

export { ParsedUserAgent } from './lib/decorators/ParsedUserAgent.decorator';

Apply this change to all exports in the file for consistency.

libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.spec.ts (3)

3-78: LGTM: Well-structured test suite with comprehensive coverage.

The test suite is well-organized using Jest's describe and it functions, and follows the Arrange-Act-Assert pattern. It covers a good range of scenarios for both iOS and Android, including new and old user agent formats, as well as an unknown user agent.

Consider adding a test case for an empty user agent string to ensure robustness.


34-47: LGTM: Test case for old iOS app user agent.

This test case checks the parsing of an old iOS app user agent string, verifying the extraction of app name and OS name.

Consider adding a comment explaining why version and build number are not expected in the result for old user agent strings, to improve clarity for future developers.


49-62: LGTM: Test case for old Android app user agent.

This test case checks the parsing of an old Android app user agent string, verifying the extraction of app name and OS name.

Consider the following improvements:

  1. Add a comment explaining why version and build number are not expected in the result for old user agent strings.
  2. Clarify how the parser determines that this user agent string corresponds to Android and IslandIsApp, as it's not immediately obvious from the input string.
libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.ts (1)

75-77: Consider logging unrecognized user agent strings

When none of the patterns match, the function returns a default userAgent object with empty fields. Logging these unrecognized user agent strings can aid in monitoring and identifying new or unexpected user agents for future support.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3936331 and 067c720.

📒 Files selected for processing (3)
  • libs/nest/core/src/index.ts (1 hunks)
  • libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.spec.ts (1 hunks)
  • libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
libs/nest/core/src/index.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.spec.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🔇 Additional comments (7)
libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.spec.ts (5)

1-1: LGTM: Import statement is correct.

The import statement is properly structured and imports the necessary function for testing.


4-17: LGTM: Comprehensive test for new iOS app user agent.

This test case thoroughly checks the parsing of a new iOS app user agent string, verifying all expected fields including app name, version, build number, and OS details.


19-32: LGTM: Comprehensive test for new Android app user agent.

This test case thoroughly checks the parsing of a new Android app user agent string, verifying all expected fields including app name, version, build number, and OS details.


64-77: LGTM: Test case for unknown user agent.

This test case effectively checks the parsing of an unknown user agent string, verifying that the result contains empty objects for app and os as expected.


1-78: Adherence to coding guidelines for libs/**/*

This test file adheres to the TypeScript usage guideline. The reusability and tree-shaking guidelines are not directly applicable to test files.

libs/nest/core/src/lib/decorators/ParsedUserAgent.decorator.ts (2)

15-26: Well-defined UserAgent interface using TypeScript

The UserAgent interface is clearly defined, utilizing optional properties effectively, which promotes type safety and clarity in the codebase.


85-92: ParsedUserAgent decorator correctly injects parsed user agent information

The decorator effectively parses the user agent string from request headers and provides the parsed information in the request context, enhancing reusability across different parts of the application.

Copy link

codecov bot commented Oct 18, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 36.79%. Comparing base (cc13e8e) to head (3023a40).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...re/src/lib/decorators/ParsedUserAgent.decorator.ts 87.09% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #16472      +/-   ##
==========================================
+ Coverage   36.77%   36.79%   +0.01%     
==========================================
  Files        6844     6842       -2     
  Lines      141684   141626      -58     
  Branches    40358    40317      -41     
==========================================
- Hits        52107    52106       -1     
+ Misses      89577    89520      -57     
Flag Coverage Δ
api 3.37% <ø> (ø)
application-system-api 41.37% <ø> (ø)
application-template-api-modules 27.80% <ø> (+<0.01%) ⬆️
application-ui-shell 21.37% <ø> (ø)
license-api 42.53% <15.62%> (-0.22%) ⬇️
nest-core 43.54% <84.37%> (?)
services-auth-ids-api 51.42% <ø> (+0.03%) ⬆️
services-user-notification 46.93% <15.62%> (-0.09%) ⬇️
services-user-profile 61.77% <15.62%> (-0.43%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
libs/nest/core/src/index.ts 100.00% <100.00%> (ø)
...re/src/lib/decorators/ParsedUserAgent.decorator.ts 87.09% <87.09%> (ø)

... and 36 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc13e8e...3023a40. Read the comment docs.

@datadog-island-is
Copy link

datadog-island-is bot commented Oct 18, 2024

Datadog Report

All test runs 4dd571b 🔗

8 Total Test Services: 0 Failed, 8 Passed
🔻 Test Sessions change in coverage: 3 decreased, 2 increased, 13 no change

Test Services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
api 0 0 0 4 0 3s 1 no change Link
application-system-api 0 0 0 120 2 3m 23.63s 1 no change Link
application-template-api-modules 0 0 0 123 0 2m 27.34s 1 no change Link
application-ui-shell 0 0 0 74 0 33.7s 1 no change Link
license-api 0 0 0 80 0 27.76s 1 decreased (-0.05%) Link
services-auth-ids-api 0 0 0 152 0 1m 16.33s 1 increased (+0.01%) Link
services-user-notification 0 0 0 51 0 1m 26.07s 1 decreased (-0.08%) Link
services-user-profile 0 0 0 131 0 26.86s 1 decreased (-0.24%) Link

🔻 Code Coverage Decreases vs Default Branch (3)

  • services-user-profile - jest 52.54% (-0.24%) - Details
  • services-user-notification - jest 69.59% (-0.08%) - Details
  • license-api - jest 33.89% (-0.05%) - Details

Copy link
Member

@baering baering left a comment

Choose a reason for hiding this comment

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

LGTM

@eirikurn eirikurn added the automerge Merge this PR as soon as all checks pass label Oct 21, 2024
@kodiakhq kodiakhq bot merged commit 4e91db4 into main Oct 21, 2024
48 checks passed
@kodiakhq kodiakhq bot deleted the user-agent-app-version branch October 21, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Merge this PR as soon as all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants