Skip to content

feat: option to skip cache on M2M token#73

Merged
DanielRivers merged 2 commits intomainfrom
feat/skip-cache
Jul 7, 2025
Merged

feat: option to skip cache on M2M token#73
DanielRivers merged 2 commits intomainfrom
feat/skip-cache

Conversation

@DanielRivers
Copy link
Member

@DanielRivers DanielRivers commented Jul 7, 2025

Explain your changes

option to skip the cache on M2M tokens and fix for where the token is not string from cache

Checklist

🛟 If you need help, consider asking for advice over in the Kinde community.

@coderabbitai
Copy link

coderabbitai bot commented Jul 7, 2025

Walkthrough

The changes introduce a skipCache option to the getM2MToken function, allowing callers to bypass token caching and fetch a new token directly. The token-fetching logic is refactored into an internal function for reuse. Tests are added to verify various behaviors, including error handling and cache bypass scenarios.

Changes

File(s) Change Summary
lib/main.ts Added skipCache option to getM2MTokenOptions, refactored token fetching logic into fetchToken, updated token handling in createKindeAPI to stringify token objects.
lib/main.test.ts Added a comprehensive test suite for getM2MToken, covering caching, error handling, parameter validation, and the new skipCache option.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant getM2MToken
    participant Cache
    participant OAuth2Server

    Caller->>getM2MToken: Call with options (may include skipCache)
    alt skipCache is true
        getM2MToken->>OAuth2Server: POST /oauth2/token
        OAuth2Server-->>getM2MToken: Token response
        getM2MToken-->>Caller: Return token
    else skipCache is false or not set
        getM2MToken->>Cache: Check for cached token
        alt Token found and valid
            Cache-->>getM2MToken: Return cached token
            getM2MToken-->>Caller: Return token
        else Token missing or expired
            getM2MToken->>OAuth2Server: POST /oauth2/token
            OAuth2Server-->>getM2MToken: Token response
            getM2MToken->>Cache: Store new token
            getM2MToken-->>Caller: Return token
        end
    end
Loading

Possibly related PRs

  • feat: getM2MToken #72: Refactors and extends the getM2MToken function, directly modifying the same logic and cache usage as in this PR.

Suggested reviewers

  • evgenyk

📜 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 f9a8e58 and 68ebac6.

📒 Files selected for processing (1)
  • lib/main.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/main.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript-typescript)
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

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.

@codecov
Copy link

codecov bot commented Jul 7, 2025

Codecov Report

Attention: Patch coverage is 92.68293% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lib/main.ts 92.68% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

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

🧹 Nitpick comments (1)
lib/main.ts (1)

377-411: Remove console.log and improve error context.

The fetchToken function is well-structured with proper validation and error handling. However, there's a console.log statement that should be removed from production code.

-      console.log("returning token: ", result.json.access_token);
       return result.json.access_token;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2cc4c96 and f9a8e58.

📒 Files selected for processing (2)
  • lib/main.test.ts (3 hunks)
  • lib/main.ts (3 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: DanielRivers
PR: kinde-oss/infrastructure#72
File: lib/main.test.ts:68-70
Timestamp: 2025-06-24T11:26:45.395Z
Learning: The infrastructure library does not implement M2M token caching functionality internally. The getM2MToken function and its caching logic are handled by the Kinde platform/runtime, and this library serves as a wrapper that consumes those external capabilities through the global.kinde.cache.jwtToken interface.
lib/main.test.ts (2)
Learnt from: DanielRivers
PR: kinde-oss/infrastructure#72
File: lib/main.test.ts:68-70
Timestamp: 2025-06-24T11:26:45.395Z
Learning: The infrastructure library does not implement M2M token caching functionality internally. The getM2MToken function and its caching logic are handled by the Kinde platform/runtime, and this library serves as a wrapper that consumes those external capabilities through the global.kinde.cache.jwtToken interface.
Learnt from: DanielRivers
PR: kinde-oss/infrastructure#35
File: lib/types.ts:58-65
Timestamp: 2025-01-15T20:41:03.218Z
Learning: In the Kinde infrastructure, the `kinde.secureFetch` and `kinde.widget` bindings in `WorkflowSettings` are designed as simple feature flags using empty object types `{}`. They should not contain additional configuration properties.
lib/main.ts (1)
Learnt from: DanielRivers
PR: kinde-oss/infrastructure#72
File: lib/main.test.ts:68-70
Timestamp: 2025-06-24T11:26:45.395Z
Learning: The infrastructure library does not implement M2M token caching functionality internally. The getM2MToken function and its caching logic are handled by the Kinde platform/runtime, and this library serves as a wrapper that consumes those external capabilities through the global.kinde.cache.jwtToken interface.
🧬 Code Graph Analysis (1)
lib/main.test.ts (1)
lib/main.ts (1)
  • getM2MToken (373-430)
🪛 GitHub Check: codecov/patch
lib/main.ts

[warning] 475-477: lib/main.ts#L475-L477
Added lines #L475 - L477 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (4)
lib/main.ts (2)

370-370: LGTM: Clean addition of skipCache option.

The boolean option is well-typed and appropriately optional, maintaining backward compatibility.


413-429: LGTM: Clean implementation of cache bypass logic.

The conditional logic correctly implements the skipCache functionality while maintaining backward compatibility with the existing cache mechanism.

lib/main.test.ts (2)

32-32: LGTM: Proper import addition.

The import is correctly added to support the new test suite.


187-353: Excellent test coverage for getM2MToken functionality.

The test suite is comprehensive and well-structured, covering:

✅ Cache behavior with and without skipCache option
✅ Parameter validation and error scenarios
✅ Request body parameter verification
✅ Error handling for missing tokens and network failures
✅ onMissingOrExpired callback functionality

The tests properly use mocks and maintain isolation between test cases. This provides strong confidence in the implementation correctness.

@DanielRivers DanielRivers merged commit d812d9a into main Jul 7, 2025
7 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jul 9, 2025
2 tasks
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