feat: adds ttl_seconds support#420
Conversation
WalkthroughThis change introduces support for specifying a token time-to-live (TTL) when generating temporary tokens. It adds a new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant AuthRestClient
participant AuthAPI
Client->>AuthRestClient: grantToken({ ttl_seconds: 60 })
AuthRestClient->>AuthAPI: POST /auth/grant (body: { ttl_seconds: 60 })
AuthAPI-->>AuthRestClient: { access_token, expires_in }
AuthRestClient-->>Client: { access_token, expires_in }
Possibly related PRs
Suggested reviewers
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #420 +/- ##
==========================================
+ Coverage 75.13% 75.17% +0.04%
==========================================
Files 26 26
Lines 1150 1152 +2
Branches 291 292 +1
==========================================
+ Hits 864 866 +2
Misses 286 286 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/e2e/auth-grant-token.test.ts (1)
75-95: LGTM! Consider validating TTL impact on expires_in.The test properly validates the new ttl_seconds functionality. Consider adding an assertion to verify that the expires_in value reflects the requested TTL (e.g.,
expect(result.expires_in).toBeLessThanOrEqual(60)).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
examples/node-live-token/index.js(1 hunks)src/lib/types/GrantTokenSchema.ts(1 hunks)src/lib/types/index.ts(1 hunks)src/packages/AuthRestClient.ts(1 hunks)tests/__utils__/mocks.ts(1 hunks)tests/e2e/auth-grant-token.test.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
examples/node-live-token/index.js (6)
examples/node-agent-live/index.js (1)
deepgram(9-9)examples/node-live/index.js (1)
deepgram(9-9)examples/node-speak-live/index.js (1)
deepgram(10-10)examples/node-read/index.js (2)
deepgram(8-8)deepgram(11-16)examples/node-prerecorded/index.js (6)
deepgram(65-65)deepgram(68-75)deepgram(84-84)deepgram(91-93)deepgram(100-100)deepgram(103-105)examples/node-speak/index.js (1)
deepgram(10-10)
tests/__utils__/mocks.ts (1)
tests/e2e/__mocks__/auth.ts (1)
mockGrantTokenResponse(5-9)
src/packages/AuthRestClient.ts (3)
src/lib/types/GrantTokenSchema.ts (1)
GrantTokenSchema(1-9)src/lib/types/DeepgramResponse.ts (1)
DeepgramResponse(3-3)src/lib/types/GrantTokenResponse.ts (1)
GrantTokenResponse(1-4)
⏰ 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: Test Browser Builds
🔇 Additional comments (16)
src/lib/types/index.ts (1)
30-30: LGTM! Export addition follows conventions.The export is properly placed in alphabetical order and follows the established pattern for making the new
GrantTokenSchemainterface publicly available.tests/__utils__/mocks.ts (1)
127-147: LGTM! Mock enhancement properly supports ttl_seconds functionality.The mock correctly:
- Parses the request body to extract
ttl_secondsparameter- Dynamically adjusts the
expires_infield in the response- Handles JSON parsing errors gracefully by falling back to default response
- Maintains backward compatibility
src/lib/types/GrantTokenSchema.ts (1)
1-9: LGTM! Well-designed interface with clear documentation.The interface design is excellent:
- Extends
Record<string, unknown>for future extensibility- Optional
ttl_secondsproperty maintains backward compatibility- Clear JSDoc documentation with appropriate constraints (1-3600 seconds)
- Reasonable default example of 30 seconds
Note that the
@minimumand@maximumconstraints are documentation-only and not enforced at runtime. Consider adding runtime validation if needed.examples/node-live-token/index.js (3)
11-15: LGTM! Example demonstrates the new API correctly.The token generation now uses the simplified API with the
ttl_secondsparameter, which clearly demonstrates the new functionality.
18-26: LGTM! Enhanced logging improves user experience.The added console logging provides clear feedback about token generation status, expiration time, and token preview, which helps users understand the token lifecycle.
29-31: LGTM! Corrected client creation usage.The client creation now correctly uses the
accessTokenproperty instead of a direct key, which aligns with the established pattern for token-based authentication.src/packages/AuthRestClient.ts (4)
4-4: LGTM! Proper type import.The import of
GrantTokenSchematype is correctly added to support the new functionality.
12-12: LGTM! Documentation updated appropriately.The JSDoc documentation clearly explains the new optional
optionsparameter and its purpose for token configuration.
17-17: LGTM! Backward-compatible method signature.The optional
optionsparameter with default empty object maintains backward compatibility while enabling the new functionality.
22-25: LGTM! Proper request body serialization.The implementation correctly:
- Serializes the options object to JSON
- Sets the appropriate Content-Type header
- Maintains the existing error handling pattern
tests/e2e/auth-grant-token.test.ts (6)
26-54: LGTM! Well-structured test for basic functionality.The test properly validates the default behavior with comprehensive assertions for error handling, result structure, and data types.
56-73: LGTM! Properly updated for new method signature.The test correctly adapts to the new method signature by passing an empty options object as the first parameter, maintaining backward compatibility while testing custom endpoint functionality.
97-114: LGTM! Validates feature interaction properly.The test correctly validates that ttl_seconds works in combination with custom endpoints, ensuring feature compatibility.
116-132: LGTM! Important backward compatibility test.The test ensures that passing an empty options object maintains existing functionality, which is crucial for backward compatibility.
134-150: LGTM! Proper error handling test.The test correctly validates DeepgramError handling with proper mocking and cleanup.
152-165: LGTM! Validates proper error re-throwing.The test correctly validates that non-DeepgramError exceptions are re-thrown, maintaining expected error behavior.
Add ttl_seconds support to grant token endpoint
TL;DR
✅ Successfully implemented
ttl_secondsparameter support for the grant token endpoint🧪 SDK Implementation: Fully working with comprehensive tests - ready for production when API issue is fixed by Console Team.
🎯 What was implemented
Added support for the new
ttl_secondsparameter to thedeepgram.auth.grantToken()method, allowing users to specify custom token expiration times (1-3600 seconds).Key Features:
GrantTokenSchemainterface withttl_secondsparameterAuthRestClient.grantToken()method to accept configuration options📁 Files Changed
New Files:
src/lib/types/GrantTokenSchema.ts- Schema interface for grant token parametersModified Files:
src/lib/types/index.ts- Export new schemasrc/packages/AuthRestClient.ts- Updated method signature and implementationtests/e2e/auth-grant-token.test.ts- Added comprehensive test coveragetests/__utils__/mocks.ts- Updated mock to handle ttl_seconds parameterexamples/node-live-token/index.js- Fixed incorrect usage and added proper logging🧪 Testing
Test Results:
Test Coverage:
🚀 Usage Examples
Basic usage:
Real-world example:
Types of changes
What types of changes does your code introduce to the community JavaScript SDK?
Put an
xin the boxes that applyChecklist
Put an
xin the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.Further comments
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores