UNPKG

api

Version:

Magical SDK generation from an OpenAPI definition 🪄

48 lines (47 loc) • 1.57 kB
"use strict"; exports.__esModule = true; function stripTrailingSlash(url) { if (url[url.length - 1] === '/') { return url.slice(0, -1); } return url; } /** * With an SDK server config and an instance of OAS we should extract and prepare the server and * any server variables to be supplied to `@readme/oas-to-har`. * */ function prepareServer(spec, url, variables) { if (variables === void 0) { variables = {}; } var serverIdx; var sanitizedUrl = stripTrailingSlash(url); (spec.api.servers || []).forEach(function (server, i) { if (server.url === sanitizedUrl) { serverIdx = i; } }); // If we were able to find the passed in server in the OAS servers, we should use that! If we // couldn't and server variables were passed in we should try our best to handle that, otherwise // we should ignore the passed in server and use whever the default from the OAS is. if (serverIdx) { return { selected: serverIdx, variables: variables }; } else if (Object.keys(variables).length) { // @todo we should run `oas.replaceUrl(url)` and pass that unto `@readme/oas-to-har` } else { var server = spec.splitVariables(url); if (server) { return { selected: server.selected, variables: server.variables }; } // @todo we should pass `url` directly into `@readme/oas-to-har` as the base URL } return false; } exports["default"] = prepareServer;