Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deps/scrapbox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export type {
ErrorLike,
ExportedData,
GuestUser,
ImportedData,
MemberUser,
NotFoundError,
NotLoggedInError,
NotMemberError,
Expand Down
33 changes: 33 additions & 0 deletions rest/profile.ts
Original file line number Diff line number Diff line change
@@ -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<MemberUser | GuestUser> {
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;
}