DEV Community

Vinit Shahdeo
Vinit Shahdeo

Posted on

How to resolve server URLs containing variables in OpenAPI 3.x definitions?

The OpenAPI specification is widely used to define RESTful APIs. One of the critical components of an OpenAPI specification is the server section, which provides a list of URLs where the API can be accessed. However, working with the server section can be complicated, especially when the server URLs contain variables and other dynamic components. That's where openapi-url-resolver comes in.

Below is a server object example,

{
  "servers": [
    {
      "url": "https://{username}.gigantic-server.com:{port}/{basePath}",
      "description": "The production API server",
      "variables": {
        "username": {
          "default": "demo",
          "description": "this value is assigned by the service provider, in this example `gigantic-server.com`"
        },
        "port": {
          "enum": [
            "8443",
            "443"
          ],
          "default": "8443"
        },
        "basePath": {
          "default": "v2"
        }
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Resolve server URLs or hosts from an OpenAPI 3.x definitions

openapi-url-resolver is a lightweight NPM package that simplifies the process of resolving server URLs from OpenAPI specifications. With openapi-url-resolver, you can easily extract server information and remove protocols from resolved URLs, making it easier to work with APIs that conform to the OpenAPI specification.

npm openapi-url-resolver

Here are some of the key features of openapi-url-resolver:

  • 📦 Lightweight module with only 965 bytes in size
  • 🚀 Zero dependencies, making it easy to install and use in your projects
  • 🎯 Efficient and simple way to resolve URLs from OpenAPI specifications

You can install openapi-url-resolver via NPM:

npm install openapi-url-resolver
Enter fullscreen mode Exit fullscreen mode

Using openapi-url-resolver is simple. All you need to do is pass an OpenAPI 3.x specification object to the resolve() function, and it will return an array of resolved server URLs. You can also pass a second parameter to the resolve() function to get the server URLs with protocols.

Here's an example of how to use openapi-url-resolver:

/**
* Resolve server URL template with variables for an OpenAPI 3.x definition
*
* GitHub Repo: https://github.com/vinitshahdeo/openapi-url-resolver
* NPM Package: https://www.npmjs.com/package/openapi-url-resolver
*/
const openapiUrlResolver = require('openapi-url-resolver');
const spec = {
openapi: '3.0.0',
servers: [
{
url: 'https://{username}.gigantic-server.com:{port}/{basePath}',
description: 'The production API server',
variables: {
username: {
default: 'demo',
description: 'this value is assigned by the service provider, in this example `gigantic-server.com`'
},
port: {
enum: ['8443', '443'],
default: '8443'
},
basePath: {
default: 'v2'
}
}
}
]
};
const hosts = openapiUrlResolver.resolve(spec);
/*
[
'demo.gigantic-server.com:8443/v2',
'demo.gigantic-server.com:443/v2'
]
*/
console.log(hosts);
// Pass false as second parameter to get the server URLs with protocols.
const serverUrls = openapiUrlResolver.resolve(spec, false)
/*
[
'https://demo.gigantic-server.com:8443/v2',
'https://demo.gigantic-server.com:443/v2'
]
*/
console.log(serverUrls);

It's a great tool for developers who need to extract server information from OpenAPI specifications. It's lightweight, easy to use, and doesn't have any dependencies, making it an excellent addition to any project that uses OpenAPI.

If you're interested in learning more about openapi-url-resolver, check out the GitHub repository and give it a try in your projects!

Thank you.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay