-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from Karibash/feature/inject-package
Add inject package
- Loading branch information
Showing
42 changed files
with
2,552 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
# nestjs-modular | ||
# nestjs-modular | ||
|
||
[![codecov][coverage-badge]][coverage-link] | ||
[![license][license-badge]][license-link] | ||
[![Github][github-follower-badge]][github-follower-link] | ||
[![Twitter][twitter-follower-badge]][twitter-follower-link] | ||
|
||
| package | Version | Description | Changelog | | ||
|---------------------------------------------|---------------------------------------------------------------------|----------------------------------------------------------------------------------------|-------------------------------------------| | ||
| [`@nestjs-modular/inject`](packages/inject) | [![npm version][inject-npm-version-badge]][inject-npm-version-link] | This module provides the ability to inject features in bulk by specifying a directory. | [CHANGELOG](packages/inject/CHANGELOG.md) | | ||
|
||
## 🤝 Contributing | ||
|
||
Contributions, issues and feature requests are welcome. | ||
|
||
Feel free to check [issues page](https://github.com/Karibash/nestjs-modular/issues) if you want to contribute. | ||
|
||
## 📝 License | ||
|
||
Copyright © 2020 [@Karibash](https://twitter.com/karibash). | ||
|
||
This project is [```MIT```](https://github.com/Karibash/nestjs-modular/blob/main/LICENSE) licensed. | ||
|
||
[coverage-badge]: https://codecov.io/gh/Karibash/nestjs-modular/branch/main/graph/badge.svg | ||
[coverage-link]: https://codecov.io/gh/Karibash/nestjs-modular | ||
[license-badge]: https://img.shields.io/badge/License-MIT-green.svg | ||
[license-link]: https://github.com/Karibash/nestjs-modular/blob/main/LICENSE | ||
[github-follower-badge]: https://img.shields.io/github/followers/Karibash?label=Follow&logo=github&style=social | ||
[github-follower-link]: https://github.com/Karibash?tab=followers | ||
[twitter-follower-badge]: https://img.shields.io/twitter/follow/Karibash?label=Follow&style=social | ||
[twitter-follower-link]: https://twitter.com/intent/follow?screen_name=Karibash | ||
[inject-npm-version-badge]: https://badge.fury.io/js/@nestjs-modular%2Finject.svg | ||
[inject-npm-version-link]: https://www.npmjs.com/package/@nestjs-modular/inject |
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
Empty file.
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,35 @@ | ||
# compiled output | ||
/dist | ||
/node_modules | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
pnpm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# OS | ||
.DS_Store | ||
|
||
# Tests | ||
/coverage | ||
/.nyc_output | ||
|
||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json |
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,12 @@ | ||
# nest-js-modular-inject | ||
|
||
This is a example program describing the basic usage of @nestjs-modular/inject package. | ||
|
||
## Usage | ||
|
||
The example program can be run by executing the following command. | ||
|
||
```sh | ||
npm install | ||
npm run start | ||
``` |
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,5 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/nest-cli", | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src" | ||
} |
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,37 @@ | ||
{ | ||
"name": "nest-js-modular-inject-example", | ||
"version": "0.0.0", | ||
"description": "", | ||
"author": "", | ||
"private": true, | ||
"license": "MIT", | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "nest build", | ||
"start": "nest start", | ||
"start:mock": "APP_ENV=mock nest start", | ||
"start:dev": "nest start --watch", | ||
"start:debug": "nest start --debug --watch", | ||
"start:prod": "node dist/main" | ||
}, | ||
"dependencies": { | ||
"@nestjs/common": "^8.0.0", | ||
"@nestjs/core": "^8.0.0", | ||
"@nestjs/platform-express": "^8.0.0", | ||
"@nestjs-modular/inject": "^1.0.0", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^3.0.2", | ||
"rxjs": "^7.2.0" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/cli": "^8.0.0", | ||
"@nestjs/schematics": "^8.0.0", | ||
"@types/express": "^4.17.13", | ||
"@types/node": "^16.0.0", | ||
"source-map-support": "^0.5.20", | ||
"ts-loader": "^9.2.3", | ||
"ts-node": "^10.0.0", | ||
"tsconfig-paths": "4.0.0", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
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,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { InjectModule } from '@nestjs-modular/inject'; | ||
import path from 'path'; | ||
|
||
@Module({ | ||
imports: [ | ||
InjectModule.forRootAsync({ | ||
imports: { | ||
path: path.resolve(__dirname, './modules'), | ||
includeFileNames: [/\.module$/], | ||
includeExportNames: [/Module$/], | ||
}, | ||
}), | ||
], | ||
}) | ||
export class AppModule {} |
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,11 @@ | ||
export type AnyFunction = (...args: any[]) => any; | ||
|
||
export type KeysOfType<T, S> = { | ||
[key in keyof T]: S extends T[key] ? key : never; | ||
}[keyof T]; | ||
|
||
export type UndefinedToOptional<T> = | ||
Omit<T, KeysOfType<T, undefined>> & | ||
Partial<Pick<T, KeysOfType<T, undefined>>>; | ||
|
||
export type Fields<T> = UndefinedToOptional<Omit<T, KeysOfType<T, AnyFunction>>>; |
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,10 @@ | ||
import { Fields } from '../../core/types'; | ||
|
||
export class Task { | ||
public readonly id: string; | ||
public readonly title: string; | ||
|
||
constructor(props: Fields<Task>) { | ||
Object.assign(this, props); | ||
} | ||
} |
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,10 @@ | ||
import { Fields } from '../../core/types'; | ||
|
||
export class User { | ||
public readonly id: string; | ||
public readonly name: string; | ||
|
||
constructor(props: Fields<User>) { | ||
Object.assign(this, props); | ||
} | ||
} |
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,8 @@ | ||
import { Task } from '../entities/task.entity'; | ||
|
||
export abstract class TaskRepository { | ||
public abstract save(task: Task): Promise<Task>; | ||
public abstract delete(task: Task): Promise<void>; | ||
public abstract findAll(): Promise<Task[]>; | ||
public abstract findById(id: Task['id']): Promise<Task>; | ||
} |
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,8 @@ | ||
import { User } from '../entities/user.entity'; | ||
|
||
export abstract class UserRepository { | ||
public abstract save(user: User): Promise<User>; | ||
public abstract delete(user: User): Promise<void>; | ||
public abstract findAll(): Promise<User[]>; | ||
public abstract findById(id: User['id']): Promise<User>; | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/inject/src/infrastructures/repositories/inmemory/task.repository.ts
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,28 @@ | ||
import { Task } from '../../../domains/entities/task.entity'; | ||
import { TaskRepository } from '../../../domains/repositories/task.repository'; | ||
|
||
export class InMemoryTaskRepository extends TaskRepository { | ||
private readonly store: Record<string, Task> = {}; | ||
|
||
public async save(task: Task): Promise<Task> { | ||
this.store[task.id] = task; | ||
return task; | ||
} | ||
|
||
public async delete(task: Task): Promise<void> { | ||
delete this.store[task.id]; | ||
} | ||
|
||
public async findAll(): Promise<Task[]> { | ||
return Object.values(this.store); | ||
} | ||
|
||
public async findById(id: Task['id']): Promise<Task> { | ||
return this.store[id]; | ||
} | ||
} | ||
|
||
export const taskRepositoryProvider = { | ||
provide: TaskRepository, | ||
useClass: InMemoryTaskRepository, | ||
}; |
28 changes: 28 additions & 0 deletions
28
examples/inject/src/infrastructures/repositories/inmemory/user.repository.ts
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,28 @@ | ||
import { User } from '../../../domains/entities/user.entity'; | ||
import { UserRepository } from '../../../domains/repositories/user.repository'; | ||
|
||
export class InMemoryUserRepository extends UserRepository { | ||
private readonly store: Record<string, User> = {}; | ||
|
||
public async save(user: User): Promise<User> { | ||
this.store[user.id] = user; | ||
return user; | ||
} | ||
|
||
public async delete(user: User): Promise<void> { | ||
delete this.store[user.id]; | ||
} | ||
|
||
public async findAll(): Promise<User[]> { | ||
return Object.values(this.store); | ||
} | ||
|
||
public async findById(id: User['id']): Promise<User> { | ||
return this.store[id]; | ||
} | ||
} | ||
|
||
export const userRepositoryProvider = { | ||
provide: UserRepository, | ||
useClass: InMemoryUserRepository, | ||
}; |
27 changes: 27 additions & 0 deletions
27
examples/inject/src/infrastructures/repositories/mock/task.repository.ts
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,27 @@ | ||
import { Task } from '../../../domains/entities/task.entity'; | ||
import { TaskRepository } from '../../../domains/repositories/task.repository'; | ||
|
||
export class MockTaskRepository extends TaskRepository { | ||
public async save(task: Task): Promise<Task> { | ||
return task; | ||
} | ||
|
||
public async delete(task: Task): Promise<void> { | ||
} | ||
|
||
public async findAll(): Promise<Task[]> { | ||
return [ | ||
new Task({ id: 'task-1', title: 'task-1' }), | ||
new Task({ id: 'task-2', title: 'task-2' }), | ||
]; | ||
} | ||
|
||
public async findById(id: Task['id']): Promise<Task> { | ||
return new Task({ id: id, title: id }); | ||
} | ||
} | ||
|
||
export const taskRepositoryProvider = { | ||
provide: TaskRepository, | ||
useClass: MockTaskRepository, | ||
}; |
27 changes: 27 additions & 0 deletions
27
examples/inject/src/infrastructures/repositories/mock/user.repository.ts
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,27 @@ | ||
import { User } from '../../../domains/entities/user.entity'; | ||
import { UserRepository } from '../../../domains/repositories/user.repository'; | ||
|
||
export class MockUserRepository extends UserRepository { | ||
public async save(user: User): Promise<User> { | ||
return user; | ||
} | ||
|
||
public async delete(user: User): Promise<void> { | ||
} | ||
|
||
public async findAll(): Promise<User[]> { | ||
return [ | ||
new User({ id: 'user-1', name: 'user-1' }), | ||
new User({ id: 'user-2', name: 'user-2' }), | ||
]; | ||
} | ||
|
||
public async findById(id: User['id']): Promise<User> { | ||
return new User({ id: id, name: id }); | ||
} | ||
} | ||
|
||
export const userRepositoryProvider = { | ||
provide: UserRepository, | ||
useClass: MockUserRepository, | ||
}; |
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,10 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
|
||
import { AppModule } from './app.module'; | ||
|
||
const bootstrap = async () => { | ||
const app = await NestFactory.create(AppModule); | ||
await app.listen(3005); | ||
}; | ||
|
||
bootstrap().then(); |
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,26 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { InjectModule } from '@nestjs-modular/inject'; | ||
import path from 'path'; | ||
|
||
const isMock = process.env.APP_ENV === 'mock'; | ||
const repositoryType = isMock ? 'mock' : 'inmemory'; | ||
|
||
@Global() | ||
@Module({ | ||
imports: [ | ||
InjectModule.forRootAsync({ | ||
global: true, | ||
providers: { | ||
path: path.resolve(__dirname, `../../infrastructures/repositories/${repositoryType}`), | ||
includeFileNames: [/\.repository$/], | ||
includeExportNames: [/RepositoryProvider$/], | ||
}, | ||
exports: { | ||
path: path.resolve(__dirname, `../../infrastructures/repositories/${repositoryType}`), | ||
includeFileNames: [/\.repository$/], | ||
includeExportNames: [/RepositoryProvider$/], | ||
}, | ||
}), | ||
], | ||
}) | ||
export class CoreModule {} |
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,37 @@ | ||
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common'; | ||
|
||
import { CreateTaskUseCase } from './usecases/create-task.usecase'; | ||
import { DeleteTaskUseCase } from './usecases/delete-task.usecase'; | ||
import { GetTaskUseCase } from './usecases/get-task.usecase'; | ||
import { GetTasksUseCase } from './usecases/get-tasks.usecase'; | ||
import { Task } from '../../domains/entities/task.entity'; | ||
|
||
@Controller('tasks') | ||
export class TasksController { | ||
constructor( | ||
private readonly createTaskUseCase: CreateTaskUseCase, | ||
private readonly deleteTaskUseCase: DeleteTaskUseCase, | ||
private readonly getTaskUseCase: GetTaskUseCase, | ||
private readonly getTasksUseCase: GetTasksUseCase, | ||
) {} | ||
|
||
@Get() | ||
public async getTasks(): Promise<Task[]> { | ||
return await this.getTasksUseCase.invoke(); | ||
} | ||
|
||
@Post() | ||
public async createTask(@Body('title') title: string): Promise<Task> { | ||
return await this.createTaskUseCase.invoke(title); | ||
} | ||
|
||
@Get(':id') | ||
public async getTask(@Param('id') id: string): Promise<Task> { | ||
return await this.getTaskUseCase.invoke(id); | ||
} | ||
|
||
@Delete(':id') | ||
public async deleteTask(@Param('id') id: string): Promise<void> { | ||
return await this.deleteTaskUseCase.invoke(id); | ||
} | ||
} |
Oops, something went wrong.