feat: support token-based auth endpoint#377
Conversation
WalkthroughThe changes introduce a new example file that demonstrates the process of authenticating via a Deepgram client and transcribing an audio URL. A new function is added to perform these steps, utilizing an API key from the environment. The Deepgram client now exposes an Changes
Sequence Diagram(s)sequenceDiagram
participant User as User
participant Script as transcribeUrl Function
participant Client as DeepgramClient
participant Auth as AuthRestClient
participant Listen as Listen Module
User->>Script: Invoke transcribeUrl
Script->>Client: Create client with API key
Client->>Auth: Call grantToken for token retrieval
Auth-->>Client: Return access token
Script->>Client: Set token property with access token
Script->>Listen: Call transcribeUrl (audio URL, options)
Listen-->>Script: Return transcription result
Script->>User: Log token and transcription result
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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 (
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (3)
src/packages/AuthRestClient.ts (2)
14-16: Consider adding request body support for future flexibility.The POST request is currently sending an empty string as the request body. While this might be sufficient for the current API requirements, consider adding support for an optional request body parameter to future-proof the method if the API evolves to require additional parameters.
public async grantToken( - endpoint = ":version/auth/grant" + endpoint = ":version/auth/grant", + body: Record<string, unknown> = {} ): Promise<DeepgramResponse<GrantTokenResponse>> { try { const requestUrl = this.getRequestUrl(endpoint); - const result: GrantTokenResponse = await this.post(requestUrl, "").then((result) => + const result: GrantTokenResponse = await this.post(requestUrl, JSON.stringify(body)).then((result) => result.json() );
10-10: Add type annotation for the endpoint parameter.Adding a type annotation for the endpoint parameter would make the interface more explicit and improve code maintainability.
public async grantToken( - endpoint = ":version/auth/grant" + endpoint: string = ":version/auth/grant" ): Promise<DeepgramResponse<GrantTokenResponse>> {examples/node-auth/index.js (1)
1-1: Use ES modules syntax for consistency with the TypeScript codebase.The static analysis tools flag this CommonJS require statement. Consider using ES modules syntax for consistency with the TypeScript codebase.
- const { createClient } = require("../../dist/main/index"); + import { createClient } from "../../dist/main/index.js";🧰 Tools
🪛 ESLint
[error] 1-1: A
require()style import is forbidden.(@typescript-eslint/no-require-imports)
[error] 1-1: 'require' is not defined.
(no-undef)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
examples/node-auth/index.js(1 hunks)src/DeepgramClient.ts(2 hunks)src/lib/types/GrantTokenResponse.ts(1 hunks)src/lib/types/index.ts(1 hunks)src/packages/AuthRestClient.ts(1 hunks)src/packages/index.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/DeepgramClient.ts (1)
src/packages/AuthRestClient.ts (1)
AuthRestClient(6-27)
examples/node-auth/index.js (1)
src/index.ts (1)
createClient(46-46)
🪛 ESLint
examples/node-auth/index.js
[error] 1-1: A require() style import is forbidden.
(@typescript-eslint/no-require-imports)
[error] 1-1: 'require' is not defined.
(no-undef)
[error] 4-4: 'process' is not defined.
(no-undef)
[error] 9-9: 'console' is not defined.
(no-undef)
[error] 11-11: 'console' is not defined.
(no-undef)
[error] 23-23: 'console' is not defined.
(no-undef)
[error] 24-24: 'console' is not defined.
(no-undef)
🔇 Additional comments (7)
src/packages/index.ts (1)
5-5: LGTM: Export for new AuthRestClientThe new export follows the established pattern and is properly alphabetized among the other exports.
src/lib/types/index.ts (1)
29-29: LGTM: Export for new GrantTokenResponse typeThe new export follows the established pattern and is properly alphabetized among the other type exports.
src/lib/types/GrantTokenResponse.ts (1)
1-4: LGTM: Well-structured token response interfaceThe interface correctly defines the expected properties for a token response following standard conventions.
src/DeepgramClient.ts (1)
5-5: LGTM: Import for new AuthRestClientThe import is correctly added to the existing import statement.
src/packages/AuthRestClient.ts (1)
9-26: LGTM: Well-implemented token authorization method.The
grantTokenmethod provides a clean implementation with proper error handling for Deepgram-specific errors. The response typing is correctly structured using theDeepgramResponsegeneric type.examples/node-auth/index.js (2)
5-9: LGTM: Proper token retrieval implementation.Good error handling pattern for the token retrieval - destructuring the response to access the result and error separately, and throwing the error if present.
🧰 Tools
🪛 ESLint
[error] 9-9: 'console' is not defined.
(no-undef)
12-22: LGTM: Good token usage and transcription configuration.This example properly demonstrates setting the token on the client and configuring the transcription options. The use of a keyterm array is particularly helpful as an example of the API's capabilities.
| get auth(): AuthRestClient { | ||
| return new AuthRestClient(this.options); | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Add JSDoc documentation for consistency
The new auth getter is missing JSDoc documentation that follows the pattern established by other client getters in this class.
+ /**
+ * Returns a new instance of the AuthRestClient, which provides access to the Deepgram API's authentication functionality.
+ *
+ * @returns {AuthRestClient} A new instance of the AuthRestClient.
+ */
get auth(): AuthRestClient {
return new AuthRestClient(this.options);
}Also, consider moving this getter to be grouped with the other non-deprecated client getters for better organization.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| get auth(): AuthRestClient { | |
| return new AuthRestClient(this.options); | |
| } | |
| /** | |
| * Returns a new instance of the AuthRestClient, which provides access to the Deepgram API's authentication functionality. | |
| * | |
| * @returns {AuthRestClient} A new instance of the AuthRestClient. | |
| */ | |
| get auth(): AuthRestClient { | |
| return new AuthRestClient(this.options); | |
| } |
| const transcribeUrl = async () => { | ||
| const deepgram = createClient(process.env.DEEPGRAM_API_KEY); | ||
| const { result: token, error: tokenError } = await deepgram.auth.grantToken(); | ||
| if (tokenError) { | ||
| throw tokenError; | ||
| } | ||
| console.log("Token", token); | ||
|
|
||
| console.log("Transcribing URL", "https://dpgr.am/spacewalk.wav"); | ||
| deepgram.token = token.access_token; | ||
| const { result, error } = await deepgram.listen.prerecorded.transcribeUrl( | ||
| { | ||
| url: "https://dpgr.am/spacewalk.wav", | ||
| }, | ||
| { | ||
| model: "nova-3", | ||
| keyterm: ["spacewalk"], | ||
| } | ||
| ); | ||
|
|
||
| if (error) console.error(error); | ||
| if (!error) console.dir(result, { depth: 5 }); | ||
| }; |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Add API key validation for better error handling.
The example assumes that the DEEPGRAM_API_KEY environment variable is properly set. Consider adding validation to provide a more helpful error message if it's missing.
const transcribeUrl = async () => {
+ if (!process.env.DEEPGRAM_API_KEY) {
+ console.error("DEEPGRAM_API_KEY environment variable is not set");
+ process.exit(1);
+ }
+
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
const { result: token, error: tokenError } = await deepgram.auth.grantToken();
if (tokenError) {
throw tokenError;
}
console.log("Token", token);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const transcribeUrl = async () => { | |
| const deepgram = createClient(process.env.DEEPGRAM_API_KEY); | |
| const { result: token, error: tokenError } = await deepgram.auth.grantToken(); | |
| if (tokenError) { | |
| throw tokenError; | |
| } | |
| console.log("Token", token); | |
| console.log("Transcribing URL", "https://dpgr.am/spacewalk.wav"); | |
| deepgram.token = token.access_token; | |
| const { result, error } = await deepgram.listen.prerecorded.transcribeUrl( | |
| { | |
| url: "https://dpgr.am/spacewalk.wav", | |
| }, | |
| { | |
| model: "nova-3", | |
| keyterm: ["spacewalk"], | |
| } | |
| ); | |
| if (error) console.error(error); | |
| if (!error) console.dir(result, { depth: 5 }); | |
| }; | |
| const transcribeUrl = async () => { | |
| if (!process.env.DEEPGRAM_API_KEY) { | |
| console.error("DEEPGRAM_API_KEY environment variable is not set"); | |
| process.exit(1); | |
| } | |
| const deepgram = createClient(process.env.DEEPGRAM_API_KEY); | |
| const { result: token, error: tokenError } = await deepgram.auth.grantToken(); | |
| if (tokenError) { | |
| throw tokenError; | |
| } | |
| console.log("Token", token); | |
| console.log("Transcribing URL", "https://dpgr.am/spacewalk.wav"); | |
| deepgram.token = token.access_token; | |
| const { result, error } = await deepgram.listen.prerecorded.transcribeUrl( | |
| { | |
| url: "https://dpgr.am/spacewalk.wav", | |
| }, | |
| { | |
| model: "nova-3", | |
| keyterm: ["spacewalk"], | |
| } | |
| ); | |
| if (error) console.error(error); | |
| if (!error) console.dir(result, { depth: 5 }); | |
| }; |
🧰 Tools
🪛 ESLint
[error] 4-4: 'process' is not defined.
(no-undef)
[error] 9-9: 'console' is not defined.
(no-undef)
[error] 11-11: 'console' is not defined.
(no-undef)
[error] 23-23: 'console' is not defined.
(no-undef)
[error] 24-24: 'console' is not defined.
(no-undef)
| if (!error) console.dir(result, { depth: 5 }); | ||
| }; | ||
|
|
||
| transcribeUrl(); |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Add error handling for the main function call.
The example directly calls transcribeUrl() without any error handling. Consider wrapping the call in a try-catch block to properly handle any errors that might be thrown.
- transcribeUrl();
+ transcribeUrl().catch(error => {
+ console.error("Error during transcription:", error);
+ process.exit(1);
+ });📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| transcribeUrl(); | |
| transcribeUrl().catch(error => { | |
| console.error("Error during transcription:", error); | |
| process.exit(1); | |
| }); |
jpvajda
left a comment
There was a problem hiding this comment.
I just had one docs comment suggestion, else this passed my testing.
Summary by CodeRabbit
New Features
Documentation