https://github.com/cyclejs/jsonp
Driver for HTTP requests through JSONP
https://github.com/cyclejs/jsonp
Last synced: 6 months ago
JSON representation
Driver for HTTP requests through JSONP
- Host: GitHub
- URL: https://github.com/cyclejs/jsonp
- Owner: cyclejs
- License: mit
- Created: 2015-07-17T13:49:35.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-06-22T08:17:15.000Z (over 3 years ago)
- Last Synced: 2025-06-08T18:14:02.275Z (6 months ago)
- Language: TypeScript
- Homepage:
- Size: 258 KB
- Stars: 7
- Watchers: 2
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Cycle JSONP
A Driver for making HTTP requests through the JSONP hack, based on the [jsonp](https://github.com/webmodules/jsonp) package. This package is small, hacky (as JSONP is too), and untested. Whenever possible, use proper server and client CORS solution with the HTTP Driver.
```
npm install @cycle/jsonp
```
## Usage
```js
function main(responses) {
// This API endpoint returns a JSON response
const HELLO_URL = 'http://localhost:8080/hello';
let request$ = Rx.Observable.just(HELLO_URL);
let vtree$ = responses.JSONP
.filter(res$ => res$.request === HELLO_URL)
.mergeAll()
.startWith({text: 'Loading...'})
.map(json =>
h('div.container', [
h('h1', json.text)
])
);
return {
DOM: vtree$,
JSONP: request$
};
}
Cycle.run(main, {
DOM: makeDOMDriver('.js-container'),
JSONP: makeJSONPDriver()
})
```
# API
JSONP Driver factory.
This is a function which, when called, returns a JSONP Driver for Cycle.js
apps. The driver is also a function, and it takes a stream of requests
(URL strings) as input, and generates a metastream of responses.
**Requests**. The stream of requests should emit strings as the URL of the
remote resource over HTTP.
**Responses**. A metastream is a stream of streams. The response metastream
emits streams of responses. These streams of responses have a `request`
field attached to them (to the stream object itself) indicating which
request (from the driver input) generated this response stream. The
response streams themselves emit the response object received through the
npm `jsonp` package.
#### Returns:
*(Function)* the JSONP Driver function