Skip to content

Commit 154ba30

Browse files
committed
feat: convert all legacy options to namespaced options
1 parent 785aff4 commit 154ba30

2 files changed

Lines changed: 45 additions & 36 deletions

File tree

src/packages/AbstractClient.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,65 @@ export abstract class AbstractClient extends EventEmitter {
6363
const convertLegacyOptions = (optionsArg: DeepgramClientOptions): DeepgramClientOptions => {
6464
const newOptions: DeepgramClientOptions = {};
6565

66+
if (optionsArg._experimentalCustomFetch) {
67+
newOptions.global = {
68+
fetch: {
69+
client: optionsArg._experimentalCustomFetch,
70+
},
71+
};
72+
}
73+
74+
optionsArg = merge(optionsArg, newOptions);
75+
76+
if (optionsArg.restProxy?.url) {
77+
newOptions.global = {
78+
fetch: {
79+
options: {
80+
proxy: {
81+
url: optionsArg.restProxy?.url,
82+
},
83+
},
84+
},
85+
};
86+
}
87+
88+
optionsArg = merge(optionsArg, newOptions);
89+
6690
if (optionsArg.global?.url) {
6791
newOptions.global = {
6892
fetch: {
6993
options: {
7094
url: optionsArg.global.url,
7195
},
7296
},
97+
websocket: {
98+
options: {
99+
url: optionsArg.global.url,
100+
},
101+
},
73102
};
74103
}
75104

105+
optionsArg = merge(optionsArg, newOptions);
106+
76107
if (optionsArg.global?.headers) {
77108
newOptions.global = {
78109
fetch: {
79110
options: {
80111
headers: optionsArg.global?.headers,
81112
},
82113
},
114+
websocket: {
115+
options: {
116+
_nodeOnlyHeaders: optionsArg.global?.headers,
117+
},
118+
},
83119
};
84120
}
85121

86-
return merge(optionsArg, newOptions);
122+
optionsArg = merge(optionsArg, newOptions);
123+
124+
return optionsArg;
87125
};
88126

89127
options = convertLegacyOptions(options);

test/client.test.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,41 +76,11 @@ describe("testing creation of a deepgram client object", () => {
7676
expect(key).to.equal(mockKey);
7777
});
7878

79-
it("it should strip trailing slashes off the API URL if they're supplied", () => {
80-
const mockUrl = faker.internet.url({ appendSlash: false });
81-
const mockKey = faker.string.alphanumeric(40);
82-
const client = createClient({
83-
key: mockKey,
84-
global: { fetch: { options: { url: `${mockUrl}/` } } },
85-
});
86-
87-
// @ts-ignore
88-
const url = client.namespaceOptions.fetch.options.url;
89-
90-
// @ts-ignore
91-
const key = client.options.key;
92-
93-
expect(client).is.instanceOf(DeepgramClient);
94-
expect(url).to.equal(mockUrl);
95-
expect(key).to.equal(mockKey);
96-
});
97-
98-
it("it should still work when provided a URL without a protocol", () => {
99-
const domain = `api.mock.deepgram.com`;
100-
const client = createClient(faker.string.alphanumeric(40), {
101-
global: { url: domain },
102-
});
103-
104-
// @ts-ignore
105-
const url = client.baseUrl.hostname;
106-
107-
expect(client).is.instanceOf(DeepgramClient);
108-
expect(url).to.equal("api.mock.deepgram.com");
109-
});
110-
11179
it("it should allow for the supply of a custom header", () => {
11280
const client = createClient(faker.string.alphanumeric(40), {
113-
global: { headers: { "X-dg-test": "testing" } },
81+
global: {
82+
fetch: { options: { headers: { "X-dg-test": "testing" } } },
83+
},
11484
});
11585

11686
expect(client).is.instanceOf(DeepgramClient);
@@ -122,8 +92,9 @@ describe("testing creation of a deepgram client object", () => {
12292
};
12393

12494
const client = createClient(faker.string.alphanumeric(40), {
125-
global: { url: "https://api.mock.deepgram.com" },
126-
_experimentalCustomFetch: fetch,
95+
global: {
96+
fetch: { client: fetch },
97+
},
12798
});
12899

129100
const { result, error } = await client.manage.getProjectBalances(faker.string.uuid());

0 commit comments

Comments
 (0)