@adonisjs/fold
Advanced tools
Comparing version 5.0.9 to 5.0.10
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const path_1 = require("path"); | ||
const utils_1 = require("@adonisjs/utils"); | ||
const utils_1 = require("@poppinss/utils"); | ||
const IoCProxy_1 = require("./IoCProxy"); | ||
@@ -49,3 +49,3 @@ const Tracer_1 = require("./Tracer"); | ||
const absPath = this._makeRequirePath(baseNamespace, namespace); | ||
const importValue = utils_1.tsRequire(absPath); | ||
const importValue = utils_1.esmRequire(absPath); | ||
this._autoloadsCache.set(namespace, { diskPath: absPath, cachedValue: importValue }); | ||
@@ -52,0 +52,0 @@ } |
@@ -21,2 +21,5 @@ "use strict"; | ||
}, | ||
apply(target, ...args) { | ||
return callTrap(target, 'apply', ...args); | ||
}, | ||
deleteProperty(target, ...args) { | ||
@@ -50,7 +53,7 @@ return callTrap(target, 'deleteProperty', ...args); | ||
}; | ||
const classHandler = { | ||
const classHandler = Object.assign({}, objectHandler, { | ||
construct(target, ...args) { | ||
return callTrap(target, 'construct', args); | ||
}, | ||
}; | ||
}); | ||
class IoCProxyObject { | ||
@@ -57,0 +60,0 @@ constructor(binding, actual, container) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const utils_1 = require("@adonisjs/utils"); | ||
const utils_1 = require("@poppinss/utils"); | ||
class Registrar { | ||
@@ -9,3 +9,3 @@ constructor(ioc) { | ||
_loadProvider(providerPath) { | ||
const provider = utils_1.tsRequire(providerPath); | ||
const provider = utils_1.esmRequire(providerPath); | ||
return new provider(this.ioc); | ||
@@ -12,0 +12,0 @@ } |
@@ -1,81 +0,1 @@ | ||
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
## [5.0.9](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.8...@adonisjs/fold@5.0.9) (2019-04-25) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## [5.0.8](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.7...@adonisjs/fold@5.0.8) (2019-04-24) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## [5.0.7](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.6...@adonisjs/fold@5.0.7) (2019-03-25) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## [5.0.6](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.5...@adonisjs/fold@5.0.6) (2019-02-26) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## [5.0.5](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.4...@adonisjs/fold@5.0.5) (2019-02-26) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## [5.0.4](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.3...@adonisjs/fold@5.0.4) (2019-02-22) | ||
### Bug Fixes | ||
* **fold:** set generics to any by default ([d3a80a5](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/commit/d3a80a5)) | ||
## [5.0.3](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.2...@adonisjs/fold@5.0.3) (2019-02-10) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## [5.0.2](https://github.com/adonisjs/adonis-framework/tree/master/packages/fold/compare/@adonisjs/fold@5.0.1...@adonisjs/fold@5.0.2) (2019-02-04) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
## 5.0.1 (2019-01-26) | ||
**Note:** Version bump only for package @adonisjs/fold | ||
<a name="4.0.9"></a> | ||
@@ -82,0 +2,0 @@ ## [4.0.9](https://github.com/poppinss/adonis-fold/compare/v4.0.8...v4.0.9) (2018-07-15) |
# The MIT License | ||
Copyright 2019 thetutlage, contributors | ||
Copyright 2019 Harminder virk, contributors | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
{ | ||
"name": "@adonisjs/fold", | ||
"version": "5.0.9", | ||
"version": "5.0.10", | ||
"description": "Dependency manager and ioc container for your next NodeJs application", | ||
"main": "build/index.js", | ||
"files": [ | ||
"build/src", | ||
"build/index.js", | ||
"build/index.d.ts" | ||
], | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"mrm": "mrm --preset=@adonisjs/mrm-preset", | ||
"pretest": "npm run lint", | ||
"test": "nyc node japaFile.js", | ||
"test": "node japaFile.js", | ||
"lint": "tslint --project tsconfig.json", | ||
"clean": "del build", | ||
"doc": "typedoc", | ||
"compile": "npm run lint && npm run clean && tsc", | ||
"build": "npm run compile", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls" | ||
"build": "npm run compile && typedoc --excludePrivate && git add docs", | ||
"commit": "git-cz", | ||
"release": "np", | ||
"version": "npm run build" | ||
}, | ||
"author": "virk", | ||
"author": "adonisjs,virk", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"clear-module": "^3.2.0", | ||
"fs-extra": "^7.0.1" | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/poppinss/adonis-fold.git" | ||
}, | ||
@@ -40,13 +32,26 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/adonisjs/adonis-framework/issues" | ||
"url": "https://github.com/poppinss/adonis-fold/issues" | ||
}, | ||
"homepage": "https://adonisjs.com", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/adonisjs/adonis-framework/tree/master/packages/fold" | ||
"homepage": "https://github.com/poppinss/adonis-fold#readme", | ||
"devDependencies": { | ||
"@adonisjs/mrm-preset": "^2.0.3", | ||
"@types/node": "^12.0.2", | ||
"clear-module": "^3.2.0", | ||
"commitizen": "^3.1.1", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"del-cli": "^1.1.0", | ||
"doctoc": "^1.4.0", | ||
"fs-extra": "^8.0.1", | ||
"husky": "^2.3.0", | ||
"japa": "^2.0.10", | ||
"mrm": "^1.2.2", | ||
"np": "^5.0.2", | ||
"ts-node": "^8.1.0", | ||
"tslint": "^5.16.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typedoc": "^0.14.2", | ||
"typedoc-plugin-external-module-name": "^2.1.0", | ||
"typescript": "^3.4.5" | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"src" | ||
], | ||
"exclude": [ | ||
@@ -59,10 +64,30 @@ "test" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"tag": "next" | ||
"main": "build/index.js", | ||
"files": [ | ||
"build/src", | ||
"build/index.d.ts", | ||
"build/index.js" | ||
], | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md", | ||
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js" | ||
} | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "cz-conventional-changelog" | ||
} | ||
}, | ||
"np": { | ||
"contents": ".", | ||
"anyBranch": false | ||
}, | ||
"dependencies": { | ||
"@adonisjs/utils": "^1.4.4" | ||
"@poppinss/utils": "^1.0.1" | ||
}, | ||
"gitHead": "b2203b111e3b6e533e50b7c234ecf77398675d59" | ||
"publishConfig": { | ||
"access": "public", | ||
"tag": "old-stable" | ||
} | ||
} |
258
README.md
@@ -0,230 +1,130 @@ | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
## Table of contents | ||
- [AdonisJs Fold 🚀](#adonisjs-fold-) | ||
- [Features](#features) | ||
- [Installation](#installation) | ||
- [Basic Usage](#basic-usage) | ||
- [Moving Forward](#moving-forward) | ||
- [Tests](#tests) | ||
- [Release History](#release-history) | ||
- [Meta](#meta) | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
# AdonisJs Fold 🚀 | ||
> Dependency manager and IoC container for Node.js | ||
[![travis-image]][travis-url] | ||
[![appveyor-image]][appveyor-url] | ||
[![coveralls-image]][coveralls-url] | ||
[![npm-image]][npm-url] | ||
![](https://img.shields.io/badge/Uses-Typescript-294E80.svg?style=flat-square&colorA=ddd) | ||
[![NPM Version][npm-image]][npm-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Appveyor][appveyor-image]][appveyor-url] | ||
[![Coveralls][coveralls-image]][coveralls-url] | ||
Adonis fold is zero dependency library to enable Dependency Injection in your apps using an IoC container. | ||
Fold is a dependency manager for Node.js used by AdonisJs framework. Below is the list of features. | ||
The library itself is pretty lean. However, offers handful of tools to make DI easier and simpler. | ||
<img src="http://res.cloudinary.com/adonisjs/image/upload/q_100/v1497112678/adonis-purple_pzkmzt.svg" width="200px" align="right" hspace="30px" vspace="100px"> | ||
- Zero depedencies. | ||
- Support for fakes to ease testing. | ||
- Gazallion times faster than Node.js require. | ||
## Features | ||
> Using IoC container for simple apps can become overkill. Use it when you are creating an eco-system of plugins and wants them to play together gracefully using a global dependency store. | ||
1. Support for binding dependencies with unique namespaces. | ||
2. Autoloading multiple directories under a namespace. | ||
3. Defining aliases for bindings. | ||
4. Automatic resolution of namespaces and transparent dependency injection. | ||
5. Support for `fakes` when writing tests. | ||
6. Support for service providers, to bind dependencies in structured way. | ||
## Getting started | ||
The first step it to install the package from npm as follows. | ||
```sh | ||
npm i @adonisjs/fold | ||
# Using Yarn | ||
yarn add @adonisjs/fold | ||
## Installation | ||
You can install the package from npm. | ||
```bash | ||
npm i --save adonis-fold | ||
``` | ||
Next, is to use package by importing the container. | ||
## Basic Usage | ||
```js | ||
const { Ioc } = require('@adonisjs/fold') | ||
const ioc = new Ioc() | ||
const { ioc } = require('adonis-fold') | ||
ioc.bind('unique/namespace', () => { | ||
return 'value' | ||
class Foo { | ||
} | ||
ioc.bind('App/Foo', function () { | ||
return new Foo() | ||
}) | ||
``` | ||
The `bind` receives 2 arguments. | ||
const foo = ioc.use('App/Foo') | ||
// return Foo class instance | ||
1. The `namespace` is the unique name for the binding. | ||
2. The factory function is executed everytime the dependency is fetched from the container and return value is used. | ||
If you want the factory functions to be executed only once, then make use of the `singleton` instead of `bind`. | ||
```js | ||
ioc.singleton('unique/namespace', () => { | ||
return 'value' | ||
ioc.with('App/Foo', (Foo) => { | ||
// Only if App/Foo exists | ||
}) | ||
``` | ||
## What is Dependency Injection? | ||
Dependency Injection is a simple concept of injecting dependencies during the creation of an object vs being dependent on them globally. | ||
Simple enough! But we do not see the real power of the Ioc container, since we can instantiate the class manually too. Right? NO | ||
In the following example we `require` the database service and mailer to perform user registeration. | ||
Here are the following benefits. | ||
```js | ||
const db = require('./db') | ||
const mailer = require('./mailer') | ||
1. The author of the `Foo` class can decide how to instantiate the class and return a properly configured instance, instead of leaving it to the consumer. | ||
class UserService { | ||
async register (userData) { | ||
await db.create(userData) | ||
await mailer.sendEmail('email-template', userData.email) | ||
} | ||
} | ||
``` | ||
2. While you are making use of the Ioc container, one binding can be dependent upon others, without much work. For example | ||
Now, if you want to test the above `UserService`, there is no simply way to use a different implementation of `mailer` or `db`. | ||
Let's refactor the above code and accept the dependencies as constructor arguments. | ||
```js | ||
class UserService { | ||
constructor (db, mailer) { | ||
this.db = db | ||
this.mailer = mailer | ||
} | ||
async register (userData) { | ||
await this.db.create(userData) | ||
this.mailer.sendEmail('email-template', userData.email) | ||
class Foo { | ||
constructor (config) { | ||
// | ||
} | ||
} | ||
``` | ||
With the refactored code, testing `UserService` is simple, since you can inject alternate implementations of `db` and `mailer` during testing. | ||
## What is IoC container? | ||
If classes makes use of Dependency Injection, there has to be someone to inject these dependencies in the right order. | ||
Imagine, before using the internal modules of a framework like AdonisJs, you have to manually build classes by injecting dependencies to them. | ||
```js | ||
const Mail = require('@adonisjs/mail') | ||
const { Config, View } = require('@adonisjs/framework') | ||
const config = new Config(__dirname) | ||
const view = new View(config) | ||
const mailer = new Mail(config, view) | ||
mailer.send() | ||
``` | ||
The above code will quickly spin out of your hands for many reasons. | ||
1. It will take lots of brain power to decide, where and when to construct these classes. | ||
2. If an internal module like `@adonisjs/mail` changes the order of arguments, then the package has to be released as a breaking change. | ||
3. Finally, as an application developer, why should you even care about building these objects from hand. | ||
## Using IoC container | ||
Now that we understand Dependency Injection and the need of container, let's see how to use the container in an application. | ||
Frameworks like AdonisJs uses this module to build an entire eco-system of modules. All the framework modules are registered to the container and your application pulls them from container only. | ||
This approach has several benefits | ||
1. The user of the framework, won't have to manually construct classes. The creator of the module constructs them and binds them to the container. | ||
2. All dependencies inside the container can use each other, without requiring them in a hard coded way. The concept is generally known as loose coupling. | ||
3. Makes the testing easier, since faking the value of a namespace inside a container is simpler than proxying `require` calls. | ||
```js | ||
const { Ioc } = require('@adonisjs/fold') | ||
const container = new Ioc() | ||
container.bind('Config', function () { | ||
return { | ||
appKey: process.env.APP_KEY, | ||
database: { | ||
// config | ||
}, | ||
redis: { | ||
// config | ||
} | ||
} | ||
ioc.bind('App/Foo', function (app) { | ||
const config = app.use('App/Config') | ||
return new Foo(config) | ||
}) | ||
module.exports = { container } | ||
const foo = ioc.use('App/Foo') | ||
``` | ||
Now anywhere inside your application, you can fetch the config from the container using it's namespace. | ||
This time, we injected `App/Config` behind the scenes and the consumer of the `Foo` class won't have to worry about passing the config manually. | ||
```js | ||
const { container } = require('./app') | ||
container.use('Config') // returns object | ||
``` | ||
## Moving Forward | ||
Checkout the [official documentation](http://adonisjs.com/docs/ioc-container) at the AdonisJs website for more info. | ||
Building an eco-system of plugins on top of this approach enables loose coupling and better development experience. | ||
## Tests | ||
Tests are written using [japa](http://github.com/thetutlage/japa). Run the following commands to run tests. | ||
### Concrete classes with DI | ||
```bash | ||
npm run test:local | ||
```js | ||
class Config { | ||
constructor (configObject) { | ||
} | ||
# report coverage | ||
npm run test | ||
get (key) { | ||
// return value of a key inside the config object | ||
} | ||
} | ||
class Redis { | ||
constructor (config) { | ||
} | ||
} | ||
class Cache { | ||
constructor (config, redis) { | ||
} | ||
} | ||
# on windows | ||
npm run test:win | ||
``` | ||
### Binding them to the container | ||
## Release History | ||
```js | ||
container.bind('App/Config', function () { | ||
return new Config({ | ||
redis: { | ||
// config | ||
}, | ||
cache: { | ||
// config | ||
} | ||
}) | ||
}) | ||
Checkout [CHANGELOG.md](CHANGELOG.md) file for release history. | ||
container.bind('App/Redis', function () { | ||
return new Redis(container.use('App/Config')) | ||
}) | ||
## Meta | ||
container.bind('App/Cache', function () { | ||
return new Cache(container.use('App/Redis'), container.use('App/Config')) | ||
}) | ||
``` | ||
AdonisJs – [@adonisframework](https://twitter.com/adonisframework) – virk@adonisjs.com | ||
Now anywhere inside your application, you can use the `Cache` object without worrying about how to build it. | ||
Checkout [LICENSE.txt](LICENSE.txt) for license information | ||
```js | ||
const cache = container.use('App/Cache') | ||
``` | ||
Harminder Virk (Aman) - [https://github.com/thetutlage](https://github.com/thetutlage) | ||
## Usage with Typescript | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/adonis-fold/master.svg?style=flat-square | ||
## Change log | ||
The change log can be found in the [CHANGELOG.md](CHANGELOG.md) file. | ||
[appveyor-url]: https://ci.appveyor.com/project/thetutlage/adonis-fold | ||
## Contributing | ||
Everyone is welcome to contribute. Please go through the following guides, before getting started. | ||
[npm-image]: https://img.shields.io/npm/v/@adonisjs/fold.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/@adonisjs/fold | ||
1. [Contributing](https://adonisjs.com/contributing) | ||
2. [Code of conduct](https://adonisjs.com/code-of-conduct) | ||
[travis-image]: https://img.shields.io/travis/poppinss/adonis-fold/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/poppinss/adonis-fold | ||
## Authors & License | ||
[Harminder Virk](https://github.com/thetutlage) and [contributors](https://github.com/poppinss/adonis-fold/graphs/contributors). | ||
[coveralls-image]: https://img.shields.io/coveralls/poppinss/adonis-fold/develop.svg?style=flat-square | ||
MIT License, see the included [MIT](LICENSE.md) file. | ||
[travis-image]: https://img.shields.io/travis/poppinss/adonis-fold/master.svg?style=flat-square&logo=travis | ||
[travis-url]: https://travis-ci.org/poppinss/adonis-fold "travis" | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/adonis-fold/master.svg?style=flat-square&logo=appveyor | ||
[appveyor-url]: https://ci.appveyor.com/project/thetutlage/adonis-fold "appveyor" | ||
[coveralls-image]: https://img.shields.io/coveralls/poppinss/adonis-fold/master.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/github/poppinss/adonis-fold "coveralls" | ||
[npm-image]: https://img.shields.io/npm/v/@adonisjs/fold.svg?style=flat-square&logo=npm | ||
[npm-url]: https://npmjs.org/package/@adonisjs/fold "npm" | ||
[coveralls-url]: https://coveralls.io/github/poppinss/adonis-fold |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
504
32749
18
131
+ Added@poppinss/utils@^1.0.1
+ Added@poppinss/utils@1.0.9(transitive)
- Removed@adonisjs/utils@^1.4.4
- Removed@adonisjs/utils@1.4.4(transitive)