Skip to content

Commit e9e80f7

Browse files
authored
Merge pull request #34 from takker99/fix-link
👍 /api/pages/:prjectname/search/titles のエラー定義を詳しくした
2 parents b240f55 + 24823ae commit e9e80f7

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

rest/link.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import type { ErrorLike, SearchedTitle } from "../deps/scrapbox.ts";
1+
import type {
2+
ErrorLike,
3+
NotFoundError,
4+
NotLoggedInError,
5+
SearchedTitle,
6+
} from "../deps/scrapbox.ts";
27
import { cookie } from "./auth.ts";
38
import { UnexpectedResponseError } from "./error.ts";
49
import { tryToErrorLike } from "../is.ts";
510
import { BaseOptions, Result, setDefaults } from "./util.ts";
611

12+
/** 不正なfollowingIdを渡されたときに発生するエラー */
13+
export interface InvalidFollowingIdError extends ErrorLike {
14+
name: "InvalidFollowingIdError";
15+
}
16+
717
export interface GetLinksOptions extends BaseOptions {
818
/** 次のリンクリストを示すID */
919
followingId?: string;
@@ -20,7 +30,7 @@ export const getLinks = async (
2030
Result<{
2131
pages: SearchedTitle[];
2232
followingId: string;
23-
}, ErrorLike>
33+
}, NotFoundError | NotLoggedInError | InvalidFollowingIdError>
2434
> => {
2535
const { sid, hostName, fetch, followingId } = setDefaults(options ?? {});
2636
const path = `https://${hostName}/api/pages/${project}/search/titles${
@@ -33,6 +43,15 @@ export const getLinks = async (
3343
);
3444

3545
if (!res.ok) {
46+
if (res.status === 422) {
47+
return {
48+
ok: false,
49+
value: {
50+
name: "InvalidFollowingIdError",
51+
message: await res.text(),
52+
},
53+
};
54+
}
3655
const text = await res.text();
3756
const value = tryToErrorLike(text);
3857
if (!value) {
@@ -42,7 +61,7 @@ export const getLinks = async (
4261
body: text,
4362
});
4463
}
45-
return { ok: false, value };
64+
return { ok: false, value: value as NotFoundError | NotLoggedInError };
4665
}
4766
const pages = (await res.json()) as SearchedTitle[];
4867
return {
@@ -61,7 +80,12 @@ export const getLinks = async (
6180
export const readLinksBulk = async (
6281
project: string,
6382
options?: BaseOptions,
64-
): Promise<ErrorLike | AsyncGenerator<SearchedTitle[], void, unknown>> => {
83+
): Promise<
84+
| NotFoundError
85+
| NotLoggedInError
86+
| InvalidFollowingIdError
87+
| AsyncGenerator<SearchedTitle[], void, unknown>
88+
> => {
6589
const first = await getLinks(project, options);
6690
if (!first.ok) return first.value;
6791

@@ -90,7 +114,12 @@ export const readLinksBulk = async (
90114
export const readLinks = async (
91115
project: string,
92116
options?: BaseOptions,
93-
): Promise<ErrorLike | AsyncGenerator<SearchedTitle, void, unknown>> => {
117+
): Promise<
118+
| NotFoundError
119+
| NotLoggedInError
120+
| InvalidFollowingIdError
121+
| AsyncGenerator<SearchedTitle, void, unknown>
122+
> => {
94123
const reader = await readLinksBulk(project, options);
95124
if ("name" in reader) return reader;
96125

0 commit comments

Comments
 (0)