From 8d86d41f27ef31c45e62d03d4ec991ccb0404a8e Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Mon, 8 Jan 2024 15:09:56 +0000 Subject: [PATCH] feat: throw errors when using v2 callstack on the v3 SDK --- src/DeepgramClient.ts | 37 +++++++++++++ src/index.ts | 12 ++++- src/lib/errors.ts | 10 ++++ test/legacy.test.ts | 120 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 test/legacy.test.ts diff --git a/src/DeepgramClient.ts b/src/DeepgramClient.ts index 219c477e..c6648ba5 100644 --- a/src/DeepgramClient.ts +++ b/src/DeepgramClient.ts @@ -1,3 +1,4 @@ +import { DeepgramVersionError } from "./lib/errors"; import { AbstractClient } from "./packages/AbstractClient"; import { ListenClient } from "./packages/ListenClient"; import { ManageClient } from "./packages/ManageClient"; @@ -21,4 +22,40 @@ export default class DeepgramClient extends AbstractClient { get onprem(): OnPremClient { return new OnPremClient(this.key, this.options); } + + /** + * Major version fallback errors are below + */ + + get transcription(): any { + throw new DeepgramVersionError(); + } + + get projects(): any { + throw new DeepgramVersionError(); + } + + get keys(): any { + throw new DeepgramVersionError(); + } + + get members(): any { + throw new DeepgramVersionError(); + } + + get scopes(): any { + throw new DeepgramVersionError(); + } + + get invitation(): any { + throw new DeepgramVersionError(); + } + + get usage(): any { + throw new DeepgramVersionError(); + } + + get billing(): any { + throw new DeepgramVersionError(); + } } diff --git a/src/index.ts b/src/index.ts index 0c859252..7c70a48b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,16 @@ import DeepgramClient from "./DeepgramClient"; +import { DeepgramVersionError } from "./lib/errors"; import type { DeepgramClientOptions } from "./lib/types"; +/** + * Major version fallback error + */ +class Deepgram { + constructor(protected apiKey: string, protected apiUrl?: string, protected requireSSL?: boolean) { + throw new DeepgramVersionError(); + } +} + /** * Creates a new Deepgram Client. */ @@ -8,7 +18,7 @@ const createClient = (apiKey: string, options: DeepgramClientOptions = {}): Deep return new DeepgramClient(apiKey, options); }; -export { createClient, DeepgramClient }; +export { createClient, DeepgramClient, Deepgram }; /** * Helpful exports. diff --git a/src/lib/errors.ts b/src/lib/errors.ts index b67c2010..82cd8063 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -38,3 +38,13 @@ export class DeepgramUnknownError extends DeepgramError { this.originalError = originalError; } } + +export class DeepgramVersionError extends DeepgramError { + constructor() { + super( + `You are attempting to use an old format for a newer SDK version. Read more here: https://dpgr.am/js-v3` + ); + + this.name = "DeepgramVersionError"; + } +} diff --git a/test/legacy.test.ts b/test/legacy.test.ts new file mode 100644 index 00000000..ecb1f872 --- /dev/null +++ b/test/legacy.test.ts @@ -0,0 +1,120 @@ +import { assert, expect } from "chai"; +import { createClient, Deepgram, DeepgramVersionError } from "../src"; +import { faker } from "@faker-js/faker"; +import DeepgramClient from "../src/DeepgramClient"; + +const errorText = + "You are attempting to use an old format for a newer SDK version. Read more here: https://dpgr.am/js-v3"; + +describe("legacy error handling", () => { + let deepgram: DeepgramClient; + + beforeEach(() => { + deepgram = createClient(faker.string.alphanumeric(40), { + global: { url: "https://api.mock.deepgram.com" }, + }); + }); + + it("should create the correct client object", () => { + expect(deepgram).to.not.be.undefined; + expect(deepgram).is.instanceOf(DeepgramClient); + }); + + it("should error when using a v2 client object", async () => { + assert.throw( + () => { + new Deepgram(faker.string.alphanumeric(40)); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for transcription", async () => { + assert.throw( + () => { + deepgram.transcription.preRecorded( + { + url: "https://dpgr.am/spacewalk.wav", + }, + { + model: "nova", + callback: "http://callback/endpoint", + } + ); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for projects", async () => { + assert.throw( + () => { + deepgram.projects.list(); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for keys", async () => { + assert.throw( + () => { + deepgram.keys.list("projectId"); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for members", async () => { + assert.throw( + () => { + deepgram.members.listMembers("projectId"); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for scopes", async () => { + assert.throw( + () => { + deepgram.scopes.get("projectId", "projectMemberId"); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for invitation", async () => { + assert.throw( + () => { + deepgram.invitation.list("projectId"); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for usage", async () => { + assert.throw( + () => { + deepgram.usage.listRequests("projectId", {}); + }, + DeepgramVersionError, + errorText + ); + }); + + it("should error when using an old v2 callstack for billing", async () => { + assert.throw( + () => { + deepgram.billing.listBalances("projectId"); + }, + DeepgramVersionError, + errorText + ); + }); +});