-
-
Notifications
You must be signed in to change notification settings - Fork 99
chore: migrate axios to fetch #1609
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
Open
yifancong
wants to merge
5
commits into
main
Choose a base branch
from
chore/axios-migrate-to-fetch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a49c37
chore: migrate axios to fetch
yifancong 5668acc
chore: migrate axios to fetch
yifancong 550d18a
fix(utils): align fetch helper deps and implementation
yifancong fa9e83b
chore: update fetch utility
yifancong 2127583
chore: update rsbuild config
yifancong 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
Some comments aren't visible on the classic Files Changed page.
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,9 @@ | ||
| import { Fetch } from '@rsdoctor/utils/common'; | ||
|
|
||
| /** Thin wrapper so tests can `rs.mock('./fetch-http')` without depending on `Fetch.getFetch` internals. */ | ||
| export function fetchWithTimeout( | ||
| url: string, | ||
| options: Parameters<typeof Fetch.fetchWithTimeout>[1], | ||
| ) { | ||
| return Fetch.fetchWithTimeout(url, options); | ||
| } |
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,63 @@ | ||
| import { afterEach, describe, expect, it, rs } from '@rstest/core'; | ||
|
|
||
| const { fetchWithTimeoutMock } = rs.hoisted(() => ({ | ||
| fetchWithTimeoutMock: rs.fn(), | ||
| })); | ||
|
|
||
| rs.mock('./fetch-http', () => ({ | ||
| fetchWithTimeout: fetchWithTimeoutMock, | ||
| })); | ||
|
|
||
| import { fetchText, loadJSON } from './utils'; | ||
|
|
||
| describe('cli utils', () => { | ||
| afterEach(() => { | ||
| fetchWithTimeoutMock.mockReset(); | ||
| }); | ||
|
|
||
| it('fetchText() returns response text for 2xx', async () => { | ||
| fetchWithTimeoutMock.mockResolvedValue({ | ||
| ok: true, | ||
| text: async () => 'plain text', | ||
| } as Response); | ||
|
|
||
| const result = await fetchText('https://example.com/file.txt'); | ||
|
|
||
| expect(result).toBe('plain text'); | ||
| expect(fetchWithTimeoutMock).toBeCalledTimes(1); | ||
| expect(fetchWithTimeoutMock).toBeCalledWith( | ||
| 'https://example.com/file.txt', | ||
| expect.objectContaining({ | ||
| timeout: 60000, | ||
| headers: { | ||
| Accept: 'text/plain; charset=utf-8', | ||
| 'Accept-Encoding': 'gzip,deflate,compress', | ||
| }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('fetchText() throws when response is non-2xx', async () => { | ||
| fetchWithTimeoutMock.mockRejectedValue( | ||
| new Error('Request failed with status 404'), | ||
| ); | ||
|
|
||
| await expect(fetchText('https://example.com/missing.txt')).rejects.toThrow( | ||
| 'Request failed with status 404', | ||
| ); | ||
| }); | ||
|
|
||
| it('loadJSON() parses remote json text', async () => { | ||
| fetchWithTimeoutMock.mockResolvedValue({ | ||
| ok: true, | ||
| text: async () => '{"id":7,"name":"remote"}', | ||
| } as Response); | ||
|
|
||
| const data = await loadJSON<{ id: number; name: string }>( | ||
| 'https://example.com/data.json', | ||
| process.cwd(), | ||
| ); | ||
|
|
||
| expect(data).toStrictEqual({ id: 7, name: 'remote' }); | ||
| }); | ||
| }); |
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
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,61 @@ | ||
| import { afterEach, describe, expect, it, rs } from '@rstest/core'; | ||
| import { fetchJSONByUrl, postServerAPI } from './request'; | ||
|
|
||
| describe('request utils', () => { | ||
| const originalFetch = globalThis.fetch; | ||
| const originalNodeEnv = process.env.NODE_ENV; | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| process.env.NODE_ENV = originalNodeEnv; | ||
| }); | ||
|
|
||
| it('fetchJSONByUrl() parses json text payload', async () => { | ||
| const fetchMock = rs.fn().mockResolvedValue({ | ||
| ok: true, | ||
| text: async () => '{"name":"rsdoctor"}', | ||
| }); | ||
| globalThis.fetch = fetchMock as typeof fetch; | ||
| process.env.NODE_ENV = 'production'; | ||
|
|
||
| const data = await fetchJSONByUrl('https://example.com/manifest.json'); | ||
|
|
||
| expect(data).toStrictEqual({ name: 'rsdoctor' }); | ||
| expect(fetchMock).toBeCalledTimes(1); | ||
| }); | ||
|
|
||
| it('fetchJSONByUrl() throws for non-2xx response', async () => { | ||
| const fetchMock = rs.fn().mockResolvedValue({ | ||
| ok: false, | ||
| status: 500, | ||
| }); | ||
| globalThis.fetch = fetchMock as typeof fetch; | ||
| process.env.NODE_ENV = 'production'; | ||
|
|
||
| await expect( | ||
| fetchJSONByUrl('https://example.com/manifest.json'), | ||
| ).rejects.toThrow('Request failed with status 500'); | ||
| }); | ||
|
|
||
| it('postServerAPI() sends json body and parses response json', async () => { | ||
| const fetchMock = rs.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => ({ ok: true }), | ||
| }); | ||
| globalThis.fetch = fetchMock as typeof fetch; | ||
| process.env.NODE_ENV = 'production'; | ||
|
|
||
| const result = await (postServerAPI as any)('/api/demo', { id: 1 }); | ||
| const [url, init] = fetchMock.mock.calls[0]; | ||
|
|
||
| expect(url).toContain('/api/demo?_t='); | ||
| expect(init).toMatchObject({ | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ id: 1 }), | ||
| }); | ||
| expect(result).toStrictEqual({ ok: true }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.