88 SKConfig ,
99 SwapKitError ,
1010} from "@swapkit/helpers" ;
11-
11+ import { match , P } from "ts-pattern" ;
1212import {
1313 type BalanceResponse ,
1414 type BrokerDepositChannelParams ,
@@ -24,6 +24,8 @@ import {
2424 PriceResponseSchema ,
2525 type QuoteRequest ,
2626 type QuoteResponse ,
27+ type QuoteResponseRoute ,
28+ QuoteResponseRouteItem ,
2729 QuoteResponseSchema ,
2830 type TokenListProvidersResponse ,
2931 type TokensResponseV2 ,
@@ -32,12 +34,7 @@ import {
3234 type TrackingRequest ,
3335} from "./types" ;
3436
35- const SKRequestClient = RequestClient . extend ( {
36- dynamicHeader : ( ) => {
37- const { swapKit } = SKConfig . get ( "apiKeys" ) ;
38- return swapKit ? { "x-api-key" : swapKit } : { } ;
39- } ,
40- } ) ;
37+ export const SKRequestClient = RequestClient ;
4138
4239export async function getTrackerDetails ( json : TrackingRequest ) {
4340 const response = await SKRequestClient . post < TrackerResponse > ( getApiUrl ( "/track" ) , { json } ) ;
@@ -57,6 +54,10 @@ export async function getTrackerDetails(json: TrackingRequest) {
5754}
5855
5956export async function getSwapQuote ( json : QuoteRequest ) {
57+ const { getQuote } = SKConfig . get ( "endpoints" ) ;
58+
59+ if ( getQuote ) return getQuote ( json ) ;
60+
6061 const response = await SKRequestClient . post < QuoteResponse > ( getApiUrl ( "/quote" ) , { json } ) ;
6162
6263 if ( response . error ) {
@@ -77,6 +78,27 @@ export async function getSwapQuote(json: QuoteRequest) {
7778 }
7879}
7980
81+ export async function getRouteWithTx ( json : { routeId : string ; sourceAddress : string ; destinationAddress : string } ) {
82+ const { getRouteWithTx } = SKConfig . get ( "endpoints" ) ;
83+
84+ if ( getRouteWithTx ) return getRouteWithTx ( json ) ;
85+
86+ const response = await SKRequestClient . post < QuoteResponseRoute > ( getApiUrl ( "/swap" ) , { json } ) ;
87+
88+ try {
89+ const parsedResponse = QuoteResponseRouteItem . safeParse ( response ) ;
90+
91+ if ( ! parsedResponse . success ) {
92+ throw new SwapKitError ( "api_v2_invalid_response" , parsedResponse . error ) ;
93+ }
94+
95+ return parsedResponse . data ;
96+ } catch ( error ) {
97+ console . error ( new SwapKitError ( "api_v2_invalid_response" , error ) ) ;
98+ return response ;
99+ }
100+ }
101+
80102export async function getChainBalance < T extends Chain > ( {
81103 chain,
82104 address,
@@ -187,9 +209,20 @@ export async function getNearDepositChannel(body: NearDepositChannelParams) {
187209}
188210
189211function getApiUrl ( path ?: `/${string } `) {
190- const { isDev, apiUrl, devApiUrl } = SKConfig . get ( "envs" ) ;
191-
192- return `${ isDev ? devApiUrl : apiUrl } ${ path } ` ;
212+ const { isDev, apiUrl, devApiUrl, experimental_apiUrlQuote, experimental_apiUrlSwap } = SKConfig . get ( "envs" ) ;
213+
214+ const defaultUrl = `${ isDev ? devApiUrl : apiUrl } ${ path } ` ;
215+
216+ return match ( { experimental_apiUrlQuote, experimental_apiUrlSwap, path } )
217+ . with (
218+ { experimental_apiUrlQuote : P . string . startsWith ( "http" ) , path : "/quote" } ,
219+ ( { experimental_apiUrlQuote, path } ) => `${ experimental_apiUrlQuote } ${ path } ` ,
220+ )
221+ . with (
222+ { experimental_apiUrlSwap : P . string . startsWith ( "http" ) , path : "/swap" } ,
223+ ( { experimental_apiUrlSwap, path } ) => `${ experimental_apiUrlSwap } ${ path } ` ,
224+ )
225+ . otherwise ( ( ) => defaultUrl ) ;
193226}
194227
195228function evmAssetHasAddress ( assetString : string ) {
0 commit comments