Skip to content

Commit b1dbfb7

Browse files
0xTxbipedromcunha
andauthored
Support API key parameter in createClient SDK method (#879)
* Remove getSolverCapacity from sdk * add base url validation and api key retrieval utils * utilise api key utils * rm getPrice * support api key param * feat: changeset * rm getSolverCapacity * add unit tests --------- Co-authored-by: pedromcunha <[email protected]>
1 parent 3910284 commit b1dbfb7

File tree

19 files changed

+206
-636
lines changed

19 files changed

+206
-636
lines changed

.changeset/flat-mangos-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@relayprotocol/relay-sdk': patch
3+
---
4+
5+
Support API key parameter in `createClient` SDK method

demo/pages/sdk/actions/getPrice.tsx

Lines changed: 0 additions & 157 deletions
This file was deleted.

demo/pages/sdk/actions/getSolverCapacity.tsx

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest'
2+
import { createClient } from '../client'
3+
import { MAINNET_RELAY_API } from '../constants'
4+
import { axios } from '../utils'
5+
import { zeroAddress } from 'viem'
6+
7+
/**
8+
* Verifies that the x-api-key header is included in requests
9+
* when apiKey is configured in createClient.
10+
*/
11+
12+
let axiosRequestSpy: ReturnType<typeof mockAxiosRequest>
13+
14+
const mockAxiosRequest = () => {
15+
return vi.spyOn(axios, 'request').mockImplementation(() => {
16+
return Promise.resolve({
17+
data: { status: 'success', balances: [], steps: [] },
18+
status: 200
19+
})
20+
})
21+
}
22+
23+
const mockWallet = {
24+
vmType: 'evm' as const,
25+
getChainId: () => Promise.resolve(1),
26+
address: () => Promise.resolve(zeroAddress),
27+
handleSignMessageStep: vi.fn().mockResolvedValue('0x'),
28+
handleSendTransactionStep: vi.fn().mockResolvedValue('0x'),
29+
handleConfirmTransactionStep: vi
30+
.fn()
31+
.mockResolvedValue({ status: 'success' }),
32+
switchChain: vi.fn().mockResolvedValue(undefined)
33+
}
34+
35+
describe('API Key Header Tests', () => {
36+
beforeEach(() => {
37+
vi.clearAllMocks()
38+
vi.resetAllMocks()
39+
axiosRequestSpy = mockAxiosRequest()
40+
})
41+
42+
it('Should include x-api-key header in getQuote when apiKey is configured', async () => {
43+
const client = createClient({
44+
baseApiUrl: MAINNET_RELAY_API,
45+
apiKey: 'test-api-key'
46+
})
47+
48+
await client.actions.getQuote(
49+
{
50+
toChainId: 1,
51+
chainId: 8453,
52+
currency: '0x0000000000000000000000000000000000000000',
53+
toCurrency: '0x0000000000000000000000000000000000000000',
54+
tradeType: 'EXACT_INPUT',
55+
amount: '1000000000000000'
56+
},
57+
true
58+
)
59+
60+
expect(axiosRequestSpy).toHaveBeenCalledWith(
61+
expect.objectContaining({
62+
url: expect.stringContaining('/quote'),
63+
headers: expect.objectContaining({
64+
'x-api-key': 'test-api-key'
65+
})
66+
})
67+
)
68+
})
69+
70+
it('Should include x-api-key header in getAppFees when apiKey is configured', async () => {
71+
const client = createClient({
72+
baseApiUrl: MAINNET_RELAY_API,
73+
apiKey: 'test-api-key'
74+
})
75+
76+
await client.actions.getAppFees({
77+
wallet: '0x0000000000000000000000000000000000000000'
78+
})
79+
80+
expect(axiosRequestSpy).toHaveBeenCalledWith(
81+
expect.objectContaining({
82+
url: expect.stringContaining('/app-fees/'),
83+
headers: expect.objectContaining({
84+
'x-api-key': 'test-api-key'
85+
})
86+
})
87+
)
88+
})
89+
90+
it('Should include x-api-key header in claimAppFees when apiKey is configured', async () => {
91+
const client = createClient({
92+
baseApiUrl: MAINNET_RELAY_API,
93+
apiKey: 'test-api-key'
94+
})
95+
96+
try {
97+
await client.actions.claimAppFees({
98+
wallet: mockWallet,
99+
chainId: 1,
100+
currency: '0x0000000000000000000000000000000000000000'
101+
})
102+
} catch {}
103+
104+
expect(axiosRequestSpy).toHaveBeenCalledWith(
105+
expect.objectContaining({
106+
url: expect.stringContaining('/claim'),
107+
headers: expect.objectContaining({
108+
'x-api-key': 'test-api-key'
109+
})
110+
})
111+
)
112+
})
113+
})

0 commit comments

Comments
 (0)