Skip to content

Commit 84c0e66

Browse files
committed
refactor(REST API): Set the default CSRF token in getCSRFToken
1 parent fb1daf5 commit 84c0e66

File tree

5 files changed

+17
-38
lines changed

5 files changed

+17
-38
lines changed

rest/auth.ts

Lines changed: 9 additions & 10 deletions
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 "./options.ts";
5+
import type { ExtendedOptions } from "./options.ts";
66

77
/** HTTP headerのCookieに入れる文字列を作る
88
*
@@ -15,13 +15,12 @@ export const cookie = (sid: string): string => `connect.sid=${sid}`;
1515
* @param init 認証情報など
1616
*/
1717
export const getCSRFToken = async (
18-
init?: BaseOptions,
19-
): Promise<Result<string, NetworkError | AbortError | HTTPError>> =>
18+
init?: ExtendedOptions,
19+
): Promise<Result<string, NetworkError | AbortError | HTTPError>> => {
2020
// deno-lint-ignore no-explicit-any
21-
(globalThis as any)._csrf
22-
// deno-lint-ignore no-explicit-any
23-
? createOk((globalThis as any)._csrf)
24-
: mapForResult(
25-
await getProfile(init),
26-
(user) => user.csrfToken,
27-
);
21+
const csrf = init?.csrf ?? (globalThis as any)._csrf;
22+
return csrf ? createOk(csrf) : mapForResult(
23+
await getProfile(init),
24+
(user) => user.csrfToken,
25+
);
26+
};

rest/getTweetInfo.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import {
22
isErr,
33
mapAsyncForResult,
44
mapErrAsyncForResult,
5-
orElseAsyncForResult,
65
type Result,
76
unwrapOk,
87
} from "option-t/plain_result";
9-
import { toResultOkFromMaybe } from "option-t/maybe";
108
import type {
119
BadRequestError,
1210
InvalidURLError,
@@ -35,12 +33,9 @@ export const getTweetInfo = async (
3533
url: string | URL,
3634
init?: ExtendedOptions,
3735
): Promise<Result<TweetInfo, TweetInfoError | FetchError>> => {
38-
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
36+
const { sid, hostName, fetch } = setDefaults(init ?? {});
3937

40-
const csrfResult = await orElseAsyncForResult(
41-
toResultOkFromMaybe(csrf),
42-
() => getCSRFToken(init),
43-
);
38+
const csrfResult = await getCSRFToken(init);
4439
if (isErr(csrfResult)) return csrfResult;
4540

4641
const req = new Request(

rest/getWebPageTitle.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import {
22
isErr,
33
mapAsyncForResult,
44
mapErrAsyncForResult,
5-
orElseAsyncForResult,
65
type Result,
76
unwrapOk,
87
} from "option-t/plain_result";
9-
import { toResultOkFromMaybe } from "option-t/maybe";
108
import type {
119
BadRequestError,
1210
InvalidURLError,
@@ -34,12 +32,9 @@ export const getWebPageTitle = async (
3432
url: string | URL,
3533
init?: ExtendedOptions,
3634
): Promise<Result<string, WebPageTitleError | FetchError>> => {
37-
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
35+
const { sid, hostName, fetch } = setDefaults(init ?? {});
3836

39-
const csrfResult = await orElseAsyncForResult(
40-
toResultOkFromMaybe(csrf),
41-
() => getCSRFToken(init),
42-
);
37+
const csrfResult = await getCSRFToken(init);
4338
if (isErr(csrfResult)) return csrfResult;
4439

4540
const req = new Request(

rest/page-data.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import {
33
isErr,
44
mapAsyncForResult,
55
mapErrAsyncForResult,
6-
orElseAsyncForResult,
76
type Result,
87
unwrapOk,
98
} from "option-t/plain_result";
10-
import { toResultOkFromMaybe } from "option-t/maybe";
119
import type {
1210
ExportedData,
1311
ImportedData,
@@ -41,7 +39,7 @@ export const importPages = async (
4139
> => {
4240
if (data.pages.length === 0) return createOk("No pages to import.");
4341

44-
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
42+
const { sid, hostName, fetch } = setDefaults(init ?? {});
4543
const formData = new FormData();
4644
formData.append(
4745
"import-file",
@@ -51,10 +49,7 @@ export const importPages = async (
5149
);
5250
formData.append("name", "undefined");
5351

54-
const csrfResult = await orElseAsyncForResult(
55-
toResultOkFromMaybe(csrf),
56-
() => getCSRFToken(init),
57-
);
52+
const csrfResult = await getCSRFToken(init);
5853
if (isErr(csrfResult)) return csrfResult;
5954

6055
const req = new Request(

rest/replaceLinks.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import {
22
isErr,
33
mapAsyncForResult,
44
mapErrAsyncForResult,
5-
orElseAsyncForResult,
65
type Result,
76
unwrapOk,
87
} from "option-t/plain_result";
9-
import { toResultOkFromMaybe } from "option-t/maybe";
108
import type {
119
NotFoundError,
1210
NotLoggedInError,
@@ -41,12 +39,9 @@ export const replaceLinks = async (
4139
to: string,
4240
init?: ExtendedOptions,
4341
): Promise<Result<number, ReplaceLinksError | FetchError>> => {
44-
const { sid, hostName, fetch, csrf } = setDefaults(init ?? {});
42+
const { sid, hostName, fetch } = setDefaults(init ?? {});
4543

46-
const csrfResult = await orElseAsyncForResult(
47-
toResultOkFromMaybe(csrf),
48-
() => getCSRFToken(init),
49-
);
44+
const csrfResult = await getCSRFToken(init);
5045
if (isErr(csrfResult)) return csrfResult;
5146

5247
const req = new Request(

0 commit comments

Comments
 (0)