New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@cerebral/http

Package Overview
Dependencies
Maintainers
3
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cerebral/http - npm Package Compare versions

Comparing version 0.0.0-beta.3 to 1.0.0-1503432359023

lib/operators/httpAbort.js

7

lib/DEFAULT_OPTIONS.js

@@ -40,3 +40,3 @@ 'use strict';

if (result && xhr.getResponseHeader('Content-Type').indexOf('application/json') >= 0) {
if (result && (xhr.getResponseHeader('Content-Type') || '').indexOf('application/json') >= 0) {
result = JSON.parse(xhr.responseText);

@@ -51,6 +51,7 @@ }

headers: responseHeaders,
result: result
result: result,
isAborted: false
});
} else {
reject(new _HttpProviderError2.default(xhr.status, responseHeaders, result));
reject(new _HttpProviderError2.default(xhr.status, responseHeaders, result, xhr.responseText));
}

@@ -57,0 +58,0 @@ }

@@ -50,3 +50,3 @@ 'use strict';

response.isAborted = fileUpload.isAborted;
reject(response);
reject(new _HttpProviderError2.default(xhr.status, (0, _utils.getAllResponseHeaders)(xhr), xhr.responseText, null));
}

@@ -53,0 +53,0 @@ };

@@ -43,3 +43,3 @@ 'use strict';

function HttpProviderError(status, headers, body) {
function HttpProviderError(status, headers, result) {
var message = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

@@ -54,6 +54,8 @@ var isAborted = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;

_this.message = message;
_this.status = status;
_this.headers = headers;
_this.body = body;
_this.isAborted = isAborted;
_this.response = {
status: status,
headers: headers,
result: result,
isAborted: isAborted
};
return _this;

@@ -68,6 +70,4 @@ }

message: this.message,
status: this.status,
headers: this.headers,
body: this.body,
isAborted: this.isAborted
response: this.response,
stack: this.stack
};

@@ -74,0 +74,0 @@ }

@@ -141,2 +141,3 @@ 'use strict';

options.url = moduleOptions.baseUrl + url;
options.onProgress = typeof options.onProgress === 'string' ? context.controller.getSignal(options.onProgress) : options.onProgress;

@@ -143,0 +144,0 @@ return new _fileUpload2.default(options).send(files);

@@ -61,3 +61,12 @@ 'use strict';

var _httpAbort = require('./httpAbort');
Object.defineProperty(exports, 'httpAbort', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_httpAbort).default;
}
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=index.js.map

@@ -36,6 +36,6 @@ 'use strict';

case 'error':
reject(new _HttpProviderError2.default(event.currentTarget.status, null, null, 'request error'));
reject(new _HttpProviderError2.default(event.currentTarget.status, getAllResponseHeaders(event.currentTarget), event.currenTarget.responseText, event.currenTarget.responseText || 'request error'));
break;
case 'abort':
reject(new _HttpProviderError2.default(event.currentTarget.status, null, null, 'request abort', true));
reject(new _HttpProviderError2.default(event.currentTarget.status, getAllResponseHeaders(event.currentTarget), null, 'request abort', true));
break;

@@ -84,2 +84,6 @@ }

function parseHeaders(rawHeaders) {
if (!rawHeaders) {
return null;
}
var headerPairs = rawHeaders.replace(/\r?\n$/, '').split(/\r?\n/);

@@ -102,6 +106,6 @@

if (path && path[response.status]) {
return path[response.status](response);
return path[response.status]({ response: response });
}
return path && path.success ? path.success(response) : response;
return path && path.success ? path.success({ response: response }) : { response: response };
})

@@ -108,0 +112,0 @@ // This error will be an instance of HttpError

{
"name": "@cerebral/http",
"version": "0.0.0-beta.3",
"version": "1.0.0-1503432359023",
"description": "HTTP provider for Cerebral 2",

@@ -8,4 +8,3 @@ "main": "lib/index.js",

"scripts": {
"test": "mocha --compilers js:babel-register \"src/**/*.test.js\" && npm run test_ts",
"test_ts": "mocha --timeout 5000 --compilers js:babel-register \"typescript/typescript.spec.js\"",
"test": "mocha --compilers js:babel-register \"src/**/*.test.js\" \"test/**/*.test.js\"",
"build": "cross-env BABEL_ENV=production babel src/ --out-dir=lib/ -s",

@@ -29,11 +28,11 @@ "prepublish": "npm run build"

"dependencies": {
"function-tree": "^1.0.0-beta.3"
"function-tree": "^1.0.1-1503432359023"
},
"peerDependencies": {
"cerebral": "^2.0.0-beta.3"
"cerebral": "^2.1.0-1503432359023"
},
"devDependencies": {
"cerebral": "^2.0.0-beta.3",
"cerebral": "^2.1.0-1503432359023",
"xhr-mock": "^1.9.0"
}
}
}

@@ -6,18 +6,16 @@ # @cerebral/http

`npm install @cerebral/http --save`
`npm install @cerebral/http`
**YARN**
`yarn add @cerebral/http`
## description
The HTTP provider exposes the ability to do HTTP requests both in actions and directly in signals. It supports **cors** and file upload, with progress handling. It default to **json**, but you can configure it to whatever you want.
Read more about http in the [Cerebral in depth - Http](https://www.jsblog.io/articles/christianalfoni/cerebral_in_depth_http) article.
```js
import {set} from 'cerebral/operators'
import {httpGet} from '@cerebral/http/operators'
import {state, props} from 'cerebral/tags'
import {state, props, string} from 'cerebral/tags'
export default [
httpGet(`/items/${props`itemKey`}`),
httpGet(string`/items/${props`itemKey`}`),
set(state`app.currentItem`, props`result`)

@@ -65,3 +63,3 @@ ]

## error
## errors

@@ -76,11 +74,51 @@ ### HttpProviderError

name: 'HttpProviderError',
message: 'Some potential error message',
body: 'Message or response body',
status: 200,
isAborted: false,
headers: {},
stack: '...'
message: 'Some error message or responseText',
response: {
result: {}, // Parsed responseText
headers: {},
status: 200,
isAborted: false
},
stack: '...'
}
```
This error is available in the following scenarios:
- Inside an action
```js
function someAction ({http}) {
return http.get('/something').catch(error => ...)
}
```
- Going down an error path
```js
[
httpGet('/something'), {
success: [],
error: [
// {error: ...}
]
}
]
```
- To signal/global catch handlers
```js
const errorCatched = [
// {error: ...}
displayError
]
Controller({
catch: new Map([
[HttpProviderError, errorCatched]
])
})
```
## request

@@ -117,16 +155,47 @@

## responses
There are two types of responses from the HTTP provider. A **response** and an **error** of type *HttpProviderError*. A **response** will be received on status codes 200-299. Everything else is an **error**.
### response
```js
{
result: 'the response body',
headers: {...},
status: 200,
isAborted: false
}
```
### error
```js
{
name: 'HttpProviderError',
message: 'Some potential error message',
result: 'Message or response body',
status: 500,
isAborted: false,
headers: {},
stack: '...'
}
```
## get
*action*
### action
```js
function someGetAction ({http}) {
return http.get('/items', {
// QUERY object
}, {
// Any options defined in "Custom request"
})
const query = {}
const options = {}
return http.get('/items', query, options)
.then((response) => {
return {someResponse: response}
})
.catch((error) => {
return {someError: error}
})
}
```
*factory*
### operator
```js

@@ -137,9 +206,32 @@ import {httpGet} from '@cerebral/http/operators'

httpGet('/items'),
/*
PROPS: {
response: {...}
}
*/
]
```
// Alternatively with explicit paths
On error this will throw to the signal or global catch handler.
### operator with paths
```js
import {httpGet} from '@cerebral/http/operators'
export default [
httpGet('/items'), {
success: [],
error: [],
abort: [], // Optional
'${STATUS_CODE}': [] // Optionally any status code, ex. 404: []
success: [
/* PROPS: {response: {...}} */
],
error: [
/* PROPS: {error: {...}} */
],
abort: [
/* PROPS: {error: {...}} */
],
// Optionally any status code, ex. 404: []
'${STATUS_CODE}': [
/* PROPS: {response/error: {...}} */
]
}

@@ -149,27 +241,21 @@ ]

*output*
```javascript
{
result: 'the response',
status: 200,
// If aborted
isAborted: true
}
```
## post
*action*
### action
```js
function somePostAction ({http}) {
return http.post('/items', {
// BODY object
}, {
// Any options defined in "Custom request"
})
const data = {}
const options = {}
return http.post('/items', data, options)
.then((response) => {
return {response}
})
.catch((error) => {
return {error}
})
}
```
*factory*
### operator
```js

@@ -184,4 +270,16 @@ import {httpPost} from '@cerebral/http/operators'

}),
/*
PROPS: {
response: {...}
}
*/
]
```
// Alternatively with explicit paths
### operator with paths
```js
import {httpPost} from '@cerebral/http/operators'
import {props} from 'cerebral/tags'
export default [
httpPost('/items', {

@@ -191,6 +289,16 @@ title: props`itemTitle`,

}), {
success: [],
error: [],
abort: [], // Optional
'${STATUS_CODE}': [] // Optionally any status code, ex. 404: []
success: [
/* PROPS: {response: {...}} */
],
error: [
/* PROPS: {error: {...}} */
],
abort: [
/* PROPS: {error: {...}} */
],
// Optionally any status code, ex. 404: []
'${STATUS_CODE}': [
/* PROPS: {response/error: {...}} */
]
}

@@ -200,27 +308,21 @@ ]

*output*
```javascript
{
result: 'the response',
status: 200,
// If aborted
isAborted: true
}
```
## put
*action*
### action
```js
function somePutAction ({http}) {
return http.put('/items/1', {
// BODY object
}, {
// Any options defined in "Custom request"
})
const data = {}
const options = {}
return http.put('/items/1', data, options)
.then((response) => {
return {response}
})
.catch((error) => {
return {error}
})
}
```
*factory*
### operator
```js

@@ -231,13 +333,34 @@ import {httpPost} from '@cerebral/http/operators'

httpPut('/items', {
// BODY object
// data object
}),
/*
PROPS: {
response: {...}
}
*/
]
```
// Alternatively with explicit paths
### operator with paths
```js
import {httpPost} from '@cerebral/http/operators'
export default [
httpPut('/items', {
// BODY object
// data object
}), {
success: [],
error: [],
abort: [], // Optional
'${STATUS_CODE}': [] // Optionally any status code, ex. 404: []
success: [
/* PROPS: {response: {...}} */
],
error: [
/* PROPS: {error: {...}} */
],
abort: [
/* PROPS: {error: {...}} */
],
// Optionally any status code, ex. 404: []
'${STATUS_CODE}': [
/* PROPS: {response/error: {...}} */
]
}

@@ -247,29 +370,23 @@ ]

*output*
```javascript
{
result: 'the response',
status: 200,
// If aborted
isAborted: true
}
```
## patch
*action*
### action
```js
function somePatchAction ({http}) {
return http.patch('/items/1', {
// BODY object
}, {
// Any options defined in "Custom request"
})
const data = {}
const options = {}
return http.patch('/items/1', data, options)
.then((response) => {
return {response}
})
.catch((error) => {
return {error}
})
}
```
*factory*
### operator
```js
import {httpPost} from '@cerebral/http/operators'
import {httpPatch} from '@cerebral/http/operators'
import {state, props, string} from 'cerebral/tags'

@@ -279,9 +396,31 @@

httpPatch(string`/items/${props`itemId`}`, state`patchData`),
/*
PROPS: {
response: {...}
}
*/
]
```
// Alternatively with explicit paths
### operator with paths
```js
import {httpPatch} from '@cerebral/http/operators'
import {state, props, string} from 'cerebral/tags'
export default [
httpPatch(string`/items/${props`itemId`}`, state`patchData`), {
success: [],
error: [],
abort: [], // Optional
'${STATUS_CODE}': [] // Optionally any status code, ex. 404: []
success: [
/* PROPS: {response: {...}} */
],
error: [
/* PROPS: {error: {...}} */
],
abort: [
/* PROPS: {error: {...}} */
],
// Optionally any status code, ex. 404: []
'${STATUS_CODE}': [
/* PROPS: {response/error: {...}} */
]
}

@@ -291,27 +430,21 @@ ]

*output*
```javascript
{
result: 'the response',
status: 200,
// If aborted
isAborted: true
}
```
## delete
*action*
### action
```js
function someDeleteAction ({http}) {
return http.delete('/items/1', {
// QUERY object
}, {
// Any options defined in "Custom request"
})
const query = {}
const options = {}
return http.delete('/items/1', query, options)
.then((response) => {
return {response}
})
.catch((error) => {
return {error}
})
}
```
*factory*
### operator
```js

@@ -323,9 +456,31 @@ import {httpPost} from '@cerebral/http/operators'

httpDelete(string`/items/${state`currentItemId`}`),
/*
PROPS: {
response: {...}
}
*/
]
```
// Alternatively with explicit paths
### operator with paths
```js
import {httpPost} from '@cerebral/http/operators'
import {state} from 'cerebral/tags'
export default [
httpDelete(string`/items/${state`currentItemId`}`), {
success: [],
error: [],
abort: [], // Optional
'${STATUS_CODE}': [] // Optionally any status code, ex. 404: []
success: [
/* PROPS: {response: {...}} */
],
error: [
/* PROPS: {error: {...}} */
],
abort: [
/* PROPS: {error: {...}} */
],
// Optionally any status code, ex. 404: []
'${STATUS_CODE}': [
/* PROPS: {response/error: {...}} */
]
}

@@ -335,16 +490,5 @@ ]

*output*
```javascript
{
result: 'the response',
status: 200,
// If aborted
isAborted: true
}
```
## uploadFile
*action*
### action
```js

@@ -356,8 +500,14 @@ function someDeleteAction ({http, props}) {

headers: {},
onProgress(progress) {} // Upload progress
onProgress: 'some.signal.path' // Upload progress
})
.then((response) => {
return {response}
})
.catch((error) => {
return {error}
})
}
```
*factory*
### operator
```js

@@ -370,12 +520,17 @@ import {httpUploadFile} from '@cerebral/http/operators'

name: state`currentFileName`
}),
}), {
success: [
/* PROPS: {response: {...}} */
],
error: [
/* PROPS: {error: {...}} */
],
abort: [
/* PROPS: {error: {...}} */
],
// Alternatively with explicit paths
httpUploadFile('/uploads', props`file`, {
name: state`currentFileName`
}), {
success: [],
error: [],
abort: [], // Optional
'${STATUS_CODE}': [] // Optionally any status code, ex. 404: []
// Optionally any status code, ex. 404: []
'${STATUS_CODE}': [
/* PROPS: {response/error: {...}} */
]
}

@@ -385,35 +540,19 @@ ]

*output*
```javascript
{
result: 'the response',
status: 200,
### operator with paths
```js
import {httpUploadFile} from '@cerebral/http/operators'
import {state, props} from 'cerebral/tags'
// If aborted
isAborted: true
}
export default [
httpUploadFile('/uploads', props`file`, {
name: state`currentFileName`
}),
/*
PROPS: {
response: {...}
}
*/
]
```
## response
```js
function someGetAction ({http}) {
return http.get('/items')
// All status codes between 200 - 300, including 200
.then((response) => {
response.status // Status code of response
response.result // Parsed response text
// The response headers are returned as an object with lowercase header
// names as keys. Values belonging to the same key are separated by ', '.
response.headers // Parsed response headers
})
// All other status codes
.catch((error) => {
// HttpProviderError
error.message // {status: 500, result: 'response text', headers: {}, isAborted: false}
})
}
```
## abort

@@ -428,7 +567,7 @@ You can abort any running request, causing the request to resolve as status code **0** and set an **isAborted** property on the response object.

.catch((error) => {
if (error.message.isAborted) {
if (error.isAborted) {
return path.abort()
}
return path.error({error: error.message})
return path.error({error})
})

@@ -435,0 +574,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc