Skip to content

Commit

Permalink
feat(auth): throw Error when auth type is not a valid one (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscard0m authored Feb 3, 2021
1 parent f008655 commit ffaefb5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ export async function auth(
state: State,
options: AuthOptions
): Promise<Authentication> {
if (options.type === "app") {
return getAppAuthentication(state);
}
const { type } = options;

if (options.type === "installation") {
return getInstallationAuthentication(state, options);
switch (type) {
case "app":
return getAppAuthentication(state);
case "installation":
return getInstallationAuthentication(state, options);
case "oauth":
return getOAuthAuthentication(state, options);
default:
throw new Error(`Invalid auth type: ${type}`);
}

return getOAuthAuthentication(state, options);
}
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ test("README example for app auth", async () => {
});
});

test("throws if invalid 'type' is provided", async () => {
const auth = createAppAuth({
appId: APP_ID,
privateKey: PRIVATE_KEY,
});

// @ts-expect-error TS2322
// Details here: https://github.com/octokit/auth-app.js/issues/216#issuecomment-772106164
await expect(auth({ type: "app2" })).rejects.toEqual(
new Error("Invalid auth type: app2")
);
});

test("README example for installation auth", async () => {
const matchCreateInstallationAccessToken: MockMatcherFunction = (
url,
Expand Down

0 comments on commit ffaefb5

Please sign in to comment.