import { Inject , Injectable } from '@angular/core' ; import { HttpEvent , HttpHandler , HttpInterceptor , HttpRequest } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { TUI_IS_MOBILE } from '@taiga-ui/cdk' ; import { WalletConnectorService } from '@core/services/blockchain/wallets/wallet-connector-service/wallet-connector.service' ; type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' ; interface EndpointData { route : string ; methods : Set < HttpMethod > ; } interface WalletInfo { walletName : string ; deviceType : 'mobile' | 'desktop' ; } /**
 * Intercepts add transaction requests targeted to '*.rubic.exchange'.
 */ @ Injectable ( ) export class WalletsInfoInterceptor implements HttpInterceptor { private readonly DOMAIN_SUBSTRING = 'rubic.exchange' ; private readonly endpoints : EndpointData [ ] ; constructor ( @ Inject ( TUI_IS_MOBILE ) private readonly isMobile , private readonly walletConnectorService ) { this . endpoints = [ { route : '/api/bridges/transactions' , methods : new Set ( [ 'POST' ] ) } , { route : '/api/instant_trades/' , methods : new Set ( [ 'POST' ] ) } , { route : '/api/trades/' , methods : new Set ( [ 'PATCH' ] ) } ] ; } intercept ( httpRequest , next ) { if ( httpRequest . url . includes ( this . DOMAIN_SUBSTRING ) && this . isTransactionOperation ( httpRequest ) ) { return next . handle ( this . addWalletInfoToRequest ( httpRequest ) ) ; } return next . handle ( httpRequest ) ; } param request return private isTransactionOperation ( request ) { return this . endpoints . some ( endpoint => request . url . includes ( endpoint . route ) && endpoint . methods . has ( request . method as HttpMethod ) ) ; } param request return private addWalletInfoToRequest ( request ) { const walletsInfo = { walletName : this . walletConnectorService . provider . detailedWalletName , deviceType : this . isMobile ? 'mobile' : 'desktop' } ; return request . clone ( { body : { ... request . body , ... walletsInfo } } ) ; } }