ApiUIOptionsExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. namespace ZhonTai.ApiUI
  6. {
  7. public static class ApiUIOptionsExtensions
  8. {
  9. /// <summary>
  10. /// Injects additional CSS stylesheets into the index.html page
  11. /// </summary>
  12. /// <param name="options"></param>
  13. /// <param name="path">A path to the stylesheet - i.e. the link "href" attribute</param>
  14. /// <param name="media">The target media - i.e. the link "media" attribute</param>
  15. public static void InjectStylesheet(this ApiUIOptions options, string path, string media = "screen")
  16. {
  17. var builder = new StringBuilder(options.HeadContent);
  18. builder.AppendLine($"<link href='{path}' rel='stylesheet' media='{media}' type='text/css' />");
  19. options.HeadContent = builder.ToString();
  20. }
  21. /// <summary>
  22. /// Injects additional Javascript files into the index.html page
  23. /// </summary>
  24. /// <param name="options"></param>
  25. /// <param name="path">A path to the javascript - i.e. the script "src" attribute</param>
  26. /// <param name="type">The script type - i.e. the script "type" attribute</param>
  27. public static void InjectJavascript(this ApiUIOptions options, string path, string type = "text/javascript")
  28. {
  29. var builder = new StringBuilder(options.HeadContent);
  30. builder.AppendLine($"<script src='{path}' type='{type}'></script>");
  31. options.HeadContent = builder.ToString();
  32. }
  33. /// <summary>
  34. /// Adds Swagger JSON endpoints. Can be fully-qualified or relative to the UI page
  35. /// </summary>
  36. /// <param name="options"></param>
  37. /// <param name="url">Can be fully qualified or relative to the current host</param>
  38. /// <param name="name">The description that appears in the document selector drop-down</param>
  39. public static void SwaggerEndpoint(this ApiUIOptions options, string url, string name)
  40. {
  41. var urls = new List<UrlDescriptor>(options.ConfigObject.Urls ?? Enumerable.Empty<UrlDescriptor>());
  42. urls.Add(new UrlDescriptor { Url = url, Name = name });
  43. options.ConfigObject.Urls = urls;
  44. }
  45. /// <summary>
  46. /// Enables deep linking for tags and operations
  47. /// </summary>
  48. /// <param name="options"></param>
  49. public static void EnableDeepLinking(this ApiUIOptions options)
  50. {
  51. options.ConfigObject.DeepLinking = true;
  52. }
  53. /// <summary>
  54. /// Enables persist authorization data
  55. /// </summary>
  56. /// <param name="options"></param>
  57. public static void EnablePersistAuthorization(this ApiUIOptions options)
  58. {
  59. options.ConfigObject.PersistAuthorization = true;
  60. }
  61. /// <summary>
  62. /// Controls the display of operationId in operations list
  63. /// </summary>
  64. /// <param name="options"></param>
  65. public static void DisplayOperationId(this ApiUIOptions options)
  66. {
  67. options.ConfigObject.DisplayOperationId = true;
  68. }
  69. /// <summary>
  70. /// The default expansion depth for models (set to -1 completely hide the models)
  71. /// </summary>
  72. /// <param name="options"></param>
  73. /// <param name="depth"></param>
  74. public static void DefaultModelsExpandDepth(this ApiUIOptions options, int depth)
  75. {
  76. options.ConfigObject.DefaultModelsExpandDepth = depth;
  77. }
  78. /// <summary>
  79. /// The default expansion depth for the model on the model-example section
  80. /// </summary>
  81. /// <param name="options"></param>
  82. /// <param name="depth"></param>
  83. public static void DefaultModelExpandDepth(this ApiUIOptions options, int depth)
  84. {
  85. options.ConfigObject.DefaultModelExpandDepth = depth;
  86. }
  87. /// <summary>
  88. /// Controls how the model is shown when the API is first rendered.
  89. /// (The user can always switch the rendering for a given model by clicking the 'Model' and 'Example Value' links.)
  90. /// </summary>
  91. /// <param name="options"></param>
  92. /// <param name="modelRendering"></param>
  93. public static void DefaultModelRendering(this ApiUIOptions options, ModelRendering modelRendering)
  94. {
  95. options.ConfigObject.DefaultModelRendering = modelRendering;
  96. }
  97. /// <summary>
  98. /// Controls the display of the request duration (in milliseconds) for Try-It-Out requests
  99. /// </summary>
  100. /// <param name="options"></param>
  101. public static void DisplayRequestDuration(this ApiUIOptions options)
  102. {
  103. options.ConfigObject.DisplayRequestDuration = true;
  104. }
  105. /// <summary>
  106. /// Controls the default expansion setting for the operations and tags.
  107. /// It can be 'List' (expands only the tags), 'Full' (expands the tags and operations) or 'None' (expands nothing)
  108. /// </summary>
  109. /// <param name="options"></param>
  110. /// <param name="docExpansion"></param>
  111. public static void DocExpansion(this ApiUIOptions options, DocExpansion docExpansion)
  112. {
  113. options.ConfigObject.DocExpansion = docExpansion;
  114. }
  115. /// <summary>
  116. /// Enables filtering. The top bar will show an edit box that you can use to filter the tagged operations that are shown.
  117. /// If an expression is provided it will be used and applied initially.
  118. /// Filtering is case sensitive matching the filter expression anywhere inside the tag
  119. /// </summary>
  120. /// <param name="options"></param>
  121. /// <param name="expression"></param>
  122. public static void EnableFilter(this ApiUIOptions options, string expression = null)
  123. {
  124. options.ConfigObject.Filter = expression ?? "";
  125. }
  126. /// <summary>
  127. /// Enables the "Try it out" section by default.
  128. /// </summary>
  129. /// <param name="options"></param>
  130. public static void EnableTryItOutByDefault(this ApiUIOptions options)
  131. {
  132. options.ConfigObject.TryItOutEnabled = true;
  133. }
  134. /// <summary>
  135. /// Limits the number of tagged operations displayed to at most this many. The default is to show all operations
  136. /// </summary>
  137. /// <param name="options"></param>
  138. /// <param name="count"></param>
  139. public static void MaxDisplayedTags(this ApiUIOptions options, int count)
  140. {
  141. options.ConfigObject.MaxDisplayedTags = count;
  142. }
  143. /// <summary>
  144. /// Controls the display of vendor extension (x-) fields and values for Operations, Parameters, and Schema
  145. /// </summary>
  146. /// <param name="options"></param>
  147. public static void ShowExtensions(this ApiUIOptions options)
  148. {
  149. options.ConfigObject.ShowExtensions = true;
  150. }
  151. /// <summary>
  152. /// Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values for Parameters
  153. /// </summary>
  154. /// <param name="options"></param>
  155. public static void ShowCommonExtensions(this ApiUIOptions options)
  156. {
  157. options.ConfigObject.ShowCommonExtensions = true;
  158. }
  159. /// <summary>
  160. /// List of HTTP methods that have the Try it out feature enabled. An empty array disables Try it out for all operations.
  161. /// This does not filter the operations from the display
  162. /// </summary>
  163. /// <param name="options"></param>
  164. /// <param name="submitMethods"></param>
  165. public static void SupportedSubmitMethods(this ApiUIOptions options, params SubmitMethod[] submitMethods)
  166. {
  167. options.ConfigObject.SupportedSubmitMethods = submitMethods;
  168. }
  169. /// <summary>
  170. /// OAuth redirect URL
  171. /// </summary>
  172. /// <param name="options"></param>
  173. /// <param name="url"></param>
  174. public static void OAuth2RedirectUrl(this ApiUIOptions options, string url)
  175. {
  176. options.ConfigObject.OAuth2RedirectUrl = url;
  177. }
  178. [Obsolete("The validator is disabled by default. Use EnableValidator to enable it")]
  179. public static void ValidatorUrl(this ApiUIOptions options, string url)
  180. {
  181. options.ConfigObject.ValidatorUrl = url;
  182. }
  183. /// <summary>
  184. /// You can use this parameter to enable the swagger-ui's built-in validator (badge) functionality
  185. /// Setting it to null will disable validation
  186. /// </summary>
  187. /// <param name="options"></param>
  188. /// <param name="url"></param>
  189. public static void EnableValidator(this ApiUIOptions options, string url = "https://online.swagger.io/validator")
  190. {
  191. options.ConfigObject.ValidatorUrl = url;
  192. }
  193. /// <summary>
  194. /// Default clientId
  195. /// </summary>
  196. /// <param name="options"></param>
  197. /// <param name="value"></param>
  198. public static void OAuthClientId(this ApiUIOptions options, string value)
  199. {
  200. options.OAuthConfigObject.ClientId = value;
  201. }
  202. /// <summary>
  203. /// Default clientSecret
  204. /// </summary>
  205. /// <param name="options"></param>
  206. /// <param name="value"></param>
  207. public static void OAuthClientSecret(this ApiUIOptions options, string value)
  208. {
  209. options.OAuthConfigObject.ClientSecret = value;
  210. }
  211. /// <summary>
  212. /// realm query parameter (for oauth1) added to authorizationUrl and tokenUrl
  213. /// </summary>
  214. /// <param name="options"></param>
  215. /// <param name="value"></param>
  216. public static void OAuthRealm(this ApiUIOptions options, string value)
  217. {
  218. options.OAuthConfigObject.Realm = value;
  219. }
  220. /// <summary>
  221. /// Application name, displayed in authorization popup
  222. /// </summary>
  223. /// <param name="options"></param>
  224. /// <param name="value"></param>
  225. public static void OAuthAppName(this ApiUIOptions options, string value)
  226. {
  227. options.OAuthConfigObject.AppName = value;
  228. }
  229. /// <summary>
  230. /// Scope separator for passing scopes, encoded before calling, default value is a space (encoded value %20)
  231. /// </summary>
  232. /// <param name="options"></param>
  233. /// <param name="value"></param>
  234. public static void OAuthScopeSeparator(this ApiUIOptions options, string value)
  235. {
  236. options.OAuthConfigObject.ScopeSeparator = value;
  237. }
  238. /// <summary>
  239. /// String array of initially selected oauth scopes, default is empty array
  240. /// </summary>
  241. public static void OAuthScopes(this ApiUIOptions options, params string[] scopes)
  242. {
  243. options.OAuthConfigObject.Scopes = scopes;
  244. }
  245. /// <summary>
  246. /// Additional query parameters added to authorizationUrl and tokenUrl
  247. /// </summary>
  248. /// <param name="options"></param>
  249. /// <param name="value"></param>
  250. public static void OAuthAdditionalQueryStringParams(
  251. this ApiUIOptions options,
  252. Dictionary<string, string> value)
  253. {
  254. options.OAuthConfigObject.AdditionalQueryStringParams = value;
  255. }
  256. /// <summary>
  257. /// Only activated for the accessCode flow. During the authorization_code request to the tokenUrl,
  258. /// pass the Client Password using the HTTP Basic Authentication scheme (Authorization header with
  259. /// Basic base64encoded[client_id:client_secret]). The default is false
  260. /// </summary>
  261. /// <param name="options"></param>
  262. public static void OAuthUseBasicAuthenticationWithAccessCodeGrant(this ApiUIOptions options)
  263. {
  264. options.OAuthConfigObject.UseBasicAuthenticationWithAccessCodeGrant = true;
  265. }
  266. /// <summary>
  267. /// Only applies to authorizatonCode flows. Proof Key for Code Exchange brings enhanced security for OAuth public clients.
  268. /// The default is false
  269. /// </summary>
  270. /// <param name="options"></param>
  271. public static void OAuthUsePkce(this ApiUIOptions options)
  272. {
  273. options.OAuthConfigObject.UsePkceWithAuthorizationCodeGrant = true;
  274. }
  275. /// <summary>
  276. /// Function to intercept remote definition, "Try it out", and OAuth 2.0 requests.
  277. /// </summary>
  278. /// <param name="options"></param>
  279. /// <param name="value">MUST be a valid Javascript function: (request: SwaggerRequest) => SwaggerRequest</param>
  280. public static void UseRequestInterceptor(this ApiUIOptions options, string value)
  281. {
  282. options.Interceptors.RequestInterceptorFunction = value;
  283. }
  284. /// <summary>
  285. /// Function to intercept remote definition, "Try it out", and OAuth 2.0 responses.
  286. /// </summary>
  287. /// <param name="options"></param>
  288. /// <param name="value">MUST be a valid Javascript function: (response: SwaggerResponse ) => SwaggerResponse </param>
  289. public static void UseResponseInterceptor(this ApiUIOptions options, string value)
  290. {
  291. options.Interceptors.ResponseInterceptorFunction = value;
  292. }
  293. }
  294. }