From 0e4d6bc3539ec29f4c6e6abd4c7ab95ade6bcc4a Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 8 Aug 2024 08:52:57 +0900 Subject: [PATCH] feat(REST API): Implement `getLinks.toRequest` and `getLinks.fromResponse` --- rest/link.ts | 62 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/rest/link.ts b/rest/link.ts index 9797386..369da87 100644 --- a/rest/link.ts +++ b/rest/link.ts @@ -39,29 +39,51 @@ export type LinksError = | InvalidFollowingIdError | HTTPError; -/** 指定したprojectのリンクデータを取得する +/** Get the links of the specified project * - * @param project データを取得したいproject + * @param project The project to get the data from + * @param options Options for the request + * @return a promise that resolves to the parsed data */ -export const getLinks = async ( - project: string, - options?: GetLinksOptions, -): Promise> => { - const { sid, hostName, fetch, followingId } = setDefaults(options ?? {}); +export interface GetLinks { + ( + project: string, + options?: GetLinksOptions, + ): Promise>; - const req = new Request( + /** Create a request to `GET /api/pages/:project/search/titles` + * + * @param project The project to get the data from + * @param options Options for the request + * @return The request object + */ + toRequest: (project: string, options?: GetLinksOptions) => Request; + + /** Parse the response from `GET /api/pages/:project/search/titles` + * + * @param response The response object + * @return a promise that resolves to the parsed data + */ + fromResponse: ( + response: Response, + ) => Promise>; +} + +const getLinks_toRequest: GetLinks["toRequest"] = (project, options) => { + const { sid, hostName, followingId } = setDefaults(options ?? {}); + + return new Request( `https://${hostName}/api/pages/${project}/search/titles${ followingId ? `?followingId=${followingId}` : "" }`, sid ? { headers: { Cookie: cookie(sid) } } : undefined, ); +}; - const res = await fetch(req); - if (isErr(res)) return res; - - return mapAsyncForResult( +const getLinks_fromResponse: GetLinks["fromResponse"] = async (response) => + mapAsyncForResult( await mapErrAsyncForResult( - responseIntoResult(unwrapOk(res)), + responseIntoResult(response), async (error) => error.response.status === 422 ? { @@ -79,8 +101,22 @@ export const getLinks = async ( followingId: res.headers.get("X-following-id") ?? "", })), ); + +/** 指定したprojectのリンクデータを取得する + * + * @param project データを取得したいproject + */ +export const getLinks: GetLinks = async (project, options) => { + const res = await setDefaults(options ?? {}).fetch( + getLinks_toRequest(project, options), + ); + if (isErr(res)) return res; + return getLinks_fromResponse(unwrapOk(res)); }; +getLinks.toRequest = getLinks_toRequest; +getLinks.fromResponse = getLinks_fromResponse; + /** 指定したprojectの全てのリンクデータを取得する * * responseで返ってきたリンクデータの塊ごとに返す