@@ -16,6 +16,22 @@ const textTypes: (string | RegExp)[] = [
1616 / ^ t e x t \/ .+ / ,
1717] ;
1818
19+ /**
20+ * Determines if the given object is a POJO.
21+ * @param obj Object under test.
22+ * @returns `true` if it is a POJO, or `false` otherwise.
23+ */
24+ function isPojo ( obj : unknown ) : obj is Record < string , any > {
25+ if ( obj === null || typeof obj !== 'object' ) {
26+ return false ;
27+ }
28+ const proto = Object . getPrototypeOf ( obj ) ;
29+ if ( proto == null ) {
30+ return true ;
31+ }
32+ return proto === Object . prototype ;
33+ }
34+
1935/**
2036 * # DrFetch
2137 *
@@ -230,4 +246,98 @@ export class DrFetch<T = unknown> {
230246 body
231247 } as T ;
232248 }
249+
250+ #processBody( body : BodyInit | null | Record < string , any > | undefined ) {
251+ let headers : Record < string , string > = { } ;
252+ if ( isPojo ( body ) || Array . isArray ( body ) ) {
253+ body = JSON . stringify ( body ) ;
254+ headers [ 'content-type' ] = 'application/json' ;
255+ }
256+ return [ body as BodyInit | null , headers ] as const ;
257+ }
258+
259+ /**
260+ * Shortcut method to emit a GET HTTP request.
261+ * @param url URL for the fetch function call.
262+ * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
263+ */
264+ get ( url : URL | string ) {
265+ return this . fetch ( url , { method : 'GET' } ) ;
266+ }
267+
268+ /**
269+ * Shortcut method to emit a POST HTTP request.
270+ * @param url URL for the fetch function call.
271+ * @param body The data to send as body.
272+ *
273+ * If a POJO is passed, it will be stringified and the `Content-Type` header of the request will be set to
274+ * `'application/json'`. This is also true with arrays.
275+ *
276+ * > **NOTE**: You must make sure that the POJO or the array (and its elements) you pass as body are serializable.
277+ *
278+ * Any other body type will not generate a `Content-Type` header and will be reliant on what the `fetch()` function
279+ * does in those cases.
280+ * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
281+ */
282+ post ( url : URL | string , body ?: BodyInit | null | Record < string , any > ) {
283+ const [ pBody , headers ] = this . #processBody( body ) ;
284+ return this . fetch ( url , { method : 'POST' , body : pBody , headers } ) ;
285+ }
286+
287+ /**
288+ * Shortcut method to emit a PATCH HTTP request.
289+ * @param url URL for the fetch function call.
290+ * @param body The data to send as body.
291+ *
292+ * If a POJO is passed, it will be stringified and the `Content-Type` header of the request will be set to
293+ * `'application/json'`. This is also true with arrays.
294+ *
295+ * > **NOTE**: You must make sure that the POJO or the array (and its elements) you pass as body are serializable.
296+ *
297+ * Any other body type will not generate a `Content-Type` header and will be reliant on what the `fetch()` function
298+ * does in those cases.
299+ * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
300+ */
301+ patch ( url : URL | string , body ?: BodyInit | null | Record < string , any > ) {
302+ const [ pBody , headers ] = this . #processBody( body ) ;
303+ return this . fetch ( url , { method : 'PATCH' , body : pBody , headers } ) ;
304+ }
305+
306+ /**
307+ * Shortcut method to emit a DELETE HTTP request.
308+ * @param url URL for the fetch function call.
309+ * @param body The data to send as body.
310+ *
311+ * If a POJO is passed, it will be stringified and the `Content-Type` header of the request will be set to
312+ * `'application/json'`. This is also true with arrays.
313+ *
314+ * > **NOTE**: You must make sure that the POJO or the array (and its elements) you pass as body are serializable.
315+ *
316+ * Any other body type will not generate a `Content-Type` header and will be reliant on what the `fetch()` function
317+ * does in those cases.
318+ * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
319+ */
320+ delete ( url : URL | string , body ?: BodyInit | null | Record < string , any > ) {
321+ const [ pBody , headers ] = this . #processBody( body ) ;
322+ return this . fetch ( url , { method : 'DELETE' , body : pBody , headers } ) ;
323+ }
324+
325+ /**
326+ * Shortcut method to emit a PUT HTTP request.
327+ * @param url URL for the fetch function call.
328+ * @param body The data to send as body.
329+ *
330+ * If a POJO is passed, it will be stringified and the `Content-Type` header of the request will be set to
331+ * `'application/json'`. This is also true with arrays.
332+ *
333+ * > **NOTE**: You must make sure that the POJO or the array (and its elements) you pass as body are serializable.
334+ *
335+ * Any other body type will not generate a `Content-Type` header and will be reliant on what the `fetch()` function
336+ * does in those cases.
337+ * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
338+ */
339+ put ( url : URL | string , body ?: BodyInit | null | Record < string , any > ) {
340+ const [ pBody , headers ] = this . #processBody( body ) ;
341+ return this . fetch ( url , { method : 'PUT' , body : pBody , headers } ) ;
342+ }
233343}
0 commit comments