From 3d70afc4e7931de40993e39f9877c72cf359b50b Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Sat, 12 Feb 2022 08:31:11 +0900 Subject: [PATCH 1/2] :recycle: Use encodeTitleURI --- rest/pages.ts | 10 +++------- rest/utils.ts | 8 -------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/rest/pages.ts b/rest/pages.ts index 0e6be38..66d909a 100644 --- a/rest/pages.ts +++ b/rest/pages.ts @@ -4,12 +4,8 @@ import type { NotMemberError, Page, } from "../deps/scrapbox.ts"; -import { - cookie, - encodeTitle, - makeCustomError, - tryToErrorLike, -} from "./utils.ts"; +import { cookie, makeCustomError, tryToErrorLike } from "./utils.ts"; +import { encodeTitleURI } from "../title.ts"; import type { Result } from "./utils.ts"; /** Options for `getPage()` */ @@ -34,7 +30,7 @@ export async function getPage( > > { const path = `https://scrapbox.io/api/pages/${project}/${ - encodeTitle(title) + encodeTitleURI(title) }?followRename=${options?.followRename ?? true}`; const res = await fetch( diff --git a/rest/utils.ts b/rest/utils.ts index 4ef63ad..f337bf9 100644 --- a/rest/utils.ts +++ b/rest/utils.ts @@ -56,11 +56,3 @@ export function makeCustomError(name: string, message: string) { error.message = message; return error; } - -export const toTitleLc = (title: string) => - title.toLowerCase().replaceAll(" ", "_"); -export const encodeTitle = (title: string) => - title.replaceAll(" ", "_").replace( - /[/?#\{}^|<>]/g, - (char) => encodeURIComponent(char), - ); From 382bb130c20c5ef06742fec728ed53ba181c71fd Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Sat, 12 Feb 2022 08:35:26 +0900 Subject: [PATCH 2/2] :sparkles: Enable to get page list --- deps/scrapbox.ts | 1 + rest/pages.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/deps/scrapbox.ts b/deps/scrapbox.ts index a522627..62e1109 100644 --- a/deps/scrapbox.ts +++ b/deps/scrapbox.ts @@ -11,6 +11,7 @@ export type { NotMemberProject, NotPrivilegeError, Page, + PageList, Scrapbox, } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.8/mod.ts"; import type { Page } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.0.8/mod.ts"; diff --git a/rest/pages.ts b/rest/pages.ts index 66d909a..84a0c55 100644 --- a/rest/pages.ts +++ b/rest/pages.ts @@ -3,6 +3,7 @@ import type { NotLoggedInError, NotMemberError, Page, + PageList, } from "../deps/scrapbox.ts"; import { cookie, makeCustomError, tryToErrorLike } from "./utils.ts"; import { encodeTitleURI } from "../title.ts"; @@ -64,3 +65,84 @@ export async function getPage( const value = (await res.json()) as Page; return { ok: true, value }; } + +/** Options for `listPages()` */ +export interface ListPagesOption { + /** the sort of page list to return + * + * @default "updated" + */ + sort?: + | "updatedWithMe" + | "updated" + | "created" + | "accessed" + | "pageRank" + | "linked" + | "views" + | "title"; + /** the index getting page list from + * + * @default 0 + */ + skip?: number; + /** threshold of the length of page list + * + * @default 100 + */ + limit?: number; + /** connect.sid */ + sid?: string; +} +/** 指定したprojectのページを一覧する + * + * @param project 一覧したいproject + * @param options オプション 取得範囲や並び順を決める + */ +export async function listPages( + project: string, + options?: ListPagesOption, +): Promise< + Result< + PageList, + NotFoundError | NotLoggedInError | NotMemberError + > +> { + const { sort, limit, skip } = options ?? {}; + const params = new URLSearchParams(); + if (sort !== undefined) params.append("sort", sort); + if (limit !== undefined) params.append("limit", `${limit}`); + if (skip !== undefined) params.append("skip", `${skip}`); + const path = `https://scrapbox.io/api/pages/${project}?${params.toString()}`; + + const res = await fetch( + path, + options?.sid + ? { + headers: { + Cookie: cookie(options.sid), + }, + } + : undefined, + ); + + if (!res.ok) { + const value = tryToErrorLike(await res.text()) as + | false + | NotFoundError + | NotLoggedInError + | NotMemberError; + if (!value) { + throw makeCustomError( + "UnexpectedError", + `Unexpected error has occuerd when fetching "${path}"`, + ); + } + return { + ok: false, + value, + }; + } + const value = (await res.json()) as PageList; + return { ok: true, value }; +}