A Universal(read: isomorphic) JavaScript library for fetching data from remote datasources.
This describes what the api would look like for a fetcher based on RxJs.
| // server setup | |
| import { net, registrar } from 'profetchrx'; | |
| import postService from './postService'; | |
| import catService from './catService'; | |
| var services = registrar(postService, catService); | |
| // or | |
| var services = registrar(postService)(catService); | |
| // or | |
| var postServices = registrar(postService); | |
| var postAndCatServices = postService(catService); | |
| // express | |
| app.use(net.middleware.express(registrar /* or */ postAndCatServices)); | |
| // koa | |
| app.use(net.middleware.koa(registrar /* or */ postAndCatServices)); | |
| // how da hell does hapi work? |
| import fetchrx from 'profecthrx'; | |
| const catImageService = fetchrx({ | |
| corsPath: 'http://thecatapi.com', | |
| service: 'catImages', | |
| endpoint: '/api/images/get', | |
| headers: { Awesome: 'X' } | |
| }); | |
| gatImageService() | |
| .subscibe( | |
| function(catImage) { | |
| console.log('Got cats', uri); | |
| } | |
| ) |
| // Universal; | |
| import fetchrx from 'profetchrx'; | |
| import { Actions } from 'thundercats'; | |
| const postService = fetchrx({ | |
| service: 'postService', | |
| endpoint: '/api', | |
| headers: { Awesome: 'X' } | |
| }); | |
| class PostActions extends Actions { | |
| constructor() { | |
| super(); | |
| } | |
| static displayName = 'PostActions' | |
| createPost(data) { | |
| postService | |
| .create({ data }) | |
| .subscribe( | |
| ); | |
| } | |
| getPosts({ slug }) { | |
| postService | |
| .read({ slug }) | |
| .subscribe( | |
| ::this.setPosts | |
| // this.onError | |
| ); | |
| } | |
| setPosts(posts) { | |
| return { posts }; | |
| } | |
| } |
| import Rx from 'rx'; | |
| export default function() { | |
| return { | |
| serviceName: 'postService', | |
| read: (ctx) { | |
| const slug = ctx | |
| .pluck('params') | |
| .pluck('slug'); | |
| const isAuthor = ctx | |
| .pluck('req') | |
| .pluck('userId') | |
| .map(userId => !!userId); | |
| retrun Rx.Observable.combineLatest( | |
| slug, | |
| isAuthor, | |
| (slug, isAuthor) => ({ slug, isAuthor }) | |
| ).flatMap(queryCtx => { | |
| return getPostsFromDB(queryCtx); | |
| }); | |
| } | |
| }; | |
| } | |
| // or | |
| export default service('postService') | |
| .map(create(ctx => { | |
| return ctx | |
| .pluck('body') | |
| .pluck('data') | |
| .do(validateData) | |
| .flatMap(data => { | |
| createPost(data); | |
| }); | |
| })) | |
| .map(read(ctx => { | |
| const slug = ctx | |
| .pluck('params') | |
| .pluck('slug'); | |
| const isAuthor = ctx | |
| .pluck('req') | |
| .pluck('userId') | |
| .map(userId => !!userId); | |
| return Rx.Observable.combineLatest( | |
| slug, | |
| isAuthor, | |
| (slug, isAuthor) => ({ slug, isAuthor }) | |
| ).flatMap(queryCtx => { | |
| return getPostsFromDB(queryCtx); | |
| }); | |
| })) | |
| .map(subService('list')) | |
| .map(read(ctx => { | |
| return ctx | |
| .pluck('params') | |
| .flatMap(({ catagory }) => { | |
| return getPostsFromDB({ catagory }); | |
| }); | |
| })); |