Skip to content

Commit 94b53a3

Browse files
committed
✨ Get project information
1 parent 573aef7 commit 94b53a3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

deps/scrapbox.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ export type {
33
ExportedData,
44
GuestUser,
55
ImportedData,
6+
MemberProject,
67
MemberUser,
78
NotFoundError,
89
NotLoggedInError,
910
NotMemberError,
11+
NotMemberProject,
1012
NotPrivilegeError,
1113
Page,
1214
Scrapbox,

rest/project.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export async function getProjectId(project: string) {
2+
const res = await fetch(`https://scrapbox.io/api/projects/${project}`);
3+
const json = (await res.json()) as MemberProject;
4+
return json.id;
5+
}
6+
7+
import type {
8+
MemberProject,
9+
NotFoundError,
10+
NotLoggedInError,
11+
NotMemberError,
12+
NotMemberProject,
13+
} from "../deps/scrapbox.ts";
14+
import { cookie, makeCustomError, Result, tryToErrorLike } from "./utils.ts";
15+
16+
export interface ProjectInit {
17+
/** connect.sid */ sid: string;
18+
}
19+
/** get the project information
20+
*
21+
* @param project project name to get
22+
* @param init connect.sid etc.
23+
*/
24+
export async function getProject(
25+
project: string,
26+
init?: ProjectInit,
27+
): Promise<
28+
Result<
29+
MemberProject | NotMemberProject,
30+
NotFoundError | NotMemberError | NotLoggedInError
31+
>
32+
> {
33+
const path = `https://scrapbox.io/api/projects/${project}`;
34+
const res = await fetch(
35+
path,
36+
init?.sid
37+
? {
38+
headers: {
39+
Cookie: cookie(init.sid),
40+
},
41+
}
42+
: undefined,
43+
);
44+
45+
if (!res.ok) {
46+
const value = tryToErrorLike(await res.json()) as
47+
| false
48+
| NotFoundError
49+
| NotMemberError
50+
| NotLoggedInError;
51+
if (!value) {
52+
throw makeCustomError(
53+
"UnexpectedError",
54+
`Unexpected error has occuerd when fetching "${path}"`,
55+
);
56+
}
57+
return { ok: false, value };
58+
}
59+
const value = (await res.json()) as MemberProject | NotMemberProject;
60+
return { ok: true, value };
61+
}

0 commit comments

Comments
 (0)