Skip to content

Commit 382bb13

Browse files
committed
✨ Enable to get page list
1 parent 3d70afc commit 382bb13

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

deps/scrapbox.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type {
1111
NotMemberProject,
1212
NotPrivilegeError,
1313
Page,
14+
PageList,
1415
Scrapbox,
1516
} from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.8/mod.ts";
1617
import type { Page } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.8/mod.ts";

rest/pages.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
NotLoggedInError,
44
NotMemberError,
55
Page,
6+
PageList,
67
} from "../deps/scrapbox.ts";
78
import { cookie, makeCustomError, tryToErrorLike } from "./utils.ts";
89
import { encodeTitleURI } from "../title.ts";
@@ -64,3 +65,84 @@ export async function getPage(
6465
const value = (await res.json()) as Page;
6566
return { ok: true, value };
6667
}
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

Comments
 (0)