Skip to content

Commit b779348

Browse files
authored
feat: improve experience around usage of custom API endpoints (#230)
1 parent e5fac7a commit b779348

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/packages/AbstractClient.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,46 @@ export abstract class AbstractClient {
3131
);
3232
}
3333

34-
if (this.willProxy()) {
34+
let baseUrlString: string = this.options.global.url;
35+
let proxyUrlString: string;
36+
37+
/**
38+
* Check if the base URL provided is missing a protocol and warn in the console.
39+
*/
40+
if (!baseUrlString.startsWith("http") && !baseUrlString.startsWith("ws")) {
41+
console.warn(
42+
`The base URL provided does not begin with http, https, ws, or wss and will default to https as standard.`
43+
);
44+
}
45+
46+
/**
47+
* Applying proxy to base URL.
48+
*/
49+
if (this.options.restProxy?.url) {
50+
/**
51+
* Prevent client using a real API key when using a proxy configuration.
52+
*/
3553
if (this.key !== "proxy") {
3654
throw new DeepgramError(
3755
`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.`
3856
);
3957
}
4058

41-
this.baseUrl = this.resolveBaseUrl(this.options.restProxy?.url as string);
59+
proxyUrlString = this.options.restProxy.url;
4260

43-
if (this.options.global.headers) {
44-
this.options.global.headers["X-Deepgram-Proxy"] = this.options.global.url;
61+
/**
62+
* Check if the proxy URL provided is missing a protocol and warn in the console.
63+
*/
64+
if (!proxyUrlString.startsWith("http") && !proxyUrlString.startsWith("ws")) {
65+
console.warn(
66+
`The proxy URL provided does not begin with http, https, ws, or wss and will default to https as standard.`
67+
);
4568
}
46-
} else {
47-
this.baseUrl = this.resolveBaseUrl(this.options.global.url);
69+
70+
baseUrlString = proxyUrlString;
4871
}
72+
73+
this.baseUrl = this.resolveBaseUrl(baseUrlString);
4974
}
5075

5176
protected resolveBaseUrl(url: string) {

src/packages/AbstractRestfulClient.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export abstract class AbstractRestfulClient extends AbstractClient {
1212
constructor(protected key: string, protected options: DeepgramClientOptions) {
1313
super(key, options);
1414

15-
if (isBrowser() && !this.willProxy()) {
15+
if (isBrowser() && !this._willProxy()) {
1616
throw new DeepgramError(
1717
"Due to CORS we are unable to support REST-based API calls to our API from the browser. Please consider using a proxy, and including a `restProxy: { url: ''}` in your Deepgram client options."
1818
);
@@ -130,4 +130,10 @@ export abstract class AbstractRestfulClient extends AbstractClient {
130130
): Promise<any> {
131131
return this._handleRequest(fetcher, "DELETE", url, headers, parameters);
132132
}
133+
134+
private _willProxy() {
135+
const proxyUrl = this.options.restProxy?.url;
136+
137+
return !!proxyUrl;
138+
}
133139
}

0 commit comments

Comments
 (0)