Skip to content

Commit

Permalink
feat: improve experience around usage of custom API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes committed Jan 11, 2024
1 parent 7c41660 commit bef00f6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
37 changes: 31 additions & 6 deletions src/packages/AbstractClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,46 @@ export abstract class AbstractClient {
);
}

if (this.willProxy()) {
let baseUrlString: string = this.options.global.url;
let proxyUrlString: string;

/**
* Check if the base URL provided is missing a protocol and warn in the console.
*/
if (!baseUrlString.startsWith("http") && !baseUrlString.startsWith("ws")) {
console.warn(
`The base URL provided does not begin with http, https, ws, or wss and will default to https as standard.`
);
}

/**
* Applying proxy to base URL.
*/
if (this.options.restProxy?.url) {
/**
* Prevent client using a real API key when using a proxy configuration.
*/
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);
proxyUrlString = this.options.restProxy.url;

if (this.options.global.headers) {
this.options.global.headers["X-Deepgram-Proxy"] = this.options.global.url;
/**
* Check if the proxy URL provided is missing a protocol and warn in the console.
*/
if (!proxyUrlString.startsWith("http") && !proxyUrlString.startsWith("ws")) {
console.warn(
`The proxy URL provided does not begin with http, https, ws, or wss and will default to https as standard.`
);
}
} else {
this.baseUrl = this.resolveBaseUrl(this.options.global.url);

baseUrlString = proxyUrlString;
}

this.baseUrl = this.resolveBaseUrl(baseUrlString);
}

protected resolveBaseUrl(url: string) {
Expand Down
8 changes: 7 additions & 1 deletion src/packages/AbstractRestfulClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class AbstractRestfulClient extends AbstractClient {
constructor(protected key: string, protected options: DeepgramClientOptions) {
super(key, options);

if (isBrowser() && !this.willProxy()) {
if (isBrowser() && !this._willProxy()) {
throw new DeepgramError(
"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."
);
Expand Down Expand Up @@ -130,4 +130,10 @@ export abstract class AbstractRestfulClient extends AbstractClient {
): Promise<any> {
return this._handleRequest(fetcher, "DELETE", url, headers, parameters);
}

private _willProxy() {
const proxyUrl = this.options.restProxy?.url;

return !!proxyUrl;
}
}

0 comments on commit bef00f6

Please sign in to comment.