Skip to content

Commit a7a4752

Browse files
committed
feat: tie together namespace config with fetch and websocket options
1 parent 6e78fce commit a7a4752

11 files changed

Lines changed: 179 additions & 236 deletions

File tree

examples/node-prerecorded/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const transcribeUrl = async () => {
1515
);
1616

1717
if (error) console.error(error);
18-
if (!error) console.dir(result, { depth: null });
18+
if (!error) console.dir(result, { depth: 1 });
1919
};
2020

2121
const transcribeFile = async () => {
@@ -29,7 +29,7 @@ const transcribeFile = async () => {
2929
});
3030

3131
if (error) console.error(error);
32-
if (!error) console.dir(result, { depth: null });
32+
if (!error) console.dir(result, { depth: 1 });
3333
};
3434

3535
transcribeUrl();

examples/node-speak/index.js

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,22 @@
11
const { createClient } = require("../../dist/main/index");
22
const fs = require("fs");
3+
const { pipeline } = require("stream");
4+
const { promisify } = require("util");
5+
const streamPipeline = promisify(pipeline);
36

47
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
58

69
const text = "Hello, how can I help you today?";
710

811
const getAudio = async () => {
9-
const response = await deepgram.speak.request({ text }, { model: "aura-asteria-en" });
10-
const stream = await response.getStream();
11-
const headers = await response.getHeaders();
12-
if (stream) {
13-
const buffer = await getAudioBuffer(stream);
12+
const { result } = await deepgram.speak.request({ text }, { model: "aura-asteria-en" });
1413

15-
fs.writeFile("audio.wav", buffer, (err) => {
16-
if (err) {
17-
console.error("Error writing audio to file:", err);
18-
} else {
19-
console.log("Audio file written to audio.wav");
20-
}
21-
});
22-
} else {
23-
console.error("Error generating audio:", stream);
14+
if (!result.ok) {
15+
throw new Error(`HTTP error! Status: ${result}`);
2416
}
2517

26-
if (headers) {
27-
console.log("Headers:", headers);
28-
}
29-
};
30-
31-
// helper function to convert stream to audio buffer
32-
const getAudioBuffer = async (response) => {
33-
const reader = response.getReader();
34-
const chunks = [];
35-
36-
while (true) {
37-
const { done, value } = await reader.read();
38-
if (done) break;
39-
40-
chunks.push(value);
41-
}
42-
43-
const dataArray = chunks.reduce(
44-
(acc, chunk) => Uint8Array.from([...acc, ...chunk]),
45-
new Uint8Array(0)
46-
);
47-
48-
return Buffer.from(dataArray.buffer);
18+
const fileStream = fs.createWriteStream("audio.wav");
19+
await streamPipeline(result.body, fileStream);
4920
};
5021

5122
getAudio();

src/DeepgramClient.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { DeepgramVersionError } from "./lib/errors";
2-
import { AbstractClient } from "./packages/AbstractClient";
3-
import { ListenClient } from "./packages/ListenClient";
4-
import { ManageClient } from "./packages/ManageClient";
5-
import { OnPremClient } from "./packages/OnPremClient";
6-
import { ReadClient } from "./packages/ReadClient";
7-
import { SpeakClient } from "./packages/SpeakClient";
2+
import {
3+
AbstractClient,
4+
ListenClient,
5+
ManageClient,
6+
ReadClient,
7+
SelfHostedClient as OnPremClient,
8+
SelfHostedClient,
9+
SpeakClient,
10+
} from "./packages";
811

912
/**
10-
* The DeepgramClient class provides access to various Deepgram API clients, including ListenClient, ManageClient, OnPremClient, ReadClient, and SpeakClient.
13+
* The DeepgramClient class provides access to various Deepgram API clients, including ListenClient, ManageClient, SelfHostedClient, ReadClient, and SpeakClient.
1114
*
1215
* @see https://github.com/deepgram/deepgram-js-sdk
1316
*/
@@ -20,8 +23,16 @@ export default class DeepgramClient extends AbstractClient {
2023
return new ManageClient(this.options);
2124
}
2225

26+
/**
27+
* backwards compatibility for renaming onprem to selfhosted
28+
* @deprecated
29+
*/
2330
get onprem(): OnPremClient {
24-
return new OnPremClient(this.options);
31+
return this.selfhosted;
32+
}
33+
34+
get selfhosted(): SelfHostedClient {
35+
return new SelfHostedClient(this.options);
2536
}
2637

2738
get read(): ReadClient {

src/lib/types/Fetch.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
export type Fetch = typeof fetch;
22

3-
export interface FetchOptions {
4-
method?: RequestMethodType;
5-
headers?: Record<string, string>;
6-
cache?: "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached"; // default, no-cache, reload, force-cache, only-if-cached
7-
credentials?: "include" | "same-origin" | "omit"; // include, same-origin, omit
8-
redirect?: "manual" | "follow" | "error"; // manual, follow, error
9-
referrerPolicy?: // no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
10-
| "no-referrer"
11-
| "no-referrer-when-downgrade"
12-
| "origin"
13-
| "origin-when-cross-origin"
14-
| "same-origin"
15-
| "strict-origin"
16-
| "strict-origin-when-cross-origin"
17-
| "unsafe-url";
18-
}
3+
export type FetchOptions = RequestInit & { method?: RequestMethodType; duplex?: string };
194

20-
export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; // GET, POST, PUT, DELETE, etc.
21-
22-
export interface FetchParameters {
23-
/**
24-
* Pass in an AbortController's signal to cancel the request.
25-
*/
26-
signal?: AbortSignal;
27-
}
5+
export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";

src/packages/AbstractRestClient.ts

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { DeepgramApiError, DeepgramError, DeepgramUnknownError } from "../lib/errors";
22
import { Readable } from "stream";
33
import { fetchWithAuth, resolveResponse } from "../lib/fetch";
4-
import type { Fetch, FetchParameters, RequestMethodType } from "../lib/types/Fetch";
4+
import type { Fetch, FetchOptions, RequestMethodType } from "../lib/types/Fetch";
55
import { AbstractClient } from "./AbstractClient";
66
import { DeepgramClientOptions } from "../lib/types";
77
import { isBrowser } from "../lib/helpers";
8+
import merge from "deepmerge";
89

910
export abstract class AbstractRestClient extends AbstractClient {
1011
protected fetch: Fetch;
@@ -49,113 +50,85 @@ export abstract class AbstractRestClient extends AbstractClient {
4950
}
5051
}
5152

52-
protected _getRequestParams(
53+
protected _getRequestOptions(
5354
method: RequestMethodType,
54-
headers?: Record<string, string>,
55-
parameters?: FetchParameters,
56-
body?: string | Buffer | Readable
57-
) {
58-
const params: { [k: string]: any } = {
59-
...this.options?.fetch,
60-
method,
61-
headers: { ...this.options?.fetch?.headers, ...headers } || {},
62-
};
63-
64-
if (method === "GET") {
65-
return params;
66-
}
55+
bodyOrOptions?: string | Buffer | Readable | FetchOptions,
56+
options?: FetchOptions
57+
): FetchOptions {
58+
let reqOptions: FetchOptions = { method };
6759

68-
params.body = body;
69-
params.duplex = "half";
60+
if (method === "GET" || method === "DELETE") {
61+
reqOptions = { ...reqOptions, ...(bodyOrOptions as FetchOptions) };
62+
} else {
63+
reqOptions = {
64+
duplex: "half",
65+
body: bodyOrOptions as BodyInit,
66+
...reqOptions,
67+
...options,
68+
};
69+
}
7070

71-
return { ...params, ...parameters };
71+
return merge(this.namespaceOptions.fetch.options, reqOptions);
7272
}
7373

7474
protected async _handleRequest(
75-
fetcher: Fetch,
75+
method: "GET" | "DELETE",
76+
url: URL,
77+
options?: FetchOptions
78+
): Promise<Response>;
79+
protected async _handleRequest(
80+
method: "POST" | "PUT" | "PATCH",
81+
url: URL,
82+
body: string | Buffer | Readable,
83+
options?: FetchOptions
84+
): Promise<Response>;
85+
protected async _handleRequest(
7686
method: RequestMethodType,
77-
url: string | URL,
78-
headers?: Record<string, string>,
79-
parameters?: FetchParameters,
80-
body?: string | Buffer | Readable
81-
): Promise<any> {
87+
url: URL,
88+
bodyOrOptions?: string | Buffer | Readable | FetchOptions,
89+
options?: FetchOptions
90+
): Promise<Response> {
8291
return new Promise((resolve, reject) => {
83-
fetcher(url, this._getRequestParams(method, headers, parameters, body))
84-
.then((result) => {
85-
if (!result.ok) throw result;
86-
87-
return result.json();
88-
})
89-
.then((data) => resolve(data))
90-
.catch((error) => this._handleError(error, reject));
91-
});
92-
}
92+
const fetcher = this.fetch;
9393

94-
protected async _handleRawRequest(
95-
fetcher: Fetch,
96-
method: RequestMethodType,
97-
url: string | URL,
98-
headers?: Record<string, string>,
99-
parameters?: FetchParameters,
100-
body?: string | Buffer | Readable
101-
): Promise<any> {
102-
return new Promise((resolve, reject) => {
103-
fetcher(url, this._getRequestParams(method, headers, parameters, body))
94+
fetcher(url, this._getRequestOptions(method, bodyOrOptions, options))
10495
.then((result) => {
10596
if (!result.ok) throw result;
106-
107-
return result;
97+
resolve(result);
10898
})
109-
.then((data) => resolve(data))
11099
.catch((error) => this._handleError(error, reject));
111100
});
112101
}
113102

114-
protected async get(
115-
fetcher: Fetch,
116-
url: string | URL,
117-
headers?: Record<string, string>,
118-
parameters?: FetchParameters
119-
): Promise<any> {
120-
return this._handleRequest(fetcher, "GET", url, headers, parameters);
103+
protected async get(url: URL, options?: FetchOptions): Promise<any> {
104+
return this._handleRequest("GET", url, options);
121105
}
122106

123107
protected async post(
124-
fetcher: Fetch,
125-
url: string | URL,
108+
url: URL,
126109
body: string | Buffer | Readable,
127-
headers?: Record<string, string>,
128-
parameters?: FetchParameters
110+
options?: FetchOptions
129111
): Promise<any> {
130-
return this._handleRequest(fetcher, "POST", url, headers, parameters, body);
112+
return this._handleRequest("POST", url, body, options);
131113
}
132114

133115
protected async put(
134-
fetcher: Fetch,
135-
url: string | URL,
116+
url: URL,
136117
body: string | Buffer | Readable,
137-
headers?: Record<string, string>,
138-
parameters?: FetchParameters
118+
options?: FetchOptions
139119
): Promise<any> {
140-
return this._handleRequest(fetcher, "PUT", url, headers, parameters, body);
120+
return this._handleRequest("PUT", url, body, options);
141121
}
142122

143123
protected async patch(
144-
fetcher: Fetch,
145-
url: string | URL,
124+
url: URL,
146125
body: string | Buffer | Readable,
147-
headers?: Record<string, string>,
148-
parameters?: FetchParameters
126+
options?: FetchOptions
149127
): Promise<any> {
150-
return this._handleRequest(fetcher, "PATCH", url, headers, parameters, body);
128+
return this._handleRequest("PATCH", url, body, options);
151129
}
152130

153-
protected async delete(
154-
fetcher: Fetch,
155-
url: string | URL,
156-
headers?: Record<string, string>,
157-
parameters?: FetchParameters
158-
): Promise<any> {
159-
return this._handleRequest(fetcher, "DELETE", url, headers, parameters);
131+
protected async delete(url: URL, options?: FetchOptions): Promise<any> {
132+
return this._handleRequest("DELETE", url, options);
160133
}
161134
}

0 commit comments

Comments
 (0)