123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using System;
- using System.Text;
- using System.Linq;
- using System.Collections.Generic;
- namespace ZhonTai.ApiUI
- {
- public static class ApiUIOptionsExtensions
- {
- /// <summary>
- /// Injects additional CSS stylesheets into the index.html page
- /// </summary>
- /// <param name="options"></param>
- /// <param name="path">A path to the stylesheet - i.e. the link "href" attribute</param>
- /// <param name="media">The target media - i.e. the link "media" attribute</param>
- public static void InjectStylesheet(this ApiUIOptions options, string path, string media = "screen")
- {
- var builder = new StringBuilder(options.HeadContent);
- builder.AppendLine($"<link href='{path}' rel='stylesheet' media='{media}' type='text/css' />");
- options.HeadContent = builder.ToString();
- }
- /// <summary>
- /// Injects additional Javascript files into the index.html page
- /// </summary>
- /// <param name="options"></param>
- /// <param name="path">A path to the javascript - i.e. the script "src" attribute</param>
- /// <param name="type">The script type - i.e. the script "type" attribute</param>
- public static void InjectJavascript(this ApiUIOptions options, string path, string type = "text/javascript")
- {
- var builder = new StringBuilder(options.HeadContent);
- builder.AppendLine($"<script src='{path}' type='{type}'></script>");
- options.HeadContent = builder.ToString();
- }
- /// <summary>
- /// Adds Swagger JSON endpoints. Can be fully-qualified or relative to the UI page
- /// </summary>
- /// <param name="options"></param>
- /// <param name="url">Can be fully qualified or relative to the current host</param>
- /// <param name="name">The description that appears in the document selector drop-down</param>
- public static void SwaggerEndpoint(this ApiUIOptions options, string url, string name)
- {
- var urls = new List<UrlDescriptor>(options.ConfigObject.Urls ?? Enumerable.Empty<UrlDescriptor>());
- urls.Add(new UrlDescriptor { Url = url, Name = name });
- options.ConfigObject.Urls = urls;
- }
- /// <summary>
- /// Enables deep linking for tags and operations
- /// </summary>
- /// <param name="options"></param>
- public static void EnableDeepLinking(this ApiUIOptions options)
- {
- options.ConfigObject.DeepLinking = true;
- }
- /// <summary>
- /// Enables persist authorization data
- /// </summary>
- /// <param name="options"></param>
- public static void EnablePersistAuthorization(this ApiUIOptions options)
- {
- options.ConfigObject.PersistAuthorization = true;
- }
- /// <summary>
- /// Controls the display of operationId in operations list
- /// </summary>
- /// <param name="options"></param>
- public static void DisplayOperationId(this ApiUIOptions options)
- {
- options.ConfigObject.DisplayOperationId = true;
- }
- /// <summary>
- /// The default expansion depth for models (set to -1 completely hide the models)
- /// </summary>
- /// <param name="options"></param>
- /// <param name="depth"></param>
- public static void DefaultModelsExpandDepth(this ApiUIOptions options, int depth)
- {
- options.ConfigObject.DefaultModelsExpandDepth = depth;
- }
- /// <summary>
- /// The default expansion depth for the model on the model-example section
- /// </summary>
- /// <param name="options"></param>
- /// <param name="depth"></param>
- public static void DefaultModelExpandDepth(this ApiUIOptions options, int depth)
- {
- options.ConfigObject.DefaultModelExpandDepth = depth;
- }
- /// <summary>
- /// Controls how the model is shown when the API is first rendered.
- /// (The user can always switch the rendering for a given model by clicking the 'Model' and 'Example Value' links.)
- /// </summary>
- /// <param name="options"></param>
- /// <param name="modelRendering"></param>
- public static void DefaultModelRendering(this ApiUIOptions options, ModelRendering modelRendering)
- {
- options.ConfigObject.DefaultModelRendering = modelRendering;
- }
- /// <summary>
- /// Controls the display of the request duration (in milliseconds) for Try-It-Out requests
- /// </summary>
- /// <param name="options"></param>
- public static void DisplayRequestDuration(this ApiUIOptions options)
- {
- options.ConfigObject.DisplayRequestDuration = true;
- }
- /// <summary>
- /// Controls the default expansion setting for the operations and tags.
- /// It can be 'List' (expands only the tags), 'Full' (expands the tags and operations) or 'None' (expands nothing)
- /// </summary>
- /// <param name="options"></param>
- /// <param name="docExpansion"></param>
- public static void DocExpansion(this ApiUIOptions options, DocExpansion docExpansion)
- {
- options.ConfigObject.DocExpansion = docExpansion;
- }
- /// <summary>
- /// Enables filtering. The top bar will show an edit box that you can use to filter the tagged operations that are shown.
- /// If an expression is provided it will be used and applied initially.
- /// Filtering is case sensitive matching the filter expression anywhere inside the tag
- /// </summary>
- /// <param name="options"></param>
- /// <param name="expression"></param>
- public static void EnableFilter(this ApiUIOptions options, string expression = null)
- {
- options.ConfigObject.Filter = expression ?? "";
- }
- /// <summary>
- /// Enables the "Try it out" section by default.
- /// </summary>
- /// <param name="options"></param>
- public static void EnableTryItOutByDefault(this ApiUIOptions options)
- {
- options.ConfigObject.TryItOutEnabled = true;
- }
- /// <summary>
- /// Limits the number of tagged operations displayed to at most this many. The default is to show all operations
- /// </summary>
- /// <param name="options"></param>
- /// <param name="count"></param>
- public static void MaxDisplayedTags(this ApiUIOptions options, int count)
- {
- options.ConfigObject.MaxDisplayedTags = count;
- }
- /// <summary>
- /// Controls the display of vendor extension (x-) fields and values for Operations, Parameters, and Schema
- /// </summary>
- /// <param name="options"></param>
- public static void ShowExtensions(this ApiUIOptions options)
- {
- options.ConfigObject.ShowExtensions = true;
- }
- /// <summary>
- /// Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values for Parameters
- /// </summary>
- /// <param name="options"></param>
- public static void ShowCommonExtensions(this ApiUIOptions options)
- {
- options.ConfigObject.ShowCommonExtensions = true;
- }
- /// <summary>
- /// List of HTTP methods that have the Try it out feature enabled. An empty array disables Try it out for all operations.
- /// This does not filter the operations from the display
- /// </summary>
- /// <param name="options"></param>
- /// <param name="submitMethods"></param>
- public static void SupportedSubmitMethods(this ApiUIOptions options, params SubmitMethod[] submitMethods)
- {
- options.ConfigObject.SupportedSubmitMethods = submitMethods;
- }
- /// <summary>
- /// OAuth redirect URL
- /// </summary>
- /// <param name="options"></param>
- /// <param name="url"></param>
- public static void OAuth2RedirectUrl(this ApiUIOptions options, string url)
- {
- options.ConfigObject.OAuth2RedirectUrl = url;
- }
- [Obsolete("The validator is disabled by default. Use EnableValidator to enable it")]
- public static void ValidatorUrl(this ApiUIOptions options, string url)
- {
- options.ConfigObject.ValidatorUrl = url;
- }
- /// <summary>
- /// You can use this parameter to enable the swagger-ui's built-in validator (badge) functionality
- /// Setting it to null will disable validation
- /// </summary>
- /// <param name="options"></param>
- /// <param name="url"></param>
- public static void EnableValidator(this ApiUIOptions options, string url = "https://online.swagger.io/validator")
- {
- options.ConfigObject.ValidatorUrl = url;
- }
- /// <summary>
- /// Default clientId
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value"></param>
- public static void OAuthClientId(this ApiUIOptions options, string value)
- {
- options.OAuthConfigObject.ClientId = value;
- }
- /// <summary>
- /// Default clientSecret
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value"></param>
- public static void OAuthClientSecret(this ApiUIOptions options, string value)
- {
- options.OAuthConfigObject.ClientSecret = value;
- }
- /// <summary>
- /// realm query parameter (for oauth1) added to authorizationUrl and tokenUrl
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value"></param>
- public static void OAuthRealm(this ApiUIOptions options, string value)
- {
- options.OAuthConfigObject.Realm = value;
- }
- /// <summary>
- /// Application name, displayed in authorization popup
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value"></param>
- public static void OAuthAppName(this ApiUIOptions options, string value)
- {
- options.OAuthConfigObject.AppName = value;
- }
- /// <summary>
- /// Scope separator for passing scopes, encoded before calling, default value is a space (encoded value %20)
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value"></param>
- public static void OAuthScopeSeparator(this ApiUIOptions options, string value)
- {
- options.OAuthConfigObject.ScopeSeparator = value;
- }
- /// <summary>
- /// String array of initially selected oauth scopes, default is empty array
- /// </summary>
- public static void OAuthScopes(this ApiUIOptions options, params string[] scopes)
- {
- options.OAuthConfigObject.Scopes = scopes;
- }
- /// <summary>
- /// Additional query parameters added to authorizationUrl and tokenUrl
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value"></param>
- public static void OAuthAdditionalQueryStringParams(
- this ApiUIOptions options,
- Dictionary<string, string> value)
- {
- options.OAuthConfigObject.AdditionalQueryStringParams = value;
- }
- /// <summary>
- /// Only activated for the accessCode flow. During the authorization_code request to the tokenUrl,
- /// pass the Client Password using the HTTP Basic Authentication scheme (Authorization header with
- /// Basic base64encoded[client_id:client_secret]). The default is false
- /// </summary>
- /// <param name="options"></param>
- public static void OAuthUseBasicAuthenticationWithAccessCodeGrant(this ApiUIOptions options)
- {
- options.OAuthConfigObject.UseBasicAuthenticationWithAccessCodeGrant = true;
- }
- /// <summary>
- /// Only applies to authorizatonCode flows. Proof Key for Code Exchange brings enhanced security for OAuth public clients.
- /// The default is false
- /// </summary>
- /// <param name="options"></param>
- public static void OAuthUsePkce(this ApiUIOptions options)
- {
- options.OAuthConfigObject.UsePkceWithAuthorizationCodeGrant = true;
- }
- /// <summary>
- /// Function to intercept remote definition, "Try it out", and OAuth 2.0 requests.
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value">MUST be a valid Javascript function: (request: SwaggerRequest) => SwaggerRequest</param>
- public static void UseRequestInterceptor(this ApiUIOptions options, string value)
- {
- options.Interceptors.RequestInterceptorFunction = value;
- }
- /// <summary>
- /// Function to intercept remote definition, "Try it out", and OAuth 2.0 responses.
- /// </summary>
- /// <param name="options"></param>
- /// <param name="value">MUST be a valid Javascript function: (response: SwaggerResponse ) => SwaggerResponse </param>
- public static void UseResponseInterceptor(this ApiUIOptions options, string value)
- {
- options.Interceptors.ResponseInterceptorFunction = value;
- }
- }
- }
|