clove/system
src/lib/server/plural/openapi.ts
/* src/lib/server/plural/openapi.ts
* Copyright (c) 2026 Clove Nytrix Doughmination Twilight
* Licensed under the DASL-1.2 Licence.
* See LICENCE.md in the project root for full licence information.
*/
import { OpenAPIHono, z } from "@hono/zod-openapi";
import type { Context } from "hono";
import { dumps } from "jpcl-lang/core";
import { stringify as stringifyYaml } from "yaml";
import type { Env } from "./hono";
import { ErrorSchema } from "./schemas";
const errorBody = {
content: {
"application/json": {
schema: ErrorSchema,
},
},
};
export const errorResponses = {
400: { ...errorBody, description: "Bad request" },
401: { ...errorBody, description: "Not signed in, or the token is invalid" },
403: { ...errorBody, description: "Signed in, but missing the required role" },
404: { ...errorBody, description: "Not found" },
422: { ...errorBody, description: "The body or query failed validation" },
} as const;
// A 200 JSON response plus the shared error responses
export function ok<T extends z.ZodType>(schema: T, description = "OK") {
return {
200: {
content: {
"application/json": {
schema,
},
},
description,
},
...errorResponses,
};
}
export const formatQuery = z.object({
format: z.enum(["jpcl", "json", "yml"]).default("jpcl").openapi({
description: "Response format. Defaults to JPCL.",
}),
});
export function okWithFormats<T extends z.ZodType>(schema: T, jpclDescription = "The response as JPCL text") {
const responses = ok(schema);
return {
...responses,
200: {
...responses[200],
content: {
...responses[200].content,
"application/yaml": {
schema: z.string().openapi({ description: "The response as YAML" }),
},
"text/plain": {
schema: z.string().openapi({ description: jpclDescription }),
},
},
},
};
}
export function dataResponse<T extends object>(
c: Context<Env>,
format: z.infer<typeof formatQuery>["format"],
data: T,
jpclDocument: object = data,
) {
switch (format) {
case "json":
return c.json(data, 200);
case "yml":
return c.text(stringifyYaml(data), 200, {
"Content-Type": "application/yaml",
});
default:
return c.text(dumps(jpclDocument), 200);
}
}
export function jsonBody<T extends z.ZodType>(schema: T) {
return {
body: {
required: true,
content: {
"application/json": {
schema,
},
},
},
};
}
export const signedIn = [{ bearerAuth: [] }];
function issuesText(issues: ReadonlyArray<{ path: PropertyKey[]; message: string }>): string {
return issues
.map((issue) => (issue.path.length ? `${issue.path.map(String).join(".")}: ${issue.message}` : issue.message))
.join("; ");
}
// Every route group uses this, so validation failures all come back as 422 { detail }
export function router() {
return new OpenAPIHono<Env>({
defaultHook: (result, c) => {
if (!result.success) return c.json({ detail: issuesText(result.error.issues) }, 422);
},
});
}