From 24823ae4b44117f8520d466c36012f13d9c6fd53 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 25 Mar 2022 10:11:08 +0900 Subject: [PATCH] =?UTF-8?q?:+1:=20/api/pages/:prjectname/search/titles=20?= =?UTF-8?q?=E3=81=AE=E3=82=A8=E3=83=A9=E3=83=BC=E5=AE=9A=E7=BE=A9=E3=82=92?= =?UTF-8?q?=E8=A9=B3=E3=81=97=E3=81=8F=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rest/link.ts | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/rest/link.ts b/rest/link.ts index b848a45..c4c28d8 100644 --- a/rest/link.ts +++ b/rest/link.ts @@ -1,9 +1,19 @@ -import type { ErrorLike, SearchedTitle } from "../deps/scrapbox.ts"; +import type { + ErrorLike, + NotFoundError, + NotLoggedInError, + SearchedTitle, +} from "../deps/scrapbox.ts"; import { cookie } from "./auth.ts"; import { UnexpectedResponseError } from "./error.ts"; import { tryToErrorLike } from "../is.ts"; import { BaseOptions, Result, setDefaults } from "./util.ts"; +/** 不正なfollowingIdを渡されたときに発生するエラー */ +export interface InvalidFollowingIdError extends ErrorLike { + name: "InvalidFollowingIdError"; +} + export interface GetLinksOptions extends BaseOptions { /** 次のリンクリストを示すID */ followingId?: string; @@ -20,7 +30,7 @@ export const getLinks = async ( Result<{ pages: SearchedTitle[]; followingId: string; - }, ErrorLike> + }, NotFoundError | NotLoggedInError | InvalidFollowingIdError> > => { const { sid, hostName, fetch, followingId } = setDefaults(options ?? {}); const path = `https://${hostName}/api/pages/${project}/search/titles${ @@ -33,6 +43,15 @@ export const getLinks = async ( ); if (!res.ok) { + if (res.status === 422) { + return { + ok: false, + value: { + name: "InvalidFollowingIdError", + message: await res.text(), + }, + }; + } const text = await res.text(); const value = tryToErrorLike(text); if (!value) { @@ -42,7 +61,7 @@ export const getLinks = async ( body: text, }); } - return { ok: false, value }; + return { ok: false, value: value as NotFoundError | NotLoggedInError }; } const pages = (await res.json()) as SearchedTitle[]; return { @@ -61,7 +80,12 @@ export const getLinks = async ( export const readLinksBulk = async ( project: string, options?: BaseOptions, -): Promise> => { +): Promise< + | NotFoundError + | NotLoggedInError + | InvalidFollowingIdError + | AsyncGenerator +> => { const first = await getLinks(project, options); if (!first.ok) return first.value; @@ -90,7 +114,12 @@ export const readLinksBulk = async ( export const readLinks = async ( project: string, options?: BaseOptions, -): Promise> => { +): Promise< + | NotFoundError + | NotLoggedInError + | InvalidFollowingIdError + | AsyncGenerator +> => { const reader = await readLinksBulk(project, options); if ("name" in reader) return reader;