Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/roitinnovation/villar
Villar powerful in helping to create factories for NodeJs
https://github.com/roitinnovation/villar
Last synced: about 2 months ago
JSON representation
Villar powerful in helping to create factories for NodeJs
- Host: GitHub
- URL: https://github.com/roitinnovation/villar
- Owner: roitinnovation
- License: mit
- Created: 2020-09-20T04:19:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-26T12:21:21.000Z (over 2 years ago)
- Last Synced: 2024-10-13T19:17:14.128Z (3 months ago)
- Language: TypeScript
- Size: 318 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Villar factory
Facilitates factory creation and infrastructure## Usage
-----------
### Classes and decorators1. Implementation
1. **key** defines which key the impl represents
2. **implName** if the instance is initialized with the original class name
3. **ref** distinguishes if there are two identical keys for different implementations2. VillarImplDiscovery
1. **findImpl(string)** searches for the implementation according to the key provided3. NestVillarImplDiscovery
1. **findImpl(string)** has the same function as VillarImplDiscovery but does search for impl in the context of nest injection### Usage example
#### Configurate the impls
```javascript
// The interface
export interface Calculator {
calc(num1: number, num2: number): number
}import { Implementation } from "villar"
// Addition impl
@Implementation({
key: '+'
})
// At NestJs use @Injectable()
export class AdditionCalculator implements Calculator {calc(num1: number, num2: number): number {
return num1 + num2
}}
// Subtraction
@Implementation({
key: '-'
})
// At NestJs use @Injectable()
export class SubtractionCalculator implements Calculator {calc(num1: number, num2: number): number {
return num1 - num2
}
}// Division
// includes attr is like in key
@Implementation({
key: 'any_division_inclues(/)',
includes: true
})
export class DivisionCalculator implements Calculator {calc(num1: number, num2: number): number {
return num1 / num2
}}
// Multiplication
// truthCustom is function from custom truth impl selector
// function (key: string, metadata: any)
@Implementation({
key: 'multiplication*',
truthCustom: (key: string, metadata: any): boolean => {
return key.includes('*') && metadata?.value == 10
}
})
export class MultiplicationCalculator implements Calculator {calc(num1: number, num2: number): number {
return num1 * num2
}}
```
#### Discovery impls
```javascript
import { VillarImplResolver } from "villar"
// Register impls
VillarImplResolver.register(SubtractionCalculator, AdditionCalculator)// Addition Impl
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl('+')
console.log(calculator?.calc(10, 1)) //result: 11// Subtraction Impl result
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl('-')
console.log(calculator?.calc(10, 1)) //result: 9// Anu Impl rsult
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl('any_key')
console.log(calculator?.calc(10, 1)) //result: undefined// Division
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl('*')
console.log(calculator?.calc(10, 2)) //result: 5// Multiplication
const options: any = {
metadata: {
value: 10
}
}const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl('*', options)
console.log(calculator?.calc(10, 2)) //result: 20
```#### NestJs Discovery impls
```javascript
// Configure AppModule
@Module({
imports: [],
controllers: [AppController],
providers: [
AdditionCalculator, // Custom impl
SubtractionCalculator // Custom impl,
NestVillarImplDiscovery, // Helper Villar, import with import { NestVillarImplDiscovery } from "villar",
anysProviders....
],
})
export class AppModule {}// Example in controller
@Controller()
export class AppController {
constructor(
private readonly implDiscovery: NestVillarImplDiscovery
) {}@Get("execute-calc/:operator/:value/:value2")
executeCalc(@Param("operator") operator: string, @Param("value") value: number, @Param("value2") value2: number) {
const calculator = this.implDiscovery.findImpl(operator)
return calculator?.calc(value, value2)
}
}```