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" ;
2
7
import { cookie } from "./auth.ts" ;
3
8
import { UnexpectedResponseError } from "./error.ts" ;
4
9
import { tryToErrorLike } from "../is.ts" ;
5
10
import { BaseOptions , Result , setDefaults } from "./util.ts" ;
6
11
12
+ /** 不正なfollowingIdを渡されたときに発生するエラー */
13
+ export interface InvalidFollowingIdError extends ErrorLike {
14
+ name : "InvalidFollowingIdError" ;
15
+ }
16
+
7
17
export interface GetLinksOptions extends BaseOptions {
8
18
/** 次のリンクリストを示すID */
9
19
followingId ?: string ;
@@ -20,7 +30,7 @@ export const getLinks = async (
20
30
Result < {
21
31
pages : SearchedTitle [ ] ;
22
32
followingId : string ;
23
- } , ErrorLike >
33
+ } , NotFoundError | NotLoggedInError | InvalidFollowingIdError >
24
34
> => {
25
35
const { sid, hostName, fetch, followingId } = setDefaults ( options ?? { } ) ;
26
36
const path = `https://${ hostName } /api/pages/${ project } /search/titles${
@@ -33,6 +43,15 @@ export const getLinks = async (
33
43
) ;
34
44
35
45
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
+ }
36
55
const text = await res . text ( ) ;
37
56
const value = tryToErrorLike ( text ) ;
38
57
if ( ! value ) {
@@ -42,7 +61,7 @@ export const getLinks = async (
42
61
body : text ,
43
62
} ) ;
44
63
}
45
- return { ok : false , value } ;
64
+ return { ok : false , value : value as NotFoundError | NotLoggedInError } ;
46
65
}
47
66
const pages = ( await res . json ( ) ) as SearchedTitle [ ] ;
48
67
return {
@@ -61,7 +80,12 @@ export const getLinks = async (
61
80
export const readLinksBulk = async (
62
81
project : string ,
63
82
options ?: BaseOptions ,
64
- ) : Promise < ErrorLike | AsyncGenerator < SearchedTitle [ ] , void , unknown > > => {
83
+ ) : Promise <
84
+ | NotFoundError
85
+ | NotLoggedInError
86
+ | InvalidFollowingIdError
87
+ | AsyncGenerator < SearchedTitle [ ] , void , unknown >
88
+ > => {
65
89
const first = await getLinks ( project , options ) ;
66
90
if ( ! first . ok ) return first . value ;
67
91
@@ -90,7 +114,12 @@ export const readLinksBulk = async (
90
114
export const readLinks = async (
91
115
project : string ,
92
116
options ?: BaseOptions ,
93
- ) : Promise < ErrorLike | AsyncGenerator < SearchedTitle , void , unknown > > => {
117
+ ) : Promise <
118
+ | NotFoundError
119
+ | NotLoggedInError
120
+ | InvalidFollowingIdError
121
+ | AsyncGenerator < SearchedTitle , void , unknown >
122
+ > => {
94
123
const reader = await readLinksBulk ( project , options ) ;
95
124
if ( "name" in reader ) return reader ;
96
125
0 commit comments