GithubHelp home page GithubHelp logo

luoli's Introduction

Luoli

Wrap your functions with sexy middlewares without the hassle.

Why use Luoli ?

When you are writing functions or services in node.js you sometime want to generate side-effects, share a context or simply add logging it's a not easy to do it in a elegant way without a lot of boilerplate.

Luoli help you to add middlewares to everything without modifying the signature of your functions.

Simple exemple

// The function you want to wrap
const UpdateUser = () => (id, payload) => new Promise(r => setTimeout(r, 200))

// prepare the middleware
const enhancer = applyMiddleware(timerMiddleware)

// create the config to pass to middlewares
const config = { user: { role: 'ADMIN' } }

// create the useService function using the config and enhancer
const useService = useMiddleware(config, enhancer)

// get the binded useService function
const updateUser = useService(UpdateUser)

const res = await updateUser(1, { name: 'Jessica' })

console.log(res)

// Output
// UpdateUser: 200.86 ms

Now you can check in the console the duration every call of updateUser.

Luoli lifecycle

const middleware = config => {
  // 1. Initialize the plugin on startup
  const someData = {}

  return serviceContext => {
    // 2. access data of the wrapped function
    return executionContext => {

      return next => async (...args) => {
        // 3. access data from the exection context this is a new object in every call

        // here calling next is the equivalent of calling directly the service method
        const res = await next(...args) // 4, 5.

        // 6. service terminated you can clean up, patch the result, etc

        return res
      }
    }
  }
}

const Service = () => {
  // 4. use the execution context

  return async (data1, data2) => {
    // 5. the true service logic

    return 'something'
  }
}

const enhancer = applyMiddleware(middleware(config)) // run 1

const useService = useContext({}, enhancer) // run 2

const service = useService(Service) // run 3

service() // run 3, 4, 5 and 6

luoli's People

Contributors

shiro-42 avatar

Stargazers

Roman avatar Maxime JOUBERT avatar  avatar

Watchers

James Cloos 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.