Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve experience around usage of custom API endpoints #230

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}