Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5d54b5
commit 99b18cc
Showing
19 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
apps/nestjs-rabbitmq-example/src/credit-card/credit-card.module.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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CreateChargeController } from './presentation/controllers/create-charge.controller'; | ||
import { CreditCardModule } from '@libs/credit-card/credit-card.module'; | ||
import { IdempotencyModule } from '../idempotency/idempotency.module'; | ||
|
||
@Module({ | ||
imports: [CreditCardModule], | ||
imports: [CreditCardModule, IdempotencyModule], | ||
controllers: [CreateChargeController], | ||
}) | ||
export class CreditCardApiModule {} |
17 changes: 13 additions & 4 deletions
17
...tjs-rabbitmq-example/src/credit-card/presentation/controllers/create-charge.controller.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
4 changes: 4 additions & 0 deletions
4
apps/nestjs-rabbitmq-example/src/idempotency/domain/models/idempotency.model.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,4 @@ | ||
export type IdempotencyModel = { | ||
key: string; | ||
response: any; | ||
}; |
7 changes: 7 additions & 0 deletions
7
apps/nestjs-rabbitmq-example/src/idempotency/domain/repositories/idempotency.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,7 @@ | ||
import { IdempotencyModel } from '../models/idempotency.model'; | ||
|
||
export interface IIdempotencyRepository { | ||
find(key: string): Promise<IdempotencyModel | undefined>; | ||
preSave(key: string): Promise<void>; | ||
update(key: string, response: any): Promise<void>; | ||
} |
1 change: 1 addition & 0 deletions
1
apps/nestjs-rabbitmq-example/src/idempotency/domain/repositories/index.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 @@ | ||
export * from './idempotency.repository'; |
9 changes: 9 additions & 0 deletions
9
apps/nestjs-rabbitmq-example/src/idempotency/idempotency.module.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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { Redis } from '../infra/redis/redis'; | ||
import { IdempotencyRepository } from './infra/repositories/idempotency.repository'; | ||
|
||
@Module({ | ||
providers: [Redis, IdempotencyRepository], | ||
exports: [IdempotencyRepository], | ||
}) | ||
export class IdempotencyModule {} |
64 changes: 64 additions & 0 deletions
64
...nestjs-rabbitmq-example/src/idempotency/infra/interceptors/idempotency-key.interceptor.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,64 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
BadRequestException, | ||
Inject, | ||
} from '@nestjs/common'; | ||
import { Observable, of } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
import { IdempotencyRepository } from '../repositories'; | ||
import { IIdempotencyRepository } from '@api/idempotency/domain/repositories'; | ||
|
||
@Injectable() | ||
export class IdempotencyKeyInterceptor implements NestInterceptor { | ||
constructor( | ||
@Inject(IdempotencyRepository) | ||
private readonly idempotencyRepository: IIdempotencyRepository, | ||
) {} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<any>> { | ||
const ctx = context.switchToHttp(); | ||
const request = ctx.getRequest<Request>(); | ||
const idempotencyKey = request.headers['x-idempotency-key']; | ||
|
||
if (!idempotencyKey) { | ||
throw new BadRequestException( | ||
"Header 'x-idempotency-key' is required for this request.", | ||
); | ||
} | ||
|
||
if (!this.isValidUUID(idempotencyKey)) { | ||
throw new BadRequestException( | ||
"Header 'x-idempotency-key' must be a UUID.", | ||
); | ||
} | ||
|
||
const idempotencyModel = await this.idempotencyRepository.find( | ||
idempotencyKey, | ||
); | ||
|
||
if (idempotencyModel) { | ||
return of(idempotencyModel.response); | ||
} | ||
|
||
await this.idempotencyRepository.preSave(idempotencyKey); | ||
|
||
return next.handle().pipe( | ||
tap(async (data) => { | ||
await this.idempotencyRepository.update(idempotencyKey, data); | ||
return data; | ||
}), | ||
); | ||
} | ||
|
||
private isValidUUID(uuid: string) { | ||
const uuidRegex = | ||
/(?:^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$)|(?:^0{8}-0{4}-0{4}-0{4}-0{12}$)/u; | ||
return uuidRegex.test(uuid); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
apps/nestjs-rabbitmq-example/src/idempotency/infra/interceptors/index.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 @@ | ||
export * from './idempotency-key.interceptor'; |
24 changes: 24 additions & 0 deletions
24
apps/nestjs-rabbitmq-example/src/idempotency/infra/repositories/idempotency.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,24 @@ | ||
import { Redis } from 'apps/nestjs-rabbitmq-example/src/infra/redis/redis'; | ||
import { IIdempotencyRepository } from '../../domain/repositories/idempotency.repository'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { IdempotencyModel } from '../../domain/models/idempotency.model'; | ||
|
||
@Injectable() | ||
export class IdempotencyRepository implements IIdempotencyRepository { | ||
constructor(private readonly redis: Redis) {} | ||
|
||
async find(key: string): Promise<IdempotencyModel | undefined> { | ||
const data = await this.redis.get(key); | ||
if (data) { | ||
return { key, response: data }; | ||
} | ||
} | ||
|
||
async preSave(key: string): Promise<void> { | ||
await this.redis.set(key); | ||
} | ||
|
||
async update(key: string, response: any): Promise<void> { | ||
await this.redis.set(key, response); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
apps/nestjs-rabbitmq-example/src/idempotency/infra/repositories/index.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 @@ | ||
export * from './idempotency.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,14 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class Redis { | ||
private static data = new Map<string, any>(); | ||
|
||
async get(key: string) { | ||
return Promise.resolve(Redis.data.get(key)); | ||
} | ||
|
||
async set(key: string, value?: any) { | ||
Promise.resolve(Redis.data.set(key, value)); | ||
} | ||
} |
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 * from './create-charge.use-case'; |
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 './psp-service.interface'; | ||
export * from './publisher.interface'; |
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 * from './credit-card-charge.entity'; |
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 * from './create-charge.use-case'; |
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 * from './pagarme.service'; |
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 * from './create-charge.publisher'; |
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