@cerebral/http
Advanced tools
Comparing version 4.2.1 to 4.2.2-1523781648414
@@ -28,5 +28,7 @@ 'use strict'; | ||
}; | ||
xhr.open(options.method, options.baseUrl + options.url); | ||
xhr.timeout = options.timeout || 0; | ||
xhr.open(options.method, options.baseUrl + options.url); | ||
options.onRequest(xhr, options); | ||
@@ -33,0 +35,0 @@ |
{ | ||
"name": "@cerebral/http", | ||
"version": "4.2.1", | ||
"version": "4.2.2-1523781648414", | ||
"description": "HTTP provider for Cerebral 2", | ||
@@ -27,8 +27,8 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"cerebral": "^4.2.1" | ||
"cerebral": "^4.2.1-1523781648414" | ||
}, | ||
"devDependencies": { | ||
"cerebral": "^4.2.1", | ||
"cerebral": "^4.2.1-1523781648414", | ||
"xhr-mock": "^2.1.0" | ||
} | ||
} |
256
README.md
# @cerebral/http | ||
## Install | ||
**NPM** | ||
@@ -9,2 +10,3 @@ | ||
## 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. | ||
@@ -15,5 +17,5 @@ | ||
```js | ||
import {set} from 'cerebral/operators' | ||
import {httpGet} from '@cerebral/http/operators' | ||
import {state, props, string} from 'cerebral/tags' | ||
import { set } from 'cerebral/operators' | ||
import { httpGet } from '@cerebral/http/operators' | ||
import { state, props, string } from 'cerebral/tags' | ||
@@ -31,3 +33,3 @@ export default [ | ||
```js | ||
import {Controller, Module} from 'cerebral' | ||
import { Controller, Module } from 'cerebral' | ||
import HttpProvider from '@cerebral/http' | ||
@@ -42,3 +44,3 @@ | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
'Accept': 'application/json' | ||
Accept: 'application/json' | ||
}, | ||
@@ -58,3 +60,3 @@ | ||
const app = Module({ | ||
providers: { http } | ||
providers: { http } | ||
}) | ||
@@ -68,3 +70,3 @@ | ||
```js | ||
function updateDefaultHttpOptions({http}) { | ||
function updateDefaultHttpOptions({ http }) { | ||
http.updateOptions({ | ||
@@ -77,8 +79,10 @@ // Updated options | ||
## abort | ||
You can abort any running request, causing the request to resolve as status code **0** and sets the type to **abort** on the error object. | ||
```js | ||
function searchItems({input, state, path, http}) { | ||
function searchItems({ input, state, path, http }) { | ||
http.abort('/items*') // regexp string | ||
return http.get(`/items?query=${input.query}`) | ||
return http | ||
.get(`/items?query=${input.query}`) | ||
.then(path.success) | ||
@@ -90,3 +94,3 @@ .catch((error) => { | ||
return path.error({error}) | ||
return path.error({ error }) | ||
}) | ||
@@ -96,3 +100,4 @@ } | ||
export default [ | ||
searchItems, { | ||
searchItems, | ||
{ | ||
success: [], | ||
@@ -106,2 +111,3 @@ error: [], | ||
## cors | ||
Cors has been turned into a "black box" by jQuery. Cors is actually a very simple concept, but due to a lot of confusion of "Request not allowed", **cors** has been an option to help out. In HttpProvider we try to give you the insight to understand how cors actually works. | ||
@@ -111,7 +117,7 @@ | ||
- text/plain | ||
- application/x-www-form-urlencoded | ||
- application/json; charset=UTF-8 | ||
* text/plain | ||
* application/x-www-form-urlencoded | ||
* application/json; charset=UTF-8 | ||
Note that this is only related to the **request**. If you want to define what you want as response, you set the **Accept** header, which is *application/json* by default. | ||
Note that this is only related to the **request**. If you want to define what you want as response, you set the **Accept** header, which is _application/json_ by default. | ||
@@ -141,3 +147,3 @@ ## errors | ||
- Inside an action | ||
* Inside an action | ||
@@ -150,7 +156,8 @@ ```js | ||
- Going down an error path | ||
* Going down an error path | ||
```js | ||
[ | ||
httpGet('/something'), { | ||
;[ | ||
httpGet('/something'), | ||
{ | ||
success: [], | ||
@@ -164,3 +171,3 @@ error: [ | ||
- To module catch handlers | ||
* To module catch handlers | ||
@@ -174,5 +181,3 @@ ```js | ||
Module({ | ||
catch: [ | ||
[HttpProviderError, errorCatched] | ||
] | ||
catch: [[HttpProviderError, errorCatched]] | ||
}) | ||
@@ -184,13 +189,15 @@ ``` | ||
### action | ||
```js | ||
function someDeleteAction ({http}) { | ||
function someDeleteAction({ http }) { | ||
const query = {} | ||
const options = {} | ||
return http.delete('/items/1', query, options) | ||
return http | ||
.delete('/items/1', query, options) | ||
.then((response) => { | ||
return {response} | ||
return { response } | ||
}) | ||
.catch((error) => { | ||
return {error} | ||
return { error } | ||
}) | ||
@@ -201,13 +208,14 @@ } | ||
### operator | ||
```js | ||
import {httpDelete} from '@cerebral/http/operators' | ||
import {state} from 'cerebral/tags' | ||
import { httpDelete } from '@cerebral/http/operators' | ||
import { state } from 'cerebral/tags' | ||
export default [ | ||
httpDelete(string`/items/${state`currentItemId`}`), | ||
httpDelete(string`/items/${state`currentItemId`}`) | ||
/* | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
] | ||
@@ -217,8 +225,10 @@ ``` | ||
### operator with paths | ||
```js | ||
import {httpDelete} from '@cerebral/http/operators' | ||
import {state} from 'cerebral/tags' | ||
import { httpDelete } from '@cerebral/http/operators' | ||
import { state } from 'cerebral/tags' | ||
export default [ | ||
httpDelete(string`/items/${state`currentItemId`}`), { | ||
httpDelete(string`/items/${state`currentItemId`}`), | ||
{ | ||
success: [ | ||
@@ -248,13 +258,15 @@ /* PROPS: {response: {...}} */ | ||
### action | ||
```js | ||
function someGetAction ({http}) { | ||
function someGetAction({ http }) { | ||
const query = {} | ||
const options = {} | ||
return http.get('/items', query, options) | ||
return http | ||
.get('/items', query, options) | ||
.then((response) => { | ||
return {someResponse: response} | ||
return { someResponse: response } | ||
}) | ||
.catch((error) => { | ||
return {someError: error} | ||
return { someError: error } | ||
}) | ||
@@ -265,12 +277,13 @@ } | ||
### operator | ||
```js | ||
import {httpGet} from '@cerebral/http/operators' | ||
import { httpGet } from '@cerebral/http/operators' | ||
export default [ | ||
httpGet('/items'), | ||
httpGet('/items') | ||
/* | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
] | ||
@@ -282,7 +295,9 @@ ``` | ||
### operator with paths | ||
```js | ||
import {httpGet} from '@cerebral/http/operators' | ||
import { httpGet } from '@cerebral/http/operators' | ||
export default [ | ||
httpGet('/items'), { | ||
httpGet('/items'), | ||
{ | ||
success: [ | ||
@@ -312,13 +327,15 @@ /* PROPS: {response: {...}} */ | ||
### action | ||
```js | ||
function somePatchAction ({http}) { | ||
function somePatchAction({ http }) { | ||
const data = {} | ||
const options = {} | ||
return http.patch('/items/1', data, options) | ||
return http | ||
.patch('/items/1', data, options) | ||
.then((response) => { | ||
return {response} | ||
return { response } | ||
}) | ||
.catch((error) => { | ||
return {error} | ||
return { error } | ||
}) | ||
@@ -329,13 +346,14 @@ } | ||
### operator | ||
```js | ||
import {httpPatch} from '@cerebral/http/operators' | ||
import {state, props, string} from 'cerebral/tags' | ||
import { httpPatch } from '@cerebral/http/operators' | ||
import { state, props, string } from 'cerebral/tags' | ||
export default [ | ||
httpPatch(string`/items/${props`itemId`}`, state`patchData`), | ||
httpPatch(string`/items/${props`itemId`}`, state`patchData`) | ||
/* | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
] | ||
@@ -345,8 +363,10 @@ ``` | ||
### operator with paths | ||
```js | ||
import {httpPatch} from '@cerebral/http/operators' | ||
import {state, props, string} from 'cerebral/tags' | ||
import { httpPatch } from '@cerebral/http/operators' | ||
import { state, props, string } from 'cerebral/tags' | ||
export default [ | ||
httpPatch(string`/items/${props`itemId`}`, state`patchData`), { | ||
httpPatch(string`/items/${props`itemId`}`, state`patchData`), | ||
{ | ||
success: [ | ||
@@ -376,13 +396,15 @@ /* PROPS: {response: {...}} */ | ||
### action | ||
```js | ||
function somePostAction ({http}) { | ||
function somePostAction({ http }) { | ||
const data = {} | ||
const options = {} | ||
return http.post('/items', data, options) | ||
return http | ||
.post('/items', data, options) | ||
.then((response) => { | ||
return {response} | ||
return { response } | ||
}) | ||
.catch((error) => { | ||
return {error} | ||
return { error } | ||
}) | ||
@@ -393,5 +415,6 @@ } | ||
### operator | ||
```js | ||
import {httpPost} from '@cerebral/http/operators' | ||
import {props} from 'cerebral/tags' | ||
import { httpPost } from '@cerebral/http/operators' | ||
import { props } from 'cerebral/tags' | ||
@@ -402,8 +425,8 @@ export default [ | ||
foo: 'bar' | ||
}), | ||
}) | ||
/* | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
] | ||
@@ -413,5 +436,6 @@ ``` | ||
### operator with paths | ||
```js | ||
import {httpPost} from '@cerebral/http/operators' | ||
import {props} from 'cerebral/tags' | ||
import { httpPost } from '@cerebral/http/operators' | ||
import { props } from 'cerebral/tags' | ||
@@ -422,3 +446,4 @@ export default [ | ||
foo: 'bar' | ||
}), { | ||
}), | ||
{ | ||
success: [ | ||
@@ -448,13 +473,15 @@ /* PROPS: {response: {...}} */ | ||
### action | ||
```js | ||
function somePutAction ({http}) { | ||
function somePutAction({ http }) { | ||
const data = {} | ||
const options = {} | ||
return http.put('/items/1', data, options) | ||
return http | ||
.put('/items/1', data, options) | ||
.then((response) => { | ||
return {response} | ||
return { response } | ||
}) | ||
.catch((error) => { | ||
return {error} | ||
return { error } | ||
}) | ||
@@ -465,4 +492,5 @@ } | ||
### operator | ||
```js | ||
import {httpPut} from '@cerebral/http/operators' | ||
import { httpPut } from '@cerebral/http/operators' | ||
@@ -472,8 +500,8 @@ export default [ | ||
// data object | ||
}), | ||
}) | ||
/* | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
] | ||
@@ -483,4 +511,5 @@ ``` | ||
### operator with paths | ||
```js | ||
import {httpPut} from '@cerebral/http/operators' | ||
import { httpPut } from '@cerebral/http/operators' | ||
@@ -490,3 +519,4 @@ export default [ | ||
// data object | ||
}), { | ||
}), | ||
{ | ||
success: [ | ||
@@ -514,5 +544,7 @@ /* PROPS: {response: {...}} */ | ||
## 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**. | ||
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 | ||
@@ -527,2 +559,3 @@ { | ||
### error | ||
```js | ||
@@ -574,15 +607,17 @@ { | ||
### action | ||
```js | ||
function someDeleteAction ({http, props}) { | ||
return http.uploadFile('/upload', props.files, { | ||
name: 'filename.png', // Default to "files" | ||
data: {}, // Additional form data | ||
headers: {}, | ||
onProgress: 'some.signal.path' // Upload progress | ||
}) | ||
function someDeleteAction({ http, props }) { | ||
return http | ||
.uploadFile('/upload', props.files, { | ||
name: 'filename.png', // Default to "files" | ||
data: {}, // Additional form data | ||
headers: {}, | ||
onProgress: 'some.signal.path' // Upload progress | ||
}) | ||
.then((response) => { | ||
return {response} | ||
return { response } | ||
}) | ||
.catch((error) => { | ||
return {error} | ||
return { error } | ||
}) | ||
@@ -593,5 +628,6 @@ } | ||
### operator | ||
```js | ||
import {httpUploadFile} from '@cerebral/http/operators' | ||
import {state, props} from 'cerebral/tags' | ||
import { httpUploadFile } from '@cerebral/http/operators' | ||
import { state, props } from 'cerebral/tags' | ||
@@ -601,3 +637,4 @@ export default [ | ||
name: state`currentFileName` | ||
}), { | ||
}), | ||
{ | ||
success: [ | ||
@@ -622,5 +659,6 @@ /* PROPS: {response: {...}} */ | ||
### operator with paths | ||
```js | ||
import {httpUploadFile} from '@cerebral/http/operators' | ||
import {state, props} from 'cerebral/tags' | ||
import { httpUploadFile } from '@cerebral/http/operators' | ||
import { state, props } from 'cerebral/tags' | ||
@@ -630,9 +668,9 @@ export default [ | ||
name: state`currentFileName` | ||
}), | ||
}) | ||
/* | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
PROPS: { | ||
response: {...} | ||
} | ||
*/ | ||
] | ||
``` |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
77838
636
34
2