Skip to content

Commit dc3ba27

Browse files
authored
chore: add getGitHubUser() JSDocs and test (denoland#545)
TSIA
1 parent 6a33945 commit dc3ba27

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

plugins/kv_oauth.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,44 @@ import { isStripeEnabled, stripe } from "@/utils/stripe.ts";
1818

1919
const oauth2Client = createGitHubOAuth2Client();
2020

21-
// deno-lint-ignore no-explicit-any
22-
async function getGitHubUser(accessToken: string): Promise<any> {
21+
interface GitHubUser {
22+
login: string;
23+
email: string;
24+
}
25+
26+
/**
27+
* Returns the GitHub profile information of the user with the given access
28+
* token.
29+
*
30+
* @see {@link https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-the-authenticated-user}
31+
*
32+
* @example
33+
* ```ts
34+
* import { getGitHubUser } from "@/plugins/kv_oauth.ts";
35+
*
36+
* const user = await getGitHubUser("<access token>");
37+
* user.login; // Returns "octocat"
38+
* user.email; // Returns "[email protected]"
39+
* ```
40+
*/
41+
export async function getGitHubUser(accessToken: string) {
2342
const response = await fetch("https://api.github.com/user", {
2443
headers: { authorization: `Bearer ${accessToken}` },
2544
});
2645
if (!response.ok) {
2746
const { message } = await response.json();
2847
throw new Error(message);
2948
}
30-
return await response.json();
49+
return await response.json() as Promise<GitHubUser>;
3150
}
3251

3352
/**
34-
* This custom plugin centralizes all authentication logic using the {@link https://deno.land/x/deno_kv_oauth|Deno KV OAuth} module.
53+
* This custom plugin centralizes all authentication logic using the
54+
* {@link https://deno.land/x/deno_kv_oauth|Deno KV OAuth} module.
3555
*
36-
* The implementation is based off Deno KV OAuth's own {@link https://deno.land/x/deno_kv_oauth/src/fresh_plugin.ts?source|Fresh plugin implementation}.
56+
* The implementation is based off Deno KV OAuth's own
57+
* {@link https://deno.land/x/deno_kv_oauth/src/fresh_plugin.ts?source|Fresh plugin}
58+
* implementation.
3759
*/
3860
export default {
3961
name: "kv-oauth",

plugins/kv_oauth_test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
2+
import { assertRejects } from "std/assert/assert_rejects.ts";
3+
import { getGitHubUser } from "./kv_oauth.ts";
4+
5+
Deno.test("[plugins] getGitHubUser()", async () => {
6+
await assertRejects(
7+
async () => await getGitHubUser("access token"),
8+
Error,
9+
"Bad credentials",
10+
);
11+
});

0 commit comments

Comments
 (0)