diff --git a/docs/openapi-core.md b/docs/openapi-core.md index 7485fc87..25f42a56 100644 --- a/docs/openapi-core.md +++ b/docs/openapi-core.md @@ -258,6 +258,18 @@ Suppose you want to customise the look and feels of the Swagger UI page. In this // DO SOMETHING AFTER CALLING THE BASE METHOD } + + // Declare if you want to add a request interceptor function to the SwaggerUI instance. + public override Task GetRequestInterceptorAsync() + { + return Task.FromResult("function(request) {/* DO SOMETHING WITH THE REQUEST */; return request;}"); + } + + // Declare if you want to add a response interceptor function to the SwaggerUI instance. + public override Task GetResponseInterceptorAsync() + { + return Task.FromResult("function(response) {/* DO SOMETHING WITH THE RESPONSE */; return response;}"); + } } ``` diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IOpenApiCustomUIOptions.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IOpenApiCustomUIOptions.cs index 03a10150..0875004a 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IOpenApiCustomUIOptions.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IOpenApiCustomUIOptions.cs @@ -28,5 +28,17 @@ public interface IOpenApiCustomUIOptions /// /// The JavaScript string for custom UI. Task GetJavaScriptAsync(); + + /// + /// Gets the custom request interceptor to be used by Swagger UI. + /// + /// The JavaScript string containing a function that operates on a request. + Task GetRequestInterceptorAsync(); + + /// + /// Gets the custom response interceptor to be used by Swagger UI. + /// + /// The JavaScript string containing a function that operates on a response. + Task GetResponseInterceptorAsync(); } } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiCustomUIOptions.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiCustomUIOptions.cs index eb66b5eb..d637d783 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiCustomUIOptions.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiCustomUIOptions.cs @@ -99,6 +99,18 @@ public virtual async Task GetJavaScriptAsync() return await this.GetJavaScript.Invoke().ConfigureAwait(false); } + /// + public virtual Task GetRequestInterceptorAsync() + { + return Task.FromResult(string.Empty); + } + + /// + public virtual Task GetResponseInterceptorAsync() + { + return Task.FromResult(string.Empty); + } + private async Task ReadFromStreamAsync(string path) { using (var stream = this.Assembly.GetManifestResourceStream($"{this.Assembly.GetName().Name}.{path}")) diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs index fb93cdd3..e4647098 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs @@ -25,6 +25,8 @@ public class SwaggerUI : ISwaggerUI private const string SwaggerUIStandalonePresetJsPlaceholder = "[[SWAGGER_UI_STANDALONE_PRESET_JS]]"; private const string SwaggerUIApiPrefix = "[[SWAGGER_UI_API_PREFIX]]"; private const string SwaggerUrlPlaceholder = "[[SWAGGER_URL]]"; + private const string SwaggerUIRequestInterceptor = "[[SWAGGER_UI_REQUEST_INTERCEPTOR]]"; + private const string SwaggerUIResponseInterceptor = "[[SWAGGER_UI_RESPONSE_INTERCEPTOR]]"; private readonly string indexHtml = $"{typeof(SwaggerUI).Namespace}.dist.index.html"; private readonly string oauth2RedirectHtml = $"{typeof(SwaggerUI).Namespace}.dist.oauth2-redirect.html"; @@ -41,6 +43,8 @@ public class SwaggerUI : ISwaggerUI private string _swaggerUiCustomJs; private string _swaggerUiStandalonePresetJs; private string _swaggerUiApiPrefix; + private string _swaggerUiRequestInterceptor; + private string _swaggerUiResponseInterceptor; private string _indexHtml; private string _oauth2RedirectHtml; @@ -95,6 +99,8 @@ public async Task BuildAsync(Assembly assembly, IOpenApiCustomUIOpti { this._swaggerUiCustomCss = await options.GetStylesheetAsync(); this._swaggerUiCustomJs = await options.GetJavaScriptAsync(); + this._swaggerUiRequestInterceptor = await options.GetRequestInterceptorAsync(); + this._swaggerUiResponseInterceptor = await options.GetResponseInterceptorAsync(); } using (var stream = assembly.GetManifestResourceStream(swaggerUiCss)) @@ -179,13 +185,23 @@ private string Render(string endpoint, OpenApiAuthLevelType authLevel = OpenApiA swaggerUrl += "?" + string.Join("&", queries.SelectMany(p => p.Value.Select(q => $"{p.Key}={q}"))); } + var swaggerUiRequestInterceptor = this._swaggerUiRequestInterceptor.IsNullOrWhiteSpace() + ? string.Empty + : $"requestInterceptor: {this._swaggerUiRequestInterceptor},"; + + var swaggerUiResponseInterceptor = this._swaggerUiResponseInterceptor.IsNullOrWhiteSpace() + ? string.Empty + : $"responseInterceptor: {this._swaggerUiResponseInterceptor},"; + var html = this._indexHtml.Replace(SwaggerUITitlePlaceholder, swaggerUiTitle) .Replace(SwaggerUICssPlaceholder, this._swaggerUiCss) .Replace(SwaggerUICustomCssPlaceholder, this._swaggerUiCustomCss) .Replace(SwaggerUIBundleJsPlaceholder, this._swaggerUiBundleJs) .Replace(SwaggerUICustomJsPlaceholder, this._swaggerUiCustomJs) .Replace(SwaggerUIStandalonePresetJsPlaceholder, this._swaggerUiStandalonePresetJs) - .Replace(SwaggerUrlPlaceholder, swaggerUrl); + .Replace(SwaggerUrlPlaceholder, swaggerUrl) + .Replace(SwaggerUIRequestInterceptor, swaggerUiRequestInterceptor) + .Replace(SwaggerUIResponseInterceptor, swaggerUiResponseInterceptor); return html; } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/dist/index.html b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/dist/index.html index 23c7a31e..b9eac93a 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/dist/index.html +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/dist/index.html @@ -52,6 +52,8 @@ plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], + [[SWAGGER_UI_REQUEST_INTERCEPTOR]] + [[SWAGGER_UI_RESPONSE_INTERCEPTOR]] layout: "StandaloneLayout" }); // End Swagger UI call region