Skip to content

Commit fb1daf5

Browse files
authored
Merge pull request #187 from takker99/links
2 parents d443c8a + 0e4d6bc commit fb1daf5

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

rest/link.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,51 @@ export type LinksError =
3939
| InvalidFollowingIdError
4040
| HTTPError;
4141

42-
/** 指定したprojectのリンクデータを取得する
42+
/** Get the links of the specified project
4343
*
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
4547
*/
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>>;
5153

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(
5376
`https://${hostName}/api/pages/${project}/search/titles${
5477
followingId ? `?followingId=${followingId}` : ""
5578
}`,
5679
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
5780
);
81+
};
5882

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(
6385
await mapErrAsyncForResult(
64-
responseIntoResult(unwrapOk(res)),
86+
responseIntoResult(response),
6587
async (error) =>
6688
error.response.status === 422
6789
? {
@@ -79,8 +101,22 @@ export const getLinks = async (
79101
followingId: res.headers.get("X-following-id") ?? "",
80102
})),
81103
);
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));
82115
};
83116

117+
getLinks.toRequest = getLinks_toRequest;
118+
getLinks.fromResponse = getLinks_fromResponse;
119+
84120
/** 指定したprojectの全てのリンクデータを取得する
85121
*
86122
* responseで返ってきたリンクデータの塊ごとに返す

0 commit comments

Comments
 (0)