Skip to content

Commit e5fac7a

Browse files
authored
feat: throw errors when using v2 callstack on the v3 SDK (#226)
1 parent 7c41660 commit e5fac7a

4 files changed

Lines changed: 178 additions & 1 deletion

File tree

src/DeepgramClient.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DeepgramVersionError } from "./lib/errors";
12
import { AbstractClient } from "./packages/AbstractClient";
23
import { ListenClient } from "./packages/ListenClient";
34
import { ManageClient } from "./packages/ManageClient";
@@ -21,4 +22,40 @@ export default class DeepgramClient extends AbstractClient {
2122
get onprem(): OnPremClient {
2223
return new OnPremClient(this.key, this.options);
2324
}
25+
26+
/**
27+
* Major version fallback errors are below
28+
*/
29+
30+
get transcription(): any {
31+
throw new DeepgramVersionError();
32+
}
33+
34+
get projects(): any {
35+
throw new DeepgramVersionError();
36+
}
37+
38+
get keys(): any {
39+
throw new DeepgramVersionError();
40+
}
41+
42+
get members(): any {
43+
throw new DeepgramVersionError();
44+
}
45+
46+
get scopes(): any {
47+
throw new DeepgramVersionError();
48+
}
49+
50+
get invitation(): any {
51+
throw new DeepgramVersionError();
52+
}
53+
54+
get usage(): any {
55+
throw new DeepgramVersionError();
56+
}
57+
58+
get billing(): any {
59+
throw new DeepgramVersionError();
60+
}
2461
}

src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import DeepgramClient from "./DeepgramClient";
2+
import { DeepgramVersionError } from "./lib/errors";
23
import type { DeepgramClientOptions } from "./lib/types";
34

5+
/**
6+
* Major version fallback error
7+
*/
8+
class Deepgram {
9+
constructor(protected apiKey: string, protected apiUrl?: string, protected requireSSL?: boolean) {
10+
throw new DeepgramVersionError();
11+
}
12+
}
13+
414
/**
515
* Creates a new Deepgram Client.
616
*/
717
const createClient = (apiKey: string, options: DeepgramClientOptions = {}): DeepgramClient => {
818
return new DeepgramClient(apiKey, options);
919
};
1020

11-
export { createClient, DeepgramClient };
21+
export { createClient, DeepgramClient, Deepgram };
1222

1323
/**
1424
* Helpful exports.

src/lib/errors.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ export class DeepgramUnknownError extends DeepgramError {
3838
this.originalError = originalError;
3939
}
4040
}
41+
42+
export class DeepgramVersionError extends DeepgramError {
43+
constructor() {
44+
super(
45+
`You are attempting to use an old format for a newer SDK version. Read more here: https://dpgr.am/js-v3`
46+
);
47+
48+
this.name = "DeepgramVersionError";
49+
}
50+
}

test/legacy.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { assert, expect } from "chai";
2+
import { createClient, Deepgram, DeepgramVersionError } from "../src";
3+
import { faker } from "@faker-js/faker";
4+
import DeepgramClient from "../src/DeepgramClient";
5+
6+
const errorText =
7+
"You are attempting to use an old format for a newer SDK version. Read more here: https://dpgr.am/js-v3";
8+
9+
describe("legacy error handling", () => {
10+
let deepgram: DeepgramClient;
11+
12+
beforeEach(() => {
13+
deepgram = createClient(faker.string.alphanumeric(40), {
14+
global: { url: "https://api.mock.deepgram.com" },
15+
});
16+
});
17+
18+
it("should create the correct client object", () => {
19+
expect(deepgram).to.not.be.undefined;
20+
expect(deepgram).is.instanceOf(DeepgramClient);
21+
});
22+
23+
it("should error when using a v2 client object", async () => {
24+
assert.throw(
25+
() => {
26+
new Deepgram(faker.string.alphanumeric(40));
27+
},
28+
DeepgramVersionError,
29+
errorText
30+
);
31+
});
32+
33+
it("should error when using an old v2 callstack for transcription", async () => {
34+
assert.throw(
35+
() => {
36+
deepgram.transcription.preRecorded(
37+
{
38+
url: "https://dpgr.am/spacewalk.wav",
39+
},
40+
{
41+
model: "nova",
42+
callback: "http://callback/endpoint",
43+
}
44+
);
45+
},
46+
DeepgramVersionError,
47+
errorText
48+
);
49+
});
50+
51+
it("should error when using an old v2 callstack for projects", async () => {
52+
assert.throw(
53+
() => {
54+
deepgram.projects.list();
55+
},
56+
DeepgramVersionError,
57+
errorText
58+
);
59+
});
60+
61+
it("should error when using an old v2 callstack for keys", async () => {
62+
assert.throw(
63+
() => {
64+
deepgram.keys.list("projectId");
65+
},
66+
DeepgramVersionError,
67+
errorText
68+
);
69+
});
70+
71+
it("should error when using an old v2 callstack for members", async () => {
72+
assert.throw(
73+
() => {
74+
deepgram.members.listMembers("projectId");
75+
},
76+
DeepgramVersionError,
77+
errorText
78+
);
79+
});
80+
81+
it("should error when using an old v2 callstack for scopes", async () => {
82+
assert.throw(
83+
() => {
84+
deepgram.scopes.get("projectId", "projectMemberId");
85+
},
86+
DeepgramVersionError,
87+
errorText
88+
);
89+
});
90+
91+
it("should error when using an old v2 callstack for invitation", async () => {
92+
assert.throw(
93+
() => {
94+
deepgram.invitation.list("projectId");
95+
},
96+
DeepgramVersionError,
97+
errorText
98+
);
99+
});
100+
101+
it("should error when using an old v2 callstack for usage", async () => {
102+
assert.throw(
103+
() => {
104+
deepgram.usage.listRequests("projectId", {});
105+
},
106+
DeepgramVersionError,
107+
errorText
108+
);
109+
});
110+
111+
it("should error when using an old v2 callstack for billing", async () => {
112+
assert.throw(
113+
() => {
114+
deepgram.billing.listBalances("projectId");
115+
},
116+
DeepgramVersionError,
117+
errorText
118+
);
119+
});
120+
});

0 commit comments

Comments
 (0)