Permalink
Browse files

feat(configure): allow configuration with an object

  • Loading branch information...
doktordirk committed Sep 12, 2016
1 parent b76ce0d commit 6266e3e99d5dbd16ddf37442a4ce67d822401bb8
Showing with 89 additions and 2 deletions.
  1. +25 −0 doc/configuration.md
  2. +6 −2 src/aurelia-api.js
  3. +29 −0 src/config.js
  4. +29 −0 test/config.spec.js
@@ -15,6 +15,7 @@ defaults = {

Ways to register your endpoints in `main.js`:

## Configuration with a function
```js
aurelia.use
/* Your other plugins and init code */
@@ -50,6 +51,30 @@ aurelia.use
config.setDefaultBaseUrl('https://myapi.org/');
```


## Configuration with an object

```js
aurelia.use
/* Your other plugins and init code */
.plugin('aurelia-api', {
endpoints: [
// 1: Current host
{name: 'api'},
// 2: Specific host
{name: 'api', endpoint: 'https://myapi.org/'},
// 3: With different endpoint defaults
{name: 'weather', endpoint: 'https://weatherapi.io/', config: {headers: {x: 'foo'}}},
// 4: Without endpoint defaults
{name: 'weather', endpoint: 'https://weatherapi.io/', config: null};
],
// 6: Set default endppoint. alternatively, add default: true above
defaultEndpoint: 'api',
// 8: Set Default BaseUrl
defaultBaseUrl: 'https://myapi.org/'
})
```

Here's a more detailed explanation for every method of registering used:

## 1: Default host name
@@ -1,8 +1,12 @@
import {Aurelia} from 'aurelia-framework';
import {Config} from './config';

export function configure(aurelia: Aurelia, configCallback: Function): void {
export function configure(aurelia: Aurelia, configOrConfigure: Function): void {
let config = aurelia.container.get(Config);

configCallback(config);
if (typeof configOrConfigure === 'function') {
return configOrConfigure(config);
}

config.configure(configOrConfigure);
}
@@ -128,4 +128,33 @@ export class Config {

return this;
}


/**
* Configure with an object
*
* @param {{}} config The configuration obejct
*
* @return {Config}
* @chainable
*/
configure(config: {}): Config {
if (config.defaultBaseUrl) {
this.defaultBaseUrl = config.defaultBaseUrl;
}

config.endpoints.forEach(endpoint => {
this.registerEndpoint(endpoint.name, endpoint.endpoint, endpoint.config);

if (endpoint.default) {
this.setDefaultEndpoint(endpoint.name);
}
});

if (config.defaultEndpoint) {
this.setDefaultEndpoint(config.defaultEndpoint);
}

return this;
}
}
@@ -104,6 +104,27 @@ describe('Config', function() {
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
});
});

describe('.configure()', function() {
it('Should properly configure with an object.', function() {
let config = new Config;
let returned = config.configure(configObject);

expect(returned).toBe(config);

expect(config.endpoints.github.defaults).toEqual(defaultOptions);
expect(config.endpoints.github.client.baseUrl).toEqual(baseUrls.github);

expect(config.endpoints.boring.defaults).toEqual(defaultOptions);
expect(config.endpoints.boring.client.baseUrl).toEqual('');

expect(config.endpoints.api.defaults).toEqual(userOptions);
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);

expect(config.defaultEndpoint.defaults).toEqual(defaultOptions);
expect(config.defaultEndpoint.client.baseUrl).toEqual(baseUrls.github);
});
});
});

let baseUrls = {
@@ -121,3 +142,11 @@ let userOptions = {
'headers': {
'x-scope': 'Tests'
}};

let configObject = {
endpoints: [
{name: 'boring'},
{name: 'github', endpoint: baseUrls.github, default: true},
{name: 'api', endpoint: baseUrls.api, config: userOptions}
]
};

0 comments on commit 6266e3e

Please sign in to comment.