Comparing version 5.3.8 to 5.3.9
@@ -36,3 +36,3 @@ 'use strict'; | ||
}).catch(function (err) { | ||
return messageCallback(JSON.stringify(unknownError(err, parsedRequest))); | ||
return messageCallback(unknownError(err, parsedRequest)); | ||
}); | ||
@@ -81,10 +81,12 @@ }, | ||
return { | ||
header: _extends({}, headers, { statusCode: 400 }), | ||
body: { | ||
errors: { | ||
message: err.message | ||
} | ||
/* Status code 412 means "precondition failed", which is sort of what happened | ||
* when the fetch api itself throws an Error. | ||
*/ | ||
err.header = _extends({}, headers, { statusCode: 412 }); | ||
err.body = { | ||
errors: { | ||
message: "Unable to perform http request -- fetch threw a client side error" | ||
} | ||
}; | ||
return err; | ||
} | ||
@@ -91,0 +93,0 @@ |
@@ -473,3 +473,3 @@ 'use strict'; | ||
it('should catch unknown errors as 400', function (run) { | ||
it('should catch unknown errors as a 412', function (run) { | ||
(0, _HTTPBackend2.default)({ | ||
@@ -508,3 +508,7 @@ backendOptions: { | ||
api.onMessage(function (resp) { | ||
expect(resp).toBe('{"header":{"correlationId":"1","statusCode":400},"body":{"errors":{"message":"other error"}}}'); | ||
expect(resp instanceof Error).toBe(true); | ||
expect(resp.message).toBe("other error"); | ||
expect(resp.header.correlationId).toBe("1"); | ||
expect(resp.header.statusCode).toBe(412); | ||
expect(resp.body.errors.message).toBe("Unable to perform http request -- fetch threw a client side error"); | ||
run(); | ||
@@ -515,3 +519,3 @@ }); | ||
it('should catch root unknown errors as 400', function (run) { | ||
it('should catch root unknown errors as 412', function (run) { | ||
(0, _HTTPBackend2.default)({ | ||
@@ -535,3 +539,7 @@ backendOptions: { | ||
api.onMessage(function (resp) { | ||
expect(resp).toBe('{"header":{"correlationId":"1","statusCode":400},"body":{"errors":{"message":"ahhh"}}}'); | ||
expect(resp instanceof Error).toBe(true); | ||
expect(resp.message).toBe("ahhh"); | ||
expect(resp.header.correlationId).toBe("1"); | ||
expect(resp.header.statusCode).toBe(412); | ||
expect(resp.body.errors.message).toBe("Unable to perform http request -- fetch threw a client side error"); | ||
run(); | ||
@@ -538,0 +546,0 @@ }); |
@@ -13,2 +13,3 @@ 'use strict'; | ||
exports.requestUse = requestUse; | ||
exports.mockReturn = mockReturn; | ||
exports.startMockingRequests = startMockingRequests; | ||
@@ -23,2 +24,4 @@ exports.stopMockingRequests = stopMockingRequests; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var backend = null; | ||
@@ -77,3 +80,3 @@ var backendSet = false; | ||
var response = JSON.parse(message); | ||
var response = typeof message === 'string' ? JSON.parse(message) : message; | ||
if (!request) { | ||
@@ -135,3 +138,5 @@ request = response && response.header && requestMap[response.header.correlationId] ? requestMap[response.header.correlationId].request : null; | ||
if (response.header.statusCode !== 200) { | ||
if (response instanceof Error) { | ||
observer.onError(response); | ||
} else if (response.header.statusCode !== 200) { | ||
observer.onError({ | ||
@@ -141,3 +146,2 @@ err: response.body, | ||
}); | ||
observer.onCompleted(response.body); | ||
} else { | ||
@@ -302,2 +306,18 @@ observer.onNext(response.body); | ||
function mockReturn(key, value) { | ||
var argLength = arguments.length; | ||
startMockingRequests(); | ||
requestUse().subscribe(function (_ref) { | ||
var req = _ref.req; | ||
var reply = _ref.reply; | ||
var replyObj = { header: _extends({}, req.headers, { statusCode: 200 }) }; | ||
if (argLength === 1) replyObj.body = key;else replyObj.body = _defineProperty({}, key, value); | ||
reply(replyObj); | ||
reset(); | ||
}); | ||
} | ||
/** | ||
@@ -304,0 +324,0 @@ * If mocking requests, don't require a backend |
@@ -284,2 +284,70 @@ 'use strict'; | ||
}); | ||
it('should pass Error objects through, as long as there are header and body properties on the Error', function () { | ||
var backend = (0, _testUtils.makeMockBackend)(); | ||
(0, _request.setBackend)({ backend: backend, url: 'someUrl' }); | ||
(0, _rxws2.default)({ | ||
method: 'get', | ||
resource: 'users' | ||
}).subscribe(function (response) { | ||
expect('This should not be called').toBe('But it was'); | ||
}, function (err) { | ||
expect(err instanceof Error).toBe(true); | ||
expect(err.message).toBe("Something went wrong"); | ||
expect(err.header.statusCode).toBe(412); | ||
expect(_typeof(err.header.correlationId)).toBe('string'); | ||
}); | ||
var request = JSON.parse(backend.write.calls.argsFor(0)); | ||
var mockedMessage = new Error("Something went wrong"); | ||
mockedMessage.header = { | ||
"correlationId": request.header.correlationId, | ||
"statusCode": 412 | ||
}; | ||
mockedMessage.body = { | ||
"users": [{ | ||
name: 'Ibn Al Haytham' | ||
}] | ||
}; | ||
backend.mockServerMessage(mockedMessage); | ||
}); | ||
it('should make mocking a single return value easy for a given end point using key and value for body', function () { | ||
var returnValue = [{ 'user1': 'hi' }]; | ||
(0, _request.mockReturn)('contacts', returnValue); | ||
(0, _rxws2.default)({ | ||
method: 'get', | ||
resource: 'email-accounts.contacts', | ||
parameters: { 'email-accounts': 1 } | ||
}).pluck('contacts').subscribe(function (value) { | ||
expect(value).toEqual(returnValue); | ||
}); | ||
var returnValue2 = [{ 'user2': 'hi' }]; | ||
(0, _request.mockReturn)('address', returnValue2); | ||
(0, _rxws2.default)({ | ||
method: 'get', | ||
resource: 'email-accounts.address', | ||
parameters: { 'email-accounts': 1 } | ||
}).pluck('address').subscribe(function (value) { | ||
expect(value).toEqual(returnValue2); | ||
}); | ||
}); | ||
it('should make mocking a single return value easy for a given end point passing entire body', function () { | ||
var returnValue = [{ 'user1': 'hi' }]; | ||
(0, _request.mockReturn)({ 'contacts': returnValue }); | ||
(0, _rxws2.default)({ | ||
method: 'get', | ||
resource: 'email-accounts.contacts', | ||
parameters: { 'email-accounts': 1 } | ||
}).pluck('contacts').subscribe(function (value) { | ||
expect(value).toEqual(returnValue); | ||
}); | ||
}); | ||
}); | ||
@@ -286,0 +354,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.rx = exports.reset = exports.stopMockingRequests = exports.startMockingRequests = exports.requestUse = exports.use = exports.onNotification = exports.setBackend = exports.patch = exports.head = exports.put = exports.post = exports.get = exports.remove = undefined; | ||
exports.rx = exports.reset = exports.stopMockingRequests = exports.startMockingRequests = exports.mockReturn = exports.requestUse = exports.use = exports.onNotification = exports.setBackend = exports.patch = exports.head = exports.put = exports.post = exports.get = exports.remove = undefined; | ||
@@ -54,2 +54,3 @@ var _rx = require('rx'); | ||
_request2.default.requestUse = _request.requestUse; | ||
_request2.default.mockReturn = _request.mockReturn; | ||
@@ -71,2 +72,3 @@ _request2.default.startMockingRequests = _request.startMockingRequests; | ||
exports.requestUse = _request.requestUse; | ||
exports.mockReturn = _request.mockReturn; | ||
exports.startMockingRequests = _request.startMockingRequests; | ||
@@ -73,0 +75,0 @@ exports.stopMockingRequests = _request.stopMockingRequests; |
{ | ||
"name": "rxws", | ||
"version": "5.3.8", | ||
"version": "5.3.9", | ||
"description": "A RESTful reactive JavaScript implmentation on top of web sockets", | ||
@@ -5,0 +5,0 @@ "main": "lib/rxws.js", |
@@ -19,3 +19,3 @@ import { Observable } from 'rx'; | ||
.then(resp => setTimeout(() => messageCallback(JSON.stringify(resp)))) | ||
.catch(err => messageCallback(JSON.stringify(unknownError(err, parsedRequest)))); | ||
.catch(err => messageCallback(unknownError(err, parsedRequest))); | ||
}, | ||
@@ -66,10 +66,12 @@ | ||
return { | ||
header: { ...headers, statusCode: 400 }, | ||
body: { | ||
errors: { | ||
message: err.message | ||
} | ||
/* Status code 412 means "precondition failed", which is sort of what happened | ||
* when the fetch api itself throws an Error. | ||
*/ | ||
err.header = {...headers, statusCode: 412}; | ||
err.body = { | ||
errors: { | ||
message: "Unable to perform http request -- fetch threw a client side error" | ||
} | ||
} | ||
return err; | ||
} | ||
@@ -76,0 +78,0 @@ |
@@ -464,3 +464,3 @@ import backend from './HTTPBackend'; | ||
it('should catch unknown errors as 400', function(run) { | ||
it('should catch unknown errors as a 412', function(run) { | ||
backend({ | ||
@@ -491,3 +491,7 @@ backendOptions: { | ||
api.onMessage(function(resp) { | ||
expect(resp).toBe('{"header":{"correlationId":"1","statusCode":400},"body":{"errors":{"message":"other error"}}}'); | ||
expect(resp instanceof Error).toBe(true); | ||
expect(resp.message).toBe("other error"); | ||
expect(resp.header.correlationId).toBe("1"); | ||
expect(resp.header.statusCode).toBe(412); | ||
expect(resp.body.errors.message).toBe("Unable to perform http request -- fetch threw a client side error"); | ||
run(); | ||
@@ -498,3 +502,3 @@ }); | ||
it('should catch root unknown errors as 400', function(run) { | ||
it('should catch root unknown errors as 412', function(run) { | ||
backend({ | ||
@@ -516,3 +520,7 @@ backendOptions: { | ||
api.onMessage(function(resp) { | ||
expect(resp).toBe('{"header":{"correlationId":"1","statusCode":400},"body":{"errors":{"message":"ahhh"}}}'); | ||
expect(resp instanceof Error).toBe(true); | ||
expect(resp.message).toBe("ahhh"); | ||
expect(resp.header.correlationId).toBe("1"); | ||
expect(resp.header.statusCode).toBe(412); | ||
expect(resp.body.errors.message).toBe("Unable to perform http request -- fetch threw a client side error"); | ||
run(); | ||
@@ -519,0 +527,0 @@ }); |
@@ -66,3 +66,3 @@ import { Observable } from 'rx'; | ||
let response = JSON.parse(message); | ||
let response = typeof message === 'string' ? JSON.parse(message) : message; | ||
if (!request) { | ||
@@ -124,3 +124,5 @@ request = response && response.header && requestMap[response.header.correlationId] ? requestMap[response.header.correlationId].request : null; | ||
if (response.header.statusCode !== 200) { | ||
if (response instanceof Error) { | ||
observer.onError(response); | ||
} else if (response.header.statusCode !== 200) { | ||
observer.onError({ | ||
@@ -130,3 +132,2 @@ err: response.body, | ||
}); | ||
observer.onCompleted(response.body); | ||
} else { | ||
@@ -294,2 +295,18 @@ observer.onNext(response.body); | ||
export function mockReturn(key, value) { | ||
const argLength = arguments.length; | ||
startMockingRequests(); | ||
requestUse().subscribe(({req, reply}) => { | ||
const replyObj = {header: {...req.headers, statusCode: 200}}; | ||
if (argLength === 1) | ||
replyObj.body = key; | ||
else | ||
replyObj.body = {[key]: value}; | ||
reply(replyObj); | ||
reset(); | ||
}); | ||
} | ||
/** | ||
@@ -296,0 +313,0 @@ * If mocking requests, don't require a backend |
@@ -1,2 +0,2 @@ | ||
import makeRequest, { setBackend, reset, startMockingRequests, stopMockingRequests } from './request'; | ||
import makeRequest, { setBackend, reset, startMockingRequests, stopMockingRequests, mockReturn } from './request'; | ||
import remove from './remove'; | ||
@@ -270,2 +270,81 @@ import rxws from './rxws'; | ||
}) | ||
it('should pass Error objects through, as long as there are header and body properties on the Error', () => { | ||
let backend = makeMockBackend(); | ||
setBackend({backend: backend, url: 'someUrl'}); | ||
rxws({ | ||
method: 'get', | ||
resource: 'users' | ||
}).subscribe( | ||
response => { | ||
expect('This should not be called').toBe('But it was'); | ||
}, | ||
err => { | ||
expect(err instanceof Error).toBe(true); | ||
expect(err.message).toBe("Something went wrong"); | ||
expect(err.header.statusCode).toBe(412); | ||
expect(typeof err.header.correlationId).toBe('string'); | ||
} | ||
); | ||
let request = JSON.parse(backend.write.calls.argsFor(0)); | ||
const mockedMessage = new Error("Something went wrong"); | ||
mockedMessage.header = { | ||
"correlationId": request.header.correlationId, | ||
"statusCode": 412 | ||
}; | ||
mockedMessage.body = { | ||
"users": [ | ||
{ | ||
name: 'Ibn Al Haytham' | ||
} | ||
] | ||
}; | ||
backend.mockServerMessage(mockedMessage); | ||
}) | ||
it('should make mocking a single return value easy for a given end point using key and value for body', () => { | ||
const returnValue = [{'user1': 'hi'}]; | ||
mockReturn('contacts', returnValue); | ||
rxws({ | ||
method: 'get', | ||
resource: 'email-accounts.contacts', | ||
parameters: {'email-accounts': 1}, | ||
}) | ||
.pluck('contacts') | ||
.subscribe(value => { | ||
expect(value).toEqual(returnValue); | ||
}); | ||
const returnValue2 = [{'user2': 'hi'}]; | ||
mockReturn('address', returnValue2); | ||
rxws({ | ||
method: 'get', | ||
resource: 'email-accounts.address', | ||
parameters: {'email-accounts': 1}, | ||
}) | ||
.pluck('address') | ||
.subscribe(value => { | ||
expect(value).toEqual(returnValue2); | ||
}); | ||
}); | ||
it('should make mocking a single return value easy for a given end point passing entire body', () => { | ||
const returnValue = [{'user1': 'hi'}]; | ||
mockReturn({'contacts': returnValue}); | ||
rxws({ | ||
method: 'get', | ||
resource: 'email-accounts.contacts', | ||
parameters: {'email-accounts': 1}, | ||
}) | ||
.pluck('contacts') | ||
.subscribe(value => { | ||
expect(value).toEqual(returnValue); | ||
}); | ||
}); | ||
}); | ||
@@ -272,0 +351,0 @@ |
@@ -14,2 +14,3 @@ import rx from 'rx'; | ||
requestUse, | ||
mockReturn, | ||
startMockingRequests, | ||
@@ -31,2 +32,3 @@ stopMockingRequests, | ||
makeRequest.requestUse = requestUse; | ||
makeRequest.mockReturn = mockReturn; | ||
@@ -50,2 +52,3 @@ makeRequest.startMockingRequests = startMockingRequests; | ||
export { mockReturn } | ||
export { startMockingRequests }; | ||
@@ -52,0 +55,0 @@ export { stopMockingRequests }; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
167340
44
5235
22