mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): ♻️ Move from @hono/zod-openapi to hono-openapi
hono-openapi is easier to work with and generates better OpenAPI definitions
This commit is contained in:
parent
0576aff972
commit
58342e86e1
240 changed files with 9494 additions and 9575 deletions
|
|
@ -1,74 +0,0 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
|
||||
const schemas = {
|
||||
param: z.object({
|
||||
hash: z.string(),
|
||||
name: z.string(),
|
||||
}),
|
||||
header: z.object({
|
||||
range: z.string().optional().default(""),
|
||||
}),
|
||||
};
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/media/{hash}/{name}",
|
||||
summary: "Get media file by hash and name",
|
||||
request: {
|
||||
params: schemas.param,
|
||||
headers: schemas.header,
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media",
|
||||
content: {
|
||||
"*": {
|
||||
schema: z.any(),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "File not found",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ApiError.zodSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { hash, name } = context.req.valid("param");
|
||||
const { range } = context.req.valid("header");
|
||||
|
||||
// parse `Range` header
|
||||
const [start = 0, end = Number.POSITIVE_INFINITY] = (
|
||||
range
|
||||
.split("=") // ["Range: bytes", "0-100"]
|
||||
.at(-1) || ""
|
||||
) // "0-100"
|
||||
.split("-") // ["0", "100"]
|
||||
.map(Number); // [0, 100]
|
||||
|
||||
// Serve file from filesystem
|
||||
const file = Bun.file(`./uploads/${hash}/${name}`);
|
||||
|
||||
const buffer = await file.arrayBuffer();
|
||||
|
||||
if (!(await file.exists())) {
|
||||
throw new ApiError(404, "File not found");
|
||||
}
|
||||
|
||||
// Can't directly copy file into Response because this crashes Bun for now
|
||||
return context.body(buffer, 200, {
|
||||
"Content-Type": file.type || "application/octet-stream",
|
||||
"Content-Length": `${file.size - start}`,
|
||||
"Content-Range": `bytes ${start}-${end}/${file.size}`,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Hono doesn't type this response so this has a TS error
|
||||
}) as any;
|
||||
}),
|
||||
);
|
||||
77
api/media/[hash]/[name]/index.ts
Normal file
77
api/media/[hash]/[name]/index.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { apiRoute, handleZodError } from "@/api";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver, validator } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/media/:hash/:name",
|
||||
describeRoute({
|
||||
summary: "Get media file by hash and name",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media",
|
||||
content: {
|
||||
"*": {
|
||||
schema: resolver(z.any()),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "File not found",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(ApiError.zodSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
validator(
|
||||
"param",
|
||||
z.object({
|
||||
hash: z.string(),
|
||||
name: z.string(),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
validator(
|
||||
"header",
|
||||
z.object({
|
||||
range: z.string().optional().default(""),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { hash, name } = context.req.valid("param");
|
||||
const { range } = context.req.valid("header");
|
||||
|
||||
// parse `Range` header
|
||||
const [start = 0, end = Number.POSITIVE_INFINITY] = (
|
||||
range
|
||||
.split("=") // ["Range: bytes", "0-100"]
|
||||
.at(-1) || ""
|
||||
) // "0-100"
|
||||
.split("-") // ["0", "100"]
|
||||
.map(Number); // [0, 100]
|
||||
|
||||
// Serve file from filesystem
|
||||
const file = Bun.file(`./uploads/${hash}/${name}`);
|
||||
|
||||
const buffer = await file.arrayBuffer();
|
||||
|
||||
if (!(await file.exists())) {
|
||||
throw new ApiError(404, "File not found");
|
||||
}
|
||||
|
||||
// Can't directly copy file into Response because this crashes Bun for now
|
||||
return context.body(buffer, 200, {
|
||||
"Content-Type": file.type || "application/octet-stream",
|
||||
"Content-Length": `${file.size - start}`,
|
||||
"Content-Range": `bytes ${start}-${end}/${file.size}`,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Hono doesn't type this response so this has a TS error
|
||||
}) as any;
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { proxy } from "hono/proxy";
|
||||
import type { ContentfulStatusCode, StatusCode } from "hono/utils/http-status";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
const schemas = {
|
||||
param: z.object({
|
||||
id: z
|
||||
.string()
|
||||
.transform((val) => Buffer.from(val, "base64url").toString()),
|
||||
}),
|
||||
};
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/media/proxy/{id}",
|
||||
summary: "Proxy media through the server",
|
||||
request: {
|
||||
params: schemas.param,
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media",
|
||||
content: {
|
||||
"*": {
|
||||
schema: z.any(),
|
||||
},
|
||||
},
|
||||
},
|
||||
400: {
|
||||
description: "Invalid URL to proxy",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: ApiError.zodSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
|
||||
// Check if URL is valid
|
||||
if (!URL.canParse(id)) {
|
||||
throw new ApiError(
|
||||
400,
|
||||
"Invalid URL",
|
||||
"Should be encoded as base64url",
|
||||
);
|
||||
}
|
||||
|
||||
const media = await proxy(id, {
|
||||
// @ts-expect-error Proxy is a Bun-specific feature
|
||||
proxy: config.http.proxy_address,
|
||||
});
|
||||
|
||||
// Check if file extension ends in svg or svg
|
||||
// Cloudflare R2 serves those as application/xml
|
||||
if (
|
||||
media.headers.get("Content-Type") === "application/xml" &&
|
||||
id.endsWith(".svg")
|
||||
) {
|
||||
media.headers.set("Content-Type", "image/svg+xml");
|
||||
}
|
||||
|
||||
const realFilename =
|
||||
media.headers
|
||||
.get("Content-Disposition")
|
||||
?.match(/filename="(.+)"/)?.[1] || id.split("/").pop();
|
||||
|
||||
if (!media.body) {
|
||||
return context.body(null, media.status as StatusCode);
|
||||
}
|
||||
|
||||
return context.body(media.body, media.status as ContentfulStatusCode, {
|
||||
"Content-Type":
|
||||
media.headers.get("Content-Type") || "application/octet-stream",
|
||||
"Content-Length": media.headers.get("Content-Length") || "0",
|
||||
"Content-Security-Policy": "",
|
||||
// Real filename
|
||||
"Content-Disposition": `inline; filename="${realFilename}"`,
|
||||
});
|
||||
}),
|
||||
);
|
||||
96
api/media/proxy/[id].ts
Normal file
96
api/media/proxy/[id].ts
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import { apiRoute, handleZodError } from "@/api";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver, validator } from "hono-openapi/zod";
|
||||
import { proxy } from "hono/proxy";
|
||||
import type { ContentfulStatusCode, StatusCode } from "hono/utils/http-status";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/media/proxy/{id}",
|
||||
describeRoute({
|
||||
summary: "Proxy media through the server",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Media",
|
||||
content: {
|
||||
"*": {
|
||||
schema: resolver(z.any()),
|
||||
},
|
||||
},
|
||||
},
|
||||
400: {
|
||||
description: "Invalid URL to proxy",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(ApiError.zodSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
validator(
|
||||
"param",
|
||||
z.object({
|
||||
id: z
|
||||
.string()
|
||||
.transform((val) =>
|
||||
Buffer.from(val, "base64url").toString(),
|
||||
),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
|
||||
// Check if URL is valid
|
||||
if (!URL.canParse(id)) {
|
||||
throw new ApiError(
|
||||
400,
|
||||
"Invalid URL",
|
||||
"Should be encoded as base64url",
|
||||
);
|
||||
}
|
||||
|
||||
const media = await proxy(id, {
|
||||
// @ts-expect-error Proxy is a Bun-specific feature
|
||||
proxy: config.http.proxy_address,
|
||||
});
|
||||
|
||||
// Check if file extension ends in svg or svg
|
||||
// Cloudflare R2 serves those as application/xml
|
||||
if (
|
||||
media.headers.get("Content-Type") === "application/xml" &&
|
||||
id.endsWith(".svg")
|
||||
) {
|
||||
media.headers.set("Content-Type", "image/svg+xml");
|
||||
}
|
||||
|
||||
const realFilename =
|
||||
media.headers
|
||||
.get("Content-Disposition")
|
||||
?.match(/filename="(.+)"/)?.[1] || id.split("/").pop();
|
||||
|
||||
if (!media.body) {
|
||||
return context.body(null, media.status as StatusCode);
|
||||
}
|
||||
|
||||
return context.body(
|
||||
media.body,
|
||||
media.status as ContentfulStatusCode,
|
||||
{
|
||||
"Content-Type":
|
||||
media.headers.get("Content-Type") ||
|
||||
"application/octet-stream",
|
||||
"Content-Length":
|
||||
media.headers.get("Content-Length") || "0",
|
||||
"Content-Security-Policy": "",
|
||||
// Real filename
|
||||
"Content-Disposition": `inline; filename="${realFilename}"`,
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue