0.4.0 • Published 5 years ago

tao-js v0.4.0

Weekly downloads
177
License
Apache 2.0
Repository
github
Last release
5 years ago

tao

Functional, aspect-oriented middleware for browser-side JavaScript and Node.js

CircleCI

Examples

Provides Middleware for Arbitrary Function Libraries

Tao is a framework for aspect-oriented programming that is meant to run anywhere: client or server, React or Express, wherever you need cross-cutting business logic. One mechanism Tao provides for aspect oriented programming is middleware, or functions that run every time you call a function in your Tao library.

    return co(function*() {
      const lib = tao({
        // In tao, an "action" is a function that takes one parameter and
        // returns a promise
        stub: () => param => Promise.resolve(param),
        plusOne: () => param => Promise.resolve(param + 1)
      })();

      lib.use(action => {
        // Prints the name of the function (action) that was called
        // "stub"
        // "plusOne"
        console.log(action.name);
      });

      assert.equal(yield lib.stub(42), 42);
      assert.equal(yield lib.plusOne(41), 42);
    });
  

Integrates with Express

Tao is meant to be a meta-framework that lets you write business logic in a framework-agnostic way, and then use the wrap() function to glue your business logic to a particular framework. For example, you can write business logic and then wrap() your functions into Express route handlers.

    return co(function*() {
      const app = express();

      const lib = tao({
        hello: () => params => {
          return Promise.resolve(params.val || 'Hello');
        }
      })();

      lib.wrap((lib, name) => {
        const fn = get(lib, name);
        return (req, res) => {
          fn(Object.assign({}, req.query, req.params)).
            then(val => res.send(val)).
            catch(err => res.status(500).send(err.message));
        };
      });

      app.get('*', lib.hello);

      const server = app.listen(3000);

      const { text } = yield superagent.get('http://localhost:3000/?val=hello');

      assert.equal(text, 'hello');
    });
  
0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago