Skip to content
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
39 changes: 34 additions & 5 deletions rest/link.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import type { ErrorLike, SearchedTitle } from "../deps/scrapbox.ts";
import type {
ErrorLike,
NotFoundError,
NotLoggedInError,
SearchedTitle,
} from "../deps/scrapbox.ts";
import { cookie } from "./auth.ts";
import { UnexpectedResponseError } from "./error.ts";
import { tryToErrorLike } from "../is.ts";
import { BaseOptions, Result, setDefaults } from "./util.ts";

/** 不正なfollowingIdを渡されたときに発生するエラー */
export interface InvalidFollowingIdError extends ErrorLike {
name: "InvalidFollowingIdError";
}

export interface GetLinksOptions extends BaseOptions {
/** 次のリンクリストを示すID */
followingId?: string;
Expand All @@ -20,7 +30,7 @@ export const getLinks = async (
Result<{
pages: SearchedTitle[];
followingId: string;
}, ErrorLike>
}, NotFoundError | NotLoggedInError | InvalidFollowingIdError>
> => {
const { sid, hostName, fetch, followingId } = setDefaults(options ?? {});
const path = `https://${hostName}/api/pages/${project}/search/titles${
Expand All @@ -33,6 +43,15 @@ export const getLinks = async (
);

if (!res.ok) {
if (res.status === 422) {
return {
ok: false,
value: {
name: "InvalidFollowingIdError",
message: await res.text(),
},
};
}
const text = await res.text();
const value = tryToErrorLike(text);
if (!value) {
Expand All @@ -42,7 +61,7 @@ export const getLinks = async (
body: text,
});
}
return { ok: false, value };
return { ok: false, value: value as NotFoundError | NotLoggedInError };
}
const pages = (await res.json()) as SearchedTitle[];
return {
Expand All @@ -61,7 +80,12 @@ export const getLinks = async (
export const readLinksBulk = async (
project: string,
options?: BaseOptions,
): Promise<ErrorLike | AsyncGenerator<SearchedTitle[], void, unknown>> => {
): Promise<
| NotFoundError
| NotLoggedInError
| InvalidFollowingIdError
| AsyncGenerator<SearchedTitle[], void, unknown>
> => {
const first = await getLinks(project, options);
if (!first.ok) return first.value;

Expand Down Expand Up @@ -90,7 +114,12 @@ export const readLinksBulk = async (
export const readLinks = async (
project: string,
options?: BaseOptions,
): Promise<ErrorLike | AsyncGenerator<SearchedTitle, void, unknown>> => {
): Promise<
| NotFoundError
| NotLoggedInError
| InvalidFollowingIdError
| AsyncGenerator<SearchedTitle, void, unknown>
> => {
const reader = await readLinksBulk(project, options);
if ("name" in reader) return reader;

Expand Down