File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1
1
export type {
2
2
ErrorLike ,
3
3
ExportedData ,
4
+ GuestUser ,
4
5
ImportedData ,
6
+ MemberUser ,
5
7
NotFoundError ,
6
8
NotLoggedInError ,
7
9
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