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
Expand Up @@ -3,10 +3,12 @@ export type {
ExportedData,
GuestUser,
ImportedData,
MemberProject,
MemberUser,
NotFoundError,
NotLoggedInError,
NotMemberError,
NotMemberProject,
NotPrivilegeError,
Page,
Scrapbox,
Expand Down
61 changes: 61 additions & 0 deletions rest/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export async function getProjectId(project: string) {
const res = await fetch(`https://scrapbox.io/api/projects/${project}`);
const json = (await res.json()) as MemberProject;
return json.id;
}

import type {
MemberProject,
NotFoundError,
NotLoggedInError,
NotMemberError,
NotMemberProject,
} from "../deps/scrapbox.ts";
import { cookie, makeCustomError, Result, tryToErrorLike } from "./utils.ts";

export interface ProjectInit {
/** connect.sid */ sid: string;
}
/** get the project information
*
* @param project project name to get
* @param init connect.sid etc.
*/
export async function getProject(
project: string,
init?: ProjectInit,
): Promise<
Result<
MemberProject | NotMemberProject,
NotFoundError | NotMemberError | NotLoggedInError
>
> {
const path = `https://scrapbox.io/api/projects/${project}`;
const res = await fetch(
path,
init?.sid
? {
headers: {
Cookie: cookie(init.sid),
},
}
: undefined,
);

if (!res.ok) {
const value = tryToErrorLike(await res.json()) as
| false
| NotFoundError
| NotMemberError
| NotLoggedInError;
if (!value) {
throw makeCustomError(
"UnexpectedError",
`Unexpected error has occuerd when fetching "${path}"`,
);
}
return { ok: false, value };
}
const value = (await res.json()) as MemberProject | NotMemberProject;
return { ok: true, value };
}