Permalink
Browse files

fix(config): transfer defaults from the endpoint client configuration…

… function to the endpoint defaults
  • Loading branch information...
doktordirk committed Apr 25, 2018
1 parent b44421a commit 30dbbc58476582ff1544531772f3795ceac95050
Showing with 83 additions and 9 deletions.
  1. +9 −3 doc/configuration.md
  2. +3 −0 gulpfile.js
  3. +5 −0 src/config.js
  4. +2 −1 src/rest.js
  5. +2 −2 test/config.spec.js
  6. +3 −2 test/resources/inject-test.js
  7. +59 −1 test/rest.spec.js
@@ -29,8 +29,14 @@ aurelia.use
// 3: With different endpoint defaults
config.registerEndpoint('weather', 'https://weatherapi.io/', {headers: {x: 'foo'}});
// 4: Without endpoint defaults
// or
config.registerEndpoint('weather', configure => {
configure
.withBaseUrl('https://weatherapi.io/')
.withDefaults({headers: {x: 'foo'}});
}));
// 4: Without endpoint defaults. Use this for FormData
config.registerEndpoint('weather', 'https://weatherapi.io/', null);
// 5: Own configuration
@@ -91,7 +97,7 @@ This method allows you to specify different defaults to use for your endpoint, i

## 4: With no defaults

This method allows you to remove the defaults to use for your endpoint.
This method allows you to remove the defaults to use for your endpoint. This is needed for "multipart/form-data" requests as the content type will be set automatically if the transmitted body is of the type FormData.

## 5: Own configuration

@@ -11,6 +11,9 @@ var app = require('./node_modules/spoonx-tools/build-plugin/tasks/server').app;
var multer = require('multer')
var storage = multer.memoryStorage()
var upload = multer({storage: storage})
var bodyParser = require('body-parser');

app.use(bodyParser.json({ type: 'application/vnd.api+json' }))

app.post('/uploads', upload.single(), function(req, res) {
res.send({
@@ -70,6 +70,11 @@ export class Config {
if (typeof configureMethod === 'function') {
newClient.configure(configureMethod);

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

return this;
}

@@ -72,8 +72,9 @@ export class Rest {
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
if (typeof body === 'object' && body !== null && contentType) {
requestOptions.body = (/^application\/json/).test(contentType.toLowerCase())
requestOptions.body = (/^application\/(.+\+)?json/).test(contentType.toLowerCase())
? JSON.stringify(body)
: buildQueryString(body);
}
@@ -4,13 +4,13 @@ import {Rest} from '../src/rest';

describe('Config', function() {
describe('.registerEndpoint()', function() {
it('Should properly register an endpoint when providing a config callback.', function() {
it('Should properly register an endpoint when providing a config callback transferring client defaults.', function() {
let config = new Config;
let returned = config.registerEndpoint('github', function(configure) {
configure.withBaseUrl(baseUrls.github);
configure.withDefaults(userOptions);
});
expect(config.endpoints.github.defaults).toEqual(defaultOptions);
expect(config.endpoints.github.defaults).toEqual(userOptions);
expect(config.endpoints.github.client.defaults).toEqual(userOptions);
expect(config.endpoints.github.client.baseUrl).toEqual(baseUrls.github);
expect(returned).toBe(config);
@@ -1,12 +1,13 @@
import {inject} from 'aurelia-dependency-injection';
import {Endpoint} from '../../src/endpoint';

@inject(Endpoint.of('api'), Endpoint.of('jsonplaceholder'), Endpoint.of('form'), Endpoint.of('urlencoded'))
@inject(Endpoint.of('api'), Endpoint.of('jsonplaceholder'), Endpoint.of('form'), Endpoint.of('urlencoded'), Endpoint.of('fetchConfig'))
export class InjectTest {
constructor(apiEndpoint, jsonplaceholderEndpoint, formEndpoint, urlencodedEndpoint) {
constructor(apiEndpoint, jsonplaceholderEndpoint, formEndpoint, urlencodedEndpoint, fetchConfigEndpoint) {
this.apiEndpoint = apiEndpoint;
this.jsonplaceholderEndpoint = jsonplaceholderEndpoint;
this.formEndpoint = formEndpoint;
this.urlencodedEndpoint = urlencodedEndpoint;
this.fetchConfigEndpoint = fetchConfigEndpoint;
}
}
@@ -16,17 +16,27 @@ let options = {
'Authorization': 'Bearer aToken'
}
};
let jsonOptions = {
headers: {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json'
}
};

config.registerEndpoint('api', baseUrls.api);
config.registerEndpoint('jsonplaceholder', baseUrls.jsonplaceholder);
config.registerEndpoint('form', baseUrls.api, null);
config.registerEndpoint('urlencoded', baseUrls.api, options);
config.registerEndpoint('fetchConfig', fetchConfig => {
fetchConfig
.withBaseUrl(baseUrls.api)
.withDefaults(jsonOptions);
});

let criteria = {user: 'john', comment: 'last'};
let criteriaWithArray = {sort: ['first', 'last']};
let body = {message: 'some'};


describe('Rest', function() {
describe('.find()', function() {
it('Should find results for multiple endpoints.', function(done) {
@@ -740,6 +750,54 @@ describe('Rest', function() {
});
});

it('Should post object body (as json) with fetchConfig configuration.', function(done) {
let injectTest = container.get(InjectTest);
let responseOutput = {
response: null
};

Promise.all([
injectTest.fetchConfigEndpoint.post('posts', body)
.then(y => {
expect(y.method).toBe('POST');
expect(y.path).toBe('/posts');
expect(y.contentType).toBe(jsonOptions.headers['Content-Type']);
expect(y.body.message).toBe('some');
}),
injectTest.fetchConfigEndpoint.post('posts/', body)
.then(y => {
expect(y.path).toBe('/posts/');
expect(y.body.message).toBe('some');
})
]).then(x => {
done();
});
});

it('Should post string body as string with fetchConfig configuration.', function(done) {
let injectTest = container.get(InjectTest);
let responseOutput = {
response: null
};

Promise.all([
injectTest.fetchConfigEndpoint.post('posts', JSON.stringify(body))
.then(y => {
expect(y.method).toBe('POST');
expect(y.path).toBe('/posts');
expect(y.contentType).toBe(jsonOptions.headers['Content-Type']);
expect(y.body.message).toBe('some');
}),
injectTest.fetchConfigEndpoint.post('posts/', JSON.stringify(body))
.then(y => {
expect(y.path).toBe('/posts/');
expect(y.body.message).toBe('some');
})
]).then(x => {
done();
});
});

it('Should post body (as FormData) and options.', function(done) {
let injectTest = container.get(InjectTest);
let responseOutput = {

0 comments on commit 30dbbc5

Please sign in to comment.