Skip to content

Commit a973b7d

Browse files
committed
feat: Add the head() shortcut method
1 parent 4dca3d9 commit a973b7d

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ const localFetcher = rootFetcher.clone(true, { fetchFn: false }); // Identical p
185185

186186
> Since **v0.3.0**
187187
188-
`DrFetch` objects now provide the shortcut functions `get`, `post`, `patch`, `put` and `delete`. Except for `get`, all
189-
these accept a body parameter. When this body is a POJO or an array, the body is stringified and the `Content-Type`
190-
header is given the value `application/json`. If a body of any other type is given (that the `fetch()` function
191-
accepts, such as `FormData`), no headers are explicitly specified and therefore it is up to what `fetch()` (or the
192-
custom data-fetching function you provide) does in these cases.
188+
`DrFetch` objects now provide the shortcut functions `get`, `head`, `post`, `patch`, `put` and `delete`. Except for
189+
`get` and `head`, all these accept a body parameter. When this body is a POJO or an array, the body is stringified and
190+
the `Content-Type` header is given the value `application/json`. If a body of any other type is given (that the
191+
`fetch()` function accepts, such as `FormData`), no headers are explicitly specified and therefore it is up to what
192+
`fetch()` (or the custom data-fetching function you provide) does in these cases.
193193

194194
```typescript
195195
import type { Todo } from './myTypes.js';
@@ -315,4 +315,4 @@ expect([...makeIterableHeaders(myHeaders)].length).to.equal(2);
315315

316316
Why are you a weird fellow/gal? Anyway, prejudice aside, body typing will mean nothing to you, so forget about `for()`
317317
and anything else regarding types. Do your custom data-fetching function, add your custom body parsers and fetch away
318-
using `.fetch()`, `.get()`, `.post()`, `.put()`, `.patch()` or `.delete()`.
318+
using `.fetch()`, `.get()`, `head()`, `.post()`, `.put()`, `.patch()` or `.delete()`.

src/DrFetch.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ export class DrFetch<T = unknown> {
266266
return this.fetch(url, { method: 'GET' });
267267
}
268268

269+
/**
270+
* Shortcut method to emit a HEAD HTTP request.
271+
* @param url URL for the fetch function call.
272+
* @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
273+
*/
274+
head(url: URL | string) {
275+
return this.fetch(url, { method: 'HEAD' });
276+
}
277+
269278
/**
270279
* Shortcut method to emit a POST HTTP request.
271280
* @param url URL for the fetch function call.

src/tests/DrFetch.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const shortcutMethodsWithBody = [
1212

1313
const allShortcutMethods = [
1414
'get',
15+
'head',
1516
...shortcutMethodsWithBody
1617
] as const;
1718

0 commit comments

Comments
 (0)