SpoonX/aurelia-api

Default headers are not applied with configuration type 5

Closed this issue · 2 comments

obust commented

Hi, I am trying to configure the aurelia-api plugin to communicate with an API that communicate with the JSON API SPEC.
There are two ways to configure defaults for the fetch client https://aurelia-api.spoonx.org/configuration.html (configuration 3 and 5) but only the configuration 3 works with me.

// configuration type 3
aurelia.use
    .standardConfiguration()
    .feature('resources')
    .plugin('aurelia-api', config => {
      config
        .registerEndpoint('api', `${environment.apiBaseUrl}/`, {
          headers: {
            'Content-Type': 'application/vnd.api+json',
            'Accept': 'application/vnd.api+json'
          }
        })
        .setDefaultEndpoint('api');
    });

// configuration type 5
aurelia.use
    .standardConfiguration()
    .feature('resources')
    .plugin('aurelia-api', config => {
      config
        .registerEndpoint('api', fetchConfig => {
          fetchConfig
            .withBaseUrl(`${environment.apiBaseUrl}/`)
            .withDefaults({
              'headers': {
                'Content-Type': 'application/vnd.api+json',
                'Accept': 'application/vnd.api+json'
              }
            });
          })
        .setDefaultEndpoint('api');
    })

If I run my request with configuration 3, the headers are defined properly, however if I run my request with configuration 5 the Content-Type and Accept headers are set to the classic application/json and thus my request fail.

If I log the HttpClient to the console before the request is made, we can see that the defaults are properly saved, but apparently they are not used

HttpClient {activeRequestCount: 0, isRequesting: false, isConfigured: true, baseUrl: "http://localhost:8000/", defaults: Object…}
activeRequestCount : 0
baseUrl : "http://localhost:8000/"
defaults : Object
  headers : Object
    Accept : "application/vnd.api+json"
    Content-Type : "application/vnd.api+json"
    __proto__ : Object
  __proto__ : Object
interceptors : Array(1)
isConfigured : true
isRequesting : false

I would rather use the configuration 5 because it allows me to easily add custom interceptors (for example, automatically parse the request to json which is not done by default with this content-type).

I am doing something wrong or is there really a bug hiding there ?

There might be a bug hidden. The whole content-type part was written poorly in my opinion.

https://github.com/SpoonX/aurelia-api/blob/master/src/rest.js#L75

The options should be there. Could you figure this out and fix it? Otherwise, could you perhaps create a repository (with a tiny simple server) so I can reproduce and fix it? I'm very limited in time now (that seems to be the new fashion).

Closing this for now. If this is still relevant, feel free to reopen the issue.

Note: this is a copy-paste comment to clean up the issues. It's not personal, and I didn't review the issue before posting this. They're all outdated and I need a way to figure out what's still relevant.

Thank you for understanding :)