Implements @elysiajs/swagger for automated API documentation and introduces a feature flag system to expose service capabilities based on environment variables. Refactors authentication guards into native Elysia scoped middleware for improved integration and type safety. Updates error handling to support custom status codes and adds instance-specific headers to responses for better observability. Includes an IP fallback mechanism for bug reports that utilizes internal system info when the direct submitter IP is unavailable.
27 lines
872 B
TypeScript
27 lines
872 B
TypeScript
import { config } from "../config";
|
|
import { Log } from "../logger";
|
|
import Elysia from "elysia";
|
|
import type { UnauthorizedResponse } from "../types";
|
|
|
|
export const apiKeyGuard2 = new Elysia({ name: "api-key-guard" }).derive(
|
|
{ as: "scoped" },
|
|
({ headers, status }): UnauthorizedResponse | {} => {
|
|
const apiKey = headers["x-api-key"];
|
|
if (!apiKey || apiKey !== config.apiKey) {
|
|
throw status(401, { success: false as const, message: "Unauthorized API Key" });
|
|
}
|
|
return {};
|
|
},
|
|
);
|
|
|
|
export const adminKeyGuard2 = new Elysia({ name: "admin-key-guard" }).derive(
|
|
{ as: "scoped" },
|
|
({ headers, status }): UnauthorizedResponse | {} => {
|
|
const apiKey = headers["x-admin-key"];
|
|
if (!apiKey || apiKey !== config.adminKey) {
|
|
throw status(401, { success: false as const, message: "Unauthorized Admin Key" });
|
|
}
|
|
return {};
|
|
},
|
|
);
|