add openapi registers

This commit is contained in:
miloschwartz
2025-04-06 22:44:14 -04:00
parent a76e3e00f7
commit d260450a84
50 changed files with 852 additions and 60 deletions

View File

@@ -13,6 +13,7 @@ import { addTargets } from "../newt/targets";
import { eq } from "drizzle-orm";
import { pickPort } from "./helpers";
import { isTargetValid } from "@server/lib/validators";
import { OpenAPITags, registry } from "@server/openApi";
const createTargetParamsSchema = z
.object({
@@ -34,6 +35,24 @@ const createTargetSchema = z
export type CreateTargetResponse = Target;
registry.registerPath({
method: "put",
path: "/resource/{resourceId}/target",
description: "Create a target for a resource.",
tags: [OpenAPITags.Resource, OpenAPITags.Target],
request: {
params: createTargetParamsSchema,
body: {
content: {
"application/json": {
schema: createTargetSchema
}
}
}
},
responses: {}
});
export async function createTarget(
req: Request,
res: Response,

View File

@@ -11,6 +11,7 @@ import { addPeer } from "../gerbil/peers";
import { fromError } from "zod-validation-error";
import { removeTargets } from "../newt/targets";
import { getAllowedIps } from "./helpers";
import { OpenAPITags, registry } from "@server/openApi";
const deleteTargetSchema = z
.object({
@@ -18,6 +19,17 @@ const deleteTargetSchema = z
})
.strict();
registry.registerPath({
method: "delete",
path: "/target/{targetId}",
description: "Delete a target.",
tags: [OpenAPITags.Target],
request: {
params: deleteTargetSchema
},
responses: {}
});
export async function deleteTarget(
req: Request,
res: Response,

View File

@@ -8,6 +8,7 @@ import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import logger from "@server/logger";
import { fromError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi";
const getTargetSchema = z
.object({
@@ -15,6 +16,17 @@ const getTargetSchema = z
})
.strict();
registry.registerPath({
method: "get",
path: "/target/{targetId}",
description: "Get a target.",
tags: [OpenAPITags.Target],
request: {
params: getTargetSchema
},
responses: {}
});
export async function getTarget(
req: Request,
res: Response,

View File

@@ -8,6 +8,7 @@ import createHttpError from "http-errors";
import { z } from "zod";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi";
const listTargetsParamsSchema = z
.object({
@@ -56,6 +57,18 @@ export type ListTargetsResponse = {
pagination: { total: number; limit: number; offset: number };
};
registry.registerPath({
method: "get",
path: "/resource/{resourceId}/targets",
description: "List targets for a resource.",
tags: [OpenAPITags.Resource, OpenAPITags.Target],
request: {
params: listTargetsParamsSchema,
query: listTargetsSchema
},
responses: {}
});
export async function listTargets(
req: Request,
res: Response,

View File

@@ -12,6 +12,7 @@ import { addPeer } from "../gerbil/peers";
import { addTargets } from "../newt/targets";
import { pickPort } from "./helpers";
import { isTargetValid } from "@server/lib/validators";
import { OpenAPITags, registry } from "@server/openApi";
const updateTargetParamsSchema = z
.object({
@@ -31,6 +32,24 @@ const updateTargetBodySchema = z
message: "At least one field must be provided for update"
});
registry.registerPath({
method: "post",
path: "/target/{targetId}",
description: "Update a target.",
tags: [OpenAPITags.Target],
request: {
params: updateTargetParamsSchema,
body: {
content: {
"application/json": {
schema: updateTargetBodySchema
}
}
}
},
responses: {}
});
export async function updateTarget(
req: Request,
res: Response,