Dependency-free distribution of Swagger UI for serving interactive OpenAPI documentation from server-side projects and single-page applications
npx @tessl/cli install tessl/npm-swagger-ui-dist@5.28.0Swagger UI Dist is a dependency-free distribution package that provides everything needed to serve Swagger UI in server-side projects or single-page applications. It exposes the entire Swagger UI distribution folder as a standalone module without requiring npm dependency resolution, making it ideal for environments where traditional dependency management isn't feasible.
npm install swagger-ui-distconst { SwaggerUIBundle, SwaggerUIStandalonePreset, getAbsoluteFSPath } = require("swagger-ui-dist");ES Modules:
import { SwaggerUIBundle, SwaggerUIStandalonePreset, getAbsoluteFSPath } from "swagger-ui-dist";const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath();
// Express.js example
app.use('/swagger-ui', express.static(swaggerUiAssetPath));import { SwaggerUIBundle, SwaggerUIStandalonePreset } from "swagger-ui-dist";
// Initialize Swagger UI
SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: "#swagger-ui",
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});The swagger-ui-dist package is built around several key components:
Core SwaggerUI constructor for creating interactive OpenAPI documentation interfaces. Supports full customization with plugins, presets, and configuration options. Also provides static properties for accessing built-in presets and plugins.
function SwaggerUIBundle(options: SwaggerUIOptions): SwaggerUISystem;
// Static properties
SwaggerUIBundle.presets: {
apis: PresetApisFunction;
base: PresetBaseFunction;
};
SwaggerUIBundle.plugins: {
Auth: AuthPlugin;
Configs: ConfigsPlugin;
DeepLining: DeepLinkingPlugin;
Err: ErrPlugin;
Filter: FilterPlugin;
Icons: IconsPlugin;
JSONSchema5: JSONSchema5Plugin;
JSONSchema5Samples: JSONSchema5SamplesPlugin;
JSONSchema202012: JSONSchema202012Plugin;
JSONSchema202012Samples: JSONSchema202012SamplesPlugin;
Layout: LayoutPlugin;
Logs: LogsPlugin;
OpenAPI30: OpenAPI30Plugin;
OpenAPI31: OpenAPI31Plugin;
OnComplete: OnCompletePlugin;
RequestSnippets: RequestSnippetsPlugin;
Spec: SpecPlugin;
SwaggerClient: SwaggerClientPlugin;
Util: UtilPlugin;
View: ViewPlugin;
ViewLegacy: ViewLegacyPlugin;
DownloadUrl: DownloadUrlPlugin;
SyntaxHighlighting: SyntaxHighlightingPlugin;
Versions: VersionsPlugin;
SafeRender: SafeRenderPlugin;
};
interface SwaggerUIOptions {
url?: string;
urls?: Array<{ url: string; name: string }>;
spec?: object;
dom_id?: string;
domNode?: Element;
configUrl?: string;
deepLinking?: boolean;
displayOperationId?: boolean;
defaultModelsExpandDepth?: number;
defaultModelExpandDepth?: number;
defaultModelRendering?: 'example' | 'model';
displayRequestDuration?: boolean;
docExpansion?: 'list' | 'full' | 'none';
filter?: boolean | string;
maxDisplayedTags?: number;
showExtensions?: boolean;
showCommonExtensions?: boolean;
useUnsafeMarkdown?: boolean;
onComplete?: () => void;
syntaxHighlight?: {
activated?: boolean;
theme?: string;
};
tryItOutEnabled?: boolean;
requestSnippetsEnabled?: boolean;
requestSnippets?: {
generators?: object;
defaultExpanded?: boolean;
languages?: string[];
};
supportedSubmitMethods?: string[];
validatorUrl?: string | null;
withCredentials?: boolean;
modelPropertyMacro?: () => void;
parameterMacro?: () => void;
presets?: Array<any>;
plugins?: Array<any>;
layout?: string;
persistAuthorization?: boolean;
}
interface SwaggerUISystem {
render: (domNode: Element | string, component: string) => void;
specActions: {
updateUrl: (url: string) => void;
updateLoadingStatus: (status: string) => void;
updateSpec: (spec: string) => void;
download: (url: string) => void;
};
configsActions: {
loaded: () => void;
};
}Minimal plugin preset for standalone Swagger UI implementations, providing essential plugins without additional dependencies.
const SwaggerUIStandalonePreset: Array<SwaggerUIPlugin>;
interface SwaggerUIPlugin {
// Plugin implementation details
}Returns the absolute filesystem path to the swagger-ui-dist directory for serving static assets.
/**
* Returns the absolute filesystem path to the swagger-ui-dist directory
* @returns Absolute path string for static file serving
* @throws Error when called outside Node.js environment
*/
function getAbsoluteFSPath(): string;
/**
* Legacy alias for getAbsoluteFSPath - identical functionality
* @returns Absolute path string for static file serving
* @throws Error when called outside Node.js environment
*/
function absolutePath(): string;The package includes all necessary static assets in the dist folder:
swagger-ui.css - Complete styling for Swagger UI interfaceswagger-ui.css.map - Source map for CSS debuggingindex.css - Additional index-specific stylesswagger-ui-bundle.js - Main UMD bundle with all functionalityswagger-ui-bundle.js.map - Source map for main bundleswagger-ui-standalone-preset.js - Standalone preset bundleswagger-ui-standalone-preset.js.map - Source map for presetswagger-ui.js - Core UI bundleswagger-ui.js.map - Source map for core bundleswagger-ui-es-bundle.js - ES module bundleswagger-ui-es-bundle.js.map - Source map for ES bundleswagger-ui-es-bundle-core.js - ES module core bundleswagger-ui-es-bundle-core.js.map - Source map for ES coreindex.html - Default Swagger UI HTML pageoauth2-redirect.html - OAuth2 redirect handlerswagger-initializer.js - Default initialization scriptfavicon-16x16.png - 16x16 faviconfavicon-32x32.png - 32x32 faviconThe package implements graceful error handling:
index.js are wrapped in try-catch blocks to prevent module loading failures in browser environments where the bundles may not be availablegetAbsoluteFSPath throws an explicit Error with message "getAbsoluteFSPath can only be called within a Nodejs environment" when called outside Node.js environmentdom_id nor domNode options are specifiedconfigUrl optionOptional analytics via Scarf.sh:
scarfSettings.enabled = false in package.jsonSCARF_ANALYTICS=false environment variable// Note: This package provides JavaScript without built-in TypeScript definitions
// Types shown here are for documentation purposes
interface SwaggerUIConfig {
url?: string;
spec?: object;
dom_id?: string;
domNode?: Element;
presets?: Array<any>;
plugins?: Array<any>;
layout?: string;
// ... other configuration options as shown in SwaggerUIOptions above
}
interface StaticAssets {
css: string[];
js: string[];
html: string[];
images: string[];
}
// Plugin and preset type definitions
type PresetApisFunction = () => Array<SwaggerUIPlugin>;
type PresetBaseFunction = () => Array<SwaggerUIPlugin>;
interface SwaggerUIPlugin {
// Plugin objects contain functions and configuration
// Specific structure varies by plugin type
}
// Plugin types available through SwaggerUIBundle.plugins
interface AuthPlugin extends SwaggerUIPlugin {}
interface ConfigsPlugin extends SwaggerUIPlugin {}
interface DeepLinkingPlugin extends SwaggerUIPlugin {}
interface ErrPlugin extends SwaggerUIPlugin {}
interface FilterPlugin extends SwaggerUIPlugin {}
interface IconsPlugin extends SwaggerUIPlugin {}
interface JSONSchema5Plugin extends SwaggerUIPlugin {}
interface JSONSchema5SamplesPlugin extends SwaggerUIPlugin {}
interface JSONSchema202012Plugin extends SwaggerUIPlugin {}
interface JSONSchema202012SamplesPlugin extends SwaggerUIPlugin {}
interface LayoutPlugin extends SwaggerUIPlugin {}
interface LogsPlugin extends SwaggerUIPlugin {}
interface OpenAPI30Plugin extends SwaggerUIPlugin {}
interface OpenAPI31Plugin extends SwaggerUIPlugin {}
interface OnCompletePlugin extends SwaggerUIPlugin {}
interface RequestSnippetsPlugin extends SwaggerUIPlugin {}
interface SpecPlugin extends SwaggerUIPlugin {}
interface SwaggerClientPlugin extends SwaggerUIPlugin {}
interface UtilPlugin extends SwaggerUIPlugin {}
interface ViewPlugin extends SwaggerUIPlugin {}
interface ViewLegacyPlugin extends SwaggerUIPlugin {}
interface DownloadUrlPlugin extends SwaggerUIPlugin {}
interface SyntaxHighlightingPlugin extends SwaggerUIPlugin {}
interface VersionsPlugin extends SwaggerUIPlugin {}
interface SafeRenderPlugin extends SwaggerUIPlugin {}