@gastonyte/cycle-fetch-driver
Advanced tools
Comparing version 5.0.1 to 5.0.2
{ | ||
"name": "@gastonyte/cycle-fetch-driver", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "A Cycle.js Driver for making HTTP requests through fetch", | ||
@@ -5,0 +5,0 @@ "main": "lib/driver.js", |
@@ -28,5 +28,7 @@ # Cycle Fetch Driver | ||
import fetch from 'isomorphic-fetch'; | ||
import Cycle from '@cycle/run'; | ||
import { run } from '@cycle/run'; | ||
import FetchDriver from '@gastonyte/cycle-fetch-driver'; | ||
const main = ({ HTTP }) => { | ||
@@ -43,9 +45,9 @@ | ||
}; | ||
} | ||
}; | ||
Cycle.run(main, { | ||
run(main, { | ||
HTTP: FetchDriver({ | ||
fetch, | ||
fetch, | ||
credentials: 'same-origin' | ||
// ...otherDefaultFetchOptions | ||
// ...otherDefaultFetchOptions | ||
}) | ||
@@ -55,52 +57,13 @@ }); | ||
Simple and normal use case: | ||
Select all the responses by key or by url: | ||
```js | ||
import Stream from 'xstream'; | ||
import Cycle from '@cycle/run'; | ||
import { run } from '@cycle/run'; | ||
import FetchDriver from '@gastonyte/cycle-fetch-driver'; | ||
import flattenConcurrently from 'xstream/extra/flattenConcurrently'; | ||
import {div, h1, makeDOMDriver} from '@cycle/dom'; | ||
import fetch from 'isomorphic-fetch'; | ||
const main = ({ HTTP }) => { | ||
const HELLO_URL = 'http://localhost:8080/hello'; | ||
const request$ = Stream.of(HELLO_URL); | ||
const vtree$ = HTTP.filterByUrl(HELLO_URL) | ||
.compose(flattenConcurrently) | ||
.map(response => response.text()) | ||
.flatten() | ||
.startWith('Loading...') | ||
.map(text => | ||
div('.container', [ | ||
h1(text) | ||
]) | ||
); | ||
return { | ||
DOM: vtree$, | ||
HTTP: request$ | ||
}; | ||
} | ||
Cycle.run(main, { | ||
DOM: makeDOMDriver('#app'), | ||
HTTP: FetchDriver() | ||
}); | ||
``` | ||
Select all the responses for a certain key: | ||
```js | ||
import Stream from 'xstream'; | ||
import Cycle from '@cycle/run'; | ||
import FetchDriver from '@gastonyte/cycle-fetch-driver'; | ||
import flattenConcurrently from 'xstream/extra/flattenConcurrently'; | ||
import {div, h1, makeDOMDriver} from '@cycle/dom'; | ||
const main = ({ HTTP }) => { | ||
const HELLO_URL = 'http://localhost:8080/hello'; | ||
const HELLO_URL = 'https://jsonplaceholder.typicode.com/posts/1'; | ||
const request$ = Stream.of({ | ||
@@ -110,16 +73,20 @@ key: 'hello', | ||
}); | ||
const vtree$ = HTTP.filterByKey('hello') | ||
.compose(flattenConcurrently) | ||
.map(res => res.text()) | ||
HTTP.filterByKey('hello') // OR .filterByUrl(HELLO_URL) | ||
.flatten() | ||
.startWith('Loading...') | ||
.map(text => | ||
div('.container', [ | ||
h1(text) | ||
]) | ||
); | ||
.map(response => Stream.fromPromise(response.json())) | ||
.flatten() | ||
.map(json => ({ isLoaded: true, json })) | ||
.startWith({ isLoaded: false }) | ||
.addListener({ | ||
next: ({ isLoaded, json }) => { | ||
console.log('isLoaded:', isLoaded); | ||
if (isLoaded) { | ||
console.log('json:', json); | ||
} | ||
} | ||
}); | ||
return { | ||
DOM: vtree$, | ||
HTTP: request$ | ||
@@ -129,6 +96,7 @@ }; | ||
Cycle.run(main, { | ||
DOM: makeDOMDriver('#app'), | ||
HTTP: FetchDriver() | ||
run(main, { | ||
HTTP: FetchDriver({ | ||
fetch | ||
}) | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
169045
98