-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from lad-tech/43-DI-container
feat: implement DI container and inject decorator
- Loading branch information
Showing
27 changed files
with
599 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { inject } from '../../../src'; | ||
import { TYPES } from '../inversion.types'; | ||
import { StoragePort } from '../domain/ports'; | ||
|
||
export class Configurator { | ||
@inject(TYPES.Storage) private storage: StoragePort; | ||
|
||
public async setUserId(userId: string) { | ||
this.storage[userId] = true; | ||
} | ||
|
||
public async userIdExist(userId: string) { | ||
return !!this.storage[userId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { inject } from '../../../src'; | ||
import { TYPES } from '../inversion.types'; | ||
import { ConfiguratorPort } from '../domain/ports'; | ||
|
||
export class Repository { | ||
@inject(TYPES.Configurator) private configurator: ConfiguratorPort; | ||
|
||
public async getUserById(userId: string) { | ||
const exist = await this.configurator.userIdExist(userId); | ||
|
||
if (exist) { | ||
return { firstName: 'Jon', lastName: 'Dow' }; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Configurator'; | ||
export * from './Repository'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ConfiguratorPort { | ||
setUserId(userId: string): Promise<void>; | ||
userIdExist(userId: string): Promise<boolean>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Readable } from 'stream'; | ||
|
||
export interface MathPort { | ||
sum(params: { a: number; b: number }): Promise<{ result: number }>; | ||
sumStream(params: Readable): Promise<{ result: number }>; | ||
fibonacci(params: { length: number }): Promise<Readable>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface RepositoryPort { | ||
getUserById(userId: string): Promise<{ firstName: string; lastName: string } | null>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type StoragePort = Record<string, boolean>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './Configurator'; | ||
export * from './Math'; | ||
export * from './Repository'; | ||
export * from './Storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const TYPES = { | ||
Math: Symbol.for('Math'), | ||
Repository: Symbol('Repository'), | ||
Configurator: Symbol('Configurator'), | ||
Storage: Symbol('Storage'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { GetUserRequest, GetUserResponse } from '../interfaces'; | ||
import { inject } from '../../../src/injector'; | ||
import { methods } from '../service.schema.json'; | ||
import { TYPES } from '../inversion.types'; | ||
import { RepositoryPort } from '../domain/ports'; | ||
|
||
import { BaseMethod } from '../../../src/Method'; | ||
|
||
export class GetUser extends BaseMethod { | ||
static settings = methods.GetUser; | ||
|
||
@inject(TYPES.Repository) private repository: RepositoryPort; | ||
|
||
public async handler({ userId }: GetUserRequest): Promise<GetUserResponse> { | ||
const result = await this.repository.getUserById(userId); | ||
if (!result) { | ||
throw new Error(`User ${userId} not found!`); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { GetUserRequest, GetUserResponse } from '../interfaces'; | ||
import { inject } from '../../../src/injector'; | ||
import { methods } from '../service.schema.json'; | ||
import { TYPES } from '../inversion.types'; | ||
import { RepositoryPort } from '../domain/ports'; | ||
|
||
import { BaseMethod } from '../../../src/Method'; | ||
|
||
export class GetUserV2 extends BaseMethod { | ||
static settings = methods.GetUserV2; | ||
|
||
constructor(@inject(TYPES.Repository) private repository: RepositoryPort) { | ||
super(); | ||
} | ||
|
||
public async handler({ userId }: GetUserRequest): Promise<GetUserResponse> { | ||
const result = await this.repository.getUserById(userId); | ||
if (!result) { | ||
throw new Error(`User ${userId} not found!`); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.