Added api route to retrieve bot commands list.
This commit is contained in:
37
src/app/api/bot/route.ts
Normal file
37
src/app/api/bot/route.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { headers } from "next/headers";
|
||||||
|
import botCommands from "@/data/botCommands";
|
||||||
|
|
||||||
|
const environment = process.env.NODE_ENV || "development";
|
||||||
|
|
||||||
|
const isValidApiKey = (apiKey: string): boolean => {
|
||||||
|
const envApiKey =
|
||||||
|
process.env.API_TOKEN || process.env.NEXT_PUBLIC_API_TOKEN || "";
|
||||||
|
|
||||||
|
return apiKey === envApiKey;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function GET(/*request: Request*/) {
|
||||||
|
const headersList = await headers();
|
||||||
|
const apiKey = headersList.get("x-api-key");
|
||||||
|
|
||||||
|
if (environment === "production") {
|
||||||
|
if (!apiKey || apiKey == null) {
|
||||||
|
return new Response("No API Key provided", {
|
||||||
|
status: 401,
|
||||||
|
headers: headersList
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (apiKey !== null && !(await isValidApiKey(apiKey))) {
|
||||||
|
return new Response("Invalid API Key", {
|
||||||
|
status: 403,
|
||||||
|
headers: headersList
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.json(botCommands, {
|
||||||
|
status: 200,
|
||||||
|
headers: headersList
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user