@@ -3,6 +3,42 @@ import { app, HttpMethod } from "@azure/functions";
33import { registry } from "./registry" ;
44import { FunctionRouteConfig } from "./types" ;
55
6+ /**
7+ * Normalizes a path for OpenAPI documentation.
8+ * Ensures the path starts with a single leading slash and removes multiple consecutive slashes.
9+ *
10+ * @param azureFunctionRoutePrefix - Optional route prefix for the Azure Function
11+ * @param route - The route path
12+ * @returns Normalized path string for OpenAPI (always starts with /)
13+ */
14+ function normalizeOpenAPIPath ( azureFunctionRoutePrefix : string | undefined , route : string ) : string {
15+ let fullPath = azureFunctionRoutePrefix
16+ ? `/${ azureFunctionRoutePrefix } /${ route } `
17+ : route ;
18+
19+ // Replace multiple slashes with a single slash, and ensure leading slash
20+ fullPath = '/' + fullPath . replace ( / ^ \/ + / , '' ) ;
21+ fullPath = fullPath . replace ( / \/ + / g, '/' ) ;
22+
23+ return fullPath ;
24+ }
25+
26+ /**
27+ * Normalizes a route for Azure Functions registration.
28+ * Azure Functions expects routes without leading slashes and handles prefixes via configuration.
29+ * This function cleans the route by removing leading slashes, trailing slashes, and multiple consecutive slashes.
30+ *
31+ * @param route - The route path
32+ * @returns Normalized route for Azure Functions (no leading slash, no trailing slash)
33+ */
34+ function normalizeAzureFunctionRoute ( route : string ) : string {
35+ // Remove leading slashes, trailing slashes, and replace multiple slashes with single slash
36+ let normalizedRoute = route . replace ( / ^ \/ + / , '' ) . replace ( / \/ + $ / , '' ) . replace ( / \/ + / g, '/' ) ;
37+
38+ // If the route becomes empty (was just slashes), return empty string (root route)
39+ return normalizedRoute || '' ;
40+ }
41+
642/**
743 * Registers a function with the specified configuration.
844 *
@@ -100,19 +136,24 @@ function registerPath(
100136 isWebHook : boolean ,
101137 options : FunctionRouteConfig ) {
102138
139+ // Normalize the route for Azure Functions registration (without leading slash and prefix)
140+ const normalizedRoute = normalizeAzureFunctionRoute ( options . route ) ;
141+
103142 app . http ( name , {
104143 methods : options . methods ,
105144 authLevel : options . authLevel ,
106145 handler : options . handler ,
107- route : options . route
146+ route : normalizedRoute
108147 } ) ;
109148
110149 options . methods . forEach ( method => {
150+ // Normalize the path for OpenAPI (with prefix and leading slash)
151+ const fullPath = normalizeOpenAPIPath ( options . azureFunctionRoutePrefix , options . route ) ;
152+
111153 const routeConfig : RouteConfig = {
112154 summary : summary ,
113155 method : mapHttpMethod ( method ) ,
114- // Add the route to the OpenAPI registry, with the route prefix if it exists
115- path : ( options . azureFunctionRoutePrefix ) ? `/${ options . azureFunctionRoutePrefix } /${ options . route } ` : options . route ,
156+ path : fullPath ,
116157 security : options . security ,
117158 request : options . request ,
118159 responses : options . responses ,
0 commit comments