Skip to content

Commit b2e371c

Browse files
refactor: SendTelegramResponse conditional typing
1 parent d56cd36 commit b2e371c

File tree

4 files changed

+71
-45
lines changed

4 files changed

+71
-45
lines changed

src/web3telegram/IExecWeb3telegram.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { AbstractProvider, AbstractSigner, Eip1193Provider } from 'ethers';
22
import { IExec } from 'iexec';
3-
import {
4-
IExecDataProtectorCore,
5-
ProcessBulkRequestResponse,
6-
ProcessBulkRequestParams,
7-
} from '@iexec/dataprotector';
3+
import { IExecDataProtectorCore } from '@iexec/dataprotector';
84
import { GraphQLClient } from 'graphql-request';
95
import { fetchUserContacts } from './fetchUserContacts.js';
106
import { fetchMyContacts } from './fetchMyContacts.js';
@@ -17,7 +13,7 @@ import {
1713
Web3TelegramConfigOptions,
1814
Web3SignerProvider,
1915
FetchMyContactsParams,
20-
SendTelegramSingleResponse,
16+
SendTelegramResponse,
2117
} from './types.js';
2218
import { getChainDefaultConfig } from '../config/config.js';
2319
import { isValidProvider } from '../utils/validators.js';
@@ -114,12 +110,9 @@ export class IExecWeb3telegram {
114110
});
115111
}
116112

117-
async sendTelegram(
118-
args: SendTelegramParams
119-
): Promise<
120-
| ProcessBulkRequestResponse<ProcessBulkRequestParams>
121-
| SendTelegramSingleResponse
122-
> {
113+
async sendTelegram<Params extends SendTelegramParams>(
114+
args: Params
115+
): Promise<SendTelegramResponse<Params>> {
123116
await this.init();
124117
await isValidProvider(this.iexec);
125118
return sendTelegram({
@@ -133,11 +126,11 @@ export class IExecWeb3telegram {
133126
dappAddressOrENS: this.dappAddressOrENS,
134127
dappWhitelistAddress: this.dappWhitelistAddress,
135128
graphQLClient: this.graphQLClient,
136-
});
129+
} as any) as Promise<SendTelegramResponse<Params>>;
137130
}
138131

139132
private async resolveConfig(): Promise<Web3telegramResolvedConfig> {
140-
const chainId = await getChainIdFromProvider(this.ethProvider as any);
133+
const chainId = await getChainIdFromProvider(this.ethProvider);
141134
const chainDefaultConfig = getChainDefaultConfig(chainId, {
142135
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
143136
});
@@ -149,7 +142,7 @@ export class IExecWeb3telegram {
149142

150143
try {
151144
iexec = new IExec(
152-
{ ethProvider: this.ethProvider as any },
145+
{ ethProvider: this.ethProvider },
153146
{
154147
ipfsGatewayURL: ipfsGateway,
155148
...this.options?.iexecOptions,

src/web3telegram/sendTelegram.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import { Buffer } from 'buffer';
2-
import {
3-
ProcessBulkRequestResponse,
4-
ProcessBulkRequestParams,
5-
} from '@iexec/dataprotector';
62
import {
73
MAX_DESIRED_APP_ORDER_PRICE,
84
MAX_DESIRED_DATA_ORDER_PRICE,
@@ -20,7 +16,12 @@ import {
2016
senderNameSchema,
2117
booleanSchema,
2218
} from '../utils/validators.js';
23-
import { SendTelegramParams, SendTelegramSingleResponse } from './types.js';
19+
import {
20+
Address,
21+
GrantedAccess,
22+
SendTelegramParams,
23+
SendTelegramResponse,
24+
} from './types.js';
2425
import {
2526
DappAddressConsumer,
2627
DappWhitelistAddressConsumer,
@@ -33,7 +34,7 @@ import {
3334

3435
export type SendTelegram = typeof sendTelegram;
3536

36-
export const sendTelegram = async ({
37+
export const sendTelegram = async <Params extends SendTelegramParams>({
3738
graphQLClient = throwIfMissing(),
3839
iexec = throwIfMissing(),
3940
dataProtector = throwIfMissing(),
@@ -57,11 +58,12 @@ export const sendTelegram = async ({
5758
DappWhitelistAddressConsumer &
5859
IpfsNodeConfigConsumer &
5960
IpfsGatewayConfigConsumer &
60-
SendTelegramParams &
61-
DataProtectorConsumer): Promise<
62-
| ProcessBulkRequestResponse<ProcessBulkRequestParams>
63-
| SendTelegramSingleResponse
64-
> => {
61+
DataProtectorConsumer &
62+
Params & {
63+
protectedData?: Address;
64+
grantedAccess?: GrantedAccess[];
65+
maxProtectedDataPerTask?: number;
66+
}): Promise<SendTelegramResponse<Params>> => {
6567
try {
6668
const vUseVoucher = booleanSchema()
6769
.label('useVoucher')
@@ -154,13 +156,20 @@ export const sendTelegram = async ({
154156
bulkAccesses: grantedAccess,
155157
maxProtectedDataPerTask: vMaxProtectedDataPerTask,
156158
});
157-
const processBulkRequestResponse: ProcessBulkRequestResponse<ProcessBulkRequestParams> =
158-
await dataProtector.processBulkRequest({
159+
const processBulkRequestResponse = await dataProtector.processBulkRequest(
160+
{
159161
bulkRequest: bulkRequest.bulkRequest,
160162
useVoucher: vUseVoucher,
161163
workerpool: vWorkerpoolAddressOrEns,
162-
});
163-
return processBulkRequestResponse;
164+
}
165+
);
166+
return {
167+
tasks: processBulkRequestResponse.tasks.map((task) => ({
168+
taskId: task.taskId,
169+
dealId: task.dealId,
170+
bulkIndex: task.bulkIndex,
171+
})),
172+
} as SendTelegramResponse<Params>;
164173
}
165174

166175
// Single processing mode - protectedData is required
@@ -198,7 +207,7 @@ export const sendTelegram = async ({
198207

199208
return {
200209
taskId: result.taskId,
201-
};
210+
} as SendTelegramResponse<Params>;
202211
} catch (error) {
203212
// Protocol error detected, re-throwing as-is
204213
if ((error as any)?.isProtocolError === true) {

src/web3telegram/types.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,9 @@ export type Contact = {
3535
grantedAccess: GrantedAccess;
3636
};
3737

38-
export type SendTelegramParams = {
38+
type SendTelegramCommonParams = {
3939
senderName?: string;
4040
telegramContent: string;
41-
protectedData?: Address;
42-
/**
43-
* Granted access to process.
44-
* use prepareBulkRequest of dataprotector to create a bulk request.
45-
* if not provided, the single message will be processed.
46-
*/
47-
grantedAccess?: GrantedAccess[];
48-
maxProtectedDataPerTask?: number;
4941
label?: string;
5042
workerpoolAddressOrEns?: AddressOrENS;
5143
dataMaxPrice?: number;
@@ -54,6 +46,23 @@ export type SendTelegramParams = {
5446
useVoucher?: boolean;
5547
};
5648

49+
export type SendTelegramParams =
50+
| ({
51+
/**
52+
* protected data to process.
53+
*/
54+
protectedData: Address;
55+
} & SendTelegramCommonParams)
56+
| ({
57+
/**
58+
* Granted access to process in bulk.
59+
* use `fetchMyContacts({ bulkOnly: true })` to get granted accesses.
60+
* if not provided, the single message will be processed.
61+
*/
62+
grantedAccess: GrantedAccess[];
63+
maxProtectedDataPerTask?: number;
64+
} & SendTelegramCommonParams);
65+
5766
export type FetchMyContactsParams = {
5867
/**
5968
* Get contacts for this specific user only
@@ -69,10 +78,27 @@ export type FetchUserContactsParams = {
6978
userAddress: Address;
7079
} & FetchMyContactsParams;
7180

72-
export type SendTelegramSingleResponse = {
81+
type SendTelegramSingleResponse = {
7382
taskId: string;
7483
};
7584

85+
type SendTelegramBulkResponse = {
86+
tasks: {
87+
bulkIndex: number;
88+
taskId: string;
89+
dealId: string;
90+
}[];
91+
};
92+
93+
export type SendTelegramResponse<Params = { protectedData: Address }> =
94+
Params extends {
95+
grantedAccess: GrantedAccess[];
96+
}
97+
? SendTelegramBulkResponse
98+
: never & Params extends { protectedData: Address }
99+
? SendTelegramSingleResponse
100+
: never;
101+
76102
/**
77103
* Configuration options for web3telegram.
78104
*/

tests/e2e/sendTelegramBulk.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
22
IExecDataProtectorCore,
3-
ProcessBulkRequestResponse,
4-
ProcessBulkRequestParams,
53
ProtectedDataWithSecretProps,
64
} from '@iexec/dataprotector';
75
import { beforeAll, describe, expect, it } from '@jest/globals';
@@ -10,7 +8,7 @@ import {
108
DEFAULT_CHAIN_ID,
119
getChainDefaultConfig,
1210
} from '../../src/config/config.js';
13-
import { Contact, IExecWeb3telegram, SendTelegramSingleResponse } from '../../src/index.js';
11+
import { Contact, IExecWeb3telegram } from '../../src/index.js';
1412
import {
1513
MAX_EXPECTED_BLOCKTIME,
1614
MAX_EXPECTED_WEB2_SERVICES_TIME,
@@ -203,7 +201,7 @@ describe('web3telegram.sendTelegram() - Bulk Processing', () => {
203201
}
204202

205203
// Process the bulk request
206-
const result: ProcessBulkRequestResponse<ProcessBulkRequestParams> | SendTelegramSingleResponse = await web3telegram.sendTelegram({
204+
const result = await web3telegram.sendTelegram({
207205
telegramContent: 'Bulk test message',
208206
// protectedData is optional when grantedAccess is provided
209207
grantedAccess: bulkOrders,

0 commit comments

Comments
 (0)