Skip to content

Commit 573aef7

Browse files
authored
Merge pull request #5 from takker99/add-get-profile
✨ Get user profile
2 parents 15ec713 + 8f5f8ec commit 573aef7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

deps/scrapbox.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export type {
22
ErrorLike,
33
ExportedData,
4+
GuestUser,
45
ImportedData,
6+
MemberUser,
57
NotFoundError,
68
NotLoggedInError,
79
NotMemberError,

rest/profile.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { GuestUser, MemberUser } from "../deps/scrapbox.ts";
2+
import { cookie, makeCustomError } from "./utils.ts";
3+
4+
export interface ProfileInit {
5+
/** connect.sid */ sid: string;
6+
}
7+
/** get user profile
8+
*
9+
* @param init connect.sid etc.
10+
*/
11+
export async function getProfile(
12+
init?: ProfileInit,
13+
): Promise<MemberUser | GuestUser> {
14+
const path = "https://scrapbox.io/api/users/me";
15+
const res = await fetch(
16+
path,
17+
init?.sid
18+
? {
19+
headers: {
20+
Cookie: cookie(init.sid),
21+
},
22+
}
23+
: undefined,
24+
);
25+
26+
if (!res.ok) {
27+
throw makeCustomError(
28+
"UnexpectedError",
29+
`Unexpected error has occuerd when fetching "${path}"`,
30+
);
31+
}
32+
return (await res.json()) as MemberUser | GuestUser;
33+
}

0 commit comments

Comments
 (0)