restful.js
Advanced tools
Comparing version 0.9.3 to 0.9.4
{ | ||
"name": "restful.js", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"homepage": "https://github.com/marmelab/restful.js", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -5,4 +5,4 @@ import restful from '../src'; | ||
export default function(baseUrl, httpBackend = fetchBackend(fetch)) { | ||
export default function (baseUrl, httpBackend = fetchBackend(fetch)) { | ||
return restful(baseUrl, httpBackend); | ||
} |
@@ -13,2 +13,23 @@ 'use strict'; | ||
var _warning = require('warning'); | ||
var _warning2 = _interopRequireDefault(_warning); | ||
function parseBody(response) { | ||
return response.text().then(function (text) { | ||
if (!text || !text.length) { | ||
(0, _warning2['default'])(response.status === 204, 'You should return a 204 status code with an empty body.'); | ||
return null; | ||
} | ||
(0, _warning2['default'])(response.status !== 204, 'You should return an empty body with a 204 status code.'); | ||
try { | ||
return JSON.parse(text); | ||
} catch (error) { | ||
return text; | ||
} | ||
}); | ||
} | ||
exports['default'] = function (fetch) { | ||
@@ -28,12 +49,34 @@ return function (config) { | ||
return fetch(!queryString.length ? url : url + '?' + queryString, config).then(function (response) { | ||
return (response.status === 204 ? Promise.resolve(null) : response.json()).then(function (json) { | ||
return parseBody(response).then(function (json) { | ||
var headers = {}; | ||
var keys = response.headers.keys(); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
response.headers.forEach(function (value, name) { | ||
headers[name] = value; | ||
}); | ||
try { | ||
for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var key = _step.value; | ||
headers[key] = response.headers.get(key); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator['return']) { | ||
_iterator['return'](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
var responsePayload = { | ||
data: json, | ||
headers: headers, | ||
method: config.method ? config.method.toLowerCase() : 'get', | ||
statusCode: response.status | ||
@@ -40,0 +83,0 @@ }; |
@@ -48,5 +48,2 @@ 'use strict'; | ||
head: _bindHttpMethod('head'), | ||
one: function one(name, id) { | ||
return member(endpoint['new'](endpoint.url() + '/' + name + '/' + id)); | ||
}, // eslint-disable-line no-use-before-define | ||
patch: _bindHttpMethod('patch'), | ||
@@ -53,0 +50,0 @@ put: _bindHttpMethod('put') |
@@ -19,2 +19,6 @@ 'use strict'; | ||
var _warning = require('warning'); | ||
var _warning2 = _interopRequireDefault(_warning); | ||
/* eslint-disable new-cap */ | ||
@@ -39,5 +43,3 @@ | ||
if (_immutable.List.isList(data)) { | ||
if (decoratedEndpoint.all) { | ||
throw new Error('Unexpected array as response, you should use all method for that'); | ||
} | ||
(0, _warning2['default'])(response.get('method') !== 'get' || !decoratedEndpoint.all, 'Unexpected array as response, you should use all method for that'); | ||
@@ -50,5 +52,3 @@ return (0, _utilSerialize2['default'])(data.map(function (datum) { | ||
if (!decoratedEndpoint.all) { | ||
throw new Error('Expected array as response, you should use one method for that'); | ||
} | ||
(0, _warning2['default'])(response.get('method') !== 'get' || decoratedEndpoint.all, 'Expected array as response, you should use one method for that'); | ||
@@ -55,0 +55,0 @@ return (0, _entity2['default'])((0, _utilSerialize2['default'])(data), decoratedEndpoint); |
{ | ||
"name": "restful.js", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"repository": { | ||
@@ -19,22 +19,23 @@ "type": "git", | ||
"dependencies": { | ||
"immutable": "~3.7.5", | ||
"object-assign": "~2.0.0", | ||
"qs": "~5.2.0" | ||
"immutable": "3.7.6", | ||
"object-assign": "2.0.0", | ||
"qs": "5.2.0", | ||
"warning": "2.1.0" | ||
}, | ||
"devDependencies": { | ||
"babel": "~5.8.23", | ||
"babel-loader": "~5.3.2", | ||
"babel-eslint": "~4.0.10", | ||
"chai": "~3.2.0", | ||
"eslint": "~1.5.1", | ||
"eslint-config-airbnb": "~0.1.0", | ||
"mocha": "~2.3.0", | ||
"nock": "~2.12.0", | ||
"sinon": "~1.16.1", | ||
"webpack": "~1.9.11" | ||
"babel": "5.8.35", | ||
"babel-loader": "5.3.3", | ||
"babel-eslint": "4.0.10", | ||
"chai": "3.5.0", | ||
"eslint": "1.5.1", | ||
"eslint-config-airbnb": "0.1.0", | ||
"mocha": "2.3.4", | ||
"nock": "2.12.0", | ||
"sinon": "1.17.3", | ||
"webpack": "1.12.12" | ||
}, | ||
"optionnalDependencies": { | ||
"request": "~2.62.0", | ||
"whatwg-fetch": "~0.9.0" | ||
"request": "2.69.0", | ||
"whatwg-fetch": "0.11.0" | ||
} | ||
} |
@@ -5,4 +5,12 @@ { | ||
"id": 1, | ||
"title": "json-server", | ||
"title": "test", | ||
"author": "typicode" | ||
}, | ||
{ | ||
"title": "test", | ||
"id": 2 | ||
}, | ||
{ | ||
"title": "test", | ||
"id": 3 | ||
} | ||
@@ -21,3 +29,10 @@ ], | ||
"admin": [], | ||
"api": [] | ||
} | ||
"api": [], | ||
"authenticate": [ | ||
{ | ||
"email": "admin@wms.com", | ||
"password": "a", | ||
"id": 1 | ||
} | ||
] | ||
} |
import qs from 'qs'; | ||
import warning from 'warning'; | ||
export default function(fetch) { | ||
function parseBody(response) { | ||
return response.text().then(text => { | ||
if (!text || !text.length) { | ||
warning(response.status === 204, 'You should return a 204 status code with an empty body.'); | ||
return null; | ||
} | ||
warning(response.status !== 204, 'You should return an empty body with a 204 status code.'); | ||
try { | ||
return JSON.parse(text); | ||
} catch (error) { | ||
return text; | ||
} | ||
}); | ||
} | ||
export default function (fetch) { | ||
return (config) => { | ||
@@ -18,12 +36,13 @@ const url = config.url; | ||
.then((response) => { | ||
return (response.status === 204 ? Promise.resolve(null) : response.json()).then((json) => { | ||
return parseBody(response).then((json) => { | ||
const headers = {}; | ||
const keys = response.headers.keys(); | ||
for (const key of keys) { | ||
headers[key] = response.headers.get(key); | ||
} | ||
response.headers.forEach((value, name) => { | ||
headers[name] = value; | ||
}); | ||
const responsePayload = { | ||
data: json, | ||
headers: headers, | ||
method: config.method ? config.method.toLowerCase() : 'get', | ||
statusCode: response.status, | ||
@@ -30,0 +49,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
export default function(request) { | ||
export default function (request) { | ||
return (config) => { | ||
@@ -3,0 +3,0 @@ if (config.data) { |
@@ -15,3 +15,3 @@ import assign from 'object-assign'; | ||
function _bindHttpMethod(method) { | ||
return (...args) => { | ||
return (...args) => { | ||
const id = args.shift(); | ||
@@ -28,3 +28,2 @@ return member(endpoint.new(`${endpoint.url()}/${id}`))[method](...args); // eslint-disable-line no-use-before-define | ||
head: _bindHttpMethod('head'), | ||
one: (name, id) => member(endpoint.new(`${endpoint.url()}/${name}/${id}`)), // eslint-disable-line no-use-before-define | ||
patch: _bindHttpMethod('patch'), | ||
@@ -31,0 +30,0 @@ put: _bindHttpMethod('put'), |
import entity from './entity'; | ||
import { List } from 'immutable'; | ||
import serialize from '../util/serialize'; | ||
import warning from 'warning'; | ||
/* eslint-disable new-cap */ | ||
export default function(response, decoratedEndpoint) { | ||
export default function (response, decoratedEndpoint) { | ||
const identifier = decoratedEndpoint.identifier(); | ||
@@ -21,5 +22,3 @@ | ||
if (List.isList(data)) { | ||
if (decoratedEndpoint.all) { | ||
throw new Error('Unexpected array as response, you should use all method for that'); | ||
} | ||
warning(response.get('method') !== 'get' || !decoratedEndpoint.all, 'Unexpected array as response, you should use all method for that'); | ||
@@ -32,5 +31,3 @@ return serialize(data.map((datum) => { | ||
if (!decoratedEndpoint.all) { | ||
throw new Error('Expected array as response, you should use one method for that'); | ||
} | ||
warning(response.get('method') !== 'get' || decoratedEndpoint.all, 'Expected array as response, you should use one method for that'); | ||
@@ -37,0 +34,0 @@ return entity(serialize(data), decoratedEndpoint); |
import articles from '../fixture/articles'; | ||
import comments from '../fixture/comments'; | ||
export default function(nock) { | ||
export default function (nock) { | ||
const server = nock('http://localhost'); | ||
@@ -6,0 +6,0 @@ |
@@ -13,7 +13,6 @@ import { expect } from 'chai'; | ||
headers: { | ||
forEach: (cb) => { | ||
cb('here', 'test'); | ||
}, | ||
keys: sinon.stub().returns(['test']), | ||
get: sinon.stub().returns('here'), | ||
}, | ||
json: sinon.stub().returns(Promise.resolve({ content: 'Yes' })), | ||
text: sinon.stub().returns(Promise.resolve(JSON.stringify({ content: 'Yes' }))), | ||
status: 200, | ||
@@ -92,2 +91,3 @@ }; | ||
}, | ||
method: 'get', | ||
statusCode: 200, | ||
@@ -117,10 +117,13 @@ }); | ||
expect(_response).to.deep.equal({ | ||
data: null, | ||
data: { | ||
content: 'Yes', | ||
}, | ||
headers: { | ||
test: 'here', | ||
}, | ||
method: 'get', | ||
statusCode: 204, | ||
}); | ||
expect(response.json.callCount).to.equal(0); | ||
expect(response.text.callCount).to.equal(1); | ||
done(); | ||
@@ -156,2 +159,3 @@ }) | ||
}, | ||
method: 'get', | ||
statusCode: 404, | ||
@@ -158,0 +162,0 @@ }); |
@@ -32,3 +32,3 @@ import { expect } from 'chai'; | ||
]); | ||
expect(childMember.fake).to.be.true; | ||
expect(childMember.fake).to.equal(true); | ||
expect(typeof(childMember.all)).to.equal('function'); | ||
@@ -48,4 +48,3 @@ expect(typeof(childMember.custom)).to.equal('function'); | ||
expect(typeof(articles.one)).to.equal('function'); | ||
expect(articles.all).to.be.undefined; | ||
expect(articles.all).to.equal(undefined); | ||
expect(typeof(articles.custom)).to.equal('function'); | ||
@@ -83,21 +82,5 @@ }); | ||
expect(collection.all).to.be.undefined; | ||
expect(typeof(collection.custom)).to.equal('function'); | ||
expect(typeof(collection.one)).to.equal('function'); | ||
}); | ||
it('should create a new endpoint with correct url when one is called', () => { | ||
const collection = decorators.collection(endpoint); | ||
const childMember = collection.one('articles', 1); | ||
expect(endpoint.new.getCall(0).args).to.deep.equal([ | ||
'/url/articles/1', | ||
]); | ||
expect(childMember.fake).to.be.true; | ||
expect(typeof(childMember.all)).to.equal('function'); | ||
expect(typeof(childMember.custom)).to.equal('function'); | ||
expect(typeof(childMember.one)).to.equal('function'); | ||
}); | ||
it('should create a new endpoint with correct url when custom is called', () => { | ||
@@ -104,0 +87,0 @@ const collection = decorators.collection(endpoint); |
@@ -21,3 +21,3 @@ /* eslint-disable no-var */ | ||
}), | ||
].concat(production ? [ | ||
].concat(production ? [ | ||
new webpack.optimize.UglifyJsPlugin(), | ||
@@ -24,0 +24,0 @@ new webpack.optimize.DedupePlugin(), |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
693502
14902
4
+ Addedwarning@2.1.0
+ Addedjs-tokens@4.0.0(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedqs@5.2.0(transitive)
+ Addedwarning@2.1.0(transitive)
- Removedqs@5.2.1(transitive)
Updatedimmutable@3.7.6
Updatedobject-assign@2.0.0
Updatedqs@5.2.0