generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathAbstractClient.ts
64 lines (51 loc) · 1.85 KB
/
AbstractClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
import { DeepgramError } from "../lib/errors";
import { applySettingDefaults, stripTrailingSlash } from "../lib/helpers";
import { DeepgramClientOptions } from "../lib/types";
/**
* Deepgram Client.
*
* An isomorphic Javascript client for interacting with the Deepgram API.
* @see https://developers.deepgram.com
*/
export abstract class AbstractClient {
protected baseUrl: URL;
constructor(protected key: string, protected options: DeepgramClientOptions) {
this.key = key;
if (!key) {
this.key = process.env.DEEPGRAM_API_KEY as string;
}
if (!this.key) {
throw new DeepgramError("A deepgram API key is required");
}
this.options = applySettingDefaults(options, DEFAULT_OPTIONS);
if (!this.options.global?.url) {
throw new DeepgramError(
`An API URL is required. It should be set to ${DEFAULT_URL} by default. No idea what happened!`
);
}
if (this.willProxy()) {
if (this.key !== "proxy") {
throw new DeepgramError(
`Do not attempt to pass any other API key than the string "proxy" when making proxied REST requests. Please ensure your proxy application is responsible for writing our API key to the Authorization header.`
);
}
this.baseUrl = this.resolveBaseUrl(this.options.restProxy?.url as string);
if (this.options.global.headers) {
this.options.global.headers["X-Deepgram-Proxy"] = this.options.global.url;
}
} else {
this.baseUrl = this.resolveBaseUrl(this.options.global.url);
}
}
protected resolveBaseUrl(url: string) {
if (!/^https?:\/\//i.test(url)) {
url = "https://" + url;
}
return new URL(stripTrailingSlash(url));
}
protected willProxy() {
const proxyUrl = this.options.restProxy?.url;
return !!proxyUrl;
}
}