Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

Commit 647788b

Browse files
committed
Build 0.9.3
1 parent e02470d commit 647788b

9 files changed

+144
-48
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "restful.js",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"homepage": "https://github.com/marmelab/restful.js",
55
"authors": [
66
"Robin Bressan <[email protected]>"

dist/es5/index.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,31 @@ var _modelScope = require('./model/scope');
2828

2929
var _modelScope2 = _interopRequireDefault(_modelScope);
3030

31-
exports['default'] = function (baseUrl, httpBackend) {
31+
var instances = [];
32+
33+
function restful(baseUrl, httpBackend) {
3234
var rootScope = (0, _modelScope2['default'])();
3335
rootScope.assign('config', 'entityIdentifier', 'id');
34-
if (!baseUrl && typeof window !== undefined && window.location) {
36+
if (!baseUrl && typeof window !== 'undefined' && window.location) {
3537
rootScope.set('url', window.location.protocol + '//' + window.location.host);
3638
} else {
3739
rootScope.set('url', baseUrl);
3840
}
3941

40-
return (0, _modelDecorator.member)((0, _modelEndpoint2['default'])((0, _serviceHttp2['default'])(httpBackend))(rootScope));
42+
var rootEndpoint = (0, _modelDecorator.member)((0, _modelEndpoint2['default'])((0, _serviceHttp2['default'])(httpBackend))(rootScope));
43+
44+
instances.push(rootEndpoint);
45+
46+
return rootEndpoint;
47+
}
48+
49+
restful._instances = function () {
50+
return instances;
51+
};
52+
restful._flush = function () {
53+
return instances.length = 0;
4154
};
4255

4356
exports.fetchBackend = _httpFetch2['default'];
44-
exports.requestBackend = _httpRequest2['default'];
57+
exports.requestBackend = _httpRequest2['default'];
58+
exports['default'] = restful;

dist/es5/model/endpoint.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ exports['default'] = function (request) {
4848
config = config.set('data', (0, _immutable.fromJS)(data));
4949
}
5050

51-
scope.emit('request', (0, _utilSerialize2['default'])(config));
52-
5351
return config;
5452
}
5553

@@ -67,13 +65,17 @@ exports['default'] = function (request) {
6765
function _httpMethodFactory(method) {
6866
var expectData = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
6967

68+
var emitter = function emitter() {
69+
scope.emit.apply(scope, arguments);
70+
};
71+
7072
if (expectData) {
7173
return function (data) {
7274
var params = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
7375
var headers = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
7476

7577
var config = _generateRequestConfig(method, data, params, headers);
76-
return request(config).then(function (rawResponse) {
78+
return request(config, emitter).then(function (rawResponse) {
7779
return _onResponse(config, rawResponse);
7880
}, function (rawResponse) {
7981
return _onError(config, rawResponse);
@@ -86,7 +88,7 @@ exports['default'] = function (request) {
8688
var headers = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
8789

8890
var config = _generateRequestConfig(method, null, params, headers);
89-
return request(config).then(function (rawResponse) {
91+
return request(config, emitter).then(function (rawResponse) {
9092
return _onResponse(config, rawResponse);
9193
}, function (error) {
9294
return _onError(config, error);

dist/es5/service/http.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,51 @@ var _utilSerialize = require('../util/serialize');
1919
var _utilSerialize2 = _interopRequireDefault(_utilSerialize);
2020

2121
/* eslint-disable new-cap */
22-
function reducePromiseList(list, initialValue) {
23-
var params = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
22+
function reducePromiseList(emitter, list, initialValue) {
23+
var params = arguments.length <= 3 || arguments[3] === undefined ? [] : arguments[3];
2424

2525
return list.reduce(function (promise, nextItem) {
2626
return promise.then(function (currentValue) {
27+
emitter.apply(undefined, ['pre', (0, _utilSerialize2['default'])(currentValue)].concat(_toConsumableArray(params), [nextItem.name]));
2728
return Promise.resolve(nextItem.apply(undefined, [(0, _utilSerialize2['default'])(currentValue)].concat(_toConsumableArray(params)))).then(function (nextValue) {
2829
if (!_immutable.Iterable.isIterable(currentValue)) {
2930
return (0, _objectAssign2['default'])({}, currentValue, nextValue);
3031
}
3132

3233
return currentValue.mergeDeep(nextValue);
34+
}).then(function (nextValue) {
35+
emitter.apply(undefined, ['post', (0, _utilSerialize2['default'])(nextValue)].concat(_toConsumableArray(params), [nextItem.name]));
36+
37+
return nextValue;
3338
});
3439
});
3540
}, Promise.resolve(initialValue));
3641
}
3742

3843
exports['default'] = function (httpBackend) {
39-
return function (config) {
44+
return function (config, emitter) {
4045
var errorInterceptors = (0, _immutable.List)(config.get('errorInterceptors'));
4146
var requestInterceptors = (0, _immutable.List)(config.get('requestInterceptors'));
4247
var responseInterceptors = (0, _immutable.List)(config.get('responseInterceptors'));
4348
var currentConfig = config['delete']('errorInterceptors')['delete']('requestInterceptors')['delete']('responseInterceptors');
4449

45-
return reducePromiseList(requestInterceptors, currentConfig).then(function (transformedConfig) {
50+
function emitterFactory(type) {
51+
return function (event) {
52+
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
53+
args[_key - 1] = arguments[_key];
54+
}
55+
56+
emitter.apply(undefined, [type + ':' + event].concat(args));
57+
};
58+
}
59+
60+
return reducePromiseList(emitterFactory('request:interceptor'), requestInterceptors, currentConfig).then(function (transformedConfig) {
61+
emitter('request', (0, _utilSerialize2['default'])(transformedConfig));
4662
return httpBackend((0, _utilSerialize2['default'])(transformedConfig)).then(function (response) {
47-
return reducePromiseList(responseInterceptors, (0, _immutable.fromJS)(response), [(0, _utilSerialize2['default'])(transformedConfig)]);
63+
return reducePromiseList(emitterFactory('response:interceptor'), responseInterceptors, (0, _immutable.fromJS)(response), [(0, _utilSerialize2['default'])(transformedConfig)]);
4864
});
4965
}).then(null, function (error) {
50-
return reducePromiseList(errorInterceptors, error, [(0, _utilSerialize2['default'])(currentConfig)]).then(function (transformedError) {
66+
return reducePromiseList(emitterFactory('error:interceptor'), errorInterceptors, error, [(0, _utilSerialize2['default'])(currentConfig)]).then(function (transformedError) {
5167
return Promise.reject(transformedError);
5268
});
5369
});

dist/restful.js

+45-13
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,34 @@ return /******/ (function(modules) { // webpackBootstrap
103103

104104
var _modelScope2 = _interopRequireDefault(_modelScope);
105105

106-
exports['default'] = function (baseUrl, httpBackend) {
106+
var instances = [];
107+
108+
function restful(baseUrl, httpBackend) {
107109
var rootScope = (0, _modelScope2['default'])();
108110
rootScope.assign('config', 'entityIdentifier', 'id');
109-
if (!baseUrl && typeof window !== undefined && window.location) {
111+
if (!baseUrl && typeof window !== 'undefined' && window.location) {
110112
rootScope.set('url', window.location.protocol + '//' + window.location.host);
111113
} else {
112114
rootScope.set('url', baseUrl);
113115
}
114116

115-
return (0, _modelDecorator.member)((0, _modelEndpoint2['default'])((0, _serviceHttp2['default'])(httpBackend))(rootScope));
117+
var rootEndpoint = (0, _modelDecorator.member)((0, _modelEndpoint2['default'])((0, _serviceHttp2['default'])(httpBackend))(rootScope));
118+
119+
instances.push(rootEndpoint);
120+
121+
return rootEndpoint;
122+
}
123+
124+
restful._instances = function () {
125+
return instances;
126+
};
127+
restful._flush = function () {
128+
return instances.length = 0;
116129
};
117130

118131
exports.fetchBackend = _httpFetch2['default'];
119132
exports.requestBackend = _httpRequest2['default'];
133+
exports['default'] = restful;
120134

121135
/***/ },
122136
/* 2 */
@@ -275,8 +289,6 @@ return /******/ (function(modules) { // webpackBootstrap
275289
config = config.set('data', (0, _immutable.fromJS)(data));
276290
}
277291

278-
scope.emit('request', (0, _utilSerialize2['default'])(config));
279-
280292
return config;
281293
}
282294

@@ -294,13 +306,17 @@ return /******/ (function(modules) { // webpackBootstrap
294306
function _httpMethodFactory(method) {
295307
var expectData = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
296308

309+
var emitter = function emitter() {
310+
scope.emit.apply(scope, arguments);
311+
};
312+
297313
if (expectData) {
298314
return function (data) {
299315
var params = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
300316
var headers = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
301317

302318
var config = _generateRequestConfig(method, data, params, headers);
303-
return request(config).then(function (rawResponse) {
319+
return request(config, emitter).then(function (rawResponse) {
304320
return _onResponse(config, rawResponse);
305321
}, function (rawResponse) {
306322
return _onError(config, rawResponse);
@@ -313,7 +329,7 @@ return /******/ (function(modules) { // webpackBootstrap
313329
var headers = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
314330

315331
var config = _generateRequestConfig(method, null, params, headers);
316-
return request(config).then(function (rawResponse) {
332+
return request(config, emitter).then(function (rawResponse) {
317333
return _onResponse(config, rawResponse);
318334
}, function (error) {
319335
return _onError(config, error);
@@ -6112,35 +6128,51 @@ return /******/ (function(modules) { // webpackBootstrap
61126128
var _utilSerialize2 = _interopRequireDefault(_utilSerialize);
61136129

61146130
/* eslint-disable new-cap */
6115-
function reducePromiseList(list, initialValue) {
6116-
var params = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
6131+
function reducePromiseList(emitter, list, initialValue) {
6132+
var params = arguments.length <= 3 || arguments[3] === undefined ? [] : arguments[3];
61176133

61186134
return list.reduce(function (promise, nextItem) {
61196135
return promise.then(function (currentValue) {
6136+
emitter.apply(undefined, ['pre', (0, _utilSerialize2['default'])(currentValue)].concat(_toConsumableArray(params), [nextItem.name]));
61206137
return Promise.resolve(nextItem.apply(undefined, [(0, _utilSerialize2['default'])(currentValue)].concat(_toConsumableArray(params)))).then(function (nextValue) {
61216138
if (!_immutable.Iterable.isIterable(currentValue)) {
61226139
return (0, _objectAssign2['default'])({}, currentValue, nextValue);
61236140
}
61246141

61256142
return currentValue.mergeDeep(nextValue);
6143+
}).then(function (nextValue) {
6144+
emitter.apply(undefined, ['post', (0, _utilSerialize2['default'])(nextValue)].concat(_toConsumableArray(params), [nextItem.name]));
6145+
6146+
return nextValue;
61266147
});
61276148
});
61286149
}, Promise.resolve(initialValue));
61296150
}
61306151

61316152
exports['default'] = function (httpBackend) {
6132-
return function (config) {
6153+
return function (config, emitter) {
61336154
var errorInterceptors = (0, _immutable.List)(config.get('errorInterceptors'));
61346155
var requestInterceptors = (0, _immutable.List)(config.get('requestInterceptors'));
61356156
var responseInterceptors = (0, _immutable.List)(config.get('responseInterceptors'));
61366157
var currentConfig = config['delete']('errorInterceptors')['delete']('requestInterceptors')['delete']('responseInterceptors');
61376158

6138-
return reducePromiseList(requestInterceptors, currentConfig).then(function (transformedConfig) {
6159+
function emitterFactory(type) {
6160+
return function (event) {
6161+
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
6162+
args[_key - 1] = arguments[_key];
6163+
}
6164+
6165+
emitter.apply(undefined, [type + ':' + event].concat(args));
6166+
};
6167+
}
6168+
6169+
return reducePromiseList(emitterFactory('request:interceptor'), requestInterceptors, currentConfig).then(function (transformedConfig) {
6170+
emitter('request', (0, _utilSerialize2['default'])(transformedConfig));
61396171
return httpBackend((0, _utilSerialize2['default'])(transformedConfig)).then(function (response) {
6140-
return reducePromiseList(responseInterceptors, (0, _immutable.fromJS)(response), [(0, _utilSerialize2['default'])(transformedConfig)]);
6172+
return reducePromiseList(emitterFactory('response:interceptor'), responseInterceptors, (0, _immutable.fromJS)(response), [(0, _utilSerialize2['default'])(transformedConfig)]);
61416173
});
61426174
}).then(null, function (error) {
6143-
return reducePromiseList(errorInterceptors, error, [(0, _utilSerialize2['default'])(currentConfig)]).then(function (transformedError) {
6175+
return reducePromiseList(emitterFactory('error:interceptor'), errorInterceptors, error, [(0, _utilSerialize2['default'])(currentConfig)]).then(function (transformedError) {
61446176
return Promise.reject(transformedError);
61456177
});
61466178
});

dist/restful.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)