@@ -3,13 +3,10 @@ import type {
33 NotLoggedInError ,
44 NotMemberError ,
55 Page ,
6+ PageList ,
67} from "../deps/scrapbox.ts" ;
7- import {
8- cookie ,
9- encodeTitle ,
10- makeCustomError ,
11- tryToErrorLike ,
12- } from "./utils.ts" ;
8+ import { cookie , makeCustomError , tryToErrorLike } from "./utils.ts" ;
9+ import { encodeTitleURI } from "../title.ts" ;
1310import type { Result } from "./utils.ts" ;
1411
1512/** Options for `getPage()` */
@@ -34,7 +31,7 @@ export async function getPage(
3431 >
3532> {
3633 const path = `https://scrapbox.io/api/pages/${ project } /${
37- encodeTitle ( title )
34+ encodeTitleURI ( title )
3835 } ?followRename=${ options ?. followRename ?? true } `;
3936
4037 const res = await fetch (
@@ -68,3 +65,84 @@ export async function getPage(
6865 const value = ( await res . json ( ) ) as Page ;
6966 return { ok : true , value } ;
7067}
68+
69+ /** Options for `listPages()` */
70+ export interface ListPagesOption {
71+ /** the sort of page list to return
72+ *
73+ * @default "updated"
74+ */
75+ sort ?:
76+ | "updatedWithMe"
77+ | "updated"
78+ | "created"
79+ | "accessed"
80+ | "pageRank"
81+ | "linked"
82+ | "views"
83+ | "title" ;
84+ /** the index getting page list from
85+ *
86+ * @default 0
87+ */
88+ skip ?: number ;
89+ /** threshold of the length of page list
90+ *
91+ * @default 100
92+ */
93+ limit ?: number ;
94+ /** connect.sid */
95+ sid ?: string ;
96+ }
97+ /** 指定したprojectのページを一覧する
98+ *
99+ * @param project 一覧したいproject
100+ * @param options オプション 取得範囲や並び順を決める
101+ */
102+ export async function listPages (
103+ project : string ,
104+ options ?: ListPagesOption ,
105+ ) : Promise <
106+ Result <
107+ PageList ,
108+ NotFoundError | NotLoggedInError | NotMemberError
109+ >
110+ > {
111+ const { sort, limit, skip } = options ?? { } ;
112+ const params = new URLSearchParams ( ) ;
113+ if ( sort !== undefined ) params . append ( "sort" , sort ) ;
114+ if ( limit !== undefined ) params . append ( "limit" , `${ limit } ` ) ;
115+ if ( skip !== undefined ) params . append ( "skip" , `${ skip } ` ) ;
116+ const path = `https://scrapbox.io/api/pages/${ project } ?${ params . toString ( ) } ` ;
117+
118+ const res = await fetch (
119+ path ,
120+ options ?. sid
121+ ? {
122+ headers : {
123+ Cookie : cookie ( options . sid ) ,
124+ } ,
125+ }
126+ : undefined ,
127+ ) ;
128+
129+ if ( ! res . ok ) {
130+ const value = tryToErrorLike ( await res . text ( ) ) as
131+ | false
132+ | NotFoundError
133+ | NotLoggedInError
134+ | NotMemberError ;
135+ if ( ! value ) {
136+ throw makeCustomError (
137+ "UnexpectedError" ,
138+ `Unexpected error has occuerd when fetching "${ path } "` ,
139+ ) ;
140+ }
141+ return {
142+ ok : false ,
143+ value,
144+ } ;
145+ }
146+ const value = ( await res . json ( ) ) as PageList ;
147+ return { ok : true , value } ;
148+ }
0 commit comments