Skip to content

feat(REST API): Implement getLinks.toRequest and getLinks.fromResponse #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions rest/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<GetLinksResult, LinksError | FetchError>> => {
const { sid, hostName, fetch, followingId } = setDefaults(options ?? {});
export interface GetLinks {
(
project: string,
options?: GetLinksOptions,
): Promise<Result<GetLinksResult, LinksError | FetchError>>;

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<Result<GetLinksResult, LinksError>>;
}

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
? {
Expand All @@ -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で返ってきたリンクデータの塊ごとに返す
Expand Down