Skip to content

Commit 7a76f75

Browse files
lukeocodesBartoszJarockidependabot[bot]DamienDeepgramlox
authored
feat: Release/3.x.x 20240427 (#271)
* feat: add _experimentalCustomFetch config to override fetch (#245) * feat: allow to provide custom fetch method to AbstractRestfulClient * fix: allow to provide both fetch and fetch options * refactor: init custom fetch in resolveFetch * rename `customFetch` to `_experimentalCustomFetch` i want to avoid taking any new namespaces while i think about the best way to do this * use `_experimentalCustomFetch` instead of `customFetch` for fetch override * test: added custom fetch test --------- Co-authored-by: Luke Oliff <luke@lukeoliff.com> * chore: Bump es5-ext from 0.10.62 to 0.10.64 (#250) Bumps [es5-ext](https://github.com/medikoo/es5-ext) from 0.10.62 to 0.10.64. - [Release notes](https://github.com/medikoo/es5-ext/releases) - [Changelog](https://github.com/medikoo/es5-ext/blob/main/CHANGELOG.md) - [Commits](medikoo/es5-ext@v0.10.62...v0.10.64) --- updated-dependencies: - dependency-name: es5-ext dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: add get auth token details method to manage client (#262) * chore: edit example for is_final and endpointing together with utterance end (#264) * fix: add missing expiration_date to CreateProjectKeyResponse (#265) * fix: update user agent to include JS or node (#269) --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Bartosz Jarocki <bartosz.jarocki@hey.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: DamienDeepgram <138720050+DamienDeepgram@users.noreply.github.com> Co-authored-by: Lachlan Donald <lachlan@ljd.cc>
1 parent d2ade7b commit 7a76f75

11 files changed

Lines changed: 134 additions & 22 deletions

examples/node-live/index.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
const {
2-
createClient,
3-
LiveTranscriptionEvents,
4-
LiveTranscriptionEvent,
5-
} = require("../../dist/main/index");
1+
const { createClient, LiveTranscriptionEvents } = require("../../dist/main/index");
62
const fetch = require("cross-fetch");
73

84
const live = async () => {
95
const url = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service";
106

117
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
128

9+
// We will collect the is_final=true messages here so we can use them when the person finishes speaking
10+
let is_finals = [];
11+
1312
const connection = deepgram.listen.live({
1413
model: "nova-2",
15-
utterance_end_ms: 1500,
14+
language: "en-US",
15+
// Apply smart formatting to the output
16+
smart_format: true,
17+
// To get UtteranceEnd, the following must be set:
1618
interim_results: true,
19+
utterance_end_ms: 1000,
20+
vad_events: true,
21+
// Time in milliseconds of silence to wait for before finalizing speech
22+
endpointing: 300,
1723
});
1824

1925
connection.on(LiveTranscriptionEvents.Open, () => {
@@ -22,19 +28,45 @@ const live = async () => {
2228
});
2329

2430
connection.on(LiveTranscriptionEvents.Metadata, (data) => {
25-
console.log(data);
31+
console.log(`Deepgram Metadata: ${data}`);
2632
});
2733

2834
connection.on(LiveTranscriptionEvents.Transcript, (data) => {
29-
console.log(data.channel);
35+
const sentence = data.channel.alternatives[0].transcript;
36+
37+
// Ignore empty transcripts
38+
if (sentence.length == 0) {
39+
return;
40+
}
41+
if (data.is_final) {
42+
// We need to collect these and concatenate them together when we get a speech_final=true
43+
// See docs: https://developers.deepgram.com/docs/understand-endpointing-interim-results
44+
is_finals.push(sentence);
45+
46+
// Speech final means we have detected sufficent silence to consider this end of speech
47+
// Speech final is the lowest latency result as it triggers as soon an the endpointing value has triggered
48+
if (data.speech_final) {
49+
const utterance = is_finals.join(" ");
50+
console.log(`Speech Final: ${utterance}`);
51+
is_finals = [];
52+
} else {
53+
// These are useful if you need real time captioning and update what the Interim Results produced
54+
console.log(`Is Final: ${sentence}`);
55+
}
56+
} else {
57+
// These are useful if you need real time captioning of what is being spoken
58+
console.log(`Interim Results: ${sentence}`);
59+
}
3060
});
3161

3262
connection.on(LiveTranscriptionEvents.UtteranceEnd, (data) => {
33-
console.log(data);
63+
const utterance = is_finals.join(" ");
64+
console.log(`Deepgram UtteranceEnd: ${utterance}`);
65+
is_finals = [];
3466
});
3567

3668
connection.on(LiveTranscriptionEvents.SpeechStarted, (data) => {
37-
console.log(data);
69+
// console.log("Deepgram SpeechStarted");
3870
});
3971

4072
connection.on(LiveTranscriptionEvents.Error, (err) => {

package-lock.json

Lines changed: 32 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/constants.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { isBrowser } from "./helpers";
2+
import { DeepgramClientOptions } from "./types/DeepgramClientOptions";
23
import { FetchOptions } from "./types/Fetch";
34
import { version } from "./version";
45

6+
export const NODE_VERSION = process.versions.node;
7+
58
export const DEFAULT_HEADERS = {
69
"Content-Type": `application/json`,
710
"X-Client-Info": `@deepgram/sdk; ${isBrowser() ? "browser" : "server"}; v${version}`,
8-
"User-Agent": `@deepgram/sdk/${version}`,
11+
"User-Agent": `@deepgram/sdk/${version} ${isBrowser() ? "javascript" : `node/${NODE_VERSION}`}`,
912
};
1013

1114
export const DEFAULT_URL = "https://api.deepgram.com";
@@ -18,7 +21,7 @@ export const DEFAULT_FETCH_OPTIONS: FetchOptions = {
1821
headers: DEFAULT_HEADERS,
1922
};
2023

21-
export const DEFAULT_OPTIONS = {
24+
export const DEFAULT_OPTIONS: DeepgramClientOptions = {
2225
global: DEFAULT_GLOBAL_OPTIONS,
2326
fetch: DEFAULT_FETCH_OPTIONS,
2427
};

src/lib/fetch.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import crossFetch from "cross-fetch";
22
import { resolveHeadersConstructor } from "./helpers";
33
import type { Fetch } from "./types/Fetch";
44

5-
export const resolveFetch = (): Fetch => {
5+
export const resolveFetch = (customFetch?: Fetch): Fetch => {
66
let _fetch: Fetch;
7-
if (typeof fetch === "undefined") {
7+
if (customFetch) {
8+
_fetch = customFetch;
9+
} else if (typeof fetch === "undefined") {
810
_fetch = crossFetch as unknown as Fetch;
911
} else {
1012
_fetch = fetch;
1113
}
1214
return (...args) => _fetch(...args);
1315
};
1416

15-
export const fetchWithAuth = (apiKey: string): Fetch => {
16-
const fetch = resolveFetch();
17+
export const fetchWithAuth = (apiKey: string, customFetch?: Fetch): Fetch => {
18+
const fetch = resolveFetch(customFetch);
1719
const HeadersConstructor = resolveHeadersConstructor();
1820

1921
return async (input, init) => {

src/lib/types/CreateProjectKeyResponse.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export interface CreateProjectKeyResponse {
55
scopes: string[];
66
tags?: string[];
77
created: string;
8+
expiration_date?: string;
89
}

src/lib/types/DeepgramClientOptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FetchOptions } from "./Fetch";
1+
import { Fetch, FetchOptions } from "./Fetch";
22

33
export interface DeepgramClientOptions {
44
global?: {
@@ -13,6 +13,7 @@ export interface DeepgramClientOptions {
1313
url?: string;
1414
};
1515
fetch?: FetchOptions;
16+
_experimentalCustomFetch?: Fetch;
1617
restProxy?: {
1718
url: null | string;
1819
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface GetTokenDetailsResponse {
2+
[key: string]: unknown;
3+
}

src/lib/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type {
2626
export type { GetProjectUsageRequestsSchema } from "./GetProjectUsageRequestsSchema";
2727
export type { GetProjectUsageSummarySchema } from "./GetProjectUsageSummarySchema";
2828
export type { GetProjectUsageSummaryResponse } from "./GetProjectUsageSummaryResponse";
29+
export type { GetTokenDetailsResponse } from "./GetTokenDetailsResponse";
2930
export type {
3031
ListOnPremCredentialsResponse,
3132
OnPremCredentialResponse,

src/packages/AbstractRestfulClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export abstract class AbstractRestfulClient extends AbstractClient {
1818
);
1919
}
2020

21-
this.fetch = fetchWithAuth(this.key);
21+
this.fetch = fetchWithAuth(this.key, options._experimentalCustomFetch);
2222
}
2323

2424
protected _getErrorMessage(err: any): string {

src/packages/ManageClient.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,32 @@ import type {
2727
UpdateProjectMemberScopeSchema,
2828
UpdateProjectSchema,
2929
VoidResponse,
30+
GetTokenDetailsResponse,
3031
} from "../lib/types";
3132

3233
export class ManageClient extends AbstractRestfulClient {
34+
/**
35+
* @see https://developers.deepgram.com/docs/authenticating#test-request
36+
*/
37+
async getTokenDetails(
38+
endpoint = "v1/auth/token"
39+
): Promise<DeepgramResponse<GetTokenDetailsResponse>> {
40+
try {
41+
const url = new URL(this.baseUrl);
42+
url.pathname = endpoint;
43+
44+
const result: GetTokenDetailsResponse = await this.get(this.fetch as Fetch, url);
45+
46+
return { result, error: null };
47+
} catch (error) {
48+
if (isDeepgramError(error)) {
49+
return { result: null, error };
50+
}
51+
52+
throw error;
53+
}
54+
}
55+
3356
/**
3457
* @see https://developers.deepgram.com/reference/get-projects
3558
*/

0 commit comments

Comments
 (0)