1.4.7 • Published 4 years ago

laconiar v1.4.7

Weekly downloads
57
License
MIT
Repository
github
Last release
4 years ago

laconiar

requirejs like service for laconia

npm npm Codecov branch Commitizen friendly styled with prettier Code of Conduct Roadmap

Why?

Every time you use require you make it tightly coupled with required library. This isn't good for tests and SOLID principles.

With this you can `require' things and inject/mock them when testing

Installation

npm install --save laconiar

Usage

// app.js

module.exports = (event, {
  R: {
    fs, // this same as const fs = require('fs');
    fs: { createReadStream }, // this same as const { createReadStream } = require('fs');
    'aws-sdk': awsSDK, // this is same as const awsSDK = require('aws-sdk');
    './spam': err, // this will not work;
    `${__dirname}/spam`: spam // this will work
  }
}) => {
  // do something with required modules;
  return createReadStream();
};


// index.js

const laconia require('@laconia/core');
const R = require('laconiar');
const app = require('./app');
exports.handler = laconia(app).register(R());


// app.test.js

const app = require('./app');
describe('testing app', () => {
  it('mocking requirements', () => {
    const createReadStream = jest.fn();
    app(
      R: {
        fs: { createReadStream }
      }
    );
    expect(createReadStream).toHaveBeenCalled();
  });
});

FAQ

Should you use it?

Good question, the main feature of Laconia is to be a Micro dependency injection framework, some DI frameworks recommends to do not use it for things like require external library.

My point is that you may use it for service creation.

How use it with Tree shaking?

You couldn't, since your dependency are loaded dynamically. Isn't not impossible but not implemented yet.

Related

Contributors

Thanks goes to these people (emoji key):

hugosenari💻 📖 🚇

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © hugosenari

1.4.7

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.7

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago