Skip to content

Commit 32d0595

Browse files
authored
feat: add requests cancellation (#67)
1 parent 52bb994 commit 32d0595

File tree

3 files changed

+81
-47
lines changed

3 files changed

+81
-47
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"dependencies": {
5252
"@ethersproject/abi": "^5.6.1",
5353
"@ethersproject/contracts": "^5.4.0",
54-
"@lifinance/types": "^0.13.1",
54+
"@lifinance/types": "^0.14.0",
5555
"axios": "^0.27.2",
5656
"bignumber.js": "^9.0.1",
5757
"eth-rpc-errors": "^4.0.3",

src/services/ApiService.ts

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { QuoteRequest, TokensRequest, TokensResponse } from '@lifinance/types'
2-
import { GetStatusRequest } from '@lifinance/types/dist/api'
1+
import {
2+
GetStatusRequest,
3+
QuoteRequest,
4+
RequestOptions,
5+
TokensRequest,
6+
TokensResponse,
7+
} from '@lifinance/types'
38
import axios from 'axios'
49
import { isRoutesRequest, isStep } from '../typeguards'
510
import {
@@ -22,7 +27,8 @@ import { parseBackendError } from '../utils/parseError'
2227
import ConfigService from './ConfigService'
2328

2429
const getPossibilities = async (
25-
request?: PossibilitiesRequest
30+
request?: PossibilitiesRequest,
31+
options?: RequestOptions
2632
): Promise<PossibilitiesResponse> => {
2733
if (!request) {
2834
request = {}
@@ -39,7 +45,10 @@ const getPossibilities = async (
3945
try {
4046
const result = await axios.post<PossibilitiesResponse>(
4147
config.apiUrl + 'advanced/possibilities',
42-
request
48+
request,
49+
{
50+
signal: options?.signal,
51+
}
4352
)
4453
return result.data
4554
} catch (e) {
@@ -49,7 +58,8 @@ const getPossibilities = async (
4958

5059
const getToken = async (
5160
chain: ChainKey | ChainId,
52-
token: string
61+
token: string,
62+
options?: RequestOptions
5363
): Promise<Token> => {
5464
if (!chain) {
5565
throw new ValidationError('Required parameter "chain" is missing.')
@@ -67,31 +77,35 @@ const getToken = async (
6777
chain,
6878
token,
6979
},
80+
signal: options?.signal,
7081
})
7182
return result.data
7283
} catch (e) {
7384
throw parseBackendError(e)
7485
}
7586
}
7687

77-
const getQuote = async ({
78-
fromChain,
79-
fromToken,
80-
fromAddress,
81-
fromAmount,
82-
toChain,
83-
toToken,
84-
order,
85-
slippage,
86-
integrator,
87-
referrer,
88-
allowBridges,
89-
denyBridges,
90-
preferBridges,
91-
allowExchanges,
92-
denyExchanges,
93-
preferExchanges,
94-
}: QuoteRequest): Promise<Step> => {
88+
const getQuote = async (
89+
{
90+
fromChain,
91+
fromToken,
92+
fromAddress,
93+
fromAmount,
94+
toChain,
95+
toToken,
96+
order,
97+
slippage,
98+
integrator,
99+
referrer,
100+
allowBridges,
101+
denyBridges,
102+
preferBridges,
103+
allowExchanges,
104+
denyExchanges,
105+
preferExchanges,
106+
}: QuoteRequest,
107+
options?: RequestOptions
108+
): Promise<Step> => {
95109
if (!fromChain) {
96110
throw new ValidationError('Required parameter "fromChain" is missing.')
97111
}
@@ -133,19 +147,18 @@ const getQuote = async ({
133147
denyExchanges,
134148
preferExchanges,
135149
},
150+
signal: options?.signal,
136151
})
137152
return result.data
138153
} catch (e) {
139154
throw parseBackendError(e)
140155
}
141156
}
142157

143-
const getStatus = async ({
144-
bridge,
145-
fromChain,
146-
toChain,
147-
txHash,
148-
}: GetStatusRequest): Promise<StatusResponse> => {
158+
const getStatus = async (
159+
{ bridge, fromChain, toChain, txHash }: GetStatusRequest,
160+
options?: RequestOptions
161+
): Promise<StatusResponse> => {
149162
if (fromChain !== toChain && !bridge) {
150163
throw new ValidationError(
151164
'Parameter "bridge" is required for cross chain transfers.'
@@ -174,54 +187,64 @@ const getStatus = async ({
174187
toChain,
175188
txHash,
176189
},
190+
signal: options?.signal,
177191
})
178192
return result.data
179193
} catch (e) {
180194
throw parseBackendError(e)
181195
}
182196
}
183197

184-
const getChains = async (): Promise<Chain[]> => {
198+
const getChains = async (options?: RequestOptions): Promise<Chain[]> => {
185199
const configService = ConfigService.getInstance()
186200
const config = configService.getConfig()
187201

188202
try {
189-
const result = await axios.get<ChainsResponse>(config.apiUrl + 'chains')
203+
const result = await axios.get<ChainsResponse>(config.apiUrl + 'chains', {
204+
signal: options?.signal,
205+
})
190206
return result.data.chains
191207
} catch (e) {
192208
throw parseBackendError(e)
193209
}
194210
}
195211

196212
const getRoutes = async (
197-
routesRequest: RoutesRequest
213+
request: RoutesRequest,
214+
options?: RequestOptions
198215
): Promise<RoutesResponse> => {
199-
if (!isRoutesRequest(routesRequest)) {
216+
if (!isRoutesRequest(request)) {
200217
throw new ValidationError('Invalid routes request.')
201218
}
202219

203220
const configService = ConfigService.getInstance()
204221
const config = configService.getConfig()
205222

206223
// apply defaults
207-
routesRequest.options = {
224+
request.options = {
208225
...config.defaultRouteOptions,
209-
...routesRequest.options,
226+
...request.options,
210227
}
211228

212229
// send request
213230
try {
214231
const result = await axios.post<RoutesResponse>(
215232
config.apiUrl + 'advanced/routes',
216-
routesRequest
233+
request,
234+
{
235+
signal: options?.signal,
236+
}
217237
)
218238
return result.data
219239
} catch (e) {
220240
throw parseBackendError(e)
221241
}
222242
}
223243

224-
const getStepTransaction = async (step: Step): Promise<Step> => {
244+
const getStepTransaction = async (
245+
step: Step,
246+
options?: RequestOptions
247+
): Promise<Step> => {
225248
if (!isStep(step)) {
226249
// While the validation fails for some users we should not enforce it
227250
// eslint-disable-next-line no-console
@@ -233,28 +256,39 @@ const getStepTransaction = async (step: Step): Promise<Step> => {
233256
try {
234257
const result = await axios.post<Step>(
235258
config.apiUrl + 'advanced/stepTransaction',
236-
step
259+
step,
260+
{
261+
signal: options?.signal,
262+
}
237263
)
238264
return result.data
239265
} catch (e) {
240266
throw parseBackendError(e)
241267
}
242268
}
243269

244-
const getTools = async (request?: ToolsRequest): Promise<ToolsResponse> => {
270+
const getTools = async (
271+
request?: ToolsRequest,
272+
options?: RequestOptions
273+
): Promise<ToolsResponse> => {
245274
const configService = ConfigService.getInstance()
246275
const config = configService.getConfig()
247276
const r = await axios.get<ToolsResponse>(config.apiUrl + 'tools', {
248277
params: request,
278+
signal: options?.signal,
249279
})
250280
return r.data
251281
}
252282

253-
const getTokens = async (request?: TokensRequest): Promise<TokensResponse> => {
283+
const getTokens = async (
284+
request?: TokensRequest,
285+
options?: RequestOptions
286+
): Promise<TokensResponse> => {
254287
const configService = ConfigService.getInstance()
255288
const config = configService.getConfig()
256289
const r = await axios.get<TokensResponse>(config.apiUrl + 'tokens', {
257290
params: request,
291+
signal: options?.signal,
258292
})
259293
return r.data
260294
}

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,12 @@
10681068
"@jridgewell/resolve-uri" "^3.0.3"
10691069
"@jridgewell/sourcemap-codec" "^1.4.10"
10701070

1071-
"@lifinance/types@^0.13.1":
1072-
version "0.13.1"
1073-
resolved "https://registry.yarnpkg.com/@lifinance/types/-/types-0.13.1.tgz#31aede31c4fad530c42a7891e33dc4357319ab19"
1074-
integrity sha512-cC8GzTHPnQwjAuv5wHPESRz+F8qt67HKifSSNACfemPbCEezErlYhEkyArKYTtZzr3zgsOWx7crGCis/h/w1bQ==
1071+
"@lifinance/types@^0.14.0":
1072+
version "0.14.0"
1073+
resolved "https://registry.yarnpkg.com/@lifinance/types/-/types-0.14.0.tgz#0f900712574ea5218218a1948893010009636428"
1074+
integrity sha512-bsd/V/O+is0wHVw8N0dTlnJawXPRU5nUOpJQXQnQvuoYzOl1sXTL++HZZvP69TjeM6D+AelXbYBJ84aJclPyCw==
10751075
dependencies:
1076-
ethers "^5.4.7"
1076+
ethers "^5.6.5"
10771077

10781078
"@nodelib/[email protected]":
10791079
version "2.1.5"
@@ -2690,7 +2690,7 @@ eth-rpc-errors@^4.0.3:
26902690
dependencies:
26912691
fast-safe-stringify "^2.0.6"
26922692

2693-
ethers@^5.4.7, ethers@^5.6.5:
2693+
ethers@^5.6.5:
26942694
version "5.6.5"
26952695
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.6.5.tgz#3185ac7815dc640993408adf6f133ffabfbcbb63"
26962696
integrity sha512-9CTmplO9bv0s/aPw3HB3txGzKz3tUSI2EfO4dJo0W2WvaEq1ArgsEX6obV+bj5X3yY+Zgb1kAux8TDtJKe1FaA==

0 commit comments

Comments
 (0)