@cerebral/http
Install
NPM
npm install @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 article.
All factories of HTTP provider supports template tags.
Instantiate
const http = const app = const controller =
You can update these default options in an action:
{ http}
abort
You can abort any running request, causing the request to resolve as status code 0 and set an isAborted property on the response object.
{ http // regexp string return http } searchItems success: error: abort:
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.
Cors has nothing to do with the client. The only client configuration related to cors is the withCredentials option, which makes sure cookies are passed to the cross origin server. The only requirement for cors to work is that you pass the correct Content-Type. Now, this depends on the server in question. Some servers allows any content-type, others require a specific one. These are the typical ones:
- 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.
errors
HttpProviderError
// Error structure name: 'HttpProviderError' 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
{ return http}
- Going down an error path
success: error: // {error: ...}
- To signal/global catch handlers
const errorCatched = // {error: ...} displayError
delete
action
{ const query = {} const options = {} return http }
operator
/* PROPS: { response: {...} } */
operator with paths
success: /* PROPS: {response: {...}} */ error: /* PROPS: {error: {...}} */ abort: /* PROPS: {error: {...}} */ // Optionally any status code, ex. 404: [] '${STATUS_CODE}': /* PROPS: {response/error: {...}} */
get
action
{ const query = {} const options = {} return http }
operator
/* PROPS: { response: {...} } */
On error this will throw to the signal or global catch handler.
operator with paths
success: /* PROPS: {response: {...}} */ error: /* PROPS: {error: {...}} */ abort: /* PROPS: {error: {...}} */ // Optionally any status code, ex. 404: [] '${STATUS_CODE}': /* PROPS: {response/error: {...}} */
patch
action
{ const data = {} const options = {} return http }
operator
/* PROPS: { response: {...} } */
operator with paths
success: /* PROPS: {response: {...}} */ error: /* PROPS: {error: {...}} */ abort: /* PROPS: {error: {...}} */ // Optionally any status code, ex. 404: [] '${STATUS_CODE}': /* PROPS: {response/error: {...}} */
post
action
{ const data = {} const options = {} return http }
operator
/* PROPS: { response: {...} } */
operator with paths
success: /* PROPS: {response: {...}} */ error: /* PROPS: {error: {...}} */ abort: /* PROPS: {error: {...}} */ // Optionally any status code, ex. 404: [] '${STATUS_CODE}': /* PROPS: {response/error: {...}} */
put
action
{ const data = {} const options = {} return http }
operator
/* PROPS: { response: {...} } */
operator with paths
success: /* PROPS: {response: {...}} */ error: /* PROPS: {error: {...}} */ abort: /* PROPS: {error: {...}} */ // Optionally any status code, ex. 404: [] '${STATUS_CODE}': /* PROPS: {response/error: {...}} */
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
result: 'the response body' headers: ... status: 200 isAborted: false
error
name: 'HttpProviderError' message: 'Some potential error message' result: 'Message or response body' status: 500 isAborted: false headers: {} stack: '...'
request
{ return http}
uploadFile
action
{ return http }
operator
success: /* PROPS: {response: {...}} */ error: /* PROPS: {error: {...}} */ abort: /* PROPS: {error: {...}} */ // Optionally any status code, ex. 404: [] '${STATUS_CODE}': /* PROPS: {response/error: {...}} */
operator with paths
/* PROPS: { response: {...} } */