@@ -39,29 +39,51 @@ export type LinksError =
39
39
| InvalidFollowingIdError
40
40
| HTTPError ;
41
41
42
- /** 指定したprojectのリンクデータを取得する
42
+ /** Get the links of the specified project
43
43
*
44
- * @param project データを取得したいproject
44
+ * @param project The project to get the data from
45
+ * @param options Options for the request
46
+ * @return a promise that resolves to the parsed data
45
47
*/
46
- export const getLinks = async (
47
- project : string ,
48
- options ?: GetLinksOptions ,
49
- ) : Promise < Result < GetLinksResult , LinksError | FetchError > > => {
50
- const { sid , hostName , fetch , followingId } = setDefaults ( options ?? { } ) ;
48
+ export interface GetLinks {
49
+ (
50
+ project : string ,
51
+ options ?: GetLinksOptions ,
52
+ ) : Promise < Result < GetLinksResult , LinksError | FetchError > > ;
51
53
52
- const req = new Request (
54
+ /** Create a request to `GET /api/pages/:project/search/titles`
55
+ *
56
+ * @param project The project to get the data from
57
+ * @param options Options for the request
58
+ * @return The request object
59
+ */
60
+ toRequest : ( project : string , options ?: GetLinksOptions ) => Request ;
61
+
62
+ /** Parse the response from `GET /api/pages/:project/search/titles`
63
+ *
64
+ * @param response The response object
65
+ * @return a promise that resolves to the parsed data
66
+ */
67
+ fromResponse : (
68
+ response : Response ,
69
+ ) => Promise < Result < GetLinksResult , LinksError > > ;
70
+ }
71
+
72
+ const getLinks_toRequest : GetLinks [ "toRequest" ] = ( project , options ) => {
73
+ const { sid, hostName, followingId } = setDefaults ( options ?? { } ) ;
74
+
75
+ return new Request (
53
76
`https://${ hostName } /api/pages/${ project } /search/titles${
54
77
followingId ? `?followingId=${ followingId } ` : ""
55
78
} `,
56
79
sid ? { headers : { Cookie : cookie ( sid ) } } : undefined ,
57
80
) ;
81
+ } ;
58
82
59
- const res = await fetch ( req ) ;
60
- if ( isErr ( res ) ) return res ;
61
-
62
- return mapAsyncForResult (
83
+ const getLinks_fromResponse : GetLinks [ "fromResponse" ] = async ( response ) =>
84
+ mapAsyncForResult (
63
85
await mapErrAsyncForResult (
64
- responseIntoResult ( unwrapOk ( res ) ) ,
86
+ responseIntoResult ( response ) ,
65
87
async ( error ) =>
66
88
error . response . status === 422
67
89
? {
@@ -79,8 +101,22 @@ export const getLinks = async (
79
101
followingId : res . headers . get ( "X-following-id" ) ?? "" ,
80
102
} ) ) ,
81
103
) ;
104
+
105
+ /** 指定したprojectのリンクデータを取得する
106
+ *
107
+ * @param project データを取得したいproject
108
+ */
109
+ export const getLinks : GetLinks = async ( project , options ) => {
110
+ const res = await setDefaults ( options ?? { } ) . fetch (
111
+ getLinks_toRequest ( project , options ) ,
112
+ ) ;
113
+ if ( isErr ( res ) ) return res ;
114
+ return getLinks_fromResponse ( unwrapOk ( res ) ) ;
82
115
} ;
83
116
117
+ getLinks . toRequest = getLinks_toRequest ;
118
+ getLinks . fromResponse = getLinks_fromResponse ;
119
+
84
120
/** 指定したprojectの全てのリンクデータを取得する
85
121
*
86
122
* responseで返ってきたリンクデータの塊ごとに返す
0 commit comments