Skip to content

Commit

Permalink
feat: throw errors when using v2 callstack on the v3 SDK (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes authored Jan 25, 2024
1 parent 7c41660 commit e5fac7a
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/DeepgramClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DeepgramVersionError } from "./lib/errors";
import { AbstractClient } from "./packages/AbstractClient";
import { ListenClient } from "./packages/ListenClient";
import { ManageClient } from "./packages/ManageClient";
Expand All @@ -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();
}
}
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
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.
*/
const createClient = (apiKey: string, options: DeepgramClientOptions = {}): DeepgramClient => {
return new DeepgramClient(apiKey, options);
};

export { createClient, DeepgramClient };
export { createClient, DeepgramClient, Deepgram };

/**
* Helpful exports.
Expand Down
10 changes: 10 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
120 changes: 120 additions & 0 deletions test/legacy.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
});

0 comments on commit e5fac7a

Please sign in to comment.