From beea15c7f01457e9d1785c4cd8ea76bbaccae6fe Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:10:31 +0900 Subject: [PATCH] refactor(REST API): Define type alias of each API's error for convenient --- browser/websocket/pull.ts | 2 +- rest/auth.ts | 2 +- rest/getCodeBlock.ts | 28 ++++------- rest/getGyazoToken.ts | 13 ++--- rest/getTweetInfo.ts | 22 ++++---- rest/getWebPageTitle.ts | 22 ++++---- rest/link.ts | 42 ++++------------ rest/mod.ts | 11 +++- rest/{util.ts => options.ts} | 0 rest/page-data.ts | 29 ++++++----- rest/pages.ts | 98 +++++++++++------------------------- rest/profile.ts | 10 ++-- rest/project.ts | 55 ++++++++------------ rest/replaceLinks.ts | 22 ++++---- rest/robustFetch.ts | 4 +- rest/search.ts | 35 +++++++------ rest/snapshot.ts | 40 +++++++-------- rest/table.ts | 34 ++++--------- rest/uploadToGCS.ts | 39 ++++++-------- 19 files changed, 201 insertions(+), 307 deletions(-) rename rest/{util.ts => options.ts} (100%) diff --git a/browser/websocket/pull.ts b/browser/websocket/pull.ts index 3982e2c..4c1deb0 100644 --- a/browser/websocket/pull.ts +++ b/browser/websocket/pull.ts @@ -21,7 +21,7 @@ import { getProfile } from "../../rest/profile.ts"; import { getProject } from "../../rest/project.ts"; import type { HTTPError } from "../../rest/responseIntoResult.ts"; import type { AbortError, NetworkError } from "../../rest/robustFetch.ts"; -import type { BaseOptions } from "../../rest/util.ts"; +import type { BaseOptions } from "../../rest/options.ts"; export interface PushMetadata extends Page { projectId: string; diff --git a/rest/auth.ts b/rest/auth.ts index 483f9ad..f01923b 100644 --- a/rest/auth.ts +++ b/rest/auth.ts @@ -2,7 +2,7 @@ import { createOk, mapForResult, type Result } from "option-t/plain_result"; import { getProfile } from "./profile.ts"; import type { HTTPError } from "./responseIntoResult.ts"; import type { AbortError, NetworkError } from "./robustFetch.ts"; -import type { BaseOptions } from "./util.ts"; +import type { BaseOptions } from "./options.ts"; /** HTTP headerのCookieに入れる文字列を作る * diff --git a/rest/getCodeBlock.ts b/rest/getCodeBlock.ts index 03e2146..4a43dfd 100644 --- a/rest/getCodeBlock.ts +++ b/rest/getCodeBlock.ts @@ -5,7 +5,7 @@ import type { } from "@cosense/types/rest"; import { cookie } from "./auth.ts"; import { encodeTitleURI } from "../title.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; import { isErr, mapAsyncForResult, @@ -15,7 +15,7 @@ import { } from "option-t/plain_result"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; +import type { FetchError } from "./mod.ts"; const getCodeBlock_toRequest: GetCodeBlock["toRequest"] = ( project, @@ -70,30 +70,20 @@ export interface GetCodeBlock { * @param res 応答 * @return コード */ - fromResponse: (res: Response) => Promise< - Result< - string, - NotFoundError | NotLoggedInError | NotMemberError | HTTPError - > - >; + fromResponse: (res: Response) => Promise>; ( project: string, title: string, filename: string, options?: BaseOptions, - ): Promise< - Result< - string, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - > - >; + ): Promise>; } +export type CodeBlockError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | HTTPError; /** 指定したコードブロック中のテキストを取得する * diff --git a/rest/getGyazoToken.ts b/rest/getGyazoToken.ts index df05a46..b9bbac2 100644 --- a/rest/getGyazoToken.ts +++ b/rest/getGyazoToken.ts @@ -9,8 +9,8 @@ import type { NotLoggedInError } from "@cosense/types/rest"; import { cookie } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; +import type { FetchError } from "./mod.ts"; export interface GetGyazoTokenOptions extends BaseOptions { /** Gyazo Teamsのチーム名 @@ -20,6 +20,8 @@ export interface GetGyazoTokenOptions extends BaseOptions { gyazoTeamsName?: string; } +export type GyazoTokenError = NotLoggedInError | HTTPError; + /** Gyazo OAuth uploadで使うaccess tokenを取得する * * @param init connect.sidなど @@ -27,12 +29,7 @@ export interface GetGyazoTokenOptions extends BaseOptions { */ export const getGyazoToken = async ( init?: GetGyazoTokenOptions, -): Promise< - Result< - string | undefined, - NotLoggedInError | NetworkError | AbortError | HTTPError - > -> => { +): Promise> => { const { fetch, sid, hostName, gyazoTeamsName } = setDefaults(init ?? {}); const req = new Request( `https://${hostName}/api/login/gyazo/oauth-upload/token${ diff --git a/rest/getTweetInfo.ts b/rest/getTweetInfo.ts index 4962cac..a1f1c5c 100644 --- a/rest/getTweetInfo.ts +++ b/rest/getTweetInfo.ts @@ -16,8 +16,14 @@ import type { import { cookie, getCSRFToken } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type ExtendedOptions, setDefaults } from "./util.ts"; +import { type ExtendedOptions, setDefaults } from "./options.ts"; +import type { FetchError } from "./mod.ts"; + +export type TweetInfoError = + | SessionError + | InvalidURLError + | BadRequestError + | HTTPError; /** 指定したTweetの情報を取得する * @@ -28,17 +34,7 @@ import { type ExtendedOptions, setDefaults } from "./util.ts"; export const getTweetInfo = async ( url: string | URL, init?: ExtendedOptions, -): Promise< - Result< - TweetInfo, - | SessionError - | InvalidURLError - | BadRequestError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); const csrfResult = await orElseAsyncForResult( diff --git a/rest/getWebPageTitle.ts b/rest/getWebPageTitle.ts index 503a943..1d8eee8 100644 --- a/rest/getWebPageTitle.ts +++ b/rest/getWebPageTitle.ts @@ -15,8 +15,14 @@ import type { import { cookie, getCSRFToken } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type ExtendedOptions, setDefaults } from "./util.ts"; +import { type ExtendedOptions, setDefaults } from "./options.ts"; +import type { FetchError } from "./mod.ts"; + +export type WebPageTitleError = + | SessionError + | InvalidURLError + | BadRequestError + | HTTPError; /** 指定したURLのweb pageのtitleをscrapboxのserver経由で取得する * @@ -27,17 +33,7 @@ import { type ExtendedOptions, setDefaults } from "./util.ts"; export const getWebPageTitle = async ( url: string | URL, init?: ExtendedOptions, -): Promise< - Result< - string, - | SessionError - | InvalidURLError - | BadRequestError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); const csrfResult = await orElseAsyncForResult( diff --git a/rest/link.ts b/rest/link.ts index b8fbc24..9797386 100644 --- a/rest/link.ts +++ b/rest/link.ts @@ -15,8 +15,8 @@ import type { import { cookie } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; +import type { FetchError } from "./mod.ts"; /** 不正なfollowingIdを渡されたときに発生するエラー */ export interface InvalidFollowingIdError extends ErrorLike { @@ -33,6 +33,12 @@ export interface GetLinksResult { followingId: string; } +export type LinksError = + | NotFoundError + | NotLoggedInError + | InvalidFollowingIdError + | HTTPError; + /** 指定したprojectのリンクデータを取得する * * @param project データを取得したいproject @@ -40,17 +46,7 @@ export interface GetLinksResult { export const getLinks = async ( project: string, options?: GetLinksOptions, -): Promise< - Result< - GetLinksResult, - | NotFoundError - | NotLoggedInError - | InvalidFollowingIdError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const { sid, hostName, fetch, followingId } = setDefaults(options ?? {}); const req = new Request( @@ -96,15 +92,7 @@ export async function* readLinksBulk( project: string, options?: BaseOptions, ): AsyncGenerator< - Result< - SearchedTitle[], - | NotFoundError - | NotLoggedInError - | InvalidFollowingIdError - | NetworkError - | AbortError - | HTTPError - >, + Result, void, unknown > { @@ -131,15 +119,7 @@ export async function* readLinks( project: string, options?: BaseOptions, ): AsyncGenerator< - Result< - SearchedTitle, - | NotFoundError - | NotLoggedInError - | InvalidFollowingIdError - | NetworkError - | AbortError - | HTTPError - >, + Result, void, unknown > { diff --git a/rest/mod.ts b/rest/mod.ts index 46e6960..5a94d83 100644 --- a/rest/mod.ts +++ b/rest/mod.ts @@ -1,3 +1,8 @@ +/** Cosense REST API wrapper + * + * @module + */ + export * from "./pages.ts"; export * from "./table.ts"; export * from "./project.ts"; @@ -11,9 +16,11 @@ export * from "./getWebPageTitle.ts"; export * from "./getTweetInfo.ts"; export * from "./getGyazoToken.ts"; export * from "./auth.ts"; -export * from "./util.ts"; -export * from "./parseHTTPError.ts"; +export type { BaseOptions, ExtendedOptions } from "./options.ts"; export * from "./getCodeBlocks.ts"; export * from "./getCodeBlock.ts"; export * from "./uploadToGCS.ts"; export * from "./getCachedAt.ts"; + +export type { HTTPError } from "./responseIntoResult.ts"; +export type { AbortError, FetchError, NetworkError } from "./robustFetch.ts"; diff --git a/rest/util.ts b/rest/options.ts similarity index 100% rename from rest/util.ts rename to rest/options.ts diff --git a/rest/page-data.ts b/rest/page-data.ts index cddea9b..6cb24a6 100644 --- a/rest/page-data.ts +++ b/rest/page-data.ts @@ -18,8 +18,15 @@ import type { import { cookie, getCSRFToken } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type BaseOptions, type ExtendedOptions, setDefaults } from "./util.ts"; +import { + type BaseOptions, + type ExtendedOptions, + setDefaults, +} from "./options.ts"; +import type { FetchError } from "./mod.ts"; + +export type ImportPagesError = HTTPError; + /** projectにページをインポートする * * @param project - インポート先のprojectの名前 @@ -30,7 +37,7 @@ export const importPages = async ( data: ImportedData, init: ExtendedOptions, ): Promise< - Result + Result > => { if (data.pages.length === 0) return createOk("No pages to import."); @@ -72,6 +79,12 @@ export const importPages = async ( ); }; +export type ExportPagesError = + | NotFoundError + | NotPrivilegeError + | NotLoggedInError + | HTTPError; + /** `exportPages`の認証情報 */ export interface ExportInit extends BaseOptions { @@ -85,15 +98,7 @@ export const exportPages = async ( project: string, init: ExportInit, ): Promise< - Result< - ExportedData, - | NotFoundError - | NotPrivilegeError - | NotLoggedInError - | NetworkError - | AbortError - | HTTPError - > + Result, ExportPagesError | FetchError> > => { const { sid, hostName, fetch, metadata } = setDefaults(init ?? {}); diff --git a/rest/pages.ts b/rest/pages.ts index 1094851..bed3211 100644 --- a/rest/pages.ts +++ b/rest/pages.ts @@ -9,7 +9,7 @@ import type { import { cookie } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { encodeTitleURI } from "../title.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; import { andThenAsyncForResult, mapAsyncForResult, @@ -18,7 +18,7 @@ import { } from "option-t/plain_result"; import { unwrapOrForMaybe } from "option-t/maybe"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; +import type { FetchError } from "./robustFetch.ts"; /** Options for `getPage()` */ export interface GetPageOption extends BaseOptions { @@ -69,9 +69,7 @@ const getPage_fromResponse: GetPage["fromResponse"] = async (res) => }; } - return unwrapOrForMaybe< - NotFoundError | NotLoggedInError | NotMemberError | HTTPError - >( + return unwrapOrForMaybe( await parseHTTPError(error, [ "NotFoundError", "NotLoggedInError", @@ -101,31 +99,22 @@ export interface GetPage { * @param res 応答 * @return ページのJSONデータ */ - fromResponse: (res: Response) => Promise< - Result< - Page, - | NotFoundError - | NotLoggedInError - | NotMemberError - | TooLongURIError - | HTTPError - > - >; + fromResponse: (res: Response) => Promise>; - (project: string, title: string, options?: GetPageOption): Promise< - Result< - Page, - | NotFoundError - | NotLoggedInError - | NotMemberError - | TooLongURIError - | HTTPError - | NetworkError - | AbortError - > - >; + ( + project: string, + title: string, + options?: GetPageOption, + ): Promise>; } +export type PageError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | TooLongURIError + | HTTPError; + /** 指定したページのJSONデータを取得する * * @param project 取得したいページのproject名 @@ -137,17 +126,7 @@ export const getPage: GetPage = async ( title, options, ) => - andThenAsyncForResult< - Response, - Page, - | NotFoundError - | NotLoggedInError - | NotMemberError - | TooLongURIError - | HTTPError - | NetworkError - | AbortError - >( + andThenAsyncForResult( await setDefaults(options ?? {}).fetch( getPage_toRequest(project, title, options), ), @@ -201,26 +180,20 @@ export interface ListPages { * @param res 応答 * @return ページのJSONデータ */ - fromResponse: (res: Response) => Promise< - Result< - PageList, - NotFoundError | NotLoggedInError | NotMemberError | HTTPError - > - >; + fromResponse: (res: Response) => Promise>; - (project: string, options?: ListPagesOption): Promise< - Result< - PageList, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - > - >; + ( + project: string, + options?: ListPagesOption, + ): Promise>; } +export type ListPagesError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | HTTPError; + const listPages_toRequest: ListPages["toRequest"] = (project, options) => { const { sid, hostName, sort, limit, skip } = setDefaults( options ?? {}, @@ -243,9 +216,7 @@ const listPages_fromResponse: ListPages["fromResponse"] = async (res) => (res) => res.json() as Promise, ), async (error) => - unwrapOrForMaybe< - NotFoundError | NotLoggedInError | NotMemberError | HTTPError - >( + unwrapOrForMaybe( await parseHTTPError(error, [ "NotFoundError", "NotLoggedInError", @@ -264,16 +235,7 @@ export const listPages: ListPages = async ( project, options?, ) => - andThenAsyncForResult< - Response, - PageList, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - >( + andThenAsyncForResult( await setDefaults(options ?? {})?.fetch( listPages_toRequest(project, options), ), diff --git a/rest/profile.ts b/rest/profile.ts index 67e2996..d18b5d6 100644 --- a/rest/profile.ts +++ b/rest/profile.ts @@ -7,8 +7,8 @@ import { import type { GuestUser, MemberUser } from "@cosense/types/rest"; import { cookie } from "./auth.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import type { FetchError } from "./robustFetch.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; export interface GetProfile { /** /api/users/me の要求を組み立てる @@ -26,14 +26,16 @@ export interface GetProfile { fromResponse: ( res: Response, ) => Promise< - Result + Result >; (init?: BaseOptions): Promise< - Result + Result >; } +export type ProfileError = HTTPError; + const getProfile_toRequest: GetProfile["toRequest"] = ( init, ) => { diff --git a/rest/project.ts b/rest/project.ts index cf0feab..b749019 100644 --- a/rest/project.ts +++ b/rest/project.ts @@ -17,8 +17,8 @@ import type { import { cookie } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import type { FetchError } from "./robustFetch.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; export interface GetProject { /** /api/project/:project の要求を組み立てる @@ -37,31 +37,24 @@ export interface GetProject { * @param res 応答 * @return projectのJSONデータ */ - fromResponse: (res: Response) => Promise< - Result< - MemberProject | NotMemberProject, - | NotFoundError - | NotMemberError - | NotLoggedInError - | NetworkError - | AbortError - | HTTPError - > - >; + fromResponse: ( + res: Response, + ) => Promise>; - (project: string, options?: BaseOptions): Promise< - Result< - MemberProject | NotMemberProject, - | NotFoundError - | NotMemberError - | NotLoggedInError - | NetworkError - | AbortError - | HTTPError - > + ( + project: string, + options?: BaseOptions, + ): Promise< + Result >; } +export type ProjectError = + | NotFoundError + | NotMemberError + | NotLoggedInError + | HTTPError; + const getProject_toRequest: GetProject["toRequest"] = (project, init) => { const { sid, hostName } = setDefaults(init ?? {}); @@ -125,24 +118,16 @@ export interface ListProjects { */ fromResponse: ( res: Response, - ) => Promise< - Result< - ProjectResponse, - NotLoggedInError | NetworkError | AbortError | HTTPError - > - >; + ) => Promise>; ( projectIds: ProjectId[], init?: BaseOptions, - ): Promise< - Result< - ProjectResponse, - NotLoggedInError | NetworkError | AbortError | HTTPError - > - >; + ): Promise>; } +export type ListProjectsError = NotLoggedInError | HTTPError; + const ListProject_toRequest: ListProjects["toRequest"] = (projectIds, init) => { const { sid, hostName } = setDefaults(init ?? {}); const param = new URLSearchParams(); diff --git a/rest/replaceLinks.ts b/rest/replaceLinks.ts index 8ff2c36..774c017 100644 --- a/rest/replaceLinks.ts +++ b/rest/replaceLinks.ts @@ -15,8 +15,14 @@ import type { import { cookie, getCSRFToken } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type ExtendedOptions, setDefaults } from "./util.ts"; +import type { FetchError } from "./robustFetch.ts"; +import { type ExtendedOptions, setDefaults } from "./options.ts"; + +export type ReplaceLinksError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | HTTPError; /** 指定したproject内の全てのリンクを書き換える * @@ -34,17 +40,7 @@ export const replaceLinks = async ( from: string, to: string, init?: ExtendedOptions, -): Promise< - Result< - number, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); const csrfResult = await orElseAsyncForResult( diff --git a/rest/robustFetch.ts b/rest/robustFetch.ts index b786343..1785786 100644 --- a/rest/robustFetch.ts +++ b/rest/robustFetch.ts @@ -12,10 +12,12 @@ export interface AbortError { request: Request; } +export type FetchError = NetworkError | AbortError; + export type RobustFetch = ( input: RequestInfo | URL, init?: RequestInit, -) => Promise>; +) => Promise>; /** * Performs a network request using the Fetch API. diff --git a/rest/search.ts b/rest/search.ts index 205ea0e..45d8dbb 100644 --- a/rest/search.ts +++ b/rest/search.ts @@ -16,8 +16,15 @@ import type { import { cookie } from "./auth.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; +import type { FetchError } from "./mod.ts"; + +export type SearchForPagesError = + | NotFoundError + | NotMemberError + | NotLoggedInError + | NoQueryError + | HTTPError; /** search a project for pages * @@ -29,18 +36,7 @@ export const searchForPages = async ( query: string, project: string, init?: BaseOptions, -): Promise< - Result< - SearchResult, - | NotFoundError - | NotMemberError - | NotLoggedInError - | NoQueryError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const { sid, hostName, fetch } = setDefaults(init ?? {}); const req = new Request( @@ -68,6 +64,11 @@ export const searchForPages = async ( ); }; +export type SearchForJoinedProjectsError = + | NotLoggedInError + | NoQueryError + | HTTPError; + /** search for joined projects * * @param query 検索語句 @@ -79,7 +80,7 @@ export const searchForJoinedProjects = async ( ): Promise< Result< ProjectSearchResult, - NotLoggedInError | NoQueryError | NetworkError | AbortError | HTTPError + SearchForJoinedProjectsError | FetchError > > => { const { sid, hostName, fetch } = setDefaults(init ?? {}); @@ -107,6 +108,8 @@ export const searchForJoinedProjects = async ( ); }; +export type SearchForWatchListError = SearchForJoinedProjectsError; + /** search for watch list * * watch listと銘打っているが、実際には参加していないpublic projectならどれでも検索できる @@ -124,7 +127,7 @@ export const searchForWatchList = async ( ): Promise< Result< ProjectSearchResult, - NotLoggedInError | NoQueryError | NetworkError | AbortError | HTTPError + SearchForWatchListError | FetchError > > => { const { sid, hostName, fetch } = setDefaults(init ?? {}); diff --git a/rest/snapshot.ts b/rest/snapshot.ts index 47ced0e..8fdc70c 100644 --- a/rest/snapshot.ts +++ b/rest/snapshot.ts @@ -7,7 +7,7 @@ import type { PageSnapshotResult, } from "@cosense/types/rest"; import { cookie } from "./auth.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; import { isErr, @@ -16,14 +16,21 @@ import { type Result, unwrapOk, } from "option-t/plain_result"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; +import type { FetchError } from "./mod.ts"; /** 不正な`timestampId`を渡されたときに発生するエラー */ export interface InvalidPageSnapshotIdError extends ErrorLike { name: "InvalidPageSnapshotIdError"; } +export type SnapshotError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | InvalidPageSnapshotIdError + | HTTPError; + /** get a page snapshot * * @param options connect.sid etc. @@ -33,18 +40,7 @@ export const getSnapshot = async ( pageId: string, timestampId: string, options?: BaseOptions, -): Promise< - Result< - PageSnapshotResult, - | NotFoundError - | NotLoggedInError - | NotMemberError - | InvalidPageSnapshotIdError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const { sid, hostName, fetch } = setDefaults(options ?? {}); const req = new Request( @@ -74,6 +70,12 @@ export const getSnapshot = async ( ); }; +export type SnapshotTimestampIdsError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | HTTPError; + /** * Retrieves the timestamp IDs for a specific page in a project. * @@ -88,15 +90,7 @@ export const getTimestampIds = async ( pageId: string, options?: BaseOptions, ): Promise< - Result< - PageSnapshotList, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - > + Result > => { const { sid, hostName, fetch } = setDefaults(options ?? {}); diff --git a/rest/table.ts b/rest/table.ts index 91e195f..c34d381 100644 --- a/rest/table.ts +++ b/rest/table.ts @@ -5,7 +5,7 @@ import type { } from "@cosense/types/rest"; import { cookie } from "./auth.ts"; import { encodeTitleURI } from "../title.ts"; -import { type BaseOptions, setDefaults } from "./util.ts"; +import { type BaseOptions, setDefaults } from "./options.ts"; import { isErr, mapAsyncForResult, @@ -15,7 +15,7 @@ import { } from "option-t/plain_result"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; import { parseHTTPError } from "./parseHTTPError.ts"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; +import type { FetchError } from "./mod.ts"; const getTable_toRequest: GetTable["toRequest"] = ( project, @@ -53,6 +53,12 @@ const getTable_fromResponse: GetTable["fromResponse"] = async (res) => (res) => res.text(), ); +export type TableError = + | NotFoundError + | NotLoggedInError + | NotMemberError + | HTTPError; + export interface GetTable { /** /api/table/:project/:title/:filename.csv の要求を組み立てる * @@ -74,34 +80,14 @@ export interface GetTable { * @param res 応答 * @return ページのJSONデータ */ - fromResponse: (res: Response) => Promise< - Result< - string, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - > - >; + fromResponse: (res: Response) => Promise>; ( project: string, title: string, filename: string, options?: BaseOptions, - ): Promise< - Result< - string, - | NotFoundError - | NotLoggedInError - | NotMemberError - | NetworkError - | AbortError - | HTTPError - > - >; + ): Promise>; } /** 指定したテーブルをCSV形式で得る diff --git a/rest/uploadToGCS.ts b/rest/uploadToGCS.ts index 98c287f..77173bc 100644 --- a/rest/uploadToGCS.ts +++ b/rest/uploadToGCS.ts @@ -1,5 +1,9 @@ import { cookie, getCSRFToken } from "./auth.ts"; -import { type BaseOptions, type ExtendedOptions, setDefaults } from "./util.ts"; +import { + type BaseOptions, + type ExtendedOptions, + setDefaults, +} from "./options.ts"; import type { ErrorLike, NotFoundError } from "@cosense/types/rest"; import { Md5 } from "@std/hash"; import { @@ -13,7 +17,7 @@ import { unwrapOk, } from "option-t/plain_result"; import { toResultOkFromMaybe } from "option-t/maybe"; -import type { AbortError, NetworkError } from "./robustFetch.ts"; +import type { FetchError } from "./robustFetch.ts"; import { type HTTPError, responseIntoResult } from "./responseIntoResult.ts"; /** uploadしたファイルのメタデータ */ @@ -25,6 +29,12 @@ export interface GCSFile { originalName: string; } +export type UploadGCSError = + | GCSError + | NotFoundError + | FileCapacityError + | HTTPError; + /** 任意のファイルをscrapbox.ioにuploadする * * @param file uploadしたいファイル @@ -35,17 +45,7 @@ export const uploadToGCS = async ( file: File, projectId: string, options?: ExtendedOptions, -): Promise< - Result< - GCSFile, - | GCSError - | NotFoundError - | FileCapacityError - | NetworkError - | AbortError - | HTTPError - > -> => { +): Promise> => { const md5 = `${new Md5().update(await file.arrayBuffer())}`; const res = await uploadRequest(file, projectId, md5, options); if (isErr(res)) return res; @@ -82,10 +82,7 @@ const uploadRequest = async ( md5: string, init?: ExtendedOptions, ): Promise< - Result< - GCSFile | UploadRequest, - FileCapacityError | NetworkError | AbortError | HTTPError - > + Result > => { const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); const body = { @@ -142,9 +139,7 @@ const upload = async ( signedUrl: string, file: File, init?: BaseOptions, -): Promise< - Result -> => { +): Promise> => { const { sid, fetch } = setDefaults(init ?? {}); const res = await fetch( signedUrl, @@ -180,9 +175,7 @@ const verify = async ( fileId: string, md5: string, init?: ExtendedOptions, -): Promise< - Result -> => { +): Promise> => { const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); const csrfResult = await orElseAsyncForResult( toResultOkFromMaybe(csrf),