16
16
from litestar .connection import Request
17
17
from litestar .router import Router
18
18
19
+
19
20
__all__ = (
20
21
"OpenAPIRenderPlugin" ,
21
22
"RapidocRenderPlugin" ,
@@ -363,6 +364,7 @@ def __init__(
363
364
js_url : str | None = None ,
364
365
css_url : str | None = None ,
365
366
path : str | Sequence [str ] = "/scalar" ,
367
+ options : dict [str , Any ] | None = None ,
366
368
** kwargs : Any ,
367
369
) -> None :
368
370
"""Initialize the Scalar OpenAPI UI render plugin.
@@ -375,10 +377,13 @@ def __init__(
375
377
css_url: Download url for the Scalar CSS bundle.
376
378
If not provided, the Litestar-provided CSS will be used.
377
379
path: Path to serve the OpenAPI UI at.
380
+ options: Scalar configuration options.
381
+ If not provided the default Scalar configuration will be used.
378
382
**kwargs: Additional arguments to pass to the base class.
379
383
"""
380
384
self .js_url = js_url or f"https://cdn.jsdelivr.net/npm/@scalar/api-reference@{ version } "
381
385
self .css_url = css_url or self ._default_css_url
386
+ self .options = options
382
387
super ().__init__ (path = path , ** kwargs )
383
388
384
389
def render (self , request : Request , openapi_schema : dict [str , Any ]) -> bytes :
@@ -412,6 +417,7 @@ def render(self, request: Request, openapi_schema: dict[str, Any]) -> bytes:
412
417
id="api-reference"
413
418
data-url="{ self .get_openapi_json_route (request )} ">
414
419
</script>
420
+ { self .render_options ()}
415
421
<script src="{ self .js_url } " crossorigin></script>
416
422
"""
417
423
@@ -423,6 +429,16 @@ def render(self, request: Request, openapi_schema: dict[str, Any]) -> bytes:
423
429
</html>
424
430
""" .encode ()
425
431
432
+ def render_options (self ) -> str :
433
+ """Render options to Scalar configuration."""
434
+ if not self .options :
435
+ return ""
436
+ return f"""
437
+ <script>
438
+ document.getElementById('api-reference').dataset.configuration = '{ msgspec .json .encode (self .options ).decode ()} '
439
+ </script>
440
+ """
441
+
426
442
427
443
class StoplightRenderPlugin (OpenAPIRenderPlugin ):
428
444
"""Render an OpenAPI schema using StopLight Elements."""
0 commit comments