OpenAPI Server URL Templating

1.3.0 · active · verified Sun Apr 19

The `openapi-server-url-templating` library provides a robust, specification-compliant mechanism for parsing, validating, and performing substitutions on server URLs as defined within the OpenAPI Specification. It comprehensively supports server URL templating features, including Server Variables, across various OpenAPI versions from 3.0.0 through 3.1.1. The current stable version is 1.3.0, released in December 2024, indicating an active development and maintenance cadence. A key differentiator is its foundational role in establishing the official ABNF grammar for Server URL Templating within the OpenAPI Specification itself, which ensures a high degree of fidelity and accuracy to the standard. The library leverages `apg-lite` for its underlying parsing and validation capabilities, offering a reliable tool for developers working with OpenAPI definitions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing, validating, and substituting an OpenAPI server URL template using defined server variables and supplied values, including usage of default values.

import { parse, validate, substitute } from 'openapi-server-url-templating';

const urlTemplate = 'https://{username}.gigantic-server.com:{port}/{basePath}';

// 1. Parse the URL template
const parseResult = parse(urlTemplate);
console.log('Parse Success:', parseResult.result.success);

// 2. Define server variables for validation and substitution
const serverVariables = {
  username: { default: 'demo', enum: ['demo', 'admin'] },
  port: { default: '8443', enum: ['8443', '443'] },
  basePath: { default: 'v2', enum: ['v1', 'v2'] }
};

// 3. Validate the URL template with variables
const validationResult = validate(urlTemplate, serverVariables);
console.log('Validation Success:', validationResult.result.success);

// 4. Substitute variables to get the final URL
const values = { username: 'admin', port: '443', basePath: 'v1' };
const substitutedUrl = substitute(urlTemplate, serverVariables, values);
console.log('Substituted URL:', substitutedUrl); // Expected: https://admin.gigantic-server.com:443/v1

// Example with default values
const substitutedUrlWithDefaults = substitute(urlTemplate, serverVariables, {});
console.log('Substituted URL (defaults):', substitutedUrlWithDefaults); // Expected: https://demo.gigantic-server.com:8443/v2

view raw JSON →