Skip to content

Commit

Permalink
feat: plug rest clients into new options
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes committed Apr 30, 2024
1 parent 21046a7 commit 2ae14d0
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 231 deletions.
1 change: 0 additions & 1 deletion src/lib/types/CreateProjectKeySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ interface CommonOptions extends Record<string, unknown> {
comment: string;
scopes: string[];
tags?: string[];
[key: string]: unknown;
}
2 changes: 0 additions & 2 deletions src/lib/types/TranscriptionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ interface TranscriptionSchema extends Record<string, unknown> {
* @see https://developers.deepgram.com/docs/extra-metadata
*/
extra?: string[] | string;

[key: string]: unknown;
}

interface PrerecordedSchema extends TranscriptionSchema {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types/UpdateProjectMemberScopeSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface UpdateProjectMemberScopeSchema {
export interface UpdateProjectMemberScopeSchema extends Record<string, unknown> {
scope: string;
}
1 change: 0 additions & 1 deletion src/lib/types/UpdateProjectSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface UpdateProjectSchema extends Record<string, unknown> {
name?: string;
company?: string;
[key: string]: unknown;
}
36 changes: 28 additions & 8 deletions src/packages/AbstractClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,43 @@ export abstract class AbstractClient extends EventEmitter {
}

/**
* Generates a URL for the specified endpoint and transcription options.
* Generates a URL for an API endpoint with optional query parameters and transcription options.
*
* @param endpoint - The endpoint URL, which may contain a "{version}" placeholder that will be replaced with the client's version.
* @param transcriptionOptions - The transcription options to include as query parameters in the URL.
* @returns A URL object representing the generated URL.
* @param endpoint - The API endpoint URL, which may contain placeholders for fields.
* @param fields - An optional object containing key-value pairs to replace placeholders in the endpoint URL.
* @param transcriptionOptions - Optional transcription options to include as query parameters in the URL.
* @returns A URL object representing the constructed API request URL.
*/
public getRequestUrl(
endpoint: string,
transcriptionOptions: LiveSchema | TranscriptionSchema
fields: { [key: string]: string } = { version: this.version },
transcriptionOptions?: {
[key: string]: unknown;
}
): URL {
/**
* Version the URL endpoints if they can be versioned.
* If we pass in fields without a version, set a version.
*/
endpoint = endpoint.replace("{version}", this.version);
fields.version = this.version;

/**
* Version and template the endpoint for input argument..
*/
endpoint = endpoint.replace(/:(\w+)/g, function (_, key) {
return fields![key];
});

/**
* Create a URL object.
*/
const url = new URL(endpoint as string, this.baseUrl);
appendSearchParams(url.searchParams, transcriptionOptions);

/**
* If there are transcription options, append them to the request as URL querystring parameters
*/
if (transcriptionOptions) {
appendSearchParams(url.searchParams, transcriptionOptions);
}

return url;
}
Expand Down
2 changes: 1 addition & 1 deletion src/packages/ListenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ListenClient extends AbstractClient {
return new PrerecordedClient(this.options);
}

public live(transcriptionOptions: LiveSchema = {}, endpoint = "{version}/listen") {
public live(transcriptionOptions: LiveSchema = {}, endpoint = ":version/listen") {
return new LiveClient(this.options, transcriptionOptions, endpoint);
}
}
4 changes: 2 additions & 2 deletions src/packages/LiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class LiveClient extends AbstractLiveClient {
constructor(
options: DeepgramClientOptions,
transcriptionOptions: LiveSchema = {},
endpoint: string = "{version}/listen"
endpoint: string = ":version/listen"
) {
super(options);

const requestUrl = this.getRequestUrl(endpoint, transcriptionOptions);
const requestUrl = this.getRequestUrl(endpoint, {}, transcriptionOptions);
this._socket = new w3cwebsocket(requestUrl.toString(), ["token", this.key]);

this._socket.onopen = () => {
Expand Down
Loading

0 comments on commit 2ae14d0

Please sign in to comment.