Skip to content

✨ Get user profile #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2022
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;
}