Skip to content

Commit 85d31eb

Browse files
authored
Merge pull request #12 from takker99/add-listpages
Add `listPages()`
2 parents 38b5941 + 382bb13 commit 85d31eb

File tree

3 files changed

+86
-15
lines changed

3 files changed

+86
-15
lines changed

deps/scrapbox.ts

+1
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

+85-7
Original file line numberDiff line numberDiff line change
@@ -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";
1310
import 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+
}

rest/utils.ts

-8
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,3 @@ export function makeCustomError(name: string, message: string) {
5656
error.message = message;
5757
return error;
5858
}
59-
60-
export const toTitleLc = (title: string) =>
61-
title.toLowerCase().replaceAll(" ", "_");
62-
export const encodeTitle = (title: string) =>
63-
title.replaceAll(" ", "_").replace(
64-
/[/?#\{}^|<>]/g,
65-
(char) => encodeURIComponent(char),
66-
);

0 commit comments

Comments
 (0)