-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
11 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
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 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,17 @@ | ||
import { RequestHandler } from "express"; | ||
import { z } from "zod"; | ||
import { JSend } from "@/utils/jsend"; | ||
import { getData } from "@/services/hello.controller"; | ||
import { validateBody } from "@/utils/validation"; | ||
|
||
const controller: RequestHandler = async (req, res, next) => { | ||
const bodySchema = z.object({ id: z.string(), foo: z.string().optional() }); | ||
|
||
const { id } = await validateBody(req, bodySchema); | ||
|
||
const data = getData(id); | ||
|
||
res.status(200).json(JSend.success(data)); | ||
}; | ||
|
||
export default controller; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { RequestHandler } from "express"; | ||
|
||
const handler: RequestHandler = (req, res, next) => {}; | ||
|
||
export default handler; |
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,7 @@ | ||
export function getData(id: string) { | ||
return { | ||
id, | ||
foo: "bar", | ||
hello: "world", | ||
}; | ||
} |
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,19 @@ | ||
declare global { | ||
export type JSendResponse<T> = | ||
| { | ||
status: "success"; | ||
data: T; | ||
} | ||
| { | ||
status: "fail"; | ||
data: T; | ||
} | ||
| { | ||
status: "error"; | ||
message: string; | ||
code?: number; | ||
data?: T; | ||
}; | ||
} | ||
|
||
export default {}; |
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,36 @@ | ||
/** | ||
* JSend is a specification that gives a consistent structure for JSON responses. Make sure to use | ||
* this in all of your JSON responses in controllers | ||
* | ||
* See the JSend specification for more information | ||
* | ||
* {@link https://github.com/omniti-labs/jsend} | ||
*/ | ||
export namespace JSend { | ||
export function success<T>(data: T): JSendResponse<T> { | ||
return { | ||
status: "success", | ||
data, | ||
}; | ||
} | ||
|
||
export function fail<T>(data: T): JSendResponse<T> { | ||
return { | ||
status: "fail", | ||
data, | ||
}; | ||
} | ||
|
||
export function error<T>( | ||
message: string, | ||
code?: number, | ||
data?: T | ||
): JSendResponse<T> { | ||
return { | ||
status: "error", | ||
message, | ||
code, | ||
data, | ||
}; | ||
} | ||
} |
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,32 @@ | ||
import { Request } from "express"; | ||
import { z } from "zod"; | ||
|
||
export class ValidationError extends Error { | ||
public readonly formattedError: any; | ||
|
||
constructor(formattedError: any) { | ||
super(); | ||
this.formattedError = formattedError; | ||
} | ||
} | ||
|
||
async function validate<T extends z.ZodSchema>( | ||
schema: T, | ||
value: any | ||
): Promise<Required<z.infer<T>>> { | ||
const { success, error, data } = await schema.safeParseAsync(value); | ||
|
||
if (!success) { | ||
throw new ValidationError(error.format()); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
export async function validateBody(req: Request, schema: z.ZodSchema) { | ||
return await validate(schema, req.body); | ||
} | ||
|
||
export async function validateQuery(req: Request, schema: z.ZodSchema) { | ||
return await validate(schema, req.query); | ||
} |