Add new route: api/gallery/[gid]

This commit is contained in:
2023-06-12 17:37:19 +08:00
parent 7c5e7d485e
commit c450b64e33
6 changed files with 79 additions and 5 deletions

10
server/gallery.ts Normal file
View File

@@ -0,0 +1,10 @@
import { GMeta, PMeta, Tag } from "../db.ts";
import { JSONResult } from "./utils.ts";
export type GalleryData = {
meta: GMeta;
tags: Tag[];
pages: PMeta[];
};
export type GalleryResult = JSONResult<GalleryData>;

View File

@@ -2,3 +2,35 @@ export function get_ws_host() {
const protocol = document.location.protocol === "https:" ? "wss:" : "ws:";
return `${protocol}//${document.location.host}`;
}
export type JSONResult<T> = {
ok: true;
status: 0;
data: T;
} | {
ok: false;
status: Exclude<number, 0>;
error: string;
};
function gen_response<T>(d: JSONResult<T>) {
let status = 200;
if (d.status !== 0) {
status = (d.status >= 400 && d.status < 600) ? d.status : 400;
}
return new Response(JSON.stringify(d), {
status,
headers: { "Content-Type": "application/json" },
});
}
export function return_error<T = unknown>(
status: Exclude<number, 0>,
error: string,
) {
return gen_response<T>({ ok: false, status, error });
}
export function return_data<T = unknown>(data: T) {
return gen_response<T>({ ok: true, status: 0, data });
}