File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 11export type {
22 ErrorLike ,
33 ExportedData ,
4+ GuestUser ,
45 ImportedData ,
6+ MemberUser ,
57 NotFoundError ,
68 NotLoggedInError ,
79 NotMemberError ,
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments