-
Notifications
You must be signed in to change notification settings - Fork 12
Add timeout parameter to request #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
992d65e
feat: add timeout functionality to ApiClient request methods
damian-rakus b9425c0
Merge branch 'main' into feature/damianra/add-timeout-param
damian-rakus f5a9e4d
Add propert integration tests
damian-rakus aa7db7d
Cleanup nock after each test
damian-rakus fd26714
Downgrade nock to be compatible with node 18.18.1
damian-rakus 1fa9f67
Revert changes to api-client.test.ts
damian-rakus 6f003cb
Add missing comma
damian-rakus 77edf1b
Use const with endpoint instead of hardcoding it in tests
damian-rakus da6a899
Add timeout + option validation
damian-rakus a4e8797
Add test that validate options parsing
damian-rakus 6802d43
Update version and changelog
damian-rakus 1e72da5
Update zod version to be compatible with typescript
damian-rakus 3aa7f74
Add note
damian-rakus 5e59693
Push changelog
damian-rakus 406b640
Move the method to api-client.ts
damian-rakus edc8eff
Improve code
damian-rakus 8dbb1a8
Add comments
damian-rakus 57b31d3
Add example
damian-rakus 3092705
Remove clearAllMocks manually
damian-rakus 17e82d9
Adjust test cases
damian-rakus 28e4f88
Add to README
damian-rakus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import nock from 'nock'; | ||
| import { ApiClient } from '../lib/api-client'; | ||
| import { MONDAY_API_ENDPOINT } from '../lib/constants'; | ||
|
|
||
| describe('ApiClient timeout integration', () => { | ||
| beforeAll(() => { | ||
| nock.disableNetConnect(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| nock.abortPendingRequests(); | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| nock.restore(); | ||
| nock.enableNetConnect(); | ||
| }); | ||
|
RomKadria marked this conversation as resolved.
|
||
|
|
||
| describe('request method', () => { | ||
| it('should abort request when timeout is exceeded', async () => { | ||
| nock(MONDAY_API_ENDPOINT) | ||
| .post('') | ||
| .delay(2000) | ||
| .reply(200, { data: { users: [] } }); | ||
|
|
||
| const apiClient = new ApiClient({ token: 'test-token' }); | ||
| const query = '{ users { id } }'; | ||
|
|
||
| await expect(apiClient.request(query, undefined, { timeout: 100 })).rejects.toThrow('The user aborted a request.'); | ||
| }); | ||
|
|
||
| it('should complete successfully when response arrives before timeout', async () => { | ||
| nock(MONDAY_API_ENDPOINT) | ||
| .post('') | ||
| .delay(50) | ||
| .reply(200, { data: { users: [{ id: '1' }] } }); | ||
|
|
||
| const apiClient = new ApiClient({ token: 'test-token' }); | ||
| const query = '{ users { id } }'; | ||
|
|
||
| const result = await apiClient.request(query, undefined, { timeout: 500 }); | ||
| expect(result).toEqual({ users: [{ id: '1' }] }); | ||
| }); | ||
|
|
||
| it('should work without timeout option', async () => { | ||
| nock(MONDAY_API_ENDPOINT) | ||
| .post('') | ||
| .reply(200, { data: { users: [{ id: '1', name: 'John' }] } }); | ||
|
|
||
| const apiClient = new ApiClient({ token: 'test-token' }); | ||
| const query = '{ users { id name } }'; | ||
|
|
||
| const result = await apiClient.request(query); | ||
| expect(result).toEqual({ users: [{ id: '1', name: 'John' }] }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('rawRequest method', () => { | ||
| it('should abort rawRequest when timeout is exceeded', async () => { | ||
| nock(MONDAY_API_ENDPOINT) | ||
| .post('') | ||
| .delay(2000) | ||
| .reply(200, { data: { users: [] } }); | ||
|
|
||
| const apiClient = new ApiClient({ token: 'test-token' }); | ||
| const query = '{ users { id } }'; | ||
|
|
||
| await expect(apiClient.rawRequest(query, undefined, { timeout: 100 })).rejects.toThrow('The user aborted a request.'); | ||
| }); | ||
|
|
||
| it('should complete rawRequest successfully when response arrives before timeout', async () => { | ||
| nock(MONDAY_API_ENDPOINT) | ||
| .post('') | ||
| .delay(50) | ||
| .reply(200, { data: { users: [{ id: '1' }] } }); | ||
|
|
||
| const apiClient = new ApiClient({ token: 'test-token' }); | ||
| const query = '{ users { id } }'; | ||
|
|
||
| const result = await apiClient.rawRequest(query, undefined, { timeout: 500 }); | ||
| expect(result.data).toEqual({ users: [{ id: '1' }] }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.