@autorest/openapi-to-cadl
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
93 lines • 4.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformBaseUrl = exports.transformEndpointParameters = exports.transformServiceInformation = void 0;
const codemodel_1 = require("@autorest/codemodel");
const get_endpoint_1 = require("../utils/get-endpoint");
const schemas_1 = require("../utils/schemas");
function transformServiceInformation(model) {
var _a;
return {
name: model.info.title,
doc: (_a = model.info.description) !== null && _a !== void 0 ? _a : "// FIXME: (miissing-service-description) Add service description",
endpoint: (0, get_endpoint_1.getFirstEndpoint)(model),
endpointParameters: transformEndpointParameters(model),
version: getApiVersion(model),
};
}
exports.transformServiceInformation = transformServiceInformation;
function transformEndpointParameters(model) {
var _a;
const globalParameters = ((_a = model.globalParameters) !== null && _a !== void 0 ? _a : []).filter((p) => { var _a, _b; return p.implementation === "Client" && ((_b = (_a = p.protocol) === null || _a === void 0 ? void 0 : _a.http) === null || _b === void 0 ? void 0 : _b.in) === "uri"; });
return globalParameters.map((p) => {
var _a;
return ({
doc: (_a = p.language.default.description) !== null && _a !== void 0 ? _a : "",
name: p.language.default.serializedName,
});
});
}
exports.transformEndpointParameters = transformEndpointParameters;
function getApiVersion(model) {
if (!model.globalParameters || !model.globalParameters.length) {
return undefined;
}
const apiVersionParam = model.globalParameters
.filter((gp) => { var _a; return gp.implementation === codemodel_1.ImplementationLocation.Client && ((_a = gp.protocol.http) === null || _a === void 0 ? void 0 : _a.in) === codemodel_1.ParameterLocation.Query; })
.find((param) => param.language.default.serializedName === "api-version");
if (apiVersionParam && (0, schemas_1.isConstantSchema)(apiVersionParam.schema)) {
return apiVersionParam.schema.value.value.toString();
}
return undefined;
}
function getEndpointParameter(codeModel) {
if (!codeModel.globalParameters || !codeModel.globalParameters.length) {
return [];
}
const urlParameters = codeModel.globalParameters.filter((gp) => { var _a; return gp.implementation === codemodel_1.ImplementationLocation.Client && ((_a = gp.protocol.http) === null || _a === void 0 ? void 0 : _a.in) === codemodel_1.ParameterLocation.Uri; });
// Currently only support one parametrized host
if (!urlParameters.length) {
return [];
}
return urlParameters.map((urlParameter) => {
let value;
if ((0, schemas_1.isConstantSchema)(urlParameter.schema)) {
value = urlParameter.schema.value.value;
}
return {
name: urlParameter.language.default.serializedName,
type: "string",
description: urlParameter.language.default.description,
value,
};
});
}
function transformBaseUrl(codeModel) {
var _a;
let endpoint = "";
let isCustom = false;
const $host = (codeModel.globalParameters || []).find((p) => {
const { name } = p.language.default;
return name === "$host" && Boolean(p.clientDefaultValue);
});
let urlParameters = [];
if (!$host) {
// There are some swaggers that contain no operations for those we'll keep an empty endpoint
if (codeModel.operationGroups && codeModel.operationGroups.length) {
// No support yet for multi-baseUrl yet Issue #553
const { requests } = codeModel.operationGroups[0].operations[0];
urlParameters = getEndpointParameter(codeModel);
isCustom = true;
endpoint = (_a = requests === null || requests === void 0 ? void 0 : requests[0].protocol.http) === null || _a === void 0 ? void 0 : _a.uri;
}
}
else {
endpoint = $host.clientDefaultValue;
}
return {
urlParameters,
endpoint: endpoint,
isCustom,
};
}
exports.transformBaseUrl = transformBaseUrl;
//# sourceMappingURL=transform-service-information.js.map