result-tsk
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -143,1 +143,7 @@ # Changelog | ||
- Added static method to create a result object and also an instance method to set error from other result object. | ||
## [2.2.1] — 2024-08-06 | ||
### Update | ||
- Documentation was improved |
{ | ||
"name": "result-tsk", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "result tool to use with or without NodeTskeleton template project", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -16,8 +16,8 @@ # Result tool 🧰 | ||
export class UseCaseProductGet extends BaseUseCase { | ||
constructor(private productQueryService: IProductQueryService) { | ||
export class GetProductUseCase extends BaseUseCase { | ||
constructor(private readonly productQueryService: IProductQueryService) { | ||
super(); | ||
} | ||
async Execute(idMask: string): Promise<IResultT<ProductDto>> { | ||
async execute(idMask: string): Promise<IResultT<ProductDto>> { | ||
// We create the instance of our type of result at the beginning of the use case. | ||
@@ -31,3 +31,3 @@ const result = new ResultT<ProductDto>(); | ||
// Ways to use the result. | ||
// Ways to use the result: | ||
// A. Common way (Not so clean) | ||
@@ -39,3 +39,3 @@ const product: Product = await this.productQueryService.getByMaskId(idMask); | ||
this.resources.get(this.resources.keys.PRODUCT_DOES_NOT_EXIST), | ||
this.resultCodes.NOT_FOUND, | ||
ApplicationStatus.NOT_FOUND, | ||
); | ||
@@ -53,11 +53,12 @@ return result; | ||
// D. Way three: returning a new result object and using it to set in result | ||
// D. Way three: returning a new result object and using it to set in UseCase result | ||
const { product, resultError } = await this.getProduct(idMask); | ||
if (!product) return result.fromResult(resultError); | ||
// Or | ||
// Or | ||
if (resultError?.hasError()) return result.fromResult(resultError); | ||
// Manage the UseCase response | ||
const productDto = this.mapper.mapObject<Product, ProductDto>(product, new ProductDto()); | ||
// The result object also helps you with the response data. | ||
result.setData(productDto, this.resultCodes.SUCCESS); | ||
result.setData(productDto, ApplicationStatus.SUCCESS); | ||
// And finally you give it back. | ||
@@ -82,3 +83,3 @@ return result; | ||
// C. Way two using the result object | ||
// C. Way two by using the result object | ||
private async getProduct(result: IResult, idMask: string): Promise<Product> { | ||
@@ -93,3 +94,3 @@ const product: Product = await this.productQueryService.getByMaskId(idMask); | ||
// D. Way three using a new instance of result object | ||
// D. Way three by using a new instance of result object | ||
private async getProduct(idMask: string): Promise<{ value?: Product, result?: IResult }> { | ||
@@ -114,3 +115,3 @@ const product: Product = await this.productQueryService.getByMaskId(idMask); | ||
// Generic result | ||
const resultWithType = new ResultT<ProductDto>(); | ||
const resultTyped = new ResultT<Product>(); | ||
// Simple result | ||
@@ -126,3 +127,3 @@ const resultWithoutType = new Result(); | ||
it("should return a 400 error if quantity is null or zero", async () => { | ||
// ... some lines after | ||
// ... some lines after | ||
itemDto.quantity = null; | ||
@@ -129,0 +130,0 @@ |
28061
220