@@ -4,6 +4,8 @@ import type {
4
4
NotLoggedInError ,
5
5
NotMemberError ,
6
6
NotMemberProject ,
7
+ ProjectId ,
8
+ ProjectResponse ,
7
9
} from "../deps/scrapbox-rest.ts" ;
8
10
import { cookie } from "./auth.ts" ;
9
11
import { UnexpectedResponseError } from "./error.ts" ;
@@ -50,3 +52,43 @@ export const getProject = async (
50
52
const value = ( await res . json ( ) ) as MemberProject | NotMemberProject ;
51
53
return { ok : true , value } ;
52
54
} ;
55
+
56
+ /** list the projects' information
57
+ *
58
+ * @param projectIds project ids. This must have more than 1 id
59
+ * @param init connect.sid etc.
60
+ */
61
+ export const listProjects = async (
62
+ projectIds : ProjectId [ ] ,
63
+ init ?: BaseOptions ,
64
+ ) : Promise < Result < ProjectResponse , NotLoggedInError > > => {
65
+ const { sid, hostName, fetch } = setDefaults ( init ?? { } ) ;
66
+ const param = new URLSearchParams ( ) ;
67
+ for ( const id of projectIds ) {
68
+ param . append ( "ids" , id ) ;
69
+ }
70
+ const path = `https://${ hostName } /api/projects?${ param . toString ( ) } ` ;
71
+ const res = await fetch (
72
+ path ,
73
+ sid ? { headers : { Cookie : cookie ( sid ) } } : undefined ,
74
+ ) ;
75
+
76
+ if ( ! res . ok ) {
77
+ const text = await res . text ( ) ;
78
+ const value = tryToErrorLike ( text ) ;
79
+ if ( ! value ) {
80
+ throw new UnexpectedResponseError ( {
81
+ path : new URL ( path ) ,
82
+ ...res ,
83
+ body : text ,
84
+ } ) ;
85
+ }
86
+ return {
87
+ ok : false ,
88
+ value : value as NotLoggedInError ,
89
+ } ;
90
+ }
91
+
92
+ const value = ( await res . json ( ) ) as ProjectResponse ;
93
+ return { ok : true , value } ;
94
+ } ;
0 commit comments