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: rework client configuration to make DX better #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

hckhanh
Copy link
Owner

@hckhanh hckhanh commented Dec 2, 2024

BREAKING CHANGE: Simplified the API by moving client metadata from the request to the constructor in GoogleSafeBrowsing. This change streamlines the threatMatches.find method usage by reducing redundancy, making it easier to initialize and use the API client in different contexts. Updated relevant documentation and tests to reflect these modifications.

Summary by Sourcery

Rework the GoogleSafeBrowsing client configuration by moving client metadata from the request to the constructor, simplifying the API usage and improving developer experience. Update documentation and tests to align with these changes.

New Features:

  • Introduce a new client metadata parameter in the GoogleSafeBrowsing constructor to streamline API client initialization.

Enhancements:

  • Simplify the threatMatches.find method by removing redundant client metadata from the request and incorporating it into the constructor.

Documentation:

  • Update the README documentation to reflect changes in the API usage, specifically the new constructor signature for GoogleSafeBrowsing.

Tests:

  • Modify existing tests to accommodate the new client metadata parameter in the GoogleSafeBrowsing constructor.

BREAKING CHANGE: Simplified the API by moving client metadata from the request to the constructor in GoogleSafeBrowsing. This change streamlines the threatMatches.find method usage by reducing redundancy, making it easier to initialize and use the API client in different contexts. Updated relevant documentation and tests to reflect these modifications.
@hckhanh hckhanh self-assigned this Dec 2, 2024
Copy link

sourcery-ai bot commented Dec 2, 2024

Reviewer's Guide by Sourcery

The PR refactors the GoogleSafeBrowsing client configuration by moving client metadata from the request payload to the constructor. This simplifies the API usage by reducing parameter redundancy and making the client initialization more straightforward. The implementation involves modifying the class constructor, updating the findThreatMatches method signature, and removing the now-unnecessary FindThreatMatchesRequest interface.

Updated class diagram for GoogleSafeBrowsing

classDiagram
    class GoogleSafeBrowsing {
        - String apiKey
        - ClientInfo client
        - String endpoint
        + GoogleSafeBrowsing(String apiKey, ClientInfo client, String endpoint)
        + findThreatMatches(ThreatInfo threatInfo) Promise<FindThreatMatchesResponse>
    }

    class ClientInfo {
        + String clientId
        + String clientVersion
    }

    class ThreatInfo {
        + List<String> threatTypes
        + List<String> platformTypes
        + List<String> threatEntryTypes
        + List<ThreatEntry> threatEntries
    }

    class ThreatEntry {
        + String url
    }
Loading

File-Level Changes

Change Details Files
Moved client metadata configuration from request to constructor
  • Added client metadata as a required constructor parameter
  • Added private readonly client field to store metadata
  • Removed client metadata from findThreatMatches request parameter
  • Updated constructor documentation with new client parameter
src/GoogleSafeBrowsing.ts
Simplified findThreatMatches method signature
  • Changed parameter type from FindThreatMatchesRequest to ThreatInfo
  • Updated method documentation and examples
  • Removed FindThreatMatchesRequest interface
  • Modified request body construction to use stored client metadata
src/GoogleSafeBrowsing.ts
Updated documentation and tests to reflect new API structure
  • Updated README examples with new client initialization and method usage
  • Modified test cases to use new API structure
  • Updated API section headers in README
README.md
src/GoogleSafeBrowsing.test.ts

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

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

Here's what I looked at during the review
  • 🟡 General issues: 4 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

src/GoogleSafeBrowsing.ts Outdated Show resolved Hide resolved
src/GoogleSafeBrowsing.ts Show resolved Hide resolved

Finds the threat entries that match the Safe Browsing lists.

```ts
import { GoogleSafeBrowsing } from '@hckhanh/google-safe-browsing'

const client = new GoogleSafeBrowsing('apiKey')
const client = new GoogleSafeBrowsing('apiKey', {
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider documenting the purpose and requirements for clientId and clientVersion configuration parameters

Adding a brief explanation of these configuration parameters would help developers understand how to properly set up the client.

// Initialize Google Safe Browsing client with API key and required identifiers
// clientId: Unique identifier for your application instance
// clientVersion: Current version of your application
const client = new GoogleSafeBrowsing('apiKey', {

README.md Outdated
threatEntryTypes: ['URL'],
threatEntries: [
{ url: 'http://malware.testing.google.test/testing/malware/' },
],
})

const hasRisk = result.matches !== undefined && result.matches.length > 0
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider documenting the response structure and possible values

It would be helpful to include documentation about the structure of the response object and what developers can expect in different scenarios.

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

This update introduces a new method to detect threats from URLs using the Google Safe Browsing API. It includes enhanced error handling with more descriptive error messages and updates to the README.md with usage examples. Additionally, tests have been added to confirm the correct detection of malicious links.
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