Skip to content

Commit 5d528d5

Browse files
committed
Add possible pass URL to request function.
1 parent c6e376b commit 5d528d5

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
Next release
5+
------------
6+
7+
* Add support `URL` instance for `request` helper.
8+
49
v1.0.8
510
------
611

src/browser/request.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ function parseXhrHeaders(raw: string): Headers {
9898
return headers;
9999
}
100100

101-
export function request(url: string): Promise<Response>;
102-
export function request(url: string, init?: RequestInit): Promise<Response>;
103-
export function request(url: string, init: UploadRequestInit): Promise<Response>;
101+
export function request(url: URL | string): Promise<Response>;
102+
export function request(url: URL | string, init?: RequestInit): Promise<Response>;
103+
export function request(url: URL | string, init: UploadRequestInit): Promise<Response>;
104+
105+
export function request(url: URL | string, init?: unknown): Promise<Response> {
106+
if (url instanceof URL) {
107+
url = url.href;
108+
}
104109

105-
export function request(url: string, init?: unknown): Promise<Response> {
106110
if (init && isUploadRequestInit(init)) {
107111
return xhrRequest(url, init);
108112
}

tests/browser/request.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ describe('request', () => {
2020
expect(response).toStrictEqual(new Response(null, {status: status}));
2121
});
2222

23+
it('succeeds with URL instance', async () => {
24+
await request(new URL('/path?k=v', 'https://foo.local'));
25+
26+
expect(fetchMock).toHaveBeenCalledOnce();
27+
expect(fetchMock).toHaveBeenCalledWith('https://foo.local/path?k=v', undefined);
28+
});
29+
2330
it('succeeds for GET request via fetch with custom options', async () => {
2431
const resultResponse = new Response(null, {
2532
status: 200,

0 commit comments

Comments
 (0)