From 957dbb1b659a8e324199c0059bf5b78885df33ad Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Sun, 19 May 2024 16:20:57 +0100 Subject: [PATCH] feat: tie together namespace config with fetch and websocket options --- examples/node-prerecorded/index.js | 4 +- examples/node-speak/index.js | 45 ++----- src/DeepgramClient.ts | 27 ++-- src/lib/types/Fetch.ts | 26 +--- src/packages/AbstractRestClient.ts | 125 +++++++----------- src/packages/ManageClient.ts | 86 +++++++----- src/packages/PrerecordedClient.ts | 42 ++---- src/packages/ReadClient.ts | 18 ++- .../{OnPremClient.ts => SelfHostedClient.ts} | 20 +-- src/packages/SpeakClient.ts | 18 ++- src/packages/index.ts | 4 +- 11 files changed, 179 insertions(+), 236 deletions(-) rename src/packages/{OnPremClient.ts => SelfHostedClient.ts} (83%) diff --git a/examples/node-prerecorded/index.js b/examples/node-prerecorded/index.js index 102fa943..7f955050 100644 --- a/examples/node-prerecorded/index.js +++ b/examples/node-prerecorded/index.js @@ -15,7 +15,7 @@ const transcribeUrl = async () => { ); if (error) console.error(error); - if (!error) console.dir(result, { depth: null }); + if (!error) console.dir(result, { depth: 1 }); }; const transcribeFile = async () => { @@ -29,7 +29,7 @@ const transcribeFile = async () => { }); if (error) console.error(error); - if (!error) console.dir(result, { depth: null }); + if (!error) console.dir(result, { depth: 1 }); }; transcribeUrl(); diff --git a/examples/node-speak/index.js b/examples/node-speak/index.js index b3617969..215414e6 100644 --- a/examples/node-speak/index.js +++ b/examples/node-speak/index.js @@ -1,51 +1,22 @@ const { createClient } = require("../../dist/main/index"); const fs = require("fs"); +const { pipeline } = require("stream"); +const { promisify } = require("util"); +const streamPipeline = promisify(pipeline); const deepgram = createClient(process.env.DEEPGRAM_API_KEY); const text = "Hello, how can I help you today?"; const getAudio = async () => { - const response = await deepgram.speak.request({ text }, { model: "aura-asteria-en" }); - const stream = await response.getStream(); - const headers = await response.getHeaders(); - if (stream) { - const buffer = await getAudioBuffer(stream); + const { result } = await deepgram.speak.request({ text }, { model: "aura-asteria-en" }); - fs.writeFile("audio.wav", buffer, (err) => { - if (err) { - console.error("Error writing audio to file:", err); - } else { - console.log("Audio file written to audio.wav"); - } - }); - } else { - console.error("Error generating audio:", stream); + if (!result.ok) { + throw new Error(`HTTP error! Status: ${result}`); } - if (headers) { - console.log("Headers:", headers); - } -}; - -// helper function to convert stream to audio buffer -const getAudioBuffer = async (response) => { - const reader = response.getReader(); - const chunks = []; - - while (true) { - const { done, value } = await reader.read(); - if (done) break; - - chunks.push(value); - } - - const dataArray = chunks.reduce( - (acc, chunk) => Uint8Array.from([...acc, ...chunk]), - new Uint8Array(0) - ); - - return Buffer.from(dataArray.buffer); + const fileStream = fs.createWriteStream("audio.wav"); + await streamPipeline(result.body, fileStream); }; getAudio(); diff --git a/src/DeepgramClient.ts b/src/DeepgramClient.ts index 1aa09312..8fd4bcb7 100644 --- a/src/DeepgramClient.ts +++ b/src/DeepgramClient.ts @@ -1,13 +1,16 @@ import { DeepgramVersionError } from "./lib/errors"; -import { AbstractClient } from "./packages/AbstractClient"; -import { ListenClient } from "./packages/ListenClient"; -import { ManageClient } from "./packages/ManageClient"; -import { OnPremClient } from "./packages/OnPremClient"; -import { ReadClient } from "./packages/ReadClient"; -import { SpeakClient } from "./packages/SpeakClient"; +import { + AbstractClient, + ListenClient, + ManageClient, + ReadClient, + SelfHostedClient as OnPremClient, + SelfHostedClient, + SpeakClient, +} from "./packages"; /** - * The DeepgramClient class provides access to various Deepgram API clients, including ListenClient, ManageClient, OnPremClient, ReadClient, and SpeakClient. + * The DeepgramClient class provides access to various Deepgram API clients, including ListenClient, ManageClient, SelfHostedClient, ReadClient, and SpeakClient. * * @see https://github.com/deepgram/deepgram-js-sdk */ @@ -20,8 +23,16 @@ export default class DeepgramClient extends AbstractClient { return new ManageClient(this.options); } + /** + * backwards compatibility for renaming onprem to selfhosted + * @deprecated + */ get onprem(): OnPremClient { - return new OnPremClient(this.options); + return this.selfhosted; + } + + get selfhosted(): SelfHostedClient { + return new SelfHostedClient(this.options); } get read(): ReadClient { diff --git a/src/lib/types/Fetch.ts b/src/lib/types/Fetch.ts index 725f2e17..0496c325 100644 --- a/src/lib/types/Fetch.ts +++ b/src/lib/types/Fetch.ts @@ -1,27 +1,5 @@ export type Fetch = typeof fetch; -export interface FetchOptions { - method?: RequestMethodType; - headers?: Record; - cache?: "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached"; // default, no-cache, reload, force-cache, only-if-cached - credentials?: "include" | "same-origin" | "omit"; // include, same-origin, omit - redirect?: "manual" | "follow" | "error"; // manual, follow, error - referrerPolicy?: // no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url - | "no-referrer" - | "no-referrer-when-downgrade" - | "origin" - | "origin-when-cross-origin" - | "same-origin" - | "strict-origin" - | "strict-origin-when-cross-origin" - | "unsafe-url"; -} +export type FetchOptions = RequestInit & { method?: RequestMethodType; duplex?: string }; -export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; // GET, POST, PUT, DELETE, etc. - -export interface FetchParameters { - /** - * Pass in an AbortController's signal to cancel the request. - */ - signal?: AbortSignal; -} +export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; diff --git a/src/packages/AbstractRestClient.ts b/src/packages/AbstractRestClient.ts index c499c069..d60477bc 100644 --- a/src/packages/AbstractRestClient.ts +++ b/src/packages/AbstractRestClient.ts @@ -1,10 +1,11 @@ import { DeepgramApiError, DeepgramError, DeepgramUnknownError } from "../lib/errors"; import { Readable } from "stream"; import { fetchWithAuth, resolveResponse } from "../lib/fetch"; -import type { Fetch, FetchParameters, RequestMethodType } from "../lib/types/Fetch"; +import type { Fetch, FetchOptions, RequestMethodType } from "../lib/types/Fetch"; import { AbstractClient } from "./AbstractClient"; import { DeepgramClientOptions } from "../lib/types"; import { isBrowser } from "../lib/helpers"; +import merge from "deepmerge"; export abstract class AbstractRestClient extends AbstractClient { protected fetch: Fetch; @@ -49,113 +50,85 @@ export abstract class AbstractRestClient extends AbstractClient { } } - protected _getRequestParams( + protected _getRequestOptions( method: RequestMethodType, - headers?: Record, - parameters?: FetchParameters, - body?: string | Buffer | Readable - ) { - const params: { [k: string]: any } = { - ...this.options?.fetch, - method, - headers: { ...this.options?.fetch?.headers, ...headers } || {}, - }; - - if (method === "GET") { - return params; - } + bodyOrOptions?: string | Buffer | Readable | FetchOptions, + options?: FetchOptions + ): FetchOptions { + let reqOptions: FetchOptions = { method }; - params.body = body; - params.duplex = "half"; + if (method === "GET" || method === "DELETE") { + reqOptions = { ...reqOptions, ...(bodyOrOptions as FetchOptions) }; + } else { + reqOptions = { + duplex: "half", + body: bodyOrOptions as BodyInit, + ...reqOptions, + ...options, + }; + } - return { ...params, ...parameters }; + return merge(this.namespaceOptions.fetch.options, reqOptions); } protected async _handleRequest( - fetcher: Fetch, + method: "GET" | "DELETE", + url: URL, + options?: FetchOptions + ): Promise; + protected async _handleRequest( + method: "POST" | "PUT" | "PATCH", + url: URL, + body: string | Buffer | Readable, + options?: FetchOptions + ): Promise; + protected async _handleRequest( method: RequestMethodType, - url: string | URL, - headers?: Record, - parameters?: FetchParameters, - body?: string | Buffer | Readable - ): Promise { + url: URL, + bodyOrOptions?: string | Buffer | Readable | FetchOptions, + options?: FetchOptions + ): Promise { return new Promise((resolve, reject) => { - fetcher(url, this._getRequestParams(method, headers, parameters, body)) - .then((result) => { - if (!result.ok) throw result; - - return result.json(); - }) - .then((data) => resolve(data)) - .catch((error) => this._handleError(error, reject)); - }); - } + const fetcher = this.fetch; - protected async _handleRawRequest( - fetcher: Fetch, - method: RequestMethodType, - url: string | URL, - headers?: Record, - parameters?: FetchParameters, - body?: string | Buffer | Readable - ): Promise { - return new Promise((resolve, reject) => { - fetcher(url, this._getRequestParams(method, headers, parameters, body)) + fetcher(url, this._getRequestOptions(method, bodyOrOptions, options)) .then((result) => { if (!result.ok) throw result; - - return result; + resolve(result); }) - .then((data) => resolve(data)) .catch((error) => this._handleError(error, reject)); }); } - protected async get( - fetcher: Fetch, - url: string | URL, - headers?: Record, - parameters?: FetchParameters - ): Promise { - return this._handleRequest(fetcher, "GET", url, headers, parameters); + protected async get(url: URL, options?: FetchOptions): Promise { + return this._handleRequest("GET", url, options); } protected async post( - fetcher: Fetch, - url: string | URL, + url: URL, body: string | Buffer | Readable, - headers?: Record, - parameters?: FetchParameters + options?: FetchOptions ): Promise { - return this._handleRequest(fetcher, "POST", url, headers, parameters, body); + return this._handleRequest("POST", url, body, options); } protected async put( - fetcher: Fetch, - url: string | URL, + url: URL, body: string | Buffer | Readable, - headers?: Record, - parameters?: FetchParameters + options?: FetchOptions ): Promise { - return this._handleRequest(fetcher, "PUT", url, headers, parameters, body); + return this._handleRequest("PUT", url, body, options); } protected async patch( - fetcher: Fetch, - url: string | URL, + url: URL, body: string | Buffer | Readable, - headers?: Record, - parameters?: FetchParameters + options?: FetchOptions ): Promise { - return this._handleRequest(fetcher, "PATCH", url, headers, parameters, body); + return this._handleRequest("PATCH", url, body, options); } - protected async delete( - fetcher: Fetch, - url: string | URL, - headers?: Record, - parameters?: FetchParameters - ): Promise { - return this._handleRequest(fetcher, "DELETE", url, headers, parameters); + protected async delete(url: URL, options?: FetchOptions): Promise { + return this._handleRequest("DELETE", url, options); } } diff --git a/src/packages/ManageClient.ts b/src/packages/ManageClient.ts index a97b5600..b6470a1d 100644 --- a/src/packages/ManageClient.ts +++ b/src/packages/ManageClient.ts @@ -41,7 +41,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint); - const result: GetTokenDetailsResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetTokenDetailsResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -61,7 +63,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint); - const result: GetProjectsResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectsResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -82,7 +86,7 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: GetProjectResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectResponse = await this.get(requestUrl).then((result) => result.json()); return { result, error: null }; } catch (error) { @@ -106,7 +110,9 @@ export class ManageClient extends AbstractRestClient { const requestUrl = this.getRequestUrl(endpoint, { projectId }, options); const body = JSON.stringify(options); - const result: MessageResponse = await this.patch(this.fetch as Fetch, requestUrl, body); + const result: MessageResponse = await this.patch(requestUrl, body).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -127,7 +133,7 @@ export class ManageClient extends AbstractRestClient { ): Promise { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - await this.delete(this.fetch as Fetch, requestUrl); + await this.delete(requestUrl); return { error: null }; } catch (error) { @@ -148,7 +154,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: GetProjectKeysResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectKeysResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -170,7 +178,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, keyId }); - const result: GetProjectKeyResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectKeyResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -194,10 +204,8 @@ export class ManageClient extends AbstractRestClient { const requestUrl = this.getRequestUrl(endpoint, { projectId }, options); const body = JSON.stringify(options); - const result: CreateProjectKeyResponse = await this.post( - this.fetch as Fetch, - requestUrl, - body + const result: CreateProjectKeyResponse = await this.post(requestUrl, body).then((result) => + result.json() ); return { result, error: null }; @@ -220,7 +228,7 @@ export class ManageClient extends AbstractRestClient { ): Promise { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, keyId }); - await this.delete(this.fetch as Fetch, requestUrl); + await this.delete(requestUrl); return { error: null }; } catch (error) { @@ -241,7 +249,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: GetProjectMembersResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectMembersResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -263,7 +273,7 @@ export class ManageClient extends AbstractRestClient { ): Promise { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, memberId }); - await this.delete(this.fetch as Fetch, requestUrl); + await this.delete(requestUrl); return { error: null }; } catch (error) { @@ -285,9 +295,8 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, memberId }); - const result: GetProjectMemberScopesResponse = await this.get( - this.fetch as Fetch, - requestUrl + const result: GetProjectMemberScopesResponse = await this.get(requestUrl).then((result) => + result.json() ); return { result, error: null }; @@ -313,7 +322,9 @@ export class ManageClient extends AbstractRestClient { const requestUrl = this.getRequestUrl(endpoint, { projectId, memberId }, options); const body = JSON.stringify(options); - const result: MessageResponse = await this.put(this.fetch as Fetch, requestUrl, body); + const result: MessageResponse = await this.put(requestUrl, body).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -334,7 +345,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: GetProjectInvitesResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectInvitesResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -358,7 +371,9 @@ export class ManageClient extends AbstractRestClient { const requestUrl = this.getRequestUrl(endpoint, { projectId }, options); const body = JSON.stringify(options); - const result: MessageResponse = await this.post(this.fetch as Fetch, requestUrl, body); + const result: MessageResponse = await this.post(requestUrl, body).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -380,7 +395,7 @@ export class ManageClient extends AbstractRestClient { ): Promise { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, email }); - await this.delete(this.fetch as Fetch, requestUrl); + await this.delete(requestUrl).then((result) => result.json()); return { error: null }; } catch (error) { @@ -401,7 +416,7 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: MessageResponse = await this.delete(this.fetch as Fetch, requestUrl); + const result: MessageResponse = await this.delete(requestUrl).then((result) => result.json()); return { result, error: null }; } catch (error) { @@ -423,9 +438,8 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }, options); - const result: GetProjectUsageRequestsResponse = await this.get( - this.fetch as Fetch, - requestUrl + const result: GetProjectUsageRequestsResponse = await this.get(requestUrl).then((result) => + result.json() ); return { result, error: null }; @@ -448,9 +462,8 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, requestId }); - const result: GetProjectUsageRequestResponse = await this.get( - this.fetch as Fetch, - requestUrl + const result: GetProjectUsageRequestResponse = await this.get(requestUrl).then((result) => + result.json() ); return { result, error: null }; @@ -473,9 +486,8 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }, options); - const result: GetProjectUsageSummaryResponse = await this.get( - this.fetch as Fetch, - requestUrl + const result: GetProjectUsageSummaryResponse = await this.get(requestUrl).then((result) => + result.json() ); return { result, error: null }; @@ -498,7 +510,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }, options); - const result: GetProjectUsageFieldsResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectUsageFieldsResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -519,7 +533,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: GetProjectBalancesResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectBalancesResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -541,7 +557,9 @@ export class ManageClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, balanceId }); - const result: GetProjectBalanceResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: GetProjectBalanceResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { diff --git a/src/packages/PrerecordedClient.ts b/src/packages/PrerecordedClient.ts index 8a12d017..80618be9 100644 --- a/src/packages/PrerecordedClient.ts +++ b/src/packages/PrerecordedClient.ts @@ -35,10 +35,8 @@ export class PrerecordedClient extends AbstractRestClient { } const requestUrl = this.getRequestUrl(endpoint, {}, { ...{}, ...options }); - const result: SyncPrerecordedResponse = await this.post( - this.fetch as Fetch, - requestUrl, - body + const result: SyncPrerecordedResponse = await this.post(requestUrl, body).then((result) => + result.json() ); return { result, error: null }; @@ -72,14 +70,9 @@ export class PrerecordedClient extends AbstractRestClient { } const requestUrl = this.getRequestUrl(endpoint, {}, { ...{}, ...options }); - const result: SyncPrerecordedResponse = await this.post( - this.fetch as Fetch, - requestUrl, - body, - { - "Content-Type": "deepgram/audio+video", - } - ); + const result: SyncPrerecordedResponse = await this.post(requestUrl, body, { + headers: { "Content-Type": "audio/*" }, + }).then((result) => result.json()); return { result, error: null }; } catch (error) { @@ -111,10 +104,8 @@ export class PrerecordedClient extends AbstractRestClient { {}, { ...options, callback: callback.toString() } ); - const result: AsyncPrerecordedResponse = await this.post( - this.fetch as Fetch, - requestUrl, - body + const result: AsyncPrerecordedResponse = await this.post(requestUrl, body).then((result) => + result.json() ); return { result, error: null }; @@ -142,27 +133,14 @@ export class PrerecordedClient extends AbstractRestClient { throw new DeepgramError("Unknown transcription source type"); } - // const transcriptionOptions: PrerecordedSchema = { - // ...options, - // ...{ callback: callback.toString() }, - // }; - - // const url = new URL(endpoint, this.baseUrl); - // appendSearchParams(url.searchParams, transcriptionOptions); - const requestUrl = this.getRequestUrl( endpoint, {}, { ...options, callback: callback.toString() } ); - const result: AsyncPrerecordedResponse = await this.post( - this.fetch as Fetch, - requestUrl, - body, - { - "Content-Type": "deepgram/audio+video", - } - ); + const result: AsyncPrerecordedResponse = await this.post(requestUrl, body, { + headers: { "Content-Type": "deepgram/audio+video" }, + }).then((result) => result.json()); return { result, error: null }; } catch (error) { diff --git a/src/packages/ReadClient.ts b/src/packages/ReadClient.ts index 6f444a41..8952cbe9 100644 --- a/src/packages/ReadClient.ts +++ b/src/packages/ReadClient.ts @@ -36,7 +36,9 @@ export class ReadClient extends AbstractRestClient { } const requestUrl = this.getRequestUrl(endpoint, {}, { ...{}, ...options }); - const result: SyncAnalyzeResponse = await this.post(this.fetch as Fetch, requestUrl, body); + const result: SyncAnalyzeResponse = await this.post(requestUrl, body).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -69,7 +71,9 @@ export class ReadClient extends AbstractRestClient { } const requestUrl = this.getRequestUrl(endpoint, {}, { ...{}, ...options }); - const result: SyncAnalyzeResponse = await this.post(this.fetch as Fetch, requestUrl, body); + const result: SyncAnalyzeResponse = await this.post(requestUrl, body).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -101,7 +105,9 @@ export class ReadClient extends AbstractRestClient { {}, { ...options, callback: callback.toString() } ); - const result: AsyncAnalyzeResponse = await this.post(this.fetch as Fetch, requestUrl, body); + const result: AsyncAnalyzeResponse = await this.post(requestUrl, body).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -133,9 +139,9 @@ export class ReadClient extends AbstractRestClient { {}, { ...options, callback: callback.toString() } ); - const result: AsyncAnalyzeResponse = await this.post(this.fetch as Fetch, requestUrl, body, { - "Content-Type": "deepgram/audio+video", - }); + const result: AsyncAnalyzeResponse = await this.post(requestUrl, body, { + headers: { "Content-Type": "deepgram/audio+video" }, + }).then((result) => result.json()); return { result, error: null }; } catch (error) { diff --git a/src/packages/OnPremClient.ts b/src/packages/SelfHostedClient.ts similarity index 83% rename from src/packages/OnPremClient.ts rename to src/packages/SelfHostedClient.ts index 8a231c51..1ce35e0b 100644 --- a/src/packages/OnPremClient.ts +++ b/src/packages/SelfHostedClient.ts @@ -9,8 +9,8 @@ import type { } from "../lib/types"; import { AbstractRestClient } from "./AbstractRestClient"; -export class OnPremClient extends AbstractRestClient { - public namespace: string = "onprem"; +export class SelfHostedClient extends AbstractRestClient { + public namespace: string = "selfhosted"; /** * @see https://developers.deepgram.com/reference/list-credentials @@ -21,7 +21,9 @@ export class OnPremClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId }); - const result: ListOnPremCredentialsResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: ListOnPremCredentialsResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -43,7 +45,9 @@ export class OnPremClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, credentialsId }); - const result: OnPremCredentialResponse = await this.get(this.fetch as Fetch, requestUrl); + const result: OnPremCredentialResponse = await this.get(requestUrl).then((result) => + result.json() + ); return { result, error: null }; } catch (error) { @@ -67,10 +71,8 @@ export class OnPremClient extends AbstractRestClient { const requestUrl = this.getRequestUrl(endpoint, { projectId }); const body = JSON.stringify(options); - const result: OnPremCredentialResponse = await this.post( - this.fetch as Fetch, - requestUrl, - body + const result: OnPremCredentialResponse = await this.post(requestUrl, body).then((result) => + result.json() ); return { result, error: null }; @@ -93,7 +95,7 @@ export class OnPremClient extends AbstractRestClient { ): Promise> { try { const requestUrl = this.getRequestUrl(endpoint, { projectId, credentialsId }); - const result: MessageResponse = await this.delete(this.fetch as Fetch, requestUrl); + const result: MessageResponse = await this.delete(requestUrl).then((result) => result.json()); return { result, error: null }; } catch (error) { diff --git a/src/packages/SpeakClient.ts b/src/packages/SpeakClient.ts index 5f1899a4..dfc090a1 100644 --- a/src/packages/SpeakClient.ts +++ b/src/packages/SpeakClient.ts @@ -1,6 +1,6 @@ -import { DeepgramError, DeepgramUnknownError, isDeepgramError } from "../lib/errors"; -import { appendSearchParams, isTextSource } from "../lib/helpers"; -import { Fetch, SpeakSchema, TextSource } from "../lib/types"; +import { DeepgramError, DeepgramUnknownError } from "../lib/errors"; +import { isTextSource } from "../lib/helpers"; +import { SpeakSchema, TextSource } from "../lib/types"; import { AbstractRestClient } from "./AbstractRestClient"; export class SpeakClient extends AbstractRestClient { @@ -24,11 +24,15 @@ export class SpeakClient extends AbstractRestClient { throw new DeepgramError("Unknown transcription source type"); } - const speakOptions: SpeakSchema = { ...{ model: "aura-asteria-en" }, ...options }; + const requestUrl = this.getRequestUrl( + endpoint, + {}, + { ...{ model: "aura-asteria-en" }, ...options } + ); + this.result = await this.post(requestUrl, body, { + headers: { Accept: "audio/*", "Content-Type": "application/json" }, + }); - const url = new URL(endpoint, this.baseUrl); - appendSearchParams(url.searchParams, speakOptions); - this.result = await this._handleRawRequest(this.fetch as Fetch, "POST", url, {}, {}, body); return this; } catch (error) { throw error; diff --git a/src/packages/index.ts b/src/packages/index.ts index 0a351ba9..f902b88e 100644 --- a/src/packages/index.ts +++ b/src/packages/index.ts @@ -4,5 +4,7 @@ export { AbstractLiveClient } from "./AbstractLiveClient"; export { ListenClient } from "./ListenClient"; export { LiveClient } from "./LiveClient"; export { ManageClient } from "./ManageClient"; -export { OnPremClient } from "./OnPremClient"; +export { SelfHostedClient } from "./SelfHostedClient"; export { PrerecordedClient } from "./PrerecordedClient"; +export { ReadClient } from "./ReadClient"; +export { SpeakClient } from "./SpeakClient";