Skip to content

Commit

Permalink
feat: expose getRouteRulesForPath from nitro runtime (#2238)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Apr 3, 2024
1 parent 2d6381d commit 09d23d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export interface CachedEventHandlerOptions<T = any>
shouldBypassCache?: (event: H3Event) => boolean | Promise<boolean>;
getKey?: (event: H3Event) => string | Promise<string>;
headersOnly?: boolean;
varies?: string[];
varies?: string[] | readonly string[];
}

function escapeKey(key: string | string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
11 changes: 10 additions & 1 deletion src/runtime/route-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ export function getRouteRules(event: H3Event): NitroRouteRules {
return event.context._nitro.routeRules;
}

export function getRouteRulesForPath(path: string): NitroRouteRules {
type DeepReadonly<T> =
T extends Record<string, any>
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T extends Array<infer U>
? ReadonlyArray<DeepReadonly<U>>
: T;

export function getRouteRulesForPath(
path: string
): DeepReadonly<NitroRouteRules> {
return defu({}, ..._routeRulesMatcher.matchAll(path).reverse());
}

0 comments on commit 09d23d6

Please sign in to comment.