openapi test

This commit is contained in:
miloschwartz 2025-04-06 16:06:50 -04:00
parent e827ba185e
commit cf3093fdb0
6 changed files with 76 additions and 9 deletions

6
server/extendZod.ts Normal file
View file

@ -0,0 +1,6 @@
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
import { z } from "zod";
extendZodWithOpenApi(z);
export default function extendZod() {}

View file

@ -1,3 +1,5 @@
import "./extendZod.ts";
import { runSetupFunctions } from "./setup";
import { createApiServer } from "./apiServer";
import { createNextServer } from "./nextServer";

17
server/openApi.ts Normal file
View file

@ -0,0 +1,17 @@
import { OpenAPIRegistry } from "@asteasolutions/zod-to-openapi";
export const registry = new OpenAPIRegistry();
export const bearerAuth = registry.registerComponent(
"securitySchemes",
"Bearer Auth",
{
type: "http",
scheme: "bearer"
}
);
export enum OpenAPITags {
Site = "Site",
Org = "Organization"
}

View file

@ -13,29 +13,29 @@ import { fromError } from "zod-validation-error";
import { hash } from "@node-rs/argon2";
import { newts } from "@server/db/schemas";
import moment from "moment";
import { hashPassword } from "@server/auth/password";
import { bearerAuth, OpenAPITags, registry } from "@server/openApi";
const createSiteParamsSchema = z
.object({
orgId: z.string()
orgId: z.string().openapi({})
})
.strict();
const createSiteSchema = z
.object({
name: z.string().min(1).max(255),
exitNodeId: z.number().int().positive().optional(),
name: z.string().min(1).max(255).openapi({}),
exitNodeId: z.number().int().positive().optional().openapi({}),
// subdomain: z
// .string()
// .min(1)
// .max(255)
// .transform((val) => val.toLowerCase())
// .optional(),
pubKey: z.string().optional(),
subnet: z.string().optional(),
newtId: z.string().optional(),
secret: z.string().optional(),
type: z.string()
pubKey: z.string().optional().openapi({}),
subnet: z.string().optional().openapi({}),
newtId: z.string().optional().openapi({}),
secret: z.string().optional().openapi({}),
type: z.enum(["newt", "wireguard", "local"]).openapi({})
})
.strict();
@ -43,6 +43,25 @@ export type CreateSiteBody = z.infer<typeof createSiteSchema>;
export type CreateSiteResponse = Site;
registry.registerPath({
method: "put",
path: "/org/{orgId}/site",
description: "Create a new site",
security: [{ [bearerAuth.name]: [] }],
tags: [OpenAPITags.Site, OpenAPITags.Org],
request: {
params: createSiteParamsSchema,
body: {
content: {
"application/json": {
schema: createSiteSchema
}
}
}
},
responses: {}
});
export async function createSite(
req: Request,
res: Response,