Skip to content

Commit

Permalink
add data service api bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbl committed Apr 11, 2024
1 parent bb37600 commit 41ec318
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 31 deletions.
55 changes: 37 additions & 18 deletions packages/sdk/src/dialect-cloud-api/data-service-dapps-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { TokenProvider } from '../auth/token-provider';
import {
createHeaders,
withReThrowingDataServiceError,
} from './data-service-api';
import { createHeaders, withReThrowingDataServiceError } from './data-service-api';
import axios from 'axios';
import type { BlockchainType } from '../dapp/dapp.interface';

Expand All @@ -26,7 +23,8 @@ export class DataServiceDappsApiClient implements DataServiceDappsApi {
constructor(
private readonly baseUrl: string,
private readonly tokenProvider: TokenProvider,
) {}
) {
}

async create(
command: Omit<CreateDappCommandDto, 'publicKey'>,
Expand Down Expand Up @@ -183,39 +181,60 @@ export enum AddressTypeDto {
Wallet = 'WALLET',
}

export class DappMessageTransactionDto {
transactionServiceId!: string;
transactionParams!: Record<string, any>;
// start actions
export enum DappMessageActionTypeDto {
LINK = 'Link',
SMART_MESSAGE = 'SmartMessage',
}

interface DappMessageActionBaseDto {
type: DappMessageActionTypeDto;
}

export class DappMessageLinksActionDto implements DappMessageActionBaseDto {
type!: DappMessageActionTypeDto.LINK;
links!: DappMessageLinkAction[];
}

export class DappMessageActionDto {
export class DappMessageLinkAction {
label!: string;
url?: string;
url!: string;
}

export class UnicastDappMessageActionDto extends DappMessageActionDto {
transaction?: DappMessageTransactionDto;
export class DappMessageSmartMessageActionDto implements DappMessageActionBaseDto {
type!: DappMessageActionTypeDto.SMART_MESSAGE;
smartMessage!: SmartMessageDto;
}

class SendDappMessageCommand {
export class SmartMessageDto {
transactionServiceId!: string;
transactionParams!: Record<string, any>;
}
// end actions


class SendDappMessageCommandDto {
title?: string;
message!: string;
notificationTypeId?: string;
addressTypes?: AddressTypeDto[];
// tags?: string[];
actions?: DappMessageActionDto[];
}

export class UnicastDappMessageCommandDto extends SendDappMessageCommand {

export class UnicastDappMessageCommandDto extends SendDappMessageCommandDto {
recipientPublicKey!: string;
override actions?: UnicastDappMessageActionDto[];
actionsV2?: DappMessageLinksActionDto | DappMessageSmartMessageActionDto;
}

export class MulticastDappMessageCommandDto extends SendDappMessageCommand {
export class MulticastDappMessageCommandDto extends SendDappMessageCommandDto {
recipientPublicKeys!: string[];
actionsV2?: DappMessageLinksActionDto;
}

export type BroadcastDappMessageCommandDto = SendDappMessageCommand;
export class BroadcastDappMessageCommandDto extends SendDappMessageCommandDto {
actionsV2?: DappMessageLinksActionDto;
}

export class FindDappQueryDto {
verified?: boolean;
Expand Down
81 changes: 68 additions & 13 deletions packages/sdk/src/internal/dapp/data-service-dapp-messages.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import type {
BroadcastDappMessageCommand,
DappMessages,
MulticastDappMessageCommand,
SendDappMessageCommand,
UnicastDappMessageCommand,
import {
type BroadcastDappMessageCommand,
DappMessageActionType,
type DappMessageLinksAction,
type DappMessages,
type DappMessageSmartMessageAction,
type MulticastDappMessageCommand,
type SendDappMessageCommand,
type UnicastDappMessageCommand,
} from '../../dapp/dapp.interface';
import { toAddressTypeDto } from '../../address/addresses.interface';
import type { DataServiceDappsApi } from '../../dialect-cloud-api/data-service-dapps-api';
import {
DappMessageActionTypeDto,
DappMessageLinksActionDto,
DappMessageSmartMessageActionDto,
type DataServiceDappsApi,
} from '../../dialect-cloud-api/data-service-dapps-api';
import { withErrorParsing } from '../../dialect-cloud-api/data-service-errors';

export class DataServiceDappMessages implements DappMessages {
constructor(private readonly api: DataServiceDappsApi) {}
constructor(private readonly api: DataServiceDappsApi) {
}

async send(command: SendDappMessageCommand): Promise<void> {
if (command.addressTypes?.length === 0) {
Expand All @@ -26,45 +35,91 @@ export class DataServiceDappMessages implements DappMessages {
}

private async unicast(command: UnicastDappMessageCommand) {


const actionsV2Dto = this.getActionsV2DtoForUnicast(command.actionsV2);
return withErrorParsing(
this.api.unicast({
...command,
message: command.message,
title: command.title,
notificationTypeId: command.notificationTypeId,
recipientPublicKey: command.recipient.toString(),
addressTypes: command?.addressTypes?.map((addr) =>
toAddressTypeDto(addr),
),
actionsV2: actionsV2Dto,
}),
);
}

private getActionsV2DtoForUnicast(actions?: DappMessageLinksAction | DappMessageSmartMessageAction): DappMessageLinksActionDto | DappMessageSmartMessageActionDto | undefined {
if (!actions?.type) {
return;
}
if (actions.type === DappMessageActionType.LINK) {
return {
type: DappMessageActionTypeDto.LINK,
links: actions.links.map((link) => ({
label: link.label,
url: link.url,
})),
};
}
if (actions.type === DappMessageActionType.SMART_MESSAGE) {
return {
type: DappMessageActionTypeDto.SMART_MESSAGE,
smartMessage: {
transactionServiceId: actions.smartMessage.transactionServiceId,
transactionParams: actions.smartMessage.transactionParams,
},
};
}
}

private async multicast(command: MulticastDappMessageCommand) {
if (command.recipients.length === 0) {
return;
}
return withErrorParsing(
this.api.multicast({
...command,
message: command.message,
title: command.title,
actionsV2: this.getActionsV2DtoForLinks(command.actionsV2),
notificationTypeId: command.notificationTypeId,
recipientPublicKeys: command.recipients.map((it) => it.toString()),
addressTypes: command?.addressTypes?.map((addr) =>
toAddressTypeDto(addr),
),
// tags: command.tags,
actions: command.actions,
}),
);
}

private async broadcast(command: BroadcastDappMessageCommand) {
return withErrorParsing(
this.api.broadcast({
...command,
message: command.message,
title: command.title,
actionsV2: this.getActionsV2DtoForLinks(command.actionsV2),
notificationTypeId: command.notificationTypeId,
addressTypes: command?.addressTypes?.map((addr) =>
toAddressTypeDto(addr),
),
}),
);
}

private getActionsV2DtoForLinks(actions?: DappMessageLinksAction): DappMessageLinksActionDto | undefined {
if (!actions?.type) {
return;
}
return {
type: DappMessageActionTypeDto.LINK,
links: actions.links.map((link) => ({
label: link.label,
url: link.url,
})),
};

}

}

0 comments on commit 41ec318

Please sign in to comment.