Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 9 additions & 10 deletions rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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に入れる文字列を作る
*
Expand All @@ -15,13 +15,12 @@ export const cookie = (sid: string): string => `connect.sid=${sid}`;
* @param init 認証情報など
*/
export const getCSRFToken = async (
init?: BaseOptions,
): Promise<Result<string, NetworkError | AbortError | HTTPError>> =>
init?: ExtendedOptions,
): Promise<Result<string, NetworkError | AbortError | HTTPError>> => {
// 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,
);
};
9 changes: 2 additions & 7 deletions rest/getTweetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -35,12 +33,9 @@ export const getTweetInfo = async (
url: string | URL,
init?: ExtendedOptions,
): Promise<Result<TweetInfo, TweetInfoError | FetchError>> => {
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(
Expand Down
9 changes: 2 additions & 7 deletions rest/getWebPageTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,12 +32,9 @@ export const getWebPageTitle = async (
url: string | URL,
init?: ExtendedOptions,
): Promise<Result<string, WebPageTitleError | FetchError>> => {
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(
Expand Down
9 changes: 2 additions & 7 deletions rest/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand Down
9 changes: 2 additions & 7 deletions rest/replaceLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -41,12 +39,9 @@ export const replaceLinks = async (
to: string,
init?: ExtendedOptions,
): Promise<Result<number, ReplaceLinksError | FetchError>> => {
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(
Expand Down