Skip to content

Commit 0e0aa4a

Browse files
committed
Accept URL as input
1 parent 186f864 commit 0e0aa4a

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.14.1 (2022/07/08)
2+
3+
- Accept URL as input
4+
15
## 0.14.0 (2022/07/08)
26

37
- Ship again ESM modules, https://github.com/rollup/rollup/wiki/pkg.module

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,18 @@ Check [examples/web](examples/web)
104104

105105
## API
106106

107-
- `get(input:` [`RequestInfo`](https://fetch.spec.whatwg.org/#requestinfo)`, init?:` [`RequestInit`](https://fetch.spec.whatwg.org/#requestinit)`): ResponsePromiseWithBodyMethods`
107+
- `get(input:` [`RequestInfo`](https://fetch.spec.whatwg.org/#requestinfo)` | URL, init?:` [`RequestInit`](https://fetch.spec.whatwg.org/#requestinit)`): ResponsePromiseWithBodyMethods`
108108

109-
- `post(input: RequestInfo, body?:` [`BodyInit`](https://fetch.spec.whatwg.org/#bodyinit)`, init?: RequestInit): ResponsePromiseWithBodyMethods`
110-
- `postJSON(input: RequestInfo, body: object, init?: RequestInit): ResponsePromiseWithBodyMethods`
109+
- `post(input: RequestInfo | URL, body?:` [`BodyInit`](https://fetch.spec.whatwg.org/#bodyinit)`, init?: RequestInit): ResponsePromiseWithBodyMethods`
110+
- `postJSON(input: RequestInfo | URL, body: object, init?: RequestInit): ResponsePromiseWithBodyMethods`
111111

112-
- `put(input: RequestInfo, body?: BodyInit, init?: RequestInit): ResponsePromiseWithBodyMethods`
113-
- `putJSON(input: RequestInfo, body: object, init?: RequestInit): ResponsePromiseWithBodyMethods`
112+
- `put(input: RequestInfo | URL, body?: BodyInit, init?: RequestInit): ResponsePromiseWithBodyMethods`
113+
- `putJSON(input: RequestInfo | URL, body: object, init?: RequestInit): ResponsePromiseWithBodyMethods`
114114

115-
- `patch(input: RequestInfo, body?: BodyInit, init?: RequestInit): ResponsePromiseWithBodyMethods`
116-
- `patchJSON(input: RequestInfo, body: object, init?: RequestInit): ResponsePromiseWithBodyMethods`
115+
- `patch(input: RequestInfo | URL, body?: BodyInit, init?: RequestInit): ResponsePromiseWithBodyMethods`
116+
- `patchJSON(input: RequestInfo | URL, body: object, init?: RequestInit): ResponsePromiseWithBodyMethods`
117117

118-
- `del(input: RequestInfo, init?: RequestInit): ResponsePromiseWithBodyMethods`
118+
- `del(input: RequestInfo | URL, init?: RequestInit): ResponsePromiseWithBodyMethods`
119119

120120
- `isJSONResponse(response: `[`Response`](https://fetch.spec.whatwg.org/#response)`): boolean`
121121

src/Http.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ test('get()', async () => {
127127
expect(response).toEqual('GET');
128128
});
129129

130+
test('get() with new URL()', async () => {
131+
server.get(path, (request, reply) => {
132+
expect(request.headers['content-type']).toBeUndefined();
133+
expect(request.headers['content-length']).toBeUndefined();
134+
expect(request.body).toBeNull();
135+
reply.send(request.method);
136+
});
137+
const url = await server.listen(0);
138+
139+
const response = await get(new URL(url)).text();
140+
expect(response).toEqual('GET');
141+
});
142+
130143
test('multiple fetch()', async () => {
131144
server.get(path, (request, reply) => {
132145
reply.send(request.method);

src/Http.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
9090
// - Chrome 76: "TypeError: Failed to fetch"
9191
// - DOMException if request aborted
9292
function request<T extends BodyInit>(
93-
input: RequestInfo,
93+
input: RequestInfo | URL,
9494
headers: Headers,
9595
init: Omit<Init, 'headers'> | undefined,
9696
method: Method,
@@ -142,7 +142,7 @@ function getJSONHeaders(init?: Init) {
142142
/**
143143
* Performs a HTTP `GET` request.
144144
*/
145-
export function get(input: RequestInfo, init?: Init) {
145+
export function get(input: RequestInfo | URL, init?: Init) {
146146
return request(input, getHeaders(init), init, 'GET');
147147
}
148148

@@ -151,7 +151,7 @@ export function get(input: RequestInfo, init?: Init) {
151151
*
152152
* @see {@link postJSON()}
153153
*/
154-
export function post<T extends BodyInit>(input: RequestInfo, body?: T, init?: Init) {
154+
export function post<T extends BodyInit>(input: RequestInfo | URL, body?: T, init?: Init) {
155155
return request(input, getHeaders(init), init, 'POST', body);
156156
}
157157

@@ -166,7 +166,7 @@ export function post<T extends BodyInit>(input: RequestInfo, body?: T, init?: In
166166
//
167167
// Record<string, unknown> is compatible with "type" not with "interface": "Index signature is missing in type 'MyInterface'"
168168
// Best alternative is object, why? https://stackoverflow.com/a/58143592
169-
export function postJSON<T extends object>(input: RequestInfo, body: T, init?: Init) {
169+
export function postJSON<T extends object>(input: RequestInfo | URL, body: T, init?: Init) {
170170
return request(input, getJSONHeaders(init), init, 'POST', JSON.stringify(body));
171171
}
172172
// No need to have postFormData() and friends: the browser already sets the proper request content type
@@ -177,7 +177,7 @@ export function postJSON<T extends object>(input: RequestInfo, body: T, init?: I
177177
*
178178
* @see {@link putJSON()}
179179
*/
180-
export function put<T extends BodyInit>(input: RequestInfo, body?: T, init?: Init) {
180+
export function put<T extends BodyInit>(input: RequestInfo | URL, body?: T, init?: Init) {
181181
return request(input, getHeaders(init), init, 'PUT', body);
182182
}
183183

@@ -186,7 +186,7 @@ export function put<T extends BodyInit>(input: RequestInfo, body?: T, init?: Ini
186186
*
187187
* @see {@link put()}
188188
*/
189-
export function putJSON<T extends object>(input: RequestInfo, body: T, init?: Init) {
189+
export function putJSON<T extends object>(input: RequestInfo | URL, body: T, init?: Init) {
190190
return request(input, getJSONHeaders(init), init, 'PUT', JSON.stringify(body));
191191
}
192192

@@ -195,7 +195,7 @@ export function putJSON<T extends object>(input: RequestInfo, body: T, init?: In
195195
*
196196
* @see {@link patchJSON()}
197197
*/
198-
export function patch<T extends BodyInit>(input: RequestInfo, body?: T, init?: Init) {
198+
export function patch<T extends BodyInit>(input: RequestInfo | URL, body?: T, init?: Init) {
199199
return request(input, getHeaders(init), init, 'PATCH', body);
200200
}
201201

@@ -204,14 +204,14 @@ export function patch<T extends BodyInit>(input: RequestInfo, body?: T, init?: I
204204
*
205205
* @see {@link patch()}
206206
*/
207-
export function patchJSON<T extends object>(input: RequestInfo, body: T, init?: Init) {
207+
export function patchJSON<T extends object>(input: RequestInfo | URL, body: T, init?: Init) {
208208
return request(input, getJSONHeaders(init), init, 'PATCH', JSON.stringify(body));
209209
}
210210

211211
/**
212212
* Performs a HTTP `DELETE` request.
213213
*/
214214
// Cannot be named delete :-/
215-
export function del(input: RequestInfo, init?: Init) {
215+
export function del(input: RequestInfo | URL, init?: Init) {
216216
return request(input, getHeaders(init), init, 'DELETE');
217217
}

0 commit comments

Comments
 (0)