Skip to content

Commit c34e418

Browse files
authored
Merge pull request #181 from takker99:organize-error
refactor(REST API): Define type alias of each API's error for convenient
2 parents 187d6ed + beea15c commit c34e418

19 files changed

+201
-307
lines changed

browser/websocket/pull.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { getProfile } from "../../rest/profile.ts";
2121
import { getProject } from "../../rest/project.ts";
2222
import type { HTTPError } from "../../rest/responseIntoResult.ts";
2323
import type { AbortError, NetworkError } from "../../rest/robustFetch.ts";
24-
import type { BaseOptions } from "../../rest/util.ts";
24+
import type { BaseOptions } from "../../rest/options.ts";
2525

2626
export interface PushMetadata extends Page {
2727
projectId: string;

rest/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createOk, mapForResult, type Result } from "option-t/plain_result";
22
import { getProfile } from "./profile.ts";
33
import type { HTTPError } from "./responseIntoResult.ts";
44
import type { AbortError, NetworkError } from "./robustFetch.ts";
5-
import type { BaseOptions } from "./util.ts";
5+
import type { BaseOptions } from "./options.ts";
66

77
/** HTTP headerのCookieに入れる文字列を作る
88
*

rest/getCodeBlock.ts

+9-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
} from "@cosense/types/rest";
66
import { cookie } from "./auth.ts";
77
import { encodeTitleURI } from "../title.ts";
8-
import { type BaseOptions, setDefaults } from "./util.ts";
8+
import { type BaseOptions, setDefaults } from "./options.ts";
99
import {
1010
isErr,
1111
mapAsyncForResult,
@@ -15,7 +15,7 @@ import {
1515
} from "option-t/plain_result";
1616
import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts";
1717
import { parseHTTPError } from "./parseHTTPError.ts";
18-
import type { AbortError, NetworkError } from "./robustFetch.ts";
18+
import type { FetchError } from "./mod.ts";
1919

2020
const getCodeBlock_toRequest: GetCodeBlock["toRequest"] = (
2121
project,
@@ -70,30 +70,20 @@ export interface GetCodeBlock {
7070
* @param res 応答
7171
* @return コード
7272
*/
73-
fromResponse: (res: Response) => Promise<
74-
Result<
75-
string,
76-
NotFoundError | NotLoggedInError | NotMemberError | HTTPError
77-
>
78-
>;
73+
fromResponse: (res: Response) => Promise<Result<string, CodeBlockError>>;
7974

8075
(
8176
project: string,
8277
title: string,
8378
filename: string,
8479
options?: BaseOptions,
85-
): Promise<
86-
Result<
87-
string,
88-
| NotFoundError
89-
| NotLoggedInError
90-
| NotMemberError
91-
| NetworkError
92-
| AbortError
93-
| HTTPError
94-
>
95-
>;
80+
): Promise<Result<string, CodeBlockError | FetchError>>;
9681
}
82+
export type CodeBlockError =
83+
| NotFoundError
84+
| NotLoggedInError
85+
| NotMemberError
86+
| HTTPError;
9787

9888
/** 指定したコードブロック中のテキストを取得する
9989
*

rest/getGyazoToken.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type { NotLoggedInError } from "@cosense/types/rest";
99
import { cookie } from "./auth.ts";
1010
import { parseHTTPError } from "./parseHTTPError.ts";
1111
import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts";
12-
import type { AbortError, NetworkError } from "./robustFetch.ts";
13-
import { type BaseOptions, setDefaults } from "./util.ts";
12+
import { type BaseOptions, setDefaults } from "./options.ts";
13+
import type { FetchError } from "./mod.ts";
1414

1515
export interface GetGyazoTokenOptions extends BaseOptions {
1616
/** Gyazo Teamsのチーム名
@@ -20,19 +20,16 @@ export interface GetGyazoTokenOptions extends BaseOptions {
2020
gyazoTeamsName?: string;
2121
}
2222

23+
export type GyazoTokenError = NotLoggedInError | HTTPError;
24+
2325
/** Gyazo OAuth uploadで使うaccess tokenを取得する
2426
*
2527
* @param init connect.sidなど
2628
* @return access token
2729
*/
2830
export const getGyazoToken = async (
2931
init?: GetGyazoTokenOptions,
30-
): Promise<
31-
Result<
32-
string | undefined,
33-
NotLoggedInError | NetworkError | AbortError | HTTPError
34-
>
35-
> => {
32+
): Promise<Result<string | undefined, GyazoTokenError | FetchError>> => {
3633
const { fetch, sid, hostName, gyazoTeamsName } = setDefaults(init ?? {});
3734
const req = new Request(
3835
`https://${hostName}/api/login/gyazo/oauth-upload/token${

rest/getTweetInfo.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ import type {
1616
import { cookie, getCSRFToken } from "./auth.ts";
1717
import { parseHTTPError } from "./parseHTTPError.ts";
1818
import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts";
19-
import type { AbortError, NetworkError } from "./robustFetch.ts";
20-
import { type ExtendedOptions, setDefaults } from "./util.ts";
19+
import { type ExtendedOptions, setDefaults } from "./options.ts";
20+
import type { FetchError } from "./mod.ts";
21+
22+
export type TweetInfoError =
23+
| SessionError
24+
| InvalidURLError
25+
| BadRequestError
26+
| HTTPError;
2127

2228
/** 指定したTweetの情報を取得する
2329
*
@@ -28,17 +34,7 @@ import { type ExtendedOptions, setDefaults } from "./util.ts";
2834
export const getTweetInfo = async (
2935
url: string | URL,
3036
init?: ExtendedOptions,
31-
): Promise<
32-
Result<
33-
TweetInfo,
34-
| SessionError
35-
| InvalidURLError
36-
| BadRequestError
37-
| NetworkError
38-
| AbortError
39-
| HTTPError
40-
>
41-
> => {
37+
): Promise<Result<TweetInfo, TweetInfoError | FetchError>> => {
4238
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
4339

4440
const csrfResult = await orElseAsyncForResult(

rest/getWebPageTitle.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ import type {
1515
import { cookie, getCSRFToken } from "./auth.ts";
1616
import { parseHTTPError } from "./parseHTTPError.ts";
1717
import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts";
18-
import type { AbortError, NetworkError } from "./robustFetch.ts";
19-
import { type ExtendedOptions, setDefaults } from "./util.ts";
18+
import { type ExtendedOptions, setDefaults } from "./options.ts";
19+
import type { FetchError } from "./mod.ts";
20+
21+
export type WebPageTitleError =
22+
| SessionError
23+
| InvalidURLError
24+
| BadRequestError
25+
| HTTPError;
2026

2127
/** 指定したURLのweb pageのtitleをscrapboxのserver経由で取得する
2228
*
@@ -27,17 +33,7 @@ import { type ExtendedOptions, setDefaults } from "./util.ts";
2733
export const getWebPageTitle = async (
2834
url: string | URL,
2935
init?: ExtendedOptions,
30-
): Promise<
31-
Result<
32-
string,
33-
| SessionError
34-
| InvalidURLError
35-
| BadRequestError
36-
| NetworkError
37-
| AbortError
38-
| HTTPError
39-
>
40-
> => {
36+
): Promise<Result<string, WebPageTitleError | FetchError>> => {
4137
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
4238

4339
const csrfResult = await orElseAsyncForResult(

rest/link.ts

+11-31
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type {
1515
import { cookie } from "./auth.ts";
1616
import { parseHTTPError } from "./parseHTTPError.ts";
1717
import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts";
18-
import type { AbortError, NetworkError } from "./robustFetch.ts";
19-
import { type BaseOptions, setDefaults } from "./util.ts";
18+
import { type BaseOptions, setDefaults } from "./options.ts";
19+
import type { FetchError } from "./mod.ts";
2020

2121
/** 不正なfollowingIdを渡されたときに発生するエラー */
2222
export interface InvalidFollowingIdError extends ErrorLike {
@@ -33,24 +33,20 @@ export interface GetLinksResult {
3333
followingId: string;
3434
}
3535

36+
export type LinksError =
37+
| NotFoundError
38+
| NotLoggedInError
39+
| InvalidFollowingIdError
40+
| HTTPError;
41+
3642
/** 指定したprojectのリンクデータを取得する
3743
*
3844
* @param project データを取得したいproject
3945
*/
4046
export const getLinks = async (
4147
project: string,
4248
options?: GetLinksOptions,
43-
): Promise<
44-
Result<
45-
GetLinksResult,
46-
| NotFoundError
47-
| NotLoggedInError
48-
| InvalidFollowingIdError
49-
| NetworkError
50-
| AbortError
51-
| HTTPError
52-
>
53-
> => {
49+
): Promise<Result<GetLinksResult, LinksError | FetchError>> => {
5450
const { sid, hostName, fetch, followingId } = setDefaults(options ?? {});
5551

5652
const req = new Request(
@@ -96,15 +92,7 @@ export async function* readLinksBulk(
9692
project: string,
9793
options?: BaseOptions,
9894
): AsyncGenerator<
99-
Result<
100-
SearchedTitle[],
101-
| NotFoundError
102-
| NotLoggedInError
103-
| InvalidFollowingIdError
104-
| NetworkError
105-
| AbortError
106-
| HTTPError
107-
>,
95+
Result<SearchedTitle[], LinksError | FetchError>,
10896
void,
10997
unknown
11098
> {
@@ -131,15 +119,7 @@ export async function* readLinks(
131119
project: string,
132120
options?: BaseOptions,
133121
): AsyncGenerator<
134-
Result<
135-
SearchedTitle,
136-
| NotFoundError
137-
| NotLoggedInError
138-
| InvalidFollowingIdError
139-
| NetworkError
140-
| AbortError
141-
| HTTPError
142-
>,
122+
Result<SearchedTitle, LinksError | FetchError>,
143123
void,
144124
unknown
145125
> {

rest/mod.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/** Cosense REST API wrapper
2+
*
3+
* @module
4+
*/
5+
16
export * from "./pages.ts";
27
export * from "./table.ts";
38
export * from "./project.ts";
@@ -11,9 +16,11 @@ export * from "./getWebPageTitle.ts";
1116
export * from "./getTweetInfo.ts";
1217
export * from "./getGyazoToken.ts";
1318
export * from "./auth.ts";
14-
export * from "./util.ts";
15-
export * from "./parseHTTPError.ts";
19+
export type { BaseOptions, ExtendedOptions } from "./options.ts";
1620
export * from "./getCodeBlocks.ts";
1721
export * from "./getCodeBlock.ts";
1822
export * from "./uploadToGCS.ts";
1923
export * from "./getCachedAt.ts";
24+
25+
export type { HTTPError } from "./responseIntoResult.ts";
26+
export type { AbortError, FetchError, NetworkError } from "./robustFetch.ts";
File renamed without changes.

rest/page-data.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ import type {
1818
import { cookie, getCSRFToken } from "./auth.ts";
1919
import { parseHTTPError } from "./parseHTTPError.ts";
2020
import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts";
21-
import type { AbortError, NetworkError } from "./robustFetch.ts";
22-
import { type BaseOptions, type ExtendedOptions, setDefaults } from "./util.ts";
21+
import {
22+
type BaseOptions,
23+
type ExtendedOptions,
24+
setDefaults,
25+
} from "./options.ts";
26+
import type { FetchError } from "./mod.ts";
27+
28+
export type ImportPagesError = HTTPError;
29+
2330
/** projectにページをインポートする
2431
*
2532
* @param project - インポート先のprojectの名前
@@ -30,7 +37,7 @@ export const importPages = async (
3037
data: ImportedData<boolean>,
3138
init: ExtendedOptions,
3239
): Promise<
33-
Result<string, NetworkError | AbortError | HTTPError>
40+
Result<string, ImportPagesError | FetchError>
3441
> => {
3542
if (data.pages.length === 0) return createOk("No pages to import.");
3643

@@ -72,6 +79,12 @@ export const importPages = async (
7279
);
7380
};
7481

82+
export type ExportPagesError =
83+
| NotFoundError
84+
| NotPrivilegeError
85+
| NotLoggedInError
86+
| HTTPError;
87+
7588
/** `exportPages`の認証情報 */
7689
export interface ExportInit<withMetadata extends true | false>
7790
extends BaseOptions {
@@ -85,15 +98,7 @@ export const exportPages = async <withMetadata extends true | false>(
8598
project: string,
8699
init: ExportInit<withMetadata>,
87100
): Promise<
88-
Result<
89-
ExportedData<withMetadata>,
90-
| NotFoundError
91-
| NotPrivilegeError
92-
| NotLoggedInError
93-
| NetworkError
94-
| AbortError
95-
| HTTPError
96-
>
101+
Result<ExportedData<withMetadata>, ExportPagesError | FetchError>
97102
> => {
98103
const { sid, hostName, fetch, metadata } = setDefaults(init ?? {});
99104

0 commit comments

Comments
 (0)