-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new api route to clear all entries of specific base (#80)
- Loading branch information
Showing
4 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/runtime/server/api/_hub/cache/clear/[...base].delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { eventHandler, sendNoContent, getRouterParam, createError } from 'h3' | ||
import { requireNuxtHubAuthorization } from '../../../../utils/auth' | ||
import { requireNuxtHubFeature } from '../../../../utils/features' | ||
// @ts-expect-error useStorage not yet typed | ||
import { useStorage } from '#imports' | ||
|
||
export default eventHandler(async (event) => { | ||
await requireNuxtHubAuthorization(event) | ||
requireNuxtHubFeature('cache') | ||
|
||
const base = getRouterParam(event, 'base') || '' | ||
if (/\.([a-z0-9]+)$/i.test(base)) { | ||
throw createError({ | ||
statusCode: 400, | ||
message: 'Invalid base' | ||
}) | ||
} | ||
|
||
const storage = useStorage(`cache:nitro:${base}`) | ||
const keys = await storage.getKeys() | ||
// delete with batch of 25 keys | ||
do { | ||
const keysToDelete = keys.splice(0, 25) | ||
await Promise.all(keysToDelete.map(storage.removeItem)) | ||
} while (keys.length) | ||
|
||
return sendNoContent(event) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { eventHandler, sendNoContent } from 'h3' | ||
import { requireNuxtHubAuthorization } from '../../../../utils/auth' | ||
|
||
export default eventHandler(async (event) => { | ||
// only handles CORS for Nuxt DevTools | ||
await requireNuxtHubAuthorization(event) | ||
return sendNoContent(event) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters