Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556/** * 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<AppEnv>[] = [ createValidator('query', Definition.Schemas.Query),]
export const Handler: EndpointHandler<AppEnv, typeof Definition> = 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