Skip to content

Commit 34c453b

Browse files
authored
Merge pull request #847 from Telegram-Mini-Apps/feature/simplify-fp-ts-usage
refactor(all-packages): simplify usage of fp-ts
2 parents 2e1997c + 805c0fa commit 34c453b

File tree

105 files changed

+298
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+298
-366
lines changed

.changeset/loud-pianos-accept.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@tma.js/init-data-node": patch
3+
"@tma.js/transformers": patch
4+
"@tma.js/toolkit": patch
5+
"@tma.js/bridge": patch
6+
"@tma.js/sdk": patch
7+
---
8+
9+
Simplify usage of fp-ts to potentially avoid building problems in some applications.

packages/bridge/src/base64-url.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { throwifyFpFn } from '@tma.js/toolkit';
2-
import * as E from 'fp-ts/Either';
2+
import { either as E } from 'fp-ts';
33

44
export type DecodeBase64UrlError = DOMException;
55

packages/bridge/src/env/isTMA.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { TimeoutError } from 'better-promises';
2-
import * as TE from 'fp-ts/TaskEither';
3-
import { pipe } from 'fp-ts/function';
2+
import { taskEither as TE, function as fn } from 'fp-ts';
43
import {
54
createWindow,
65
mockSessionStorageGetItem,
@@ -79,7 +78,7 @@ describe('isTMAFp', () => {
7978
describe('complete', () => {
8079
it('should return true if current window contains TelegramWebviewProxy property', async () => {
8180
createWindow({ TelegramWebviewProxy: { postEvent: () => undefined } } as any);
82-
await pipe(
81+
await fn.pipe(
8382
isTMAFp('complete'),
8483
TE.match(
8584
() => expect.unreachable(),
@@ -94,7 +93,7 @@ describe('isTMAFp', () => {
9493
async () => {
9594
createWindow();
9695
requestFp.mockImplementationOnce(() => TE.right({}));
97-
await pipe(
96+
await fn.pipe(
9897
isTMAFp('complete'),
9998
TE.match(
10099
() => expect.unreachable(),
@@ -110,7 +109,7 @@ describe('isTMAFp', () => {
110109
async () => {
111110
createWindow();
112111
requestFp.mockImplementationOnce(() => TE.left(new TimeoutError(100)));
113-
await pipe(
112+
await fn.pipe(
114113
isTMAFp('complete'),
115114
TE.match(
116115
() => expect.unreachable(),
@@ -120,7 +119,7 @@ describe('isTMAFp', () => {
120119
expect.assertions(1);
121120

122121
requestFp.mockImplementationOnce(() => TE.left(new Error('something else')));
123-
await pipe(
122+
await fn.pipe(
124123
isTMAFp('complete'),
125124
TE.match(
126125
error => expect(error).toStrictEqual(new Error('something else')),
@@ -134,7 +133,7 @@ describe('isTMAFp', () => {
134133
it('should return false if the env is unknown', async () => {
135134
mockWindow({} as any);
136135
requestFp.mockImplementationOnce(() => TE.left(new UnknownEnvError()));
137-
await pipe(
136+
await fn.pipe(
138137
isTMAFp('complete'),
139138
TE.match(
140139
() => expect.unreachable(),

packages/bridge/src/env/isTMA.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import {
44
type BetterPromiseOptions,
55
TimeoutError,
66
} from 'better-promises';
7-
import * as E from 'fp-ts/Either';
8-
import * as TE from 'fp-ts/TaskEither';
9-
import { pipe } from 'fp-ts/function';
7+
import { either as E, taskEither as TE, function as fn } from 'fp-ts';
108

119
import { hasWebviewProxy } from '@/env/hasWebviewProxy.js';
1210
import { UnknownEnvError } from '@/errors.js';
@@ -31,9 +29,9 @@ export function isTMA(
3129
// @ts-expect-error TS doesn't get what override we are going to use.
3230
type,
3331
options,
34-
) as boolean | TE.TaskEither<isTMAError, boolean>;
32+
);
3533
return typeof monad === 'function'
36-
? BetterPromise.fn(() => throwifyAnyEither(monad))
34+
? BetterPromise.fn(() => throwifyAnyEither(monad as TE.TaskEither<any, boolean>))
3735
: monad;
3836
}
3937

@@ -65,14 +63,14 @@ export function isTMAFp(
6563
): boolean | TE.TaskEither<isTMAError, boolean> {
6664
const hasProxy = hasWebviewProxy(window);
6765
if (!type) {
68-
return hasProxy || pipe(retrieveRawLaunchParamsFp(), E.match(() => false, () => true));
66+
return hasProxy || fn.pipe(retrieveRawLaunchParamsFp(), E.match(() => false, () => true));
6967
}
7068
if (hasProxy) {
7169
return TE.right(true);
7270
}
7371
const { timeout = 100 } = options || {};
7472

75-
return pipe(
73+
return fn.pipe(
7674
request2Fp('web_app_request_theme', 'theme_changed', { ...options, timeout }),
7775
TE.match(
7876
error => (

packages/bridge/src/launch-params.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as E from 'fp-ts/Either';
2-
import * as O from 'fp-ts/Option';
1+
import { either as E, option as O } from 'fp-ts';
32
import { afterEach, describe, expect, it, vi } from 'vitest';
43

54
import {

packages/bridge/src/launch-params.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import {
44
type LaunchParamsGenType,
55
type ParseLaunchParamsQueryError,
66
} from '@tma.js/transformers';
7-
import * as E from 'fp-ts/Either';
8-
import * as O from 'fp-ts/Option';
9-
import { pipe } from 'fp-ts/function';
7+
import { either as E, function as fn, option as O } from 'fp-ts';
108

119
import { LaunchParamsRetrieveError } from '@/errors.js';
1210

@@ -39,7 +37,7 @@ export function retrieveLaunchParamsFp(): E.Either<
3937
RetrieveLaunchParamsError,
4038
RetrieveLaunchParamsResult
4139
> {
42-
return pipe(
40+
return fn.pipe(
4341
retrieveRawLaunchParamsFp(),
4442
E.chainW(parseLaunchParamsQueryFp),
4543
);
@@ -55,7 +53,7 @@ export const retrieveLaunchParams: () => RetrieveLaunchParamsResult =
5553
* @returns Raw init data from any known source.
5654
*/
5755
export function retrieveRawInitDataFp(): E.Either<RetrieveRawInitDataError, O.Option<string>> {
58-
return pipe(
56+
return fn.pipe(
5957
retrieveRawLaunchParamsFp(),
6058
E.map(raw => {
6159
const v = new URLSearchParams(raw).get('tgWebAppData');
@@ -68,7 +66,7 @@ export function retrieveRawInitDataFp(): E.Either<RetrieveRawInitDataError, O.Op
6866
* @see retrieveRawInitDataFp
6967
*/
7068
export function retrieveRawInitData(): string | undefined {
71-
return pipe(
69+
return fn.pipe(
7270
retrieveRawInitDataFp(),
7371
E.fold(err => {
7472
throw err;
@@ -101,7 +99,7 @@ export function retrieveRawLaunchParamsFp(): E.Either<RetrieveRawLaunchParamsErr
10199
errors.push({ source, error: new Error('Source is empty') });
102100
continue;
103101
}
104-
const maybeError = pipe(
102+
const maybeError = fn.pipe(
105103
parseLaunchParamsQueryFp(v),
106104
E.foldW(err => err, () => true as const),
107105
);

packages/bridge/src/methods/postEvent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as E from 'fp-ts/Either';
1+
import { either as E } from 'fp-ts';
22
import { createWindow } from 'test-utils';
33
import {
44
expect,

packages/bridge/src/methods/postEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as E from 'fp-ts/Either';
2-
import { pipe } from 'fp-ts/function';
1+
import { either as E, function as fpFn } from 'fp-ts';
32
import { function as fn, is, looseObject } from 'valibot';
43

54
import { hasWebviewProxy } from '@/env/hasWebviewProxy.js';
@@ -43,7 +42,7 @@ export function postEvent(
4342
eventType: MethodName,
4443
eventData?: MethodParams<MethodName>,
4544
): void {
46-
pipe(
45+
fpFn.pipe(
4746
postEventFp(
4847
// @ts-expect-error It's ok, TS can't determine a specific override.
4948
eventType,

packages/bridge/src/start-param.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as E from 'fp-ts/Either';
1+
import { either as E } from 'fp-ts';
22
import { describe, expect, it } from 'vitest';
33

44
import {

packages/bridge/src/start-param.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { throwifyFpFn, throwifyAnyEither } from '@tma.js/toolkit';
2-
import * as E from 'fp-ts/Either';
3-
import * as J from 'fp-ts/Json';
4-
import { pipe } from 'fp-ts/function';
2+
import { either as E, function as fn, json as J } from 'fp-ts';
53

64
import { encodeBase64Url, type DecodeBase64UrlError, decodeBase64UrlFp } from '@/base64-url.js';
75

@@ -80,7 +78,7 @@ export function decodeStartParamFp<L, R>(
8078
value: string,
8179
arg2?: 'json' | ((value: string) => E.Either<L, R>),
8280
): E.Either<DecodeBase64UrlError | SyntaxError | L, R | string | J.Json> {
83-
return pipe(
81+
return fn.pipe(
8482
decodeBase64UrlFp(value),
8583
E.chain<DecodeBase64UrlError | SyntaxError | L, string, R | string | J.Json>(decoded => {
8684
if (!arg2) {

0 commit comments

Comments
 (0)