diff --git a/deps/scrapbox.ts b/deps/scrapbox.ts index 2123c87..7abfa7f 100644 --- a/deps/scrapbox.ts +++ b/deps/scrapbox.ts @@ -1,7 +1,9 @@ export type { ErrorLike, ExportedData, + GuestUser, ImportedData, + MemberUser, NotFoundError, NotLoggedInError, NotMemberError, diff --git a/rest/profile.ts b/rest/profile.ts new file mode 100644 index 0000000..60b95fe --- /dev/null +++ b/rest/profile.ts @@ -0,0 +1,33 @@ +import type { GuestUser, MemberUser } from "../deps/scrapbox.ts"; +import { cookie, makeCustomError } from "./utils.ts"; + +export interface ProfileInit { + /** connect.sid */ sid: string; +} +/** get user profile + * + * @param init connect.sid etc. + */ +export async function getProfile( + init?: ProfileInit, +): Promise { + const path = "https://scrapbox.io/api/users/me"; + const res = await fetch( + path, + init?.sid + ? { + headers: { + Cookie: cookie(init.sid), + }, + } + : undefined, + ); + + if (!res.ok) { + throw makeCustomError( + "UnexpectedError", + `Unexpected error has occuerd when fetching "${path}"`, + ); + } + return (await res.json()) as MemberUser | GuestUser; +}