Comparing version 1.0.2 to 2.0.0
@@ -1,4 +0,4 @@ | ||
import diContainer from './lib/getters-container'; | ||
import diContainer from './lib/index'; | ||
export * from './lib/getters-container'; | ||
export * from './lib/index'; | ||
export default diContainer; |
@@ -1,1 +0,1 @@ | ||
module.exports = require('./lib/getters-container'); | ||
module.exports = require('./lib/index'); |
{ | ||
"name": "true-di", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Simple Dependency Injection solution for TypeScript and Javascript", | ||
"main": "index.js", | ||
"main": "./lib/index.js", | ||
"module": "./esm/index.js", | ||
"sideEffects": false, | ||
"scripts": { | ||
"test": "jest --coverage && ./.coveralls.sh", | ||
"build:es": "rm -rf ./es; tsc -p ./tsconfig.esm.json", | ||
"build:cjs": "rm -rf ./lib; tsc -p ./tsconfig.cjs.json", | ||
"build": "npm run build:es && npm run build:cjs", | ||
"prepublish": "npm run build" | ||
"test": "jest", | ||
"coveralls": "./.coveralls.sh", | ||
"lint": "eslint ./src/**/*.ts", | ||
"clear": "rm -rf ./lib; rm -rf ./esm", | ||
"compile": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json", | ||
"build": "npm run clear; npm run compile" | ||
}, | ||
@@ -46,3 +49,70 @@ "repository": { | ||
"typescript": "^3.7.5" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"airbnb/base" | ||
], | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"jest" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"rules": { | ||
"max-len": [ | ||
"error", | ||
{ | ||
"code": 100, | ||
"ignoreStrings": true | ||
} | ||
], | ||
"implicit-arrow-linebreak": "off", | ||
"import/no-unresolved": 0, | ||
"import/prefer-default-export": 0, | ||
"indent": [ | ||
2, | ||
2, | ||
{ | ||
"flatTernaryExpressions": true | ||
} | ||
], | ||
"no-unused-vars": "off", | ||
"no-undef": "error", | ||
"no-tabs": "error", | ||
"no-nested-ternary": 0, | ||
"import/extensions": 0, | ||
"arrow-parens": [ | ||
"error", | ||
"as-needed" | ||
], | ||
"operator-linebreak": 0, | ||
"no-underscore-dangle": 0, | ||
"@typescript-eslint/no-unused-vars": [ | ||
"error" | ||
], | ||
"jest/no-disabled-tests": "warn", | ||
"jest/no-focused-tests": "error", | ||
"jest/no-identical-title": "error", | ||
"jest/prefer-to-have-length": "warn", | ||
"jest/valid-expect": "error" | ||
}, | ||
"env": { | ||
"jest/globals": true | ||
}, | ||
"ignorePatterns": [ | ||
"examples/**/*", | ||
"lib/**/*", | ||
"esm/**/*" | ||
] | ||
}, | ||
"jest": { | ||
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"collectCoverage": true, | ||
"collectCoverageFrom": [ | ||
"src/**/*.ts" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"/lib/', '/es/" | ||
] | ||
} | ||
} |
# true-di | ||
`true-di` is a Simple Dependency Injection Container for TypeScript and JavaScript | ||
[![Build Status](https://travis-ci.org/DScheglov/true-di.svg?branch=master)](https://travis-ci.org/DScheglov/true-di) [![Coverage Status](https://coveralls.io/repos/github/DScheglov/true-di/badge.svg?branch=master)](https://coveralls.io/github/DScheglov/true-di?branch=master) [![npm version](https://img.shields.io/npm/v/true-di.svg?style=flat-square)](https://www.npmjs.com/package/true-di) [![npm downloads](https://img.shields.io/npm/dm/true-di.svg?style=flat-square)](https://www.npmjs.com/package/true-di) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/DScheglov/true-di/blob/master/LICENSE) | ||
Simple Dependency Injection Container for TypeScript and Javascript | ||
## Installation | ||
```bash | ||
npm i --save true-di | ||
npm i --S true-di | ||
``` | ||
## Usage | ||
```bash | ||
yarn add true-di | ||
``` | ||
```ts | ||
## Documentation | ||
[Read Documentation on Git Book](https://dscheglov.gitbook.io/true-di/) | ||
## Usage Example: | ||
**./src/container.ts** | ||
```typescript | ||
import diContainer from 'true-di'; | ||
import { ILogger, IDataSourceService, IECommerceService } from './interface'; | ||
import { IContainer } from './interfaces'; | ||
import Logger from './Logger'; | ||
@@ -21,15 +32,73 @@ import DataSourceService from './DataSourceService'; | ||
export interface IContainer { | ||
logger: ILogger, | ||
dataSourceService: IDataSource, | ||
ecommerceService: ICommerceService, | ||
} | ||
const container = diContainer<IContainer>({ | ||
logger: () => new Logger(), | ||
dataService: ({ logger }) => new DataSourceService(logger), | ||
ecommerceService: ({ logger, dataSource }) => new ECommerceService(logger, dataSource), | ||
logger: () => | ||
new Logger(), | ||
dataSourceService: ({ logger }) => | ||
new DataSourceService(logger), | ||
ecommerceService: ({ logger, dataSourceService }) => | ||
new ECommerceService(logger, dataSourceService), | ||
}); | ||
export default container; | ||
``` | ||
``` | ||
**./src/controller.ts** | ||
```typescript | ||
import Express from 'express'; | ||
export const getOrders = async (req: Express.Request, res: Express.Response) => { | ||
const { ecommerceService } = req.container; | ||
res.json( | ||
await ecommerceService.getOrders() | ||
); | ||
} | ||
``` | ||
**./src/index.ts** | ||
```typescript | ||
import express from 'express'; | ||
import container from './container'; | ||
import { getOrders } from './controller'; | ||
const app = express(); | ||
app.use((req, res, next) => { | ||
req.container = container; | ||
next(); | ||
}); | ||
app.get('/orders', getOrders); | ||
app.listen(8080); | ||
``` | ||
### [Live Demo on Sandbox](https://codesandbox.io/s/express-di-example-w2gme?expanddevtools=1&fontsize=14&hidenavigation=1&initialpath=%2Forders&module=%2Fsrc%2Fcontainer.ts&moduleview=1&theme=dark) | ||
![Live Demo Preview](./docs/assets/image.png) | ||
## Why `true-di`? | ||
1. It's light and idiomatic. | ||
1. It works with **JavaScript** and **TypeScript**. | ||
1. It's well-typed. Items types are always known whenever your code assess them. | ||
1. It is Isomorphic: works on Back end and on Front end. | ||
1. It works with Classes and with Closures. | ||
1. It doesn't require decorators (annotations). | ||
1. It doesn't depend on `reflect-metadata`. | ||
1. It allows referring items without strings or other artificial elements. | ||
1. It uses `getters` under the hood. | ||
1. It's lazy: container doesn't create an item until your code requests | ||
1. It automatically resolves dependencies | ||
1. It immediately falls down on constructor-injection of cyclic dependency | ||
1. It supports property-, setter- cyclic dependencies | ||
1. It doesn't know anything about context but it could be easily placed into the context. | ||
1. Its containers are composable. | ||
1. It's SOLID-compatible. | ||
1. It's more SOLID-compatible then other IOC solutions. Your code doesn't need to depend on the container (nor even on its interface). | ||
1. It's testable. | ||
1. It supports symbolic names. | ||
1. It doesn't have any other dependencies (and it will be kept) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
71
104
50344
1064
1