From 09d23d69255c2f950d014974218c28384c92ac2a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 3 Apr 2024 12:55:34 +0100 Subject: [PATCH] feat: expose `getRouteRulesForPath` from nitro runtime (#2238) --- src/runtime/cache.ts | 2 +- src/runtime/index.ts | 2 +- src/runtime/route-rules.ts | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/runtime/cache.ts b/src/runtime/cache.ts index 25a230c971..a7959369b0 100644 --- a/src/runtime/cache.ts +++ b/src/runtime/cache.ts @@ -199,7 +199,7 @@ export interface CachedEventHandlerOptions shouldBypassCache?: (event: H3Event) => boolean | Promise; getKey?: (event: H3Event) => string | Promise; headersOnly?: boolean; - varies?: string[]; + varies?: string[] | readonly string[]; } function escapeKey(key: string | string[]) { diff --git a/src/runtime/index.ts b/src/runtime/index.ts index 062edd9744..d44e31ac6a 100644 --- a/src/runtime/index.ts +++ b/src/runtime/index.ts @@ -4,7 +4,7 @@ export { useNitroApp } from "./app"; export * from "./plugin"; export * from "./task"; export * from "./renderer"; -export { getRouteRules } from "./route-rules"; +export { getRouteRules, getRouteRulesForPath } from "./route-rules"; export { useStorage } from "./storage"; export { useEvent } from "./context"; export { defineNitroErrorHandler } from "./error"; diff --git a/src/runtime/route-rules.ts b/src/runtime/route-rules.ts index 74ffb5af79..d465256fce 100644 --- a/src/runtime/route-rules.ts +++ b/src/runtime/route-rules.ts @@ -74,6 +74,15 @@ export function getRouteRules(event: H3Event): NitroRouteRules { return event.context._nitro.routeRules; } -export function getRouteRulesForPath(path: string): NitroRouteRules { +type DeepReadonly = + T extends Record + ? { readonly [K in keyof T]: DeepReadonly } + : T extends Array + ? ReadonlyArray> + : T; + +export function getRouteRulesForPath( + path: string +): DeepReadonly { return defu({}, ..._routeRulesMatcher.matchAll(path).reverse()); }