@cerebral/http
Advanced tools
Comparing version 5.0.0-1521140905511 to 5.0.0-1525724201307
130
index.d.ts
@@ -1,77 +0,119 @@ | ||
import { Provider } from "function-tree" | ||
export enum HttpProviderErrorTypes { | ||
Http = 'http', | ||
Abort = 'abort', | ||
Timeout = 'timeout', | ||
} | ||
export enum HttpProviderErrorTypes {Http = 'http', Abort = 'abort', Timeout = 'timeout'} | ||
export class HttpProviderError extends Error { | ||
constructor (type: HttpProviderErrorTypes, status: number, headers: any, body: any, message?: string, isAborted?: boolean) | ||
constructor( | ||
type: HttpProviderErrorTypes, | ||
status: number, | ||
headers: any, | ||
body: any, | ||
message?: string, | ||
isAborted?: boolean | ||
) | ||
type: HttpProviderErrorTypes | ||
response: { | ||
status: number, | ||
headers: any, | ||
result: any, | ||
isAborted: boolean, | ||
status: number | ||
headers: any | ||
result: any | ||
isAborted: boolean | ||
} | ||
toJSON (): any | ||
toJSON(): any | ||
} | ||
export default function HttpProvider(moduleOptions: HttpModuleOptions): HttpProvider; | ||
export default function HttpProvider( | ||
moduleOptions: HttpModuleOptions | ||
): HttpProvider | ||
export interface HttpModuleOptions { | ||
method?: string, | ||
baseUrl?: string, | ||
headers?: { [id: string]: string }, | ||
onRequest?: (xhr: XMLHttpRequest, options: HttpResponseOptions) => void; | ||
onResponse?: (response: XMLHttpRequest, resolve: Function, reject: Function, options?: HttpResponseOptions) => void; | ||
onRequestCallback?: (xhr: XMLHttpRequest) => void; | ||
onResponseCallback?: (response: XMLHttpRequest) => void; | ||
method?: string | ||
baseUrl?: string | ||
headers?: { [id: string]: string } | ||
onRequest?: (xhr: XMLHttpRequest, options: HttpResponseOptions) => void | ||
onResponse?: ( | ||
response: XMLHttpRequest, | ||
resolve: Function, | ||
reject: Function, | ||
options?: HttpResponseOptions | ||
) => void | ||
onRequestCallback?: (xhr: XMLHttpRequest) => void | ||
onResponseCallback?: (response: XMLHttpRequest) => void | ||
} | ||
export interface HttpRequestOptions { | ||
query?: { [id: string]: any }, | ||
headers?: { [id: string]: string }, | ||
query?: { [id: string]: any } | ||
headers?: { [id: string]: string } | ||
} | ||
export interface HttpResponseOptions extends HttpRequestOptions, HttpModuleOptions { | ||
body: string | ||
export interface HttpResponseOptions | ||
extends HttpRequestOptions, | ||
HttpModuleOptions { | ||
body: string | ||
} | ||
export interface HttpRequestReponse<T> { | ||
result: T, | ||
status: string | ||
result: T | ||
status: string | ||
} | ||
export interface HttpProvider { | ||
request: any, | ||
get<T>(url: string, passedQuery?: { [id: string]: any }, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
post<T>(url: string, body: any, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
put<T>(url: string, body: any, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
patch<T>(url: string, body: any, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
delete<T>(url: string, passedQuery: { [id: string]: any }, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
uploadFile<T>(url: string, files: FileList | File, options?: FileUploadOptions): FileUpload<T>, | ||
updateOptions(newOptions: HttpModuleOptions): void; | ||
abort(regexp: string): boolean; | ||
request: any | ||
get<T>( | ||
url: string, | ||
passedQuery?: { [id: string]: any }, | ||
options?: HttpRequestOptions | ||
): Promise<HttpRequestReponse<T>> | ||
post<T>( | ||
url: string, | ||
body: any, | ||
options?: HttpRequestOptions | ||
): Promise<HttpRequestReponse<T>> | ||
put<T>( | ||
url: string, | ||
body: any, | ||
options?: HttpRequestOptions | ||
): Promise<HttpRequestReponse<T>> | ||
patch<T>( | ||
url: string, | ||
body: any, | ||
options?: HttpRequestOptions | ||
): Promise<HttpRequestReponse<T>> | ||
delete<T>( | ||
url: string, | ||
passedQuery: { [id: string]: any }, | ||
options?: HttpRequestOptions | ||
): Promise<HttpRequestReponse<T>> | ||
uploadFile<T>( | ||
url: string, | ||
files: FileList | File, | ||
options?: FileUploadOptions | ||
): FileUpload<T> | ||
updateOptions(newOptions: HttpModuleOptions): void | ||
abort(regexp: string): boolean | ||
} | ||
export interface FileUploadProgress { | ||
progress: number | ||
progress: number | ||
} | ||
export interface FileUploadOptions { | ||
name?: string, | ||
data?: { [id: string]: any }, | ||
headers?: { [id: string]: string }, | ||
onProgress?: (progress: FileUploadProgress) => void | string | ||
name?: string | ||
data?: { [id: string]: any } | ||
headers?: { [id: string]: string } | ||
onProgress?: (progress: FileUploadProgress) => void | string | ||
} | ||
export interface FileUploadResult<T> { | ||
result?: T, | ||
status: string | ||
result?: T | ||
status: string | ||
} | ||
export class FileUpload<TResponse> { | ||
constructor(options: FileUploadOptions); | ||
xhr: XMLHttpRequest; | ||
isAborted: boolean; | ||
send: (files: FileList | File[]) => Promise<FileUploadResult<TResponse>>; | ||
abort: () => void; | ||
constructor(options: FileUploadOptions) | ||
xhr: XMLHttpRequest | ||
isAborted: boolean | ||
send: (files: FileList | File[]) => Promise<FileUploadResult<TResponse>> | ||
abort: () => void | ||
} |
@@ -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 @@ |
@@ -5,3 +5,7 @@ import { Tag } from 'cerebral/tags' | ||
export function httpAbort(regexp: string | Tag): Action | ||
export function httpDelete(url: string | Tag, query?: any, options?: any): Action | ||
export function httpDelete( | ||
url: string | Tag, | ||
query?: any, | ||
options?: any | ||
): Action | ||
export function httpGet(url: string | Tag, query?: any, options?: any): Action | ||
@@ -11,2 +15,6 @@ export function httpPatch(url: string | Tag, body?: any, options?: any): Action | ||
export function httpPut(url: string | Tag, body?: any, options?: any): Action | ||
export function httpUploadFile(urlValue: string | Tag, filesValue: string[], optionsValue: any[]): Action | ||
export function httpUploadFile( | ||
urlValue: string | Tag, | ||
filesValue: string[], | ||
optionsValue: any[] | ||
): Action |
{ | ||
"name": "@cerebral/http", | ||
"version": "5.0.0-1521140905511", | ||
"version": "5.0.0-1525724201307", | ||
"description": "HTTP provider for Cerebral 2", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha --compilers js:babel-register \"src/**/*.test.js\" \"test/**/*.test.js\"", | ||
"test": "mocha --require babel-register \"src/**/*.test.js\" \"test/**/*.test.js\"", | ||
"build": "cross-env BABEL_ENV=production babel src/ --out-dir=lib/ -s", | ||
@@ -28,8 +28,4 @@ "prepublish": "npm run build" | ||
"peerDependencies": { | ||
"cerebral": "^5.0.0-1521140905511" | ||
}, | ||
"devDependencies": { | ||
"cerebral": "^5.0.0-1521140905511", | ||
"xhr-mock": "^2.1.0" | ||
"cerebral": "^5.0.0-1525724201307" | ||
} | ||
} |
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
77745
0
744
636
34