https://github.com/vinitshahdeo/openapi-url-resolver
Resolve server URLs and extract hosts from OpenAPI 3.x and Swagger specifications.
https://github.com/vinitshahdeo/openapi-url-resolver
openapi openapi-parser openapi-server-url openapi-specification openapi-url-resolver openapi3
Last synced: 4 months ago
JSON representation
Resolve server URLs and extract hosts from OpenAPI 3.x and Swagger specifications.
- Host: GitHub
- URL: https://github.com/vinitshahdeo/openapi-url-resolver
- Owner: vinitshahdeo
- License: mit
- Created: 2023-04-19T19:50:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-27T05:38:07.000Z (over 2 years ago)
- Last Synced: 2025-05-14T19:47:39.461Z (5 months ago)
- Topics: openapi, openapi-parser, openapi-server-url, openapi-specification, openapi-url-resolver, openapi3
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/openapi-url-resolver
- Size: 211 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
**openapi-url-resolver** is a **lightweight** NPM package that provides a simple and efficient way to resolve server URLs from OpenAPI specifications. It also removes protocols from the resolved URLs and allows you to **easily extract host information from OpenAPI definitions**. This package is ideal for developers working with APIs that conform to the [OpenAPI 3.x specification](https://swagger.io/specification/) and need to extract [server information](https://spec.openapis.org/oas/v3.1.0#server-object) to make API calls. Check out the blog post for more detailsβ[How to resolve server URLs containing variables in OpenAPI 3.x definitions?](https://dev.to/vinitshahdeo/how-to-resolve-server-urls-containing-variables-in-openapi-3x-definitions-kaj)
### Features:
- π **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 [server URLs](https://spec.openapis.org/oas/v3.1.0#server-object) from OpenAPI specifications## π¦ Installation
You can install `openapi-url-resolver` via [NPM](https://www.npmjs.com/package/openapi-url-resolver):
```bash
npm install openapi-url-resolver
```[](https://nodei.co/npm/openapi-url-resolver/)
## π» Usage
To use `openapi-url-resolver`, you need to pass an [OpenAPI 3.x specification](https://swagger.io/specification/) object to the `resolve()` function. This function will return an array of resolved server URLs:
```javascript
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.
```javascript
const serverUrls = openapiUrlResolver.resolve(spec, false)/*
[
'https://demo.gigantic-server.com:8443/v2',
'https://demo.gigantic-server.com:443/v2'
]*/
console.log(serverUrls)```
## π§ͺ Testing
You can test using the below command or write your own tests using the OpenAPI specifications [examples](./definitions/).
```bash
npm test
```## π« Limitations
The below are the known limitations, and they are not handled to keep it a **lightweight** and **focused** module to just extract the [server information](https://spec.openapis.org/oas/v3.1.0#server-object).
- It does not validate the OpenAPI definition. You can use [openapi-schema-validator](https://www.npmjs.com/package/openapi-schema-validator) for validating the OpenAPI definition.
- It only works with JSON format.
- Use [yaml-to-json-schema](https://www.npmjs.com/package/yaml-to-json-schema) to convert YAML to JSON.
- Use [postman-to-openapi](https://www.npmjs.com/package/postman-to-openapi) to convert Postman collection to OpenAPI 3.x## π€ Contributing
**Contributions to `openapi-url-resolver` are most welcome!**
If you find a bug or want to suggest a new feature, please [open an issue](https://github.com/vinitshahdeo/openapi-url-resolver/issues/new) on the GitHub repository. If you want to contribute code, please [fork the repository](https://github.com/vinitshahdeo/openapi-url-resolver/fork), make your changes, and **submit a pull request**. Your contributions and feedback are most welcome!
## π License
[openapi-url-resolver](https://www.npmjs.com/package/openapi-url-resolver) is authored by @[vinitshahdeo](https://github.com/vinitshahdeo) and released under the [MIT License](./LICENSE).
## β€οΈ Support
If you find this package useful, please consider [starring this repository](https://github.com/vinitshahdeo/openapi-url-resolver) on GitHub to show your appreciation. You can stay connected with me on Twitterβ[@vinit_shahdeo](https://twitter.com/Vinit_Shahdeo).
Thank you for your support! π