From 84c0e6697ad05cec948aac1aa77981ee7ced0a6d Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:03:05 +0900 Subject: [PATCH] refactor(REST API): Set the default CSRF token in `getCSRFToken` --- rest/auth.ts | 19 +++++++++---------- rest/getTweetInfo.ts | 9 ++------- rest/getWebPageTitle.ts | 9 ++------- rest/page-data.ts | 9 ++------- rest/replaceLinks.ts | 9 ++------- 5 files changed, 17 insertions(+), 38 deletions(-) diff --git a/rest/auth.ts b/rest/auth.ts index f01923b..65df74c 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 "./options.ts"; +import type { ExtendedOptions } from "./options.ts"; /** HTTP headerのCookieに入れる文字列を作る * @@ -15,13 +15,12 @@ export const cookie = (sid: string): string => `connect.sid=${sid}`; * @param init 認証情報など */ export const getCSRFToken = async ( - init?: BaseOptions, -): Promise> => + init?: ExtendedOptions, +): Promise> => { // deno-lint-ignore no-explicit-any - (globalThis as any)._csrf - // deno-lint-ignore no-explicit-any - ? createOk((globalThis as any)._csrf) - : mapForResult( - await getProfile(init), - (user) => user.csrfToken, - ); + const csrf = init?.csrf ?? (globalThis as any)._csrf; + return csrf ? createOk(csrf) : mapForResult( + await getProfile(init), + (user) => user.csrfToken, + ); +}; diff --git a/rest/getTweetInfo.ts b/rest/getTweetInfo.ts index a1f1c5c..48408d9 100644 --- a/rest/getTweetInfo.ts +++ b/rest/getTweetInfo.ts @@ -2,11 +2,9 @@ import { isErr, mapAsyncForResult, mapErrAsyncForResult, - orElseAsyncForResult, type Result, unwrapOk, } from "option-t/plain_result"; -import { toResultOkFromMaybe } from "option-t/maybe"; import type { BadRequestError, InvalidURLError, @@ -35,12 +33,9 @@ export const getTweetInfo = async ( url: string | URL, init?: ExtendedOptions, ): Promise> => { - const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); + const { sid, hostName, fetch } = setDefaults(init ?? {}); - const csrfResult = await orElseAsyncForResult( - toResultOkFromMaybe(csrf), - () => getCSRFToken(init), - ); + const csrfResult = await getCSRFToken(init); if (isErr(csrfResult)) return csrfResult; const req = new Request( diff --git a/rest/getWebPageTitle.ts b/rest/getWebPageTitle.ts index 1d8eee8..c523aff 100644 --- a/rest/getWebPageTitle.ts +++ b/rest/getWebPageTitle.ts @@ -2,11 +2,9 @@ import { isErr, mapAsyncForResult, mapErrAsyncForResult, - orElseAsyncForResult, type Result, unwrapOk, } from "option-t/plain_result"; -import { toResultOkFromMaybe } from "option-t/maybe"; import type { BadRequestError, InvalidURLError, @@ -34,12 +32,9 @@ export const getWebPageTitle = async ( url: string | URL, init?: ExtendedOptions, ): Promise> => { - const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); + const { sid, hostName, fetch } = setDefaults(init ?? {}); - const csrfResult = await orElseAsyncForResult( - toResultOkFromMaybe(csrf), - () => getCSRFToken(init), - ); + const csrfResult = await getCSRFToken(init); if (isErr(csrfResult)) return csrfResult; const req = new Request( diff --git a/rest/page-data.ts b/rest/page-data.ts index 6cb24a6..84d9850 100644 --- a/rest/page-data.ts +++ b/rest/page-data.ts @@ -3,11 +3,9 @@ import { isErr, mapAsyncForResult, mapErrAsyncForResult, - orElseAsyncForResult, type Result, unwrapOk, } from "option-t/plain_result"; -import { toResultOkFromMaybe } from "option-t/maybe"; import type { ExportedData, ImportedData, @@ -41,7 +39,7 @@ export const importPages = async ( > => { if (data.pages.length === 0) return createOk("No pages to import."); - const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); + const { sid, hostName, fetch } = setDefaults(init ?? {}); const formData = new FormData(); formData.append( "import-file", @@ -51,10 +49,7 @@ export const importPages = async ( ); formData.append("name", "undefined"); - const csrfResult = await orElseAsyncForResult( - toResultOkFromMaybe(csrf), - () => getCSRFToken(init), - ); + const csrfResult = await getCSRFToken(init); if (isErr(csrfResult)) return csrfResult; const req = new Request( diff --git a/rest/replaceLinks.ts b/rest/replaceLinks.ts index 774c017..b89e43e 100644 --- a/rest/replaceLinks.ts +++ b/rest/replaceLinks.ts @@ -2,11 +2,9 @@ import { isErr, mapAsyncForResult, mapErrAsyncForResult, - orElseAsyncForResult, type Result, unwrapOk, } from "option-t/plain_result"; -import { toResultOkFromMaybe } from "option-t/maybe"; import type { NotFoundError, NotLoggedInError, @@ -41,12 +39,9 @@ export const replaceLinks = async ( to: string, init?: ExtendedOptions, ): Promise> => { - const { sid, hostName, fetch, csrf } = setDefaults(init ?? {}); + const { sid, hostName, fetch } = setDefaults(init ?? {}); - const csrfResult = await orElseAsyncForResult( - toResultOkFromMaybe(csrf), - () => getCSRFToken(init), - ); + const csrfResult = await getCSRFToken(init); if (isErr(csrfResult)) return csrfResult; const req = new Request(