ioc-service-container
Advanced tools
Comparing version 0.1.3 to 1.0.0
@@ -8,2 +8,8 @@ # Changelog | ||
## [1.0.0] - 2020-02-15 | ||
### Added | ||
* `@injectViaId` decorator | ||
## [0.1.3] - 2020-02-02 | ||
@@ -10,0 +16,0 @@ |
@@ -1,1 +0,2 @@ | ||
export declare const inject: (target: Object, propertyKey: string) => void; | ||
export declare function inject(target: Object, propertyKey: string): void; | ||
export declare function injectViaId(serviceId: string): (target: Object, propertyKey: string) => void; |
import ServiceContainer from './ServiceContainer'; | ||
export const inject = (target, propertyKey) => { | ||
// todo further improvements set key as string not via name | ||
// https://dev.to/danywalls/using-property-decorators-in-typescript-with-a-real-example-44e | ||
export function inject(target, propertyKey) { | ||
redefineObject(target, propertyKey); | ||
} | ||
export function injectViaId(serviceId) { | ||
return function (target, propertyKey) { | ||
redefineObject(target, propertyKey, serviceId); | ||
}; | ||
} | ||
function redefineObject(target, propertyKey, serviceId) { | ||
const getter = () => { | ||
return ServiceContainer.get(propertyKey.toLowerCase()); | ||
return ServiceContainer.get((serviceId === null || serviceId === void 0 ? void 0 : serviceId.toLowerCase()) || propertyKey.toLowerCase()); | ||
}; | ||
@@ -15,2 +21,2 @@ const setter = () => { | ||
}); | ||
}; | ||
} |
{ | ||
"name": "ioc-service-container", | ||
"version": "0.1.3", | ||
"version": "1.0.0", | ||
"description": "Lightweight ioc service container", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -8,2 +8,6 @@ # ioc-service-container | ||
<a href="https://www.buymeacoffee.com/Mrcwbr" target="_blank"> | ||
<img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" > | ||
</a> | ||
## Get started | ||
@@ -14,2 +18,50 @@ | ||
## Usage | ||
First set up an Enum for preventing typos or redefinition of service ids: | ||
```typescript | ||
export enum ServiceId { | ||
TestApi = 'TestApi', | ||
TestService = 'TestService', | ||
FooApi = 'FooApi', | ||
} | ||
``` | ||
According to this you have to pass a factory of your required services to the ioc container. So at the initial script of | ||
your application you call a function named e.g. `setupServiced`: | ||
```typescript | ||
function setupServiced() { | ||
ServiceContainer.set(ServiceId.TestApi, () => new CustomTestApi()); | ||
ServiceContainer.set(ServiceId.FooApi, () => new CustomFooApi()); | ||
ServiceContainer.set(ServiceId.TestService, () => new CustomTestService()); | ||
} | ||
``` | ||
Now you have two options to inject the requested service. The first one is without the usage of TypeScript annotations. | ||
This can be used anywhere in your code: | ||
```typescript | ||
import { ServiceId } from './ServiceId'; | ||
import { ServiceContainer } from 'ioc-service-container'; | ||
const testService = ServiceContainer.get<TestService>(ServiceId.TestApi); | ||
const testApi = ServiceContainer.get<TestService>(ServiceId.TestService); | ||
``` | ||
The second option is to use the `@inject` decorator inside a class: | ||
```typescript | ||
export class CustomTestService implements TestService { | ||
@inject | ||
private readonly testApi!: TestApi; // Important is the naming of the property, its mapped to the sericeId | ||
@injectViaId(ServiceId.FooApi) | ||
private readonly nameThisHowYouWant!: FooApi // If you don't want to name your property like the service id, use this decorator | ||
} | ||
``` | ||
Your can see a demo in the `./example` folder. To run this type in `npm run example`. | ||
## Background | ||
@@ -16,0 +68,0 @@ |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7597
67
1
112