chore: adds maintenance mode for API v1 (#14555)

* Create middleware.ts

* Update middleware.ts

* Update middleware.ts

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
This commit is contained in:
Omar López
2024-04-13 00:16:14 +00:00
committed by GitHub
co-authored by Keith Williams
parent 161dcbdd4c
commit becad5ee67
+22
View File
@@ -0,0 +1,22 @@
import { get } from "@vercel/edge-config";
import { NextResponse } from "next/server";
const safeGet = async <T = unknown>(key: string): Promise<T | undefined> => {
try {
return get<T>(key);
} catch (error) {
// Don't crash if EDGE_CONFIG env var is missing
}
};
export const config = { matcher: "/:path*" };
export async function middleware() {
const isInMaintenanceMode = await safeGet<boolean>("isInMaintenanceMode");
if (!isInMaintenanceMode) return NextResponse.next();
// If is in maintenance mode, return a 503 status code
return NextResponse.json(
{ message: "API is currently under maintenance. Please try again at a later time." },
{ status: 503 }
);
}