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
5 changes: 5 additions & 0 deletions .changeset/short-bears-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-metadata": patch
---

Add configuration options for Scalar UI generation
4 changes: 2 additions & 2 deletions docs/openapi-metadata/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ app.get("/api", async (req, res) => {
});

app.get("/api/docs", (req, res) => {
const ui = generateScalarUI("/api");
const ui = generateScalarUI("/api", { theme: "purple" });
res.send(ui);
});
```
Expand All @@ -45,7 +45,7 @@ app.get("/api", async () => {
});

app.get("/api/docs", () => {
const ui = generateScalarUI("/api");
const ui = generateScalarUI("/api", { theme: "purple" });
return ui;
});
```
2 changes: 1 addition & 1 deletion docs/openapi-metadata/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ title: UI integrations
```ts twoslash
import { generateScalarUI } from "openapi-metadata/ui";

generateScalarUI("http://localhost:3000/api");
generateScalarUI("http://localhost:3000/api", { theme: "purple" });
```

## [Swagger UI](https://swagger.io/tools/swagger-ui/)
Expand Down
69 changes: 64 additions & 5 deletions packages/openapi-metadata/src/ui/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
/**
* Options for generating the Scalar UI.
*
* See: https://guides.scalar.com/scalar/scalar-api-references/configuration
*/
export type GenerateScalarUIOptions = {
url?: string;
content?: string | object | (() => object) | null;
title?: string;
slug?: string;
baseServerURL?: string;
hideClientButton?: boolean;
proxyUrl?: string;
searchHotKey?: string;
servers?: any[];
showSidebar?: boolean;
theme?:
| "alternate"
| "default"
| "moon"
| "purple"
| "solarized"
| "bluePlanet"
| "deepSpace"
| "saturn"
| "kepler"
| "elysiajs"
| "fastify"
| "mars"
| "laserwave"
| "none";
persistAuth?: boolean;
plugins?: any[];
layout?: "modern" | "classic";
isLoading?: boolean;
hideModels?: boolean;
documentDownloadType?: "yaml" | "json" | "both" | "none";
hideDownloadButton?: boolean;
hideTestRequestButton?: boolean;
hideSearch?: boolean;
darkMode?: boolean;
forceDarkModeState?: "dark" | "light";
hideDarkModeToggle?: boolean;
metaData?: any;
favicon?: string;
hiddenClients?: Record<string, boolean | string[]> | string[] | true;
defaultHttpClient?: { targetKey: string; clientKey: string };
customCss?: string;
pathRouting?: { basePath: string };
withDefaultFonts?: boolean;
defaultOpenAllTags?: boolean;
tagsSorter?: "alpha" | ((a: any, b: any) => number);
operationsSorter?: "alpha" | "method" | ((a: any, b: any) => number);
};

/**
* Generates HTML to display Scalar UI.
*
* @see https://scalar.com/
*/
export function generateScalarUI(url: string) {
export function generateScalarUI(url: string, options: Omit<GenerateScalarUIOptions, "url"> = {}) {
return `
<!doctype html>
<html>
Expand All @@ -15,11 +70,15 @@ export function generateScalarUI(url: string) {
content="width=device-width, initial-scale=1" />
</head>
<body>
<script
id="api-reference"
data-url="${url}"
data-proxy-url="https://proxy.scalar.com"></script>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
<script>
Scalar.createApiReference("#app", {
url: "${url}",
proxyUrl: "https://proxy.scalar.com",
...${JSON.stringify(options)}
});
</script>
</body>
</html>
`;
Expand Down
Loading