File tree 2 files changed +63
-0
lines changed
2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -3,10 +3,12 @@ export type {
3
3
ExportedData ,
4
4
GuestUser ,
5
5
ImportedData ,
6
+ MemberProject ,
6
7
MemberUser ,
7
8
NotFoundError ,
8
9
NotLoggedInError ,
9
10
NotMemberError ,
11
+ NotMemberProject ,
10
12
NotPrivilegeError ,
11
13
Page ,
12
14
Scrapbox ,
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments