@@ -3,13 +3,10 @@ import type {
3
3
NotLoggedInError ,
4
4
NotMemberError ,
5
5
Page ,
6
+ PageList ,
6
7
} 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" ;
13
10
import type { Result } from "./utils.ts" ;
14
11
15
12
/** Options for `getPage()` */
@@ -34,7 +31,7 @@ export async function getPage(
34
31
>
35
32
> {
36
33
const path = `https://scrapbox.io/api/pages/${ project } /${
37
- encodeTitle ( title )
34
+ encodeTitleURI ( title )
38
35
} ?followRename=${ options ?. followRename ?? true } `;
39
36
40
37
const res = await fetch (
@@ -68,3 +65,84 @@ export async function getPage(
68
65
const value = ( await res . json ( ) ) as Page ;
69
66
return { ok : true , value } ;
70
67
}
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