Giter Club home page Giter Club logo

falcor-http-datasource's Introduction

HttpDataSource

This is a Falcor DataSource which can be used to retrieve JSON Graph information from an HTTP server.

Install

npm install falcor-http-datasource

Usage

Minimalistic ES6 example, a quick dirty setup

import falcor from 'falcor';
import HttpDataSource from 'falcor-http-datasource';

var model = new falcor.Model({
  source: new HttpDataSource('/model.json')
});

If you need some additional info for your global HTTP requests consider something like

JWT

var source = new HttpDataSource('/model.json', {
  headers: {
    'Authorization': `bearer ' + token`
  }
});

POST JSON

var source = new HttpDataSource('/model.json', {
  headers: {
    'Content-Type': 'application/json'
  }
});

Cookies

var source = new HttpDataSource('/model.json', {
  withCredentials: true
});
// server must include the header `Access-Control-Allow-Credentials: true`

CORS

var source = new HttpDataSource('/model.json', {
  crossDomain: true
});

or you might want to pass it to constructor as your global AppSource

export class AppSource extends HttpDataSource {
  constructor(path, token) {
    super(path, {
      headers: {
        'Authorization': `bearer ${ token }`
      }
      timeout: 20000
    })
  }

  get(...args) {
    // returns an Observable if you wanted to map/filter/reduce/etc
    return super.get(...args)
  }
  set(...args) {
    // returns an Observable if you wanted to map/filter/reduce/etc
    return super.set(...args)
  }
  call(...args) {
    // returns an Observable if you wanted to map/filter/reduce/etc
    return super.call(...args)
  }

  onBeforeRequest(config) {
    // as of now you're able to mutate the config object before we create our xhr instance
    // you would attach any url params here
    // config.url = config.url + '&something=Value'
    console.log(config);
  }
  buildQueryObject(...args) {
    // helper method to build our url for advanced implementations
    return super.buildQueryObject(...args)
  }
}

export class FalcorModel extends falcor.Model {
  constructor(cache) {
    super({
      cache: cache,
      source: new AppSource('/model.json', user.token)
    });
  }
}

falcor-http-datasource's People

Contributors

falcor-build avatar jhusain avatar marcello3d avatar mattflix avatar mattmarcello avatar pajn avatar patrickjs avatar quramy avatar ratson avatar rmeshenberg avatar sdesai avatar syarul avatar theprimeagen avatar trxcllnt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

falcor-http-datasource's Issues

Datasource Error message is "[object Object]" when response is JSON

When the server responds to a falcor request w/ a JSON error, the message property on the error object emitted by the model is [object Object], not the incoming JSON error. E.g.

server response body

{"error":"invalid_token","error_description":"Cannot convert access token to JSON"}

model request

model.get(paths)
  .catch(err => console.log(err.message))
  .subscribe(...)

// "[object Object]"

It looks like the http-datasource assumes the response is plain text (see here).

One solution would be to simply stringify the response: errorThrown = new Error(JSON.stringify(textStatus));. Not sure if there's a smarter approach.

It also would be nice if the observable's emitted error preserved the status code, so an app could differentiate between, e.g., 401s and 500s.

module declare "falcor-browser"

is not the same as repo name "falcor-http-datasource", requiring this with;

var httpDatasource = require('falcor-browser')

return missing.

Two network requests for every get

There's a good chance I'm overlooking something simple here but just in case I figured it couldn't hurt to ask:

I'm currently working an application using Falcor and noticed my model.get calls were initiating two outbound network requests with the exact same size/url within 1ms of each other.

I finally traced it all the way to this line getting called twice:
https://github.com/Netflix/falcor-http-datasource/blob/master/src/request.js#L139

Because this is called twice:
https://github.com/Netflix/falcor-http-datasource/blob/master/src/request.js#L43

When request is only being called once here:
https://github.com/Netflix/falcor-http-datasource/blob/master/src/XMLHttpSource.js#L48

Unfortunately that's about as far as I was able to get.

Invalid GET URL created if DataSource is created with a url which already contains query params

var model = new falcor.Model({
             source: new falcor.HttpDataSource('http://foo.bar?p=baz')
            });

model.get('some.path').subscribe(...);

Results in http://foo.bar?p=baz?paths=[['some', 'path']] requests being dispatched to the remote data source.

Not sure if this is a valid use case for HttpDataSource construction (it's not explicitly addressed in the docs). That is, whether or not I'm allowed to add params to the data source url during construction.

If not, HttpDataSource/DataSource may need to provide another way to specify 'additional params', to be sent across with Falcor requests.

Requests have invalid url encoding

My falcor router / virtual jsongraph lives inside an AWS Api Gateway endpoint and a lot of the paths generated by my client side falcor-http-datasource don't get routed because the url they generate is invalid because they include {} and ". I end up having to use the following as a work around:

class CustomSource extends HttpDataSource {
  onBeforeRequest (config) {
    config.url = config.url.replace(/"/g, "'")
    var splitUrl = config.url.split('?')
    config.url = splitUrl[0] + '?' + encodeURI(splitUrl[1])
  }
}

on the server side I switch back the single and double quotes before parsing the context.

All in all, this isn't a big deal but it would be nice if it worked out of the box with stricter URL parsers.

Falcor browser is not working on all browsers.

It is currently relying on some pretty new APIs. We should probably support XMLHttpRequest level 1, and work with the windows team to confirm it works on IE9 and above.

I was also not able to get it working on Firefox.

Attaching a subscription to the request fires the HTTP request multiple times

I have a custom data source that looks like this:

function setLoaded() {
  // Do things
}

function noop() {}

class CustomSource extends HttpDataSource {
  get(...args) {
    const result = super.get(...args);
    result.subscribe(noop, noop, setLoaded);
    return result;
  }
}

But after adding that subscription, all of my network requests have been doubled.

screen shot 2015-12-18 at 2 35 55 pm

Is this a bug with falcor-http-datasource or am I not subscribing correctly?

URI encode jsonGraph payload

Given I run the command:

falcor.setValue(['somethingById', 'abc123', 'name'], 'This & That').then(something);

The JSON payload that is set to the server looks like:

screen shot 2016-01-27 at 2 49 30 pm

And an error is caused on the backend:

 SyntaxError: Unexpected end of input
     at Object.parse (native)
     at /app/node_modules/falcor-express/src/requestToContext.js:21:37
     at Array.forEach (native)
     at requestToContext (/app/node_modules/falcor-express/src/requestToContext.js:18:31
     at /app/node_modules/falcor-express/src/index.js:14:23
     ...

It seems that falcor should URI encode its jsonGraph payload before posting.

This was transplanted from Netflix/falcor#722

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.