@lifi/sdk 4.0.0-alpha.1 → 4.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -165
- package/dist/cjs/client/getClientStorage.js +3 -4
- package/dist/cjs/client/getClientStorage.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client/getClientStorage.js +4 -4
- package/dist/esm/client/getClientStorage.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/client/getClientStorage.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +17 -2
- package/src/client/getClientStorage.ts +5 -4
- package/src/version.ts +1 -1
- package/CHANGELOG.md +0 -1272
- package/package.json.tmp +0 -103
- package/src/actions/actions.unit.handlers.ts +0 -78
- package/src/actions/getChains.unit.spec.ts +0 -19
- package/src/actions/getConnections.unit.spec.ts +0 -45
- package/src/actions/getContractCallsQuote.unit.spec.ts +0 -323
- package/src/actions/getGasRecommendation.unit.spec.ts +0 -40
- package/src/actions/getNameServiceAddress.unit.spec.ts +0 -169
- package/src/actions/getQuote.int.spec.ts +0 -18
- package/src/actions/getQuote.unit.spec.ts +0 -154
- package/src/actions/getRelayedTransactionStatus.unit.spec.ts +0 -243
- package/src/actions/getRelayerQuote.unit.spec.ts +0 -220
- package/src/actions/getRoutes.unit.spec.ts +0 -112
- package/src/actions/getStatus.unit.spec.ts +0 -53
- package/src/actions/getStepTransaction.unit.spec.ts +0 -140
- package/src/actions/getToken.unit.spec.ts +0 -45
- package/src/actions/getTokenBalance.unit.spec.ts +0 -61
- package/src/actions/getTokenBalances.unit.spec.ts +0 -68
- package/src/actions/getTokenBalancesByChain.unit.spec.ts +0 -108
- package/src/actions/getTokens.unit.spec.ts +0 -16
- package/src/actions/getTools.unit.spec.ts +0 -20
- package/src/actions/getTransactionHistory.unit.spec.ts +0 -36
- package/src/actions/getWalletBalances.unit.spec.ts +0 -90
- package/src/actions/relayTransaction.unit.spec.ts +0 -229
- package/src/client/createClient.unit.spec.ts +0 -274
- package/src/client/getClientStorage.unit.spec.ts +0 -382
- package/src/core/StatusManager.unit.spec.ts +0 -298
- package/src/core/execution.unit.handlers.ts +0 -32
- package/src/core/execution.unit.mock.ts +0 -252
- package/src/core/execution.unit.spec.ts +0 -86
- package/src/core/stepComparison.unit.spec.ts +0 -89
- package/src/errors/SDKError.unit.spec.ts +0 -160
- package/src/errors/baseError.unit.spec.ts +0 -22
- package/src/errors/httpError.unit.spec.ts +0 -125
- package/src/errors/utils/baseErrorRootCause.unit.spec.ts +0 -89
- package/src/errors/utils/rootCause.unit.spec.ts +0 -36
- package/src/utils/checkPackageUpdates.unit.spec.ts +0 -71
- package/src/utils/convertQuoteToRoute.unit.spec.ts +0 -56
- package/src/utils/fetchTxErrorDetails.unit.spec.ts +0 -42
- package/src/utils/getTransactionMessage.unit.spec.ts +0 -38
- package/src/utils/isRoutesRequest.unit.spec.ts +0 -46
- package/src/utils/isStep.unit.spec.ts +0 -55
- package/src/utils/isToken.unit.spec.ts +0 -49
- package/src/utils/request.unit.spec.ts +0 -159
- package/src/utils/sleep.unit.spec.ts +0 -17
- package/src/utils/waitForResult.unit.spec.ts +0 -75
- package/src/utils/withDedupe.unit.spec.ts +0 -26
- package/tsconfig.json +0 -18
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
-
import { getTransactionFailedMessage } from './getTransactionMessage.js'
|
|
3
|
-
|
|
4
|
-
describe('getTransactionFailedMessage', () => {
|
|
5
|
-
const mockClient = {
|
|
6
|
-
getChainById: vi.fn().mockResolvedValue({
|
|
7
|
-
id: 1,
|
|
8
|
-
name: 'Ethereum',
|
|
9
|
-
}),
|
|
10
|
-
} as any
|
|
11
|
-
|
|
12
|
-
const mockStep = {
|
|
13
|
-
action: {
|
|
14
|
-
toChainId: 1,
|
|
15
|
-
toToken: { symbol: 'USDC' },
|
|
16
|
-
},
|
|
17
|
-
} as any
|
|
18
|
-
|
|
19
|
-
it('should return message without tx link', async () => {
|
|
20
|
-
const message = await getTransactionFailedMessage(mockClient, mockStep)
|
|
21
|
-
|
|
22
|
-
expect(message).toContain('Ethereum')
|
|
23
|
-
expect(message).toContain('USDC')
|
|
24
|
-
expect(message).not.toContain('block explorer')
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('should return message with tx link', async () => {
|
|
28
|
-
const txLink = 'https://etherscan.io/tx/0x123'
|
|
29
|
-
const message = await getTransactionFailedMessage(
|
|
30
|
-
mockClient,
|
|
31
|
-
mockStep,
|
|
32
|
-
txLink
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
expect(message).toContain('block explorer')
|
|
36
|
-
expect(message).toContain(txLink)
|
|
37
|
-
})
|
|
38
|
-
})
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { isRoutesRequest } from './isRoutesRequest.js'
|
|
3
|
-
|
|
4
|
-
describe('isRoutesRequest', () => {
|
|
5
|
-
const validRequest = {
|
|
6
|
-
fromChainId: 1,
|
|
7
|
-
fromAmount: '1000000000000000000',
|
|
8
|
-
fromTokenAddress: '0x111',
|
|
9
|
-
toChainId: 1,
|
|
10
|
-
toTokenAddress: '0x222',
|
|
11
|
-
options: { slippage: 0.001 },
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
it('should return true for valid request', () => {
|
|
15
|
-
expect(isRoutesRequest(validRequest)).toBe(true)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('should return true for request without options', () => {
|
|
19
|
-
expect(isRoutesRequest({ ...validRequest, options: undefined })).toBe(true)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('should return false for invalid chainId', () => {
|
|
23
|
-
expect(isRoutesRequest({ ...validRequest, fromChainId: '1' } as any)).toBe(
|
|
24
|
-
false
|
|
25
|
-
)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should return false for empty fromAmount', () => {
|
|
29
|
-
expect(isRoutesRequest({ ...validRequest, fromAmount: '' })).toBe(false)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('should return false for empty token address', () => {
|
|
33
|
-
expect(isRoutesRequest({ ...validRequest, fromTokenAddress: '' })).toBe(
|
|
34
|
-
false
|
|
35
|
-
)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
it('should return false for invalid slippage', () => {
|
|
39
|
-
expect(
|
|
40
|
-
isRoutesRequest({
|
|
41
|
-
...validRequest,
|
|
42
|
-
options: { slippage: 'invalid' },
|
|
43
|
-
} as any)
|
|
44
|
-
).toBe(false)
|
|
45
|
-
})
|
|
46
|
-
})
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { isStep } from './isStep.js'
|
|
3
|
-
|
|
4
|
-
describe('isStep', () => {
|
|
5
|
-
const validStep = {
|
|
6
|
-
id: 'test-step',
|
|
7
|
-
type: 'swap',
|
|
8
|
-
tool: 'test-tool',
|
|
9
|
-
action: {
|
|
10
|
-
fromChainId: 1,
|
|
11
|
-
fromAmount: '1000000000000000000',
|
|
12
|
-
fromToken: {
|
|
13
|
-
address: '0x111',
|
|
14
|
-
decimals: 18,
|
|
15
|
-
chainId: 1,
|
|
16
|
-
},
|
|
17
|
-
toChainId: 1,
|
|
18
|
-
toToken: {
|
|
19
|
-
address: '0x222',
|
|
20
|
-
decimals: 18,
|
|
21
|
-
chainId: 1,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
estimate: {
|
|
25
|
-
fromAmount: '1000000000000000000',
|
|
26
|
-
toAmount: '2000000000000000000',
|
|
27
|
-
toAmountMin: '1900000000000000000',
|
|
28
|
-
approvalAddress: '0x333',
|
|
29
|
-
},
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
it('should return true for valid step', () => {
|
|
33
|
-
expect(isStep(validStep as any)).toBe(true)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('should return false for invalid id', () => {
|
|
37
|
-
expect(isStep({ ...validStep, id: 123 } as any)).toBe(false)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should return false for invalid type', () => {
|
|
41
|
-
expect(isStep({ ...validStep, type: 'invalid' } as any)).toBe(false)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('should return false for empty fromAmount', () => {
|
|
45
|
-
const step = {
|
|
46
|
-
...validStep,
|
|
47
|
-
action: { ...validStep.action, fromAmount: '' },
|
|
48
|
-
}
|
|
49
|
-
expect(isStep(step as any)).toBe(false)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('should return false for invalid tool', () => {
|
|
53
|
-
expect(isStep({ ...validStep, tool: 123 } as any)).toBe(false)
|
|
54
|
-
})
|
|
55
|
-
})
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { StaticToken } from '@lifi/types'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
import { isToken } from './isToken.js'
|
|
4
|
-
|
|
5
|
-
describe('isToken', () => {
|
|
6
|
-
it('should return true for valid token', () => {
|
|
7
|
-
const token = {
|
|
8
|
-
address: '0x1234567890123456789012345678901234567890',
|
|
9
|
-
decimals: 18,
|
|
10
|
-
chainId: 1,
|
|
11
|
-
symbol: 'ETH',
|
|
12
|
-
} as StaticToken
|
|
13
|
-
|
|
14
|
-
expect(isToken(token)).toBe(true)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should return false for invalid address', () => {
|
|
18
|
-
const token = {
|
|
19
|
-
address: 123,
|
|
20
|
-
decimals: 18,
|
|
21
|
-
chainId: 1,
|
|
22
|
-
symbol: 'ETH',
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
expect(isToken(token as any)).toBe(false)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should return false for invalid decimals', () => {
|
|
29
|
-
const token = {
|
|
30
|
-
address: '0x123',
|
|
31
|
-
decimals: '18',
|
|
32
|
-
chainId: 1,
|
|
33
|
-
symbol: 'ETH',
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
expect(isToken(token as any)).toBe(false)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('should return false for invalid chainId', () => {
|
|
40
|
-
const token = {
|
|
41
|
-
address: '0x123',
|
|
42
|
-
decimals: 18,
|
|
43
|
-
chainId: '1',
|
|
44
|
-
symbol: 'ETH',
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
expect(isToken(token as any)).toBe(false)
|
|
48
|
-
})
|
|
49
|
-
})
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { HttpResponse, http } from 'msw'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
import { client, setupTestServer } from '../actions/actions.unit.handlers.js'
|
|
4
|
-
import { ValidationError } from '../errors/errors.js'
|
|
5
|
-
import type { HTTPError } from '../errors/httpError.js'
|
|
6
|
-
import { SDKError } from '../errors/SDKError.js'
|
|
7
|
-
import type { ExtendedRequestInit } from '../types/request.js'
|
|
8
|
-
import { version } from '../version.js'
|
|
9
|
-
import { request } from './request.js'
|
|
10
|
-
|
|
11
|
-
describe('request new', () => {
|
|
12
|
-
const server = setupTestServer()
|
|
13
|
-
|
|
14
|
-
it('should be able to successfully make a fetch request', async () => {
|
|
15
|
-
const url = `${client.config.apiUrl}/advanced/routes`
|
|
16
|
-
|
|
17
|
-
const response = await request<{ message: string }>(client.config, url, {
|
|
18
|
-
method: 'POST',
|
|
19
|
-
retries: 0,
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
expect(response).toEqual({})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should remove the extended request init properties that fetch does not care about', async () => {
|
|
26
|
-
const url = `${client.config.apiUrl}/advanced/routes`
|
|
27
|
-
const successResponse = { message: 'it works' }
|
|
28
|
-
|
|
29
|
-
server.use(
|
|
30
|
-
http.post(url, async ({ request }) => {
|
|
31
|
-
expect(request.headers.get('x-lifi-integrator')).toEqual('lifi-sdk')
|
|
32
|
-
expect(request.headers.get('x-lifi-sdk')).toEqual(version)
|
|
33
|
-
expect(request.headers.get('x-lifi-api-key')).toBeNull()
|
|
34
|
-
expect(request.headers.get('x-lifi-userid')).toBeNull()
|
|
35
|
-
expect(request.headers.get('x-lifi-widget')).toBeNull()
|
|
36
|
-
|
|
37
|
-
return HttpResponse.json(successResponse, { status: 200 })
|
|
38
|
-
})
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
const options: ExtendedRequestInit = {
|
|
42
|
-
method: 'POST',
|
|
43
|
-
retries: 0,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const response = await request<{ message: string }>(
|
|
47
|
-
client.config,
|
|
48
|
-
url,
|
|
49
|
-
options
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
expect(response).toEqual(successResponse)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('should update the headers information available from config', async () => {
|
|
56
|
-
const url = `${client.config.apiUrl}/advanced/routes`
|
|
57
|
-
const successResponse = { message: 'it works' }
|
|
58
|
-
|
|
59
|
-
server.use(
|
|
60
|
-
http.post(url, async ({ request }) => {
|
|
61
|
-
expect(request.headers.get('x-lifi-api-key')).toEqual('mock-apikey')
|
|
62
|
-
expect(request.headers.get('x-lifi-integrator')).toEqual('lifi-sdk')
|
|
63
|
-
expect(request.headers.get('x-lifi-sdk')).toEqual(version)
|
|
64
|
-
expect(request.headers.get('x-lifi-userid')).toEqual('user-id')
|
|
65
|
-
expect(request.headers.get('x-lifi-widget')).toEqual(
|
|
66
|
-
'mock-widget-version'
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
return HttpResponse.json(successResponse, { status: 200 })
|
|
70
|
-
})
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
const options: ExtendedRequestInit = {
|
|
74
|
-
method: 'POST',
|
|
75
|
-
retries: 0,
|
|
76
|
-
headers: {
|
|
77
|
-
'x-lifi-api-key': 'mock-apikey',
|
|
78
|
-
'x-lifi-userid': 'user-id',
|
|
79
|
-
'x-lifi-widget': 'mock-widget-version',
|
|
80
|
-
},
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const response = await request<{ message: string }>(
|
|
84
|
-
client.config,
|
|
85
|
-
url,
|
|
86
|
-
options
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
expect(response).toEqual(successResponse)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
describe('when dealing with errors', () => {
|
|
93
|
-
it('should throw an error if the Integrator property is missing from the config', async () => {
|
|
94
|
-
const originalIntegrator = client.config.integrator
|
|
95
|
-
client.config.integrator = ''
|
|
96
|
-
|
|
97
|
-
const url = `${client.config.apiUrl}/advanced/routes`
|
|
98
|
-
|
|
99
|
-
await expect(
|
|
100
|
-
request<{ message: string }>(client.config, url, {
|
|
101
|
-
method: 'POST',
|
|
102
|
-
retries: 0,
|
|
103
|
-
})
|
|
104
|
-
).rejects.toThrowError(
|
|
105
|
-
new SDKError(
|
|
106
|
-
new ValidationError(
|
|
107
|
-
'You need to provide the Integrator property. Please see documentation https://docs.li.fi/integrate-li.fi-js-sdk/set-up-the-sdk'
|
|
108
|
-
)
|
|
109
|
-
)
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
client.config.integrator = originalIntegrator
|
|
113
|
-
})
|
|
114
|
-
it('should throw a error with when the request fails', async () => {
|
|
115
|
-
expect.assertions(2)
|
|
116
|
-
|
|
117
|
-
const url = `${client.config.apiUrl}/advanced/routes`
|
|
118
|
-
const errorResponse = { message: 'something went wrong on the server' }
|
|
119
|
-
|
|
120
|
-
server.use(
|
|
121
|
-
http.post(url, async () => {
|
|
122
|
-
return HttpResponse.json(errorResponse, { status: 400 })
|
|
123
|
-
})
|
|
124
|
-
)
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
await request<{ message: string }>(client.config, url, {
|
|
128
|
-
method: 'POST',
|
|
129
|
-
retries: 0,
|
|
130
|
-
})
|
|
131
|
-
} catch (e) {
|
|
132
|
-
expect((e as SDKError).name).toEqual('SDKError')
|
|
133
|
-
expect(((e as SDKError).cause as HTTPError).status).toEqual(400)
|
|
134
|
-
}
|
|
135
|
-
})
|
|
136
|
-
it('should throw a error and attempt retries when the request fails with a 500', async () => {
|
|
137
|
-
expect.assertions(2)
|
|
138
|
-
|
|
139
|
-
const url = `${client.config.apiUrl}/advanced/routes`
|
|
140
|
-
const errorResponse = { message: 'something went wrong on the server' }
|
|
141
|
-
|
|
142
|
-
server.use(
|
|
143
|
-
http.post(url, async () => {
|
|
144
|
-
return HttpResponse.json(errorResponse, { status: 500 })
|
|
145
|
-
})
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
await request<{ message: string }>(client.config, url, {
|
|
150
|
-
method: 'POST',
|
|
151
|
-
retries: 0,
|
|
152
|
-
})
|
|
153
|
-
} catch (e) {
|
|
154
|
-
expect((e as SDKError).name).toEqual('SDKError')
|
|
155
|
-
expect(((e as SDKError).cause as HTTPError).status).toEqual(500)
|
|
156
|
-
}
|
|
157
|
-
})
|
|
158
|
-
})
|
|
159
|
-
})
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { sleep } from './sleep.js'
|
|
3
|
-
|
|
4
|
-
describe('sleep', () => {
|
|
5
|
-
it('should wait for specified time', async () => {
|
|
6
|
-
const start = Date.now()
|
|
7
|
-
await sleep(50)
|
|
8
|
-
const end = Date.now()
|
|
9
|
-
|
|
10
|
-
expect(end - start).toBeGreaterThanOrEqual(45)
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
it('should return null', async () => {
|
|
14
|
-
const result = await sleep(10)
|
|
15
|
-
expect(result).toBeNull()
|
|
16
|
-
})
|
|
17
|
-
})
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
import { waitForResult } from './waitForResult.js'
|
|
3
|
-
|
|
4
|
-
describe('utils', () => {
|
|
5
|
-
describe('waitForResult', () => {
|
|
6
|
-
let mockedFunction: any
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
mockedFunction = vi.fn()
|
|
10
|
-
vi.useFakeTimers()
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
vi.useRealTimers()
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should throw immediately if shouldRetry returns false', async () => {
|
|
18
|
-
mockedFunction.mockImplementation(() => Promise.reject('some error'))
|
|
19
|
-
const shouldRetry = vi.fn().mockReturnValue(false)
|
|
20
|
-
|
|
21
|
-
const promise = waitForResult(mockedFunction, 1000, 3, shouldRetry)
|
|
22
|
-
|
|
23
|
-
await expect(promise).rejects.toThrowError('some error')
|
|
24
|
-
expect(mockedFunction).toHaveBeenCalledTimes(1)
|
|
25
|
-
expect(shouldRetry).toHaveBeenCalledWith(0, 'some error')
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should try until repeat function succeeds', async () => {
|
|
29
|
-
mockedFunction
|
|
30
|
-
.mockResolvedValueOnce(undefined)
|
|
31
|
-
.mockResolvedValueOnce(undefined)
|
|
32
|
-
.mockResolvedValueOnce('success!')
|
|
33
|
-
|
|
34
|
-
const promise = waitForResult(mockedFunction, 1000)
|
|
35
|
-
|
|
36
|
-
// Fast-forward through retries
|
|
37
|
-
for (let i = 0; i < 2; i++) {
|
|
38
|
-
await vi.advanceTimersByTimeAsync(1000)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const result = await promise
|
|
42
|
-
expect(result).toEqual('success!')
|
|
43
|
-
expect(mockedFunction).toHaveBeenCalledTimes(3)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('should respect the interval between retries', async () => {
|
|
47
|
-
mockedFunction
|
|
48
|
-
.mockResolvedValueOnce(undefined)
|
|
49
|
-
.mockResolvedValueOnce('success!')
|
|
50
|
-
|
|
51
|
-
const promise = waitForResult(mockedFunction, 2000)
|
|
52
|
-
|
|
53
|
-
await vi.advanceTimersByTimeAsync(2000)
|
|
54
|
-
const result = await promise
|
|
55
|
-
|
|
56
|
-
expect(result).toEqual('success!')
|
|
57
|
-
expect(mockedFunction).toHaveBeenCalledTimes(2)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
it('should throw an error if repeat function fails and maxRetries is reached', async () => {
|
|
61
|
-
mockedFunction.mockImplementation(() => Promise.reject('some error'))
|
|
62
|
-
const maxRetries = 2
|
|
63
|
-
|
|
64
|
-
const promise = waitForResult(mockedFunction, 1000, maxRetries)
|
|
65
|
-
const expectPromise = expect(promise).rejects.toThrowError('some error')
|
|
66
|
-
// Fast-forward through retries
|
|
67
|
-
for (let i = 0; i < maxRetries - 1; i++) {
|
|
68
|
-
await vi.advanceTimersByTimeAsync(1000)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
await expectPromise
|
|
72
|
-
expect(mockedFunction).toHaveBeenCalledTimes(maxRetries)
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
})
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
-
import { withDedupe } from './withDedupe.js'
|
|
3
|
-
|
|
4
|
-
describe('withDedupe', () => {
|
|
5
|
-
it('should execute function without caching when disabled', async () => {
|
|
6
|
-
const fn = vi.fn().mockResolvedValue('result')
|
|
7
|
-
const result = await withDedupe(fn, { enabled: false, id: 'test' })
|
|
8
|
-
|
|
9
|
-
expect(result).toBe('result')
|
|
10
|
-
expect(fn).toHaveBeenCalledOnce()
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
it('should cache and dedupe identical requests', async () => {
|
|
14
|
-
const fn = vi.fn().mockResolvedValue('result')
|
|
15
|
-
const id = 'test-id'
|
|
16
|
-
|
|
17
|
-
const promise1 = withDedupe(fn, { enabled: true, id })
|
|
18
|
-
const promise2 = withDedupe(fn, { enabled: true, id })
|
|
19
|
-
|
|
20
|
-
const [result1, result2] = await Promise.all([promise1, promise2])
|
|
21
|
-
|
|
22
|
-
expect(result1).toBe('result')
|
|
23
|
-
expect(result2).toBe('result')
|
|
24
|
-
expect(fn).toHaveBeenCalledOnce()
|
|
25
|
-
})
|
|
26
|
-
})
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.tsbuildinfo",
|
|
5
|
-
"composite": true,
|
|
6
|
-
"sourceMap": true,
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"moduleResolution": "Node",
|
|
9
|
-
"outDir": "./dist"
|
|
10
|
-
},
|
|
11
|
-
"include": ["./src/**/*", "./src/**/*.json"],
|
|
12
|
-
"exclude": [
|
|
13
|
-
"tests",
|
|
14
|
-
"./src/**/*.spec.ts",
|
|
15
|
-
"./src/**/*.mock.ts",
|
|
16
|
-
"./src/**/*.handlers.ts"
|
|
17
|
-
]
|
|
18
|
-
}
|