1- import type { ErrorLike , SearchedTitle } from "../deps/scrapbox.ts" ;
1+ import type {
2+ ErrorLike ,
3+ NotFoundError ,
4+ NotLoggedInError ,
5+ SearchedTitle ,
6+ } from "../deps/scrapbox.ts" ;
27import { cookie } from "./auth.ts" ;
38import { UnexpectedResponseError } from "./error.ts" ;
49import { tryToErrorLike } from "../is.ts" ;
510import { BaseOptions , Result , setDefaults } from "./util.ts" ;
611
12+ /** 不正なfollowingIdを渡されたときに発生するエラー */
13+ export interface InvalidFollowingIdError extends ErrorLike {
14+ name : "InvalidFollowingIdError" ;
15+ }
16+
717export interface GetLinksOptions extends BaseOptions {
818 /** 次のリンクリストを示すID */
919 followingId ?: string ;
@@ -20,7 +30,7 @@ export const getLinks = async (
2030 Result < {
2131 pages : SearchedTitle [ ] ;
2232 followingId : string ;
23- } , ErrorLike >
33+ } , NotFoundError | NotLoggedInError | InvalidFollowingIdError >
2434> => {
2535 const { sid, hostName, fetch, followingId } = setDefaults ( options ?? { } ) ;
2636 const path = `https://${ hostName } /api/pages/${ project } /search/titles${
@@ -33,6 +43,15 @@ export const getLinks = async (
3343 ) ;
3444
3545 if ( ! res . ok ) {
46+ if ( res . status === 422 ) {
47+ return {
48+ ok : false ,
49+ value : {
50+ name : "InvalidFollowingIdError" ,
51+ message : await res . text ( ) ,
52+ } ,
53+ } ;
54+ }
3655 const text = await res . text ( ) ;
3756 const value = tryToErrorLike ( text ) ;
3857 if ( ! value ) {
@@ -42,7 +61,7 @@ export const getLinks = async (
4261 body : text ,
4362 } ) ;
4463 }
45- return { ok : false , value } ;
64+ return { ok : false , value : value as NotFoundError | NotLoggedInError } ;
4665 }
4766 const pages = ( await res . json ( ) ) as SearchedTitle [ ] ;
4867 return {
@@ -61,7 +80,12 @@ export const getLinks = async (
6180export const readLinksBulk = async (
6281 project : string ,
6382 options ?: BaseOptions ,
64- ) : Promise < ErrorLike | AsyncGenerator < SearchedTitle [ ] , void , unknown > > => {
83+ ) : Promise <
84+ | NotFoundError
85+ | NotLoggedInError
86+ | InvalidFollowingIdError
87+ | AsyncGenerator < SearchedTitle [ ] , void , unknown >
88+ > => {
6589 const first = await getLinks ( project , options ) ;
6690 if ( ! first . ok ) return first . value ;
6791
@@ -90,7 +114,12 @@ export const readLinksBulk = async (
90114export const readLinks = async (
91115 project : string ,
92116 options ?: BaseOptions ,
93- ) : Promise < ErrorLike | AsyncGenerator < SearchedTitle , void , unknown > > => {
117+ ) : Promise <
118+ | NotFoundError
119+ | NotLoggedInError
120+ | InvalidFollowingIdError
121+ | AsyncGenerator < SearchedTitle , void , unknown >
122+ > => {
94123 const reader = await readLinksBulk ( project , options ) ;
95124 if ( "name" in reader ) return reader ;
96125
0 commit comments