Skip to content

Commit 46fdb03

Browse files
committed
added browser live transcription option
1 parent b579785 commit 46fdb03

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.3.0",
44
"description": "An SDK for the Deepgram automated speech recognition platform",
55
"main": "dist/index.js",
6+
"browser": "dist/browser/index.js",
67
"types": "dist/index.d.ts",
78
"scripts": {
89
"build": "tsc -p ./",

src/browser/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { DefaultOptions } from "../constants";
2+
3+
import { Transcriber } from "./transcription";
4+
5+
export class Deepgram {
6+
private _apiUrl: string;
7+
private _apiKey: string;
8+
9+
transcription: Transcriber;
10+
11+
constructor(apiKey: string, apiUrl?: string) {
12+
this._apiKey = apiKey;
13+
this._apiUrl = apiUrl || DefaultOptions.apiUrl;
14+
15+
this._validateOptions();
16+
17+
this.transcription = new Transcriber(this._apiKey, this._apiUrl);
18+
}
19+
20+
/**
21+
* Ensures that the provided options were provided
22+
*/
23+
private _validateOptions() {
24+
if (!this._apiKey || this._apiKey.trim().length === 0) {
25+
throw new Error("DG: API key is required");
26+
}
27+
28+
if (!this._apiUrl || this._apiUrl.trim().length === 0) {
29+
throw new Error("DG: API url should be a valid url or not provided");
30+
}
31+
}
32+
}

src/browser/transcription/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
LiveTranscriptionOptions,
3+
} from "../../types";
4+
import querystring from "querystring";
5+
export class Transcriber {
6+
constructor(private _credentials: string, private _apiUrl: string) {}
7+
8+
/**
9+
* Opens a websocket to Deepgram's API for live transcriptions
10+
* @param options Options to modify transcriptions
11+
*/
12+
live(options?: LiveTranscriptionOptions): WebSocket {
13+
return new WebSocket(
14+
`wss://${this._apiUrl}/v1/listen?${querystring.stringify(options)}`,
15+
["token", this._credentials]
16+
);
17+
}
18+
}

0 commit comments

Comments
 (0)