Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Cargo.lock
.vscode
target
.nvim

.cargo
vendor
22 changes: 22 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ update-swagger-ui version:
sed "${sed_flags[@]}" "s|tags/v.*>|tags/v{{version}}.zip>|" ./utoipa-swagger-ui/src/lib.rs
sed "${sed_flags[@]}" "s|tags/v.*\.zip|tags/v{{version}}.zip|" ./utoipa-swagger-ui/build.rs

# Update vendored Scalar UI to the given version. Usage: `just update-scalar-ui latest`
update-scalar-ui version:
#!/usr/bin/env bash
set -eu -o pipefail
resource="api-reference.js"
if [ {{version}} = latest ]; then
query=""
else
query="@{{version}}"
fi
curl -sSL -o "$resource" "https://cdn.jsdelivr.net/npm/@scalar/api-reference$query"
version=`sed -n 's#.*/npm/@scalar/api-reference@\([^/]*\)/dist.*#\1#p' $resource`

sed_flags=(-i)
if [[ "$(uname)" == "Darwin" ]]; then
sed_flags+=('')
fi

echo "Update vendored Scalar UI to $version"
mv "$resource" ./utoipa-scalar/res/
sed "${sed_flags[@]}" "s|ui-version = .*\`|ui-version = ${version}\`|" ./utoipa-scalar/README.md

# Validate all examples: check formatting and run clippy.
validate-examples:
#!/usr/bin/env bash
Expand Down
5 changes: 4 additions & 1 deletion utoipa-scalar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ authors = ["Juha Kukkonen <juha7kukkonen@gmail.com>"]
rust-version.workspace = true

[package.metadata.docs.rs]
features = ["actix-web", "axum", "rocket"]
features = ["actix-web", "axum", "rocket", "vendored"]
rustdoc-args = ["--cfg", "doc_cfg"]

[features]
vendored = []

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
Expand Down
1 change: 1 addition & 0 deletions utoipa-scalar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You may find fullsize examples from utoipa's Github [repository][examples].
* **actix-web** Allows serving `Scalar` via _**`actix-web`**_. `version >= 4`
* **rocket** Allows serving `Scalar` via _**`rocket`**_. `version >=0.5`
* **axum** Allows serving `Scalar` via _**`axum`**_. `version >=0.7`
* **vendored** Allows loading `Scalar` in offline clients. `ui-version = 1.57.5`

# Install

Expand Down
8 changes: 8 additions & 0 deletions utoipa-scalar/res/api-reference.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion utoipa-scalar/res/scalar.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
type="application/json">
$spec
</script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
<script $api-refrence-src > $api-refrence-content </script>
</body>
</html>
25 changes: 19 additions & 6 deletions utoipa-scalar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ mod rocket;

const DEFAULT_HTML: &str = include_str!("../res/scalar.html");

#[cfg(feature = "vendored")]
const CONTENT: &str = include_str!("../res/api-reference.js");
#[cfg(feature = "vendored")]
const SRC: &str = "";

#[cfg(not(feature = "vendored"))]
const SRC: &str = "src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"";
#[cfg(not(feature = "vendored"))]
const CONTENT: &str = "";

/// Trait makes [`Scalar`] to accept an _`URL`_ the [Scalar][scalar] will be served via predefined
/// web server.
///
Expand Down Expand Up @@ -239,12 +249,15 @@ impl<S: Spec> Scalar<S> {
/// At this point in time, it is not possible to customize the HTML template used by the
/// [`Scalar`] instance.
pub fn to_html(&self) -> String {
self.html.replace("$title", &self.title).replace(
"$spec",
&serde_json::to_string(&self.openapi).expect(
"Invalid OpenAPI spec, expected OpenApi, String, &str or serde_json::Value",
),
)
self.html
.replace(
"$spec",
&serde_json::to_string(&self.openapi).expect(
"Invalid OpenAPI spec, expected OpenApi, String, &str or serde_json::Value",
),
)
.replace("$api-refrence-src", SRC)
.replace("$api-refrence-content", CONTENT)
}

/// Override the [default HTML template][scalar_html_quickstart] with new one. Refer to
Expand Down