UNPKG

@autorest/openapi-to-typespec

Version:

Autorest plugin to scaffold a Typespec definition from an OpenAPI document

100 lines 4.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateServiceInformation = void 0; const constants_1 = require("../constants"); const options_1 = require("../options"); const docs_1 = require("../utils/docs"); const namespace_1 = require("../utils/namespace"); function generateServiceInformation(program) { var _a, _b; const { serviceInformation } = program; const definitions = []; const { isArm } = (0, options_1.getOptions)(); if (isArm) { definitions.push(`@armProviderNamespace`); } generateUseAuth(serviceInformation.authentication, definitions); definitions.push(`@service(#{ title: "${serviceInformation.name}" })`); if (serviceInformation.versions) { definitions.push(`@versioned(Versions)`); } if (isArm && serviceInformation.armCommonTypeVersion) { if (serviceInformation.armCommonTypeVersion !== serviceInformation.userSetArmCommonTypeVersion) { definitions.push(`// FIXME: ${serviceInformation.userSetArmCommonTypeVersion ? `Common type version ${serviceInformation.userSetArmCommonTypeVersion} is not supported for now.` : "Common type version not set."} Set to v3.`); } definitions.push(`@armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.${serviceInformation.armCommonTypeVersion})`); } if (!isArm && serviceInformation.endpoint) { definitions.push(`@server("${serviceInformation.endpoint}", ${(_a = JSON.stringify(serviceInformation.doc)) !== null && _a !== void 0 ? _a : ""}`); const parametrizedHost = getEndpointParameters(serviceInformation.endpoint); const hasParameters = (serviceInformation.endpointParameters && serviceInformation.endpointParameters.length) || parametrizedHost.length; const allParams = [ ...((_b = serviceInformation.endpointParameters) !== null && _b !== void 0 ? _b : []).filter((p) => !parametrizedHost.some((e) => e.name === p.name)), ...parametrizedHost, ]; if (hasParameters) { definitions.push(", {"); for (const param of allParams !== null && allParams !== void 0 ? allParams : []) { const doc = (0, docs_1.generateDocs)(param); doc && definitions.push(doc); definitions.push(`${param.name}: ${param.name.startsWith(constants_1.ApiVersion) ? "Versions" : "string"} `); } } hasParameters && definitions.push("}"); definitions.push(")"); } const serviceDoc = (0, docs_1.generateDocs)(serviceInformation); serviceDoc && definitions.push(serviceDoc); definitions.push((0, namespace_1.getNamespaceStatement)(program)); if (serviceInformation.versions) { definitions.push(""); definitions.push(`/**\n* The available API versions.\n*/`); definitions.push(`enum Versions {`); for (const version of serviceInformation.versions) { if (isArm) { definitions.push(`@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)`); } definitions.push(`@useDependency(Azure.Core.Versions.v1_0_Preview_1)`); definitions.push(`/**\n* The ${version} API version.\n*/`); definitions.push(`${version.startsWith("v") ? "" : "v"}${version.replaceAll("-", "_").replaceAll(".", "_")}: "${version}",`); } definitions.push("}"); } return definitions.join("\n"); } exports.generateServiceInformation = generateServiceInformation; function getEndpointParameters(endpoint) { const regex = /{([^{}]+)}/g; const params = []; let match; while ((match = regex.exec(endpoint)) !== null) { params.push({ name: match[1] }); } return params; } function generateUseAuth(authDefinitions, statements) { if (!authDefinitions) { return; } const authFlows = []; for (const auth of authDefinitions) { if (auth.kind === "AadOauth2Auth") { const scopes = `[${auth.scopes.map((s) => `"${s}"`).join()}]`; authFlows.push(`AadOauth2Auth<${scopes}>`); } if (auth.kind === "ApiKeyAuth") { authFlows.push(`ApiKeyAuth<ApiKeyLocation.${auth.location}, "${auth.name}">`); } } if (!authFlows.length) { return; } statements.push(`@useAuth(${authFlows.join(" | ")})`); return; } //# sourceMappingURL=generate-service-information.js.map