Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/openapi/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export interface SwaggerDocumentOptions {
}
```

For example, if you want to make sure that the library generates operation names like `createUser` instead of `UserController_createUser`, you can set the following:
For example, if you want to make sure that the library generates operation names like `createUser` instead of `UsersController_createUser`, you can set the following:

```TypeScript
const options: SwaggerDocumentOptions = {
Expand Down
68 changes: 34 additions & 34 deletions content/recipes/async-local-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export class AlsModule {}
```ts
@@filename(app.module)
@Module({
imports: [AlsModule]
providers: [CatService],
controllers: [CatController],
imports: [AlsModule],
providers: [CatsService],
controllers: [CatsController],
})
export class AppModule implements NestModule {
constructor(
Expand All @@ -64,9 +64,9 @@ export class AppModule implements NestModule {
}
@@switch
@Module({
imports: [AlsModule]
providers: [CatService],
controllers: [CatController],
imports: [AlsModule],
providers: [CatsService],
controllers: [CatsController],
})
@Dependencies(AsyncLocalStorage)
export class AppModule {
Expand Down Expand Up @@ -96,37 +96,37 @@ export class AppModule {
3. Now, anywhere within the lifecycle of a request, we can access the local store instance.

```ts
@@filename(cat.service)
@@filename(cats.service)
@Injectable()
export class CatService {
export class CatsService {
constructor(
// We can inject the provided ALS instance.
private readonly als: AsyncLocalStorage,
private readonly catRepository: CatRepository,
private readonly catsRepository: CatsRepository,
) {}

getCatForUser() {
// The "getStore" method will always return the
// store instance associated with the given request.
const userId = this.als.getStore()["userId"] as number;
return this.catRepository.getForUser(userId);
return this.catsRepository.getForUser(userId);
}
}
@@switch
@Injectable()
@Dependencies(AsyncLocalStorage, CatRepository)
export class CatService {
constructor(als, catRepository) {
@Dependencies(AsyncLocalStorage, CatsRepository)
export class CatsService {
constructor(als, catsRepository) {
// We can inject the provided ALS instance.
this.als = als
this.catRepository = catRepository
this.catsRepository = catsRepository
}

getCatForUser() {
// The "getStore" method will always return the
// store instance associated with the given request.
const userId = this.als.getStore()["userId"] as number;
return this.catRepository.getForUser(userId);
return this.catsRepository.getForUser(userId);
}
}
```
Expand Down Expand Up @@ -175,44 +175,44 @@ A similar functionality as described [above](recipes/async-local-storage#custom-
},
}),
],
providers: [CatService],
controllers: [CatController],
providers: [CatsService],
controllers: [CatsController],
})
export class AppModule {}
```

2. And then can use the `ClsService` to access the store values.

```ts
@@filename(cat.service)
@@filename(cats.service)
@Injectable()
export class CatService {
export class CatsService {
constructor(
// We can inject the provided ClsService instance,
private readonly cls: ClsService,
private readonly catRepository: CatRepository,
private readonly catsRepository: CatsRepository,
) {}

getCatForUser() {
// and use the "get" method to retrieve any stored value.
const userId = this.cls.get('userId');
return this.catRepository.getForUser(userId);
return this.catsRepository.getForUser(userId);
}
}
@@switch
@Injectable()
@Dependencies(AsyncLocalStorage, CatRepository)
export class CatService {
constructor(cls, catRepository) {
@Dependencies(AsyncLocalStorage, CatsRepository)
export class CatsService {
constructor(cls, catsRepository) {
// We can inject the provided ClsService instance,
this.cls = cls
this.catRepository = catRepository
this.catsRepository = catsRepository
}

getCatForUser() {
// and use the "get" method to retrieve any stored value.
const userId = this.cls.get('userId');
return this.catRepository.getForUser(userId);
return this.catsRepository.getForUser(userId);
}
}
```
Expand All @@ -233,19 +233,19 @@ Since the `ClsService` is just another injectable provider, it can be entirely m
However, in certain integration tests, we might still want to use the real `ClsService` implementation. In that case, we will need to wrap the context-aware piece of code with a call to `ClsService#run` or `ClsService#runWith`.

```ts
describe('CatService', () => {
let service: CatService
describe('CatsService', () => {
let service: CatsService
let cls: ClsService
const mockCatRepository = createMock<CatRepository>()
const mockCatsRepository = createMock<CatsRepository>()

beforeEach(async () => {
const module = await Test.createTestingModule({
// Set up most of the testing module as we normally would.
providers: [
CatService,
CatsService,
{
provide: CatRepository
useValue: mockCatRepository
provide: CatsRepository
useValue: mockCatsRepository
}
],
imports: [
Expand All @@ -255,7 +255,7 @@ describe('CatService', () => {
],
}).compile()

service = module.get(CatService)
service = module.get(CatsService)

// Also retrieve the ClsService for later use.
cls = module.get(ClsService)
Expand All @@ -264,7 +264,7 @@ describe('CatService', () => {
describe('getCatForUser', () => {
it('retrieves cat based on user id', async () => {
const expectedUserId = 42
mockCatRepository.getForUser.mockImplementationOnce(
mocksCatsRepository.getForUser.mockImplementationOnce(
(id) => ({ userId: id })
)

Expand Down
14 changes: 7 additions & 7 deletions content/recipes/cqrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Let's create a handler for the `KillDragonCommand` command.
@@filename(kill-dragon.handler)
@CommandHandler(KillDragonCommand)
export class KillDragonHandler implements ICommandHandler<KillDragonCommand> {
constructor(private repository: HeroRepository) {}
constructor(private repository: HeroesRepository) {}

async execute(command: KillDragonCommand) {
const { heroId, dragonId } = command;
Expand All @@ -129,7 +129,7 @@ export class KillDragonHandler implements ICommandHandler<KillDragonCommand> {
}
@@switch
@CommandHandler(KillDragonCommand)
@Dependencies(HeroRepository)
@Dependencies(HeroesRepository)
export class KillDragonHandler {
constructor(repository) {
this.repository = repository;
Expand Down Expand Up @@ -180,15 +180,15 @@ To retrieve the hero, we need to create a query handler:
@@filename(get-hero.handler)
@QueryHandler(GetHeroQuery)
export class GetHeroHandler implements IQueryHandler<GetHeroQuery> {
constructor(private repository: HeroRepository) {}
constructor(private repository: HeroesRepository) {}

async execute(query: GetHeroQuery) {
return this.repository.findOneById(query.hero);
}
}
@@switch
@QueryHandler(GetHeroQuery)
@Dependencies(HeroRepository)
@Dependencies(HeroesRepository)
export class GetHeroHandler {
constructor(repository) {
this.repository = repository;
Expand Down Expand Up @@ -272,7 +272,7 @@ The `apply()` method is used to dispatch events. It accepts an event object as a
@CommandHandler(KillDragonCommand)
export class KillDragonHandler implements ICommandHandler<KillDragonCommand> {
constructor(
private repository: HeroRepository,
private repository: HeroesRepository,
private publisher: EventPublisher,
) {}

Expand All @@ -287,7 +287,7 @@ export class KillDragonHandler implements ICommandHandler<KillDragonCommand> {
}
@@switch
@CommandHandler(KillDragonCommand)
@Dependencies(HeroRepository, EventPublisher)
@Dependencies(HeroesRepository, EventPublisher)
export class KillDragonHandler {
constructor(repository, publisher) {
this.repository = repository;
Expand Down Expand Up @@ -341,7 +341,7 @@ Each event can have multiple **Event Handlers**.
@@filename(hero-killed-dragon.handler)
@EventsHandler(HeroKilledDragonEvent)
export class HeroKilledDragonHandler implements IEventHandler<HeroKilledDragonEvent> {
constructor(private repository: HeroRepository) {}
constructor(private repository: HeroesRepository) {}

handle(event: HeroKilledDragonEvent) {
// Business logic
Expand Down
14 changes: 7 additions & 7 deletions content/recipes/prisma.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ import { PrismaService } from './prisma.service';
import { User, Prisma } from '@prisma/client';

@Injectable()
export class UserService {
export class UsersService {
constructor(private prisma: PrismaService) {}

async user(
Expand Down Expand Up @@ -361,7 +361,7 @@ import { PrismaService } from './prisma.service';
import { Post, Prisma } from '@prisma/client';

@Injectable()
export class PostService {
export class PostsService {
constructor(private prisma: PrismaService) {}

async post(
Expand Down Expand Up @@ -414,7 +414,7 @@ export class PostService {
}
```

Your `UserService` and `PostService` currently wrap the CRUD queries that are available in Prisma Client. In a real world application, the service would also be the place to add business logic to your application. For example, you could have a method called `updatePassword` inside the `UserService` that would be responsible for updating the password of a user.
Your `UsersService` and `PostsService` currently wrap the CRUD queries that are available in Prisma Client. In a real world application, the service would also be the place to add business logic to your application. For example, you could have a method called `updatePassword` inside the `UsersService` that would be responsible for updating the password of a user.

Remember to register the new services in the app module.

Expand All @@ -434,15 +434,15 @@ import {
Put,
Delete,
} from '@nestjs/common';
import { UserService } from './user.service';
import { PostService } from './post.service';
import { UsersService } from './users.service';
import { PostsService } from './posts.service';
import { User as UserModel, Post as PostModel } from '@prisma/client';

@Controller()
export class AppController {
constructor(
private readonly userService: UserService,
private readonly postService: PostService,
private readonly userService: UsersService,
private readonly postService: PostsService,
) {}

@Get('post/:id')
Expand Down
2 changes: 1 addition & 1 deletion content/recipes/swc.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ For all [circular dependency injections](/fundamentals/circular-dependency) in y

```typescript
@Injectable()
export class UserService {
export class UsersService {
constructor(
@Inject(forwardRef(() => ProfileService))
private readonly profileService: WrapperType<ProfileService>,
Expand Down