faussaire-util

0.1.0 • Public • Published

faussaire-util v0.1

Bundle utilitary functions for faussaire

Installation

npm install --save-dev faussaire-util

Overview

Faussaire itself is a pretty low-level library. Being modular, there are many things to be added for it to be really fast and easy to use.

Runner

A Runner is a higher order function returning an other function matching the Controller.run signature.

function(params, options) => Response

To get started with ES6, just import Runner from faussaire-util

import { Runner } from 'faussaire-util'

To start using a Runner in your Controller, set it to your Controller.run method :

Route({
  template: APIRoute("posts"),
  methods: ["GET"],
  controller: Controller({
    run: Runner.group({
      store: posts,
      matcher(){
        return true;
      },
      complete(posts){
        return Response({
          data: {
            posts
          },
          status: 200,
          statusText: "OK"
        });
      }
    })
  })
})

Store

Before starting any example, let's assume we have the following store :

const store = [
  {id: 1, name: "Rewieer", group: "admin"},
  {id: 2, name: "Bardepic", group: "user"},
  {id: 3, name: "Matt", group: "user"}
];

Runner.match(obj)

Match allows you to search in a store an item matching your own defined condition. It works as it follow :

Runner.match({
  store,
  matcher(user, params){
    return user.id === params.id
  },
  success(user){
    return {
      data: { user },
      status: 200
    }
  },
  failure(){
    return {
      data: {},
      status: 404
    }
  }
});

The example pretty self-explanatory.

Runner.group(obj)

Group allows you to fetch all the items from the store matching your condition.

Runner.group({
  store,
  matcher(user, params){
    return user.group === params.group
  },
  complete(users){
    if(users){
      return {
        data: { users },
        status: 200
      }
    } else {
      return {
        data: { },
        status: 404
      }
    }
  }
});

Readme

Keywords

Package Sidebar

Install

npm i faussaire-util

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • rewieer