@@ -5,8 +5,7 @@ import type {
5
5
SearchedTitle ,
6
6
} from "../deps/scrapbox-rest.ts" ;
7
7
import { cookie } from "./auth.ts" ;
8
- import { UnexpectedResponseError } from "./error.ts" ;
9
- import { tryToErrorLike } from "../is.ts" ;
8
+ import { makeError } from "./error.ts" ;
10
9
import { BaseOptions , Result , setDefaults } from "./util.ts" ;
11
10
12
11
/** 不正なfollowingIdを渡されたときに発生するエラー */
@@ -33,15 +32,16 @@ export const getLinks = async (
33
32
} , NotFoundError | NotLoggedInError | InvalidFollowingIdError >
34
33
> => {
35
34
const { sid, hostName, fetch, followingId } = setDefaults ( options ?? { } ) ;
36
- const path = `https://${ hostName } /api/pages/${ project } /search/titles${
37
- followingId ? `?followingId=${ followingId } ` : ""
38
- } `;
39
35
40
- const res = await fetch (
41
- path ,
36
+ const req = new Request (
37
+ `https://${ hostName } /api/pages/${ project } /search/titles${
38
+ followingId ? `?followingId=${ followingId } ` : ""
39
+ } `,
42
40
sid ? { headers : { Cookie : cookie ( sid ) } } : undefined ,
43
41
) ;
44
42
43
+ const res = await fetch ( req ) ;
44
+
45
45
if ( ! res . ok ) {
46
46
if ( res . status === 422 ) {
47
47
return {
@@ -52,17 +52,10 @@ export const getLinks = async (
52
52
} ,
53
53
} ;
54
54
}
55
- const text = await res . text ( ) ;
56
- const value = tryToErrorLike ( text ) ;
57
- if ( ! value ) {
58
- throw new UnexpectedResponseError ( {
59
- path : new URL ( path ) ,
60
- ...res ,
61
- body : text ,
62
- } ) ;
63
- }
64
- return { ok : false , value : value as NotFoundError | NotLoggedInError } ;
55
+
56
+ return makeError < NotFoundError | NotLoggedInError > ( req , res ) ;
65
57
}
58
+
66
59
const pages = ( await res . json ( ) ) as SearchedTitle [ ] ;
67
60
return {
68
61
ok : true ,
0 commit comments