aixigo / hal-http-client

A status code driven JSON HAL HTTP client based on the fetch API.
MIT License
3 stars 2 forks source link

allow to specify `fetch` request options #7

Closed x1B closed 7 years ago

x1B commented 7 years ago

For example to support CORS requests with an authorization cookie, the hal client should allow clients to specify fetch request options such as credentials.

It might be useful to group options by category ("safe", "unsafe", "all").

alex3683 commented 7 years ago

Implemented on master (v0.5.0).

New Feature

init options for fetch can now globally be passed to the HAL HTTP client's create function or for each request using one of the HTTP functions (get, post, ...) or the follow API. Set all options that should be passed to fetch on fetchInit of the options object. Available options can be found here. The keys method, headers and body will be removed, since their values are controlled directly by the HAL HTTP client.

Example 1:

const hal = halHttp.create( {
   headers: { 'accept-language': 'de' },
   fetchInit: {
      mode: 'cors'
   }
} );

Example 2:

const promise = hal.get( url( '/resource' ), {
   fetchInit: {
      cache: 'no-store'
   }
} );

Global init options will be merged with init options passed to one of the API methods, were the latter will win a collision.