-
Notifications
You must be signed in to change notification settings - Fork 164
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 #22 from edwinhern/refactor/ServiceResponseStructure
Refactor: Test structure and ServiceResponse Class
- Loading branch information
Showing
26 changed files
with
391 additions
and
186 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
export class ServiceResponse<T> { | ||
import _ from 'lodash'; | ||
|
||
export enum ResponseStatus { | ||
Success, | ||
Failed, | ||
} | ||
|
||
export class ServiceResponse<T = null> { | ||
success: boolean; | ||
message: string; | ||
responseObject: T | null; | ||
errors?: unknown; | ||
responseObject: T; | ||
statusCode: number; | ||
|
||
constructor(success: boolean, message: string, responseObject: T | null, errors?: unknown) { | ||
this.success = success; | ||
constructor(success: ResponseStatus, message: string, responseObject: T, statusCode: number) { | ||
this.success = _.isEqual(success, ResponseStatus.Success); | ||
this.message = message; | ||
this.responseObject = responseObject; | ||
this.errors = errors; | ||
this.statusCode = statusCode; | ||
} | ||
} |
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 @@ | ||
import { z } from 'zod'; | ||
|
||
export const commonValidations = { | ||
id: z.string().regex(/^\d+$/, 'ID must be a number').transform(Number), | ||
// ... other common validations | ||
}; |
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,20 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { ZodError, ZodSchema } from 'zod'; | ||
|
||
import { ResponseStatus, ServiceResponse } from '@common/models/serviceResponse'; | ||
|
||
export const handleServiceResponse = (serviceResponse: ServiceResponse<any>, response: Response) => { | ||
return response.status(serviceResponse.statusCode).send(serviceResponse); | ||
}; | ||
|
||
export const validateRequest = (schema: ZodSchema) => (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
schema.parse({ body: req.body, query: req.query, params: req.params }); | ||
next(); | ||
} catch (err) { | ||
const errorMessage = `Invalid input: ${(err as ZodError).errors.map((e) => e.message).join(' ')}`; | ||
const statusCode = StatusCodes.BAD_REQUEST; | ||
res.status(statusCode).send(new ServiceResponse<null>(ResponseStatus.Failed, errorMessage, null, statusCode)); | ||
} | ||
}; |
Oops, something went wrong.