/** * Cache Clear Handler * * GET /v1/cache/clear */ import type { EndpointHandler, EndpointMiddlewareHandler, FunctionAppEnv } from '@utils/endpoint/types' import { createValidator } from '@utils/middleware/validation' import { ok, internalServerError } from '@utils/response' import { flushCacheAsync } from '#shared/cache/operations.ts' import Definition from './definition.ts' export type AppEnv = FunctionAppEnv export const Middleware: EndpointMiddlewareHandler[] = [ createValidator('query', Definition.Schemas.Query), ] export const Handler: EndpointHandler = async function (c) { try { await flushCacheAsync() if (!c.req.path.includes('/v1/')) { return new Response('Started clearing cache!', { status: 200, headers: { 'Content-Type': 'text/plain', 'x-content-type-options': 'nosniff', }, }) } return c.json(...ok({ cleared: true })) } catch (error) { if (!c.req.path.includes('/v1/')) { return new Response('Redis is unavailable, try again at a later date!', { status: 500, headers: { 'Content-Type': 'text/plain', 'x-content-type-options': 'nosniff', }, }) } return c.json(...internalServerError( c.req.path, error instanceof Error ? error.message : 'Cache clear failed' )) } } export default Handler