Skip to content

Commit a0e50f9

Browse files
authored
ui: support proxy url in titan client (#2048)
1 parent afc9651 commit a0e50f9

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

sdk/src/swap/UnifiedSwapClient.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ export class UnifiedSwapClient {
8282
connection,
8383
authToken,
8484
url,
85+
proxyUrl,
8586
}: {
8687
clientType: SwapClientType;
8788
connection: Connection;
88-
authToken?: string; // Required for Titan, optional for Jupiter
89+
authToken?: string; // Required for Titan when not using proxy, optional for Jupiter
8990
url?: string; // Optional custom URL
91+
proxyUrl?: string; // Optional proxy URL for Titan
9092
}) {
9193
this.clientType = clientType;
9294

@@ -96,13 +98,11 @@ export class UnifiedSwapClient {
9698
url,
9799
});
98100
} else if (clientType === 'titan') {
99-
if (!authToken) {
100-
throw new Error('authToken is required for Titan client');
101-
}
102101
this.client = new TitanClient({
103102
connection,
104-
authToken,
103+
authToken: authToken || '', // Not needed when using proxy
105104
url,
105+
proxyUrl,
106106
});
107107
} else {
108108
throw new Error(`Unsupported client type: ${clientType}`);

sdk/src/titan/titanClient.ts

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,23 @@ export class TitanClient {
9393
authToken: string;
9494
url: string;
9595
connection: Connection;
96+
proxyUrl?: string;
9697

9798
constructor({
9899
connection,
99100
authToken,
100101
url,
102+
proxyUrl,
101103
}: {
102104
connection: Connection;
103105
authToken: string;
104106
url?: string;
107+
proxyUrl?: string;
105108
}) {
106109
this.connection = connection;
107110
this.authToken = authToken;
108111
this.url = url ?? TITAN_API_URL;
112+
this.proxyUrl = proxyUrl;
109113
}
110114

111115
/**
@@ -157,16 +161,30 @@ export class TitanClient {
157161
}),
158162
});
159163

160-
const response = await fetch(
161-
`${this.url}/api/v1/quote/swap?${params.toString()}`,
162-
{
164+
let response: Response;
165+
166+
if (this.proxyUrl) {
167+
// Use proxy route - send parameters in request body
168+
response = await fetch(this.proxyUrl, {
169+
method: 'POST',
163170
headers: {
164-
Accept: 'application/vnd.msgpack',
165-
'Accept-Encoding': 'gzip, deflate, br',
166-
Authorization: `Bearer ${this.authToken}`,
171+
'Content-Type': 'application/json',
167172
},
168-
}
169-
);
173+
body: JSON.stringify(Object.fromEntries(params.entries())),
174+
});
175+
} else {
176+
// Direct request to Titan API
177+
response = await fetch(
178+
`${this.url}/api/v1/quote/swap?${params.toString()}`,
179+
{
180+
headers: {
181+
Accept: 'application/vnd.msgpack',
182+
'Accept-Encoding': 'gzip, deflate, br',
183+
Authorization: `Bearer ${this.authToken}`,
184+
},
185+
}
186+
);
187+
}
170188

171189
if (!response.ok) {
172190
throw new Error(
@@ -268,16 +286,30 @@ export class TitanClient {
268286
}),
269287
});
270288

271-
const response = await fetch(
272-
`${this.url}/api/v1/quote/swap?${params.toString()}`,
273-
{
289+
let response: Response;
290+
291+
if (this.proxyUrl) {
292+
// Use proxy route - send parameters in request body
293+
response = await fetch(this.proxyUrl, {
294+
method: 'POST',
274295
headers: {
275-
Accept: 'application/vnd.msgpack',
276-
'Accept-Encoding': 'gzip, deflate, br',
277-
Authorization: `Bearer ${this.authToken}`,
296+
'Content-Type': 'application/json',
278297
},
279-
}
280-
);
298+
body: JSON.stringify(Object.fromEntries(params.entries())),
299+
});
300+
} else {
301+
// Direct request to Titan API
302+
response = await fetch(
303+
`${this.url}/api/v1/quote/swap?${params.toString()}`,
304+
{
305+
headers: {
306+
Accept: 'application/vnd.msgpack',
307+
'Accept-Encoding': 'gzip, deflate, br',
308+
Authorization: `Bearer ${this.authToken}`,
309+
},
310+
}
311+
);
312+
}
281313

282314
if (!response.ok) {
283315
if (response.status === 404) {

0 commit comments

Comments
 (0)