Coder Social home page Coder Social logo

cycle-http-driver's Introduction

Cycle HTTP Driver

A Cycle.js Driver for making HTTP requests, based on superagent.

npm install @cycle/http

npm version

Usage

Basics:

import Cycle from '@cycle/core';
import {makeHTTPDriver} from '@cycle/http';

function main(responses) {
  // ...
}

const drivers = {
  HTTP: makeHTTPDriver()
}

Cycle.run(main, drivers);

Simple and normal use case:

function main(responses) {
  const HELLO_URL = 'http://localhost:8080/hello';
  let request$ = Rx.Observable.just(HELLO_URL);
  let vtree$ = responses.HTTP
    .filter(res$ => res$.request === HELLO_URL)
    .mergeAll()
    .map(res => res.text) // We expect this to be "Hello World"
    .startWith('Loading...')
    .map(text =>
      h('div.container', [
        h('h1', text)
      ])
    );

  return {
    DOM: vtree$,
    HTTP: request$
  };
}

A thorough guide to the Observable API inside main:

function main(responses) {
  // Notice $$: it means this is a metastream, in other words, an Observable
  // of Observables.
  let httpResponse$$ = responses.HTTP;

  httpResponse$$.subscribe(httpResponse$ => {
    // Notice that httpResponse$$ emits httpResponse$.
    
    // The response Observable has a special field attached to it:
    // `request`, which is the same object we emit in the Observable at the
    // return of `main`. This is useful for filtering: you can find the 
    // httpResponse$ corresponding to a certain request.
    console.log(httpResponse$.request);
  });

  let httpResponse$ = httpResponse$$.mergeAll(); // flattens the metastream
  // OR `httpResponse$$.switch()` to ignore past response streams.
  
  httpResponse$.subscribe(httpResponse => {
    // httpResponse is the object we get as response from superagent.
    // Check the documentation in superagent to know the structure of
    // this object.
    console.log(httpResponse.status); // 200
  });

  // The request Observable is the string `http://localhost:8080/ping` emitted
  // every second.
  let request$ = Rx.Observable.interval(1000)
    .map(() => 'http://localhost:8080/ping');

  return {
    HTTP: request$ // HTTP driver expects the request$ as input
  };
}

For a more advanced usage, check the Search example and the documentation.

Browser support

Sauce Test Status

IE 8 is not supported because this library depends on superagent, which knowingly doesn't support IE 8.


Build Status Dependency Status devDependency Status

cycle-http-driver's People

Contributors

benjyhirsch avatar greenkeeperio-bot avatar staltz avatar tylors avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.