ngnjs/net

NGN.NET Resource Configuration: Add "mode" option

gbdrummer opened this issue · 3 comments

Is your feature request related to a problem? Please describe.
When developing locally it is sometimes helpful to be able to disable CORS. There isn't a simple, easy way to do that that I'm aware of at present.

Describe the solution you'd like
It would useful to have a global mode configuration option when instantiating a Resource. This would accept any of the values described here: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode and apply them to all requests made by the Resource.

Describe alternatives you've considered
NONE

Additional context
NONE

Related to #8.

Currently, only a few of the fetch options are implemented: headers and partial caching (can prevent caching, but not configure the mode). The following options could be supported:

mode
The mode you want to use for the request, e.g., cors, no-cors, or same-origin.

credentials
The request credentials you want to use for the request: omit, same-origin, or include. To automatically send cookies for the current domain, this option must be provided. Starting with Chrome 50, this property also takes a FederatedCredential instance or a PasswordCredential instance.

cache
The cache mode you want to use for the request.

redirect
The redirect mode to use: follow (automatically follow redirects), error (abort with an error if a redirect occurs), or manual (handle redirects manually). In Chrome the default is follow (before Chrome 47 it defaulted to manual).

referrer
A USVString specifying the referrer of the request. This can be a same-origin URL, about:client, or an empty string.
referrerPolicy

The other options only make sense on a per-request basis, so creating a "globally" applicable configuration attribute will be more difficult to work with (and therefore will not be added).

--

The aforementioned attributes could each have their own individual property (i.e. each one is a resource configuration option), such as:

const API = new Resource({
  mode: 'no-cors',
  credentials: 'same-origin',
  cache: 'no-cache',
  redirect: 'follow', // this is already the default behavior
  referrer: 'about:client'
})

The other option is to have a single options attribute, such as:

const API = new Resource({
  options: {
    mode: 'no-cors',
    credentials: 'same-origin',
    cache: 'no-cache',
    redirect: 'follow', // this is already the default behavior
    referrer: 'about:client'
  }
})

The benefit of the second approach is it is easier to implement and will have a smaller footprint. The disadvantage is it is less intuitive and breaks form with the current approach (which is more like the prior example).

I feel like the first option would be best, to remain consistent with how the current configuration is handled. It also seems more intuitive than needing to know to add an options attribute.

I ended up implementing the individual attributes, so the following should work:

const API = new Resource({
  mode: 'no-cors',
  credentials: 'same-origin',
  cache: 'no-cache',
  redirect: 'follow', // this is already the default behavior
  referrer: 'about:client'
})

Keep in mind, using mode: 'no-cors' adheres to the corresponding fetch request mode attribute, meaning it will not return data in JavaScript when CORS is not applied and should be. See https://developer.mozilla.org/en-US/docs/Web/API/Request/mode.