feat(api): Add debug query parameter when viewing raw Lysand objects

This commit is contained in:
Jesse Wierzbinski 2024-07-21 22:33:15 +02:00
parent 7f48c990e7
commit cf5684cf26
No known key found for this signature in database
2 changed files with 30 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import { applyConfig, handleZodError } from "@/api";
import { errorResponse, jsonResponse } from "@/response";
import { errorResponse, jsonResponse, response } from "@/response";
import type { Hono } from "@hono/hono";
import { zValidator } from "@hono/zod-validator";
import type { Entity } from "@lysand-org/federation/types";
@ -26,6 +26,12 @@ export const schemas = {
param: z.object({
id: z.string().uuid(),
}),
query: z.object({
debug: z
.string()
.transform((v) => ["true", "1", "on"].includes(v.toLowerCase()))
.optional(),
}),
};
export default (app: Hono) =>
@ -33,8 +39,10 @@ export default (app: Hono) =>
meta.allowedMethods,
meta.route,
zValidator("param", schemas.param, handleZodError),
zValidator("query", schemas.query, handleZodError),
async (context) => {
const { id } = context.req.valid("param");
const { debug } = context.req.valid("query");
let foundObject: Note | LikeType | null = null;
let apiObject: Entity | null = null;
@ -63,6 +71,12 @@ export default (app: Hono) =>
return errorResponse("Object not found", 404);
}
if (debug) {
return response(JSON.stringify(apiObject, null, 4), 200, {
"Content-Type": "application/json",
});
}
return jsonResponse(apiObject);
},
);

View file

@ -1,5 +1,5 @@
import { applyConfig, handleZodError } from "@/api";
import { errorResponse, jsonResponse } from "@/response";
import { errorResponse, jsonResponse, response } from "@/response";
import type { Hono } from "@hono/hono";
import { zValidator } from "@hono/zod-validator";
import { z } from "zod";
@ -21,6 +21,12 @@ export const schemas = {
param: z.object({
uuid: z.string().uuid(),
}),
query: z.object({
debug: z
.string()
.transform((v) => ["true", "1", "on"].includes(v.toLowerCase()))
.optional(),
}),
};
export default (app: Hono) =>
@ -28,8 +34,10 @@ export default (app: Hono) =>
meta.allowedMethods,
meta.route,
zValidator("param", schemas.param, handleZodError),
zValidator("query", schemas.query, handleZodError),
async (context) => {
const { uuid } = context.req.valid("param");
const { debug } = context.req.valid("query");
const user = await User.fromId(uuid);
@ -44,6 +52,12 @@ export default (app: Hono) =>
);
}
if (debug) {
return response(JSON.stringify(user.toLysand(), null, 4), 200, {
"Content-Type": "application/json",
});
}
return jsonResponse(user.toLysand());
},
);