Skip to content

Commit 1c4fbc4

Browse files
authored
Merge pull request #88 from deepgram/require-ssl
Adding SSL options
2 parents 3beafe1 + 09872ea commit 1c4fbc4

27 files changed

+162
-55
lines changed

src/billing.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export class Billing {
44
constructor(
55
private _credentials: string,
66
private _apiUrl: string,
7+
private _requireSSL: boolean,
78
private _request: RequestFunction
89
) {}
910

@@ -18,6 +19,7 @@ export class Billing {
1819
"GET",
1920
this._credentials,
2021
this._apiUrl,
22+
this._requireSSL,
2123
`${this.apiPath}/${projectId}/balances`
2224
);
2325
}
@@ -33,6 +35,7 @@ export class Billing {
3335
"GET",
3436
this._credentials,
3537
this._apiUrl,
38+
this._requireSSL,
3639
`${this.apiPath}/${projectId}/balances/${balanceId}`
3740
);
3841
}

src/browser/httpFetch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ export async function _request<T>(
44
method: string,
55
api_key: string,
66
apiUrl: string,
7+
requireSSL: boolean,
78
path: string,
89
payload?: string
910
): Promise<T> {
10-
const url = `https://${apiUrl}${path}`;
11+
const protocol = requireSSL ? "https" : "http";
12+
const url = `${protocol}://${apiUrl}${path}`;
1113
try {
1214
const response = await fetch(url, {
1315
method: method,

src/browser/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { _request } from "./httpFetch";
1414
export class Deepgram {
1515
private _apiUrl: string;
1616
private _apiKey: string;
17+
private _requireSSL: boolean;
1718

1819
transcription: Transcriber;
1920
projects: Projects;
@@ -24,22 +25,23 @@ export class Deepgram {
2425
billing: Billing;
2526
scopes: Scopes;
2627

27-
constructor(apiKey: string, apiUrl?: string) {
28+
constructor(apiKey: string, apiUrl?: string, requireSSL?: boolean) {
2829
this._apiKey = apiKey;
2930
this._apiUrl = apiUrl || DefaultOptions.apiUrl;
31+
this._requireSSL = requireSSL || DefaultOptions.requireSSL;
3032

3133
/**
3234
* Ensures that the provided options were provided
3335
*/
3436
validateOptions(this._apiKey, this._apiUrl);
3537

36-
this.transcription = new Transcriber(this._apiKey, this._apiUrl);
37-
this.projects = new Projects(this._apiKey, this._apiUrl, _request);
38-
this.keys = new Keys(this._apiKey, this._apiUrl, _request);
39-
this.usage = new Usage(this._apiKey, this._apiUrl, _request);
40-
this.members = new Members(this._apiKey, this._apiUrl, _request);
41-
this.invitation = new Invitation(this._apiKey, this._apiUrl, _request);
42-
this.billing = new Billing(this._apiKey, this._apiUrl, _request);
43-
this.scopes = new Scopes(this._apiKey, this._apiUrl, _request);
38+
this.transcription = new Transcriber(this._apiKey, this._apiUrl, this._requireSSL);
39+
this.projects = new Projects(this._apiKey, this._apiUrl, this._requireSSL, _request);
40+
this.keys = new Keys(this._apiKey, this._apiUrl, this._requireSSL, _request);
41+
this.usage = new Usage(this._apiKey, this._apiUrl, this._requireSSL, _request);
42+
this.members = new Members(this._apiKey, this._apiUrl, this._requireSSL, _request);
43+
this.invitation = new Invitation(this._apiKey, this._apiUrl, this._requireSSL, _request);
44+
this.billing = new Billing(this._apiKey, this._apiUrl, this._requireSSL, _request);
45+
this.scopes = new Scopes(this._apiKey, this._apiUrl, this._requireSSL, _request);
4446
}
4547
}

src/browser/transcription/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import querystring from "querystring";
88
import { preRecordedTranscription } from "./preRecordedTranscription";
99

1010
export class Transcriber {
11-
constructor(private _credentials: string, private _apiUrl: string) {}
11+
constructor(private _credentials: string, private _apiUrl: string, private _requireSSL: boolean) { }
1212

1313
/**
1414
* Transcribes prerecorded audio from a file or buffer
@@ -22,6 +22,7 @@ export class Transcriber {
2222
return await preRecordedTranscription(
2323
this._credentials,
2424
this._apiUrl || "",
25+
this._requireSSL || true,
2526
source,
2627
options
2728
);
@@ -32,8 +33,10 @@ export class Transcriber {
3233
* @param options Options to modify transcriptions
3334
*/
3435
live(options?: LiveTranscriptionOptions): WebSocket {
36+
const protocol = this._requireSSL ? "wss" : "ws";
37+
3538
return new WebSocket(
36-
`wss://${this._apiUrl}/v1/listen?${querystring.stringify(options)}`,
39+
`${protocol}://${this._apiUrl}/v1/listen?${querystring.stringify(options)}`,
3740
["token", this._credentials]
3841
);
3942
}

src/browser/transcription/preRecordedTranscription.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ function isUrlSource(providedSource: UrlSource): providedSource is UrlSource {
1515
* Transcribes audio from a url
1616
* @param credentials Base64 encoded API key & secret
1717
* @param apiUrl url string of Deepgram's API
18+
* @param requireSSL Whether the request should use HTTPS or HTTP
1819
* @param source Url or Buffer of file to transcribe
1920
* @param options Options to modify transcriptions
2021
*/
2122
export const preRecordedTranscription = async (
2223
apiKey: string,
2324
apiUrl: string,
25+
requireSSL: boolean,
2426
source: UrlSource,
2527
options?: PrerecordedTranscriptionOptions
2628
): Promise<PrerecordedTranscriptionResponse> => {
@@ -35,6 +37,7 @@ export const preRecordedTranscription = async (
3537
"POST",
3638
apiKey,
3739
apiUrl,
40+
requireSSL,
3841
`/v1/listen?${querystring.stringify(transcriptionOptions)}`,
3942
body
4043
);

src/constants/defaultOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
*/
44
export const DefaultOptions = {
55
apiUrl: "api.deepgram.com",
6+
requireSSL: true,
67
};

src/httpRequest.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Readable } from "stream";
2-
import { request, RequestOptions } from "https";
2+
import https, { RequestOptions } from "https";
3+
import http from "http";
34
import { userAgent } from "./userAgent";
45

56
const _requestOptions = (
6-
api_key: string,
7+
apiKey: string,
78
apiUrl: string,
9+
requireSSL: boolean,
810
path: string,
911
method: string,
1012
payload?: string | Buffer | Readable,
@@ -18,12 +20,13 @@ const _requestOptions = (
1820

1921
const options = {
2022
host: apiUrl,
23+
protocol: requireSSL ? "https" : "http",
2124
path,
2225
method,
2326
headers: {
2427
"User-Agent": userAgent(),
2528
"Content-Type": "application/json",
26-
Authorization: `token ${api_key}`,
29+
Authorization: `token ${apiKey}`,
2730
...additionalHeaders,
2831
},
2932
};
@@ -37,24 +40,28 @@ const _requestOptions = (
3740

3841
export function _request<T>(
3942
method: string,
40-
api_key: string,
41-
apiUrl: string,
43+
apiKey: string,
44+
apiURL: string,
45+
requireSSL: boolean,
4246
path: string,
4347
payload?: string | Buffer | Readable,
4448
// eslint-disable-next-line @typescript-eslint/ban-types
4549
options?: Object
4650
): Promise<T> {
4751
const requestOptions = _requestOptions(
48-
api_key,
49-
apiUrl,
52+
apiKey,
53+
apiURL,
54+
requireSSL,
5055
path,
5156
method,
5257
payload,
5358
options
5459
);
5560
return new Promise((resolve, reject) => {
5661
try {
57-
const httpRequest = request(requestOptions, (dgRes) => {
62+
const httpClient = requireSSL ? https : http;
63+
64+
const httpRequest = httpClient.request(requestOptions, (dgRes) => {
5865
let dgResContent = "";
5966

6067
dgRes.on("data", (chunk) => {

src/index.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import { Scopes } from "./scopes";
1010

1111
import { validateOptions } from "./helpers";
1212
import { _request } from "./httpRequest";
13+
1314
export class Deepgram {
1415
private _apiUrl: string;
1516
private _apiKey: string;
17+
private _requireSSL: boolean;
1618

1719
keys: Keys;
1820
projects: Projects;
@@ -23,22 +25,62 @@ export class Deepgram {
2325
billing: Billing;
2426
scopes: Scopes;
2527

26-
constructor(apiKey: string, apiUrl?: string) {
28+
constructor(apiKey: string, apiUrl?: string, requireSSL?: boolean) {
2729
this._apiKey = apiKey;
2830
this._apiUrl = apiUrl || DefaultOptions.apiUrl;
31+
this._requireSSL = requireSSL || DefaultOptions.requireSSL;
2932

3033
/**
3134
* Ensures that the provided options were provided
3235
*/
3336
validateOptions(this._apiKey, this._apiUrl);
3437

35-
this.keys = new Keys(this._apiKey, this._apiUrl, _request);
36-
this.projects = new Projects(this._apiKey, this._apiUrl, _request);
37-
this.transcription = new Transcriber(this._apiKey, this._apiUrl);
38-
this.usage = new Usage(this._apiKey, this._apiUrl, _request);
39-
this.members = new Members(this._apiKey, this._apiUrl, _request);
40-
this.invitation = new Invitation(this._apiKey, this._apiUrl, _request);
41-
this.billing = new Billing(this._apiKey, this._apiUrl, _request);
42-
this.scopes = new Scopes(this._apiKey, this._apiUrl, _request);
38+
this.keys = new Keys(
39+
this._apiKey,
40+
this._apiUrl,
41+
this._requireSSL,
42+
_request
43+
);
44+
this.projects = new Projects(
45+
this._apiKey,
46+
this._apiUrl,
47+
this._requireSSL,
48+
_request
49+
);
50+
this.transcription = new Transcriber(
51+
this._apiKey,
52+
this._apiUrl,
53+
this._requireSSL
54+
);
55+
this.usage = new Usage(
56+
this._apiKey,
57+
this._apiUrl,
58+
this._requireSSL,
59+
_request
60+
);
61+
this.members = new Members(
62+
this._apiKey,
63+
this._apiUrl,
64+
this._requireSSL,
65+
_request
66+
);
67+
this.invitation = new Invitation(
68+
this._apiKey,
69+
this._apiUrl,
70+
this._requireSSL,
71+
_request
72+
);
73+
this.billing = new Billing(
74+
this._apiKey,
75+
this._apiUrl,
76+
this._requireSSL,
77+
_request
78+
);
79+
this.scopes = new Scopes(
80+
this._apiKey,
81+
this._apiUrl,
82+
this._requireSSL,
83+
_request
84+
);
4385
}
4486
}

src/invitation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class Invitation {
99
constructor(
1010
private _credentials: string,
1111
private _apiUrl: string,
12+
private _requireSSL: boolean,
1213
private _request: RequestFunction
1314
) {}
1415

@@ -23,6 +24,7 @@ export class Invitation {
2324
"GET",
2425
this._credentials,
2526
this._apiUrl,
27+
this._requireSSL,
2628
`${this.apiPath}/${projectId}/invites`
2729
);
2830
}
@@ -36,6 +38,7 @@ export class Invitation {
3638
"POST",
3739
this._credentials,
3840
this._apiUrl,
41+
this._requireSSL,
3942
`${this.apiPath}/${projectId}/invites`,
4043
JSON.stringify({
4144
email: options.email,
@@ -53,6 +56,7 @@ export class Invitation {
5356
"DELETE",
5457
this._credentials,
5558
this._apiUrl,
59+
this._requireSSL,
5660
`${this.apiPath}/${projectId}/leave`
5761
);
5862
}
@@ -68,6 +72,7 @@ export class Invitation {
6872
"DELETE",
6973
this._credentials,
7074
this._apiUrl,
75+
this._requireSSL,
7176
`${this.apiPath}/${projectId}/invites/${email}`
7277
);
7378
}

src/keys.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class Keys {
1010
constructor(
1111
private _credentials: string,
1212
private _apiUrl: string,
13+
private _requireSSL: boolean,
1314
private _request: RequestFunction
1415
) {}
1516

@@ -24,6 +25,7 @@ export class Keys {
2425
"GET",
2526
this._credentials,
2627
this._apiUrl,
28+
this._requireSSL,
2729
`${this.apiPath}/${projectId}/keys`
2830
);
2931

@@ -47,6 +49,7 @@ export class Keys {
4749
"GET",
4850
this._credentials,
4951
this._apiUrl,
52+
this._requireSSL,
5053
`${this.apiPath}/${projectId}/keys/${keyId}`
5154
);
5255
}
@@ -79,6 +82,7 @@ export class Keys {
7982
"POST",
8083
this._credentials,
8184
this._apiUrl,
85+
this._requireSSL,
8286
`${this.apiPath}/${projectId}/keys`,
8387
JSON.stringify({
8488
comment,
@@ -103,6 +107,7 @@ export class Keys {
103107
"DELETE",
104108
this._credentials,
105109
this._apiUrl,
110+
this._requireSSL,
106111
`${this.apiPath}/${projectId}/keys/${keyId}`
107112
);
108113
}

0 commit comments

Comments
 (0)