Permalink
Browse files

fix(config): pre-populate defaults before invoking user config function

Fixes #222
  • Loading branch information...
carusology committed Nov 6, 2018
1 parent faab34f commit 950e49481255007637d15b8a4d233774aba286bb
Showing with 35 additions and 9 deletions.
  1. +2 −3 doc/configuration.md
  2. +9 −5 src/config.js
  3. +1 −1 src/rest.js
  4. +23 −0 test/config.spec.js
@@ -43,7 +43,6 @@ aurelia.use
config.registerEndpoint('twitter', configure => {
configure
.withBaseUrl('https://api.twitter.io/')
.withDefaults(null); // use endpoint defaults
//.withDefaults({headers: {x: 'foo'}}); // uses own defaults
//.withDefaults(); // no defaults. same as omitting withDefaults()
});
@@ -56,7 +55,7 @@ aurelia.use
.registerEndpoint('auth', 'https://auth.myapi.org/')
.setDefaultEndpoint('auth');
});
// 8: Set Default BaseUrl
config.setDefaultBaseUrl('https://myapi.org/');
```
@@ -119,4 +118,4 @@ All methods return `this`, allowing you to chain the calls.

## 8: Set default base URL

All endpoints registered after this call will use this default base URL, rather than the current host URL.
All endpoints registered after this call will use this default base URL, rather than the current host URL.
@@ -1,4 +1,4 @@
import {HttpClient} from 'aurelia-fetch-client';
import {HttpClient, HttpClientConfiguration} from 'aurelia-fetch-client';
import {Rest} from './rest';

/**
@@ -68,12 +68,16 @@ export class Config {

// Manual configure of client.
if (typeof configureMethod === 'function') {
newClient.configure(configureMethod);
newClient.configure(
(newClientConfig: HttpClientConfiguration) => {
return configureMethod(
newClientConfig.withDefaults(this.endpoints[name].defaults)
);
}
);

// transfer user defaults from http-client to endpoint
if (typeof newClient.defaults === 'object' && newClient.defaults !== null) {
this.endpoints[name].defaults = newClient.defaults;
}
this.endpoints[name].defaults = newClient.defaults;

return this;
}
@@ -69,7 +69,7 @@ export class Rest {
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
request(method: string, path: string, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
let requestOptions = extend(true, {headers: {}}, this.defaults, options || {}, {method, body});
let requestOptions = extend(true, {headers: {}}, this.defaults || {}, options || {}, {method, body});
let contentType = requestOptions.headers['Content-Type'] || requestOptions.headers['content-type'];

// if body is object, stringify to json or urlencoded depending on content-type
@@ -42,6 +42,29 @@ describe('Config', function() {
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
expect(returned).toBe(config);
});

it('Should properly register an endpoint with standard defaults when config functions are applied.', function() {
let config = new Config;
let returned = config.registerEndpoint('api', function(configure) {
configure.withBaseUrl(baseUrls.api);
});

expect(config.endpoints.api.defaults).toEqual(defaultOptions);
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
expect(returned).toBe(config);
});

it('Should properly register an endpoint with null defaults when defaults specified as "null".', function() {
let config = new Config;
let returned = config.registerEndpoint('api', function(configure) {
configure.withBaseUrl(baseUrls.api);
configure.withDefaults(null);
});

expect(config.endpoints.api.defaults).toEqual(null);
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
expect(returned).toBe(config);
});
});

describe('.getEndpoint()', function() {

0 comments on commit 950e494

Please sign in to comment.