Permalink
Browse files

fix(config): apply user endpoint defaults

  • Loading branch information...
doktordirk committed Mar 23, 2016
1 parent b426dd8 commit 8b9344d7cbac3ccd15f1cf05c8881d13b1e91c72
Showing with 69 additions and 50 deletions.
  1. +18 −6 README.md
  2. +5 −6 src/config.js
  3. +10 −11 src/rest.js
  4. +1 −1 test/aurelia-api.spec.js
  5. +34 −22 test/config.spec.js
  6. +0 −2 test/endpoint.spec.js
  7. +1 −2 test/rest.spec.js
@@ -25,18 +25,18 @@ You can then pass these clients to other libs when needed.
Make sure your project uses a `main.js` file to initialize aurelia.
In your configure function, you might do something like this:

```javascript
```js
aurelia.use
/* Your other plugins and init code */
.plugin('spoonx/aurelia-api', config => {
// Current host
config.registerEndpoint('api');
// Different host
config.registerEndpoint('api', 'https://myapi.org/');
// With defaults
// With endpoint defaults
config.registerEndpoint('api', 'https://myapi.org/', {headers: {x:'foo'}});
// Own configuration
@@ -47,13 +47,25 @@ aurelia.use
});
```

Mind!, the endpoint defaults are seperate from fetchClient defaults. The former
overrule the latter. Following reasonable endpoint options are applied per
default. You can overwrite them with registerEndpoint options or with
the api request options.

```js
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
```

### Rest client

To use the Rest client, use the Endpoint resolver to inject it.

Here's an example:

```javascript
```js
import {inject} from 'aurelia-framework';
import {Endpoint} from 'spoonx/aurelia-api';
@@ -63,7 +75,7 @@ export class MyClass {
this.apiEndpoint = apiEndpoint;
this.authEndpoint = authEndpoint;
}
attached() {
this.apiEndpoint.find('product', {
category: 5,
@@ -77,7 +89,7 @@ export class MyClass {

Alternatively, you could also request your endpoint at the config. Example:

```javascript
```js
import {inject} from 'aurelia-framework';
import {Config} from 'spoonx/aurelia-api';
@@ -1,5 +1,6 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Rest} from './rest';
import extend from 'extend';

export class Config {
endpoints = {};
@@ -15,10 +16,13 @@ export class Config {
* @see http://aurelia.io/docs.html#/aurelia/fetch-client/latest/doc/api/class/HttpClientConfiguration
* @return {Config}
*/
registerEndpoint(name, configureMethod, defaults) {
registerEndpoint(name, configureMethod, defaults = {}) {
let newClient = new HttpClient();
this.endpoints[name] = new Rest(newClient);

// add custom defaults to Rest
extend(true, this.endpoints[name].defaults, defaults);

// Manual configure of client.
if (typeof configureMethod === 'function') {
newClient.configure(configureMethod);
@@ -34,11 +38,6 @@ export class Config {
// Base url is string. Configure.
newClient.configure(configure => {
configure.withBaseUrl(configureMethod);

// Set optional defaults.
if (typeof defaults === 'object') {
configure.withDefaults(defaults);
}
});

return this;
@@ -4,6 +4,13 @@ import extend from 'extend';

export class Rest {

defaults = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}

/**
* Inject the httpClient to use for requests.
*
@@ -23,18 +30,10 @@ export class Rest {
*
* @return {Promise}
*/
request(method, path, body, options) {
let requestOptions = extend(true, {
method: method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}, options || {});
request(method, path, body, options = {}) {
let requestOptions = extend(true, {}, this.defaults, options);

if (typeof options !== 'undefined') {
extend(true, requestOptions, options);
}
requestOptions.method = method;

if (typeof body === 'object') {
requestOptions.body = json(body);
@@ -28,5 +28,5 @@ function aurelia(container) {
return val;
}
}
}
};
}
@@ -1,57 +1,58 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest} from '../src/aurelia-api';
import {Container} from 'aurelia-dependency-injection';
import extend from 'extend';

describe('Config', function() {
describe('.registerEndpoint()', function() {
it('Should properly register an endpoint when providing a config callback.', function() {
var config = new Config;
var returned = config.registerEndpoint('github', function(configure) {
let config = new Config;
let returned = config.registerEndpoint('github', function(configure) {
configure.withBaseUrl(baseUrls.github);
configure.withDefaults(userOptions);
});

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

it('Should properly register an endpoint when providing an endpoint string.', function() {
var config = new Config;
var returned = config.registerEndpoint('api', baseUrls.api);
let config = new Config;
let returned = config.registerEndpoint('api', baseUrls.api);

expect(config.endpoints.api.client.defaults).toEqual({});
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 no arguments.', function() {
var config = new Config;
var returned = config.registerEndpoint('boring');
let config = new Config;
let returned = config.registerEndpoint('boring');

expect(config.endpoints.boring.client.defaults).toEqual(null);
expect(config.endpoints.boring.defaults).toEqual(defaultOptions);
expect(config.endpoints.boring.client.baseUrl).toEqual('');
expect(returned).toBe(config);
});

it('Should properly register an endpoint when providing an endpoint string and defaults.', function() {
var config = new Config;
var returned = config.registerEndpoint('api', baseUrls.api, {headers: {'x-scope': 'Tests'}});
let config = new Config;
let returned = config.registerEndpoint('api', baseUrls.api, userOptions);

expect(config.endpoints.api.defaults).toEqual(extend(true, {}, defaultOptions, userOptions));
expect(config.endpoints.api.client.baseUrl).toEqual(baseUrls.api);
expect(config.endpoints.api.client.defaults).toEqual({headers: {'x-scope': 'Tests'}});
expect(returned).toBe(config);
});
});

describe('.getEndpoint()', function() {
it('Should return the registered endpoint, or null.', function() {
var config = new Config;
let config = new Config;

config.registerEndpoint('api', baseUrls.api);

var endpoint = config.getEndpoint('api');
var nullEndpoint = config.getEndpoint('no');
var defaultNullEndpoint = config.getEndpoint();
let endpoint = config.getEndpoint('api');
let nullEndpoint = config.getEndpoint('no');
let defaultNullEndpoint = config.getEndpoint();

expect(endpoint instanceof Rest).toBe(true);
expect(endpoint.client instanceof HttpClient).toBe(true);
@@ -62,7 +63,7 @@ describe('Config', function() {

config.setDefaultEndpoint('api');

var defaultEndpoint = config.getEndpoint();
let defaultEndpoint = config.getEndpoint();

expect(defaultEndpoint instanceof Rest).toBe(true);
expect(defaultEndpoint.client instanceof HttpClient).toBe(true);
@@ -71,7 +72,7 @@ describe('Config', function() {

describe('.endpointExists()', function() {
it('Should return if given name is a registered endpoint.', function() {
var config = new Config;
let config = new Config;

config.registerEndpoint('api', baseUrls.api);

@@ -83,7 +84,7 @@ describe('Config', function() {

describe('.setDefaultEndpoint()', function() {
it('Should set the default endpoint.', function() {
var config = new Config;
let config = new Config;

config.registerEndpoint('api', baseUrls.api);
expect(config.getEndpoint()).toBe(null);
@@ -93,7 +94,18 @@ describe('Config', function() {
});
});

var baseUrls = {
let baseUrls = {
github: 'https://api.github.com',
api : 'http://jsonplaceholder.typicode.com'
};

let defaultOptions = {
'headers': {
'Accept': 'application/json',
'Content-Type': 'application/json'
}};

let userOptions = {
'headers': {
'x-scope': 'Tests'
}};
@@ -1,4 +1,3 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest, Endpoint} from '../src/aurelia-api';
import {Container} from 'aurelia-dependency-injection';
import {InjectTest} from './resource/inject-test';
@@ -30,4 +29,3 @@ describe('Endpoint', function() {
});
});
});

@@ -1,5 +1,4 @@
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest, Endpoint} from '../src/aurelia-api';
import {Config, Rest} from '../src/aurelia-api';
import {Container} from 'aurelia-dependency-injection';
import {InjectTest} from './resource/inject-test';

0 comments on commit 8b9344d

Please sign in to comment.