mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-03 17:26:38 +00:00
Move nessicary info to url params
This commit is contained in:
@@ -6,8 +6,11 @@ import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
|
||||
const createTargetSchema = z.object({
|
||||
const createTargetParamsSchema = z.object({
|
||||
resourceId: z.string().uuid(),
|
||||
});
|
||||
|
||||
const createTargetSchema = z.object({
|
||||
ip: z.string().ip(),
|
||||
method: z.string().min(1).max(10),
|
||||
port: z.number().int().min(1).max(65535),
|
||||
@@ -29,7 +32,22 @@ export async function createTarget(req: Request, res: Response, next: NextFuncti
|
||||
|
||||
const targetData = parsedBody.data;
|
||||
|
||||
const newTarget = await db.insert(targets).values(targetData).returning();
|
||||
const parsedParams = createTargetParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedParams.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { resourceId } = parsedParams.data;
|
||||
|
||||
const newTarget = await db.insert(targets).values({
|
||||
resourceId,
|
||||
...targetData
|
||||
}).returning();
|
||||
|
||||
return res.status(HttpCode.CREATED).send(
|
||||
response(res, {
|
||||
|
||||
@@ -7,10 +7,13 @@ import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { sql, eq } from 'drizzle-orm';
|
||||
|
||||
const listTargetsParamsSchema = z.object({
|
||||
resourceId: z.string().optional(),
|
||||
});
|
||||
|
||||
const listTargetsSchema = z.object({
|
||||
limit: z.string().optional().transform(Number).pipe(z.number().int().positive().default(10)),
|
||||
offset: z.string().optional().transform(Number).pipe(z.number().int().nonnegative().default(0)),
|
||||
resourceId: z.string().optional(),
|
||||
});
|
||||
|
||||
export async function listTargets(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
@@ -25,7 +28,19 @@ export async function listTargets(req: Request, res: Response, next: NextFunctio
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset, resourceId } = parsedQuery.data;
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
const parsedParams = listTargetsParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedParams.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { resourceId } = parsedParams.data;
|
||||
|
||||
let baseQuery: any = db
|
||||
.select({
|
||||
|
||||
Reference in New Issue
Block a user