@autorest/openapi-to-typespec
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
126 lines • 5.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRoutesImports = exports.getClientImports = exports.getModelsImports = exports.getMainImports = void 0;
const options_1 = require("../options");
function getMainImports(program) {
var _a;
const modules = [];
const namespaces = [];
if ((_a = program.serviceInformation.authentication) === null || _a === void 0 ? void 0 : _a.some((a) => a.kind === "AadOauth2Auth")) {
modules.push(`import "@azure-tools/typespec-azure-core";`);
namespaces.push("using Azure.Core;");
}
return {
modules,
namespaces,
};
}
exports.getMainImports = getMainImports;
function getModelsImports(program) {
var _a, _b, _c, _d, _e;
const modules = new Set();
const namespaces = new Set();
for (const choice of program.models.enums) {
for (const decorator of (_a = choice.decorators) !== null && _a !== void 0 ? _a : []) {
decorator.module && modules.add(`import "${decorator.module}";`);
decorator.namespace && namespaces.add(`using ${decorator.namespace};`);
}
}
for (const model of program.models.objects) {
((_b = model.alias) === null || _b === void 0 ? void 0 : _b.module) && modules.add(`import "${model.alias.module}";`);
for (const decorator of (_c = model.decorators) !== null && _c !== void 0 ? _c : []) {
decorator.module && modules.add(`import "${decorator.module}";`);
decorator.namespace && namespaces.add(`using ${decorator.namespace};`);
}
for (const property of (_d = model.properties) !== null && _d !== void 0 ? _d : []) {
for (const decorator of (_e = property.decorators) !== null && _e !== void 0 ? _e : []) {
decorator.module && modules.add(`import "${decorator.module}";`);
decorator.namespace && namespaces.add(`using ${decorator.namespace};`);
}
}
}
const { isArm } = (0, options_1.getOptions)();
if (isArm) {
modules.add(`import "@azure-tools/typespec-azure-resource-manager";`);
namespaces.add("using Azure.ResourceManager;");
namespaces.add("using Azure.ResourceManager.Foundations;");
}
return {
modules: [...modules],
namespaces: [...namespaces],
};
}
exports.getModelsImports = getModelsImports;
function getClientImports(program) {
var _a;
const modules = new Set();
const namespaces = new Set();
const addImports = (decs) => {
for (const dec of decs !== null && decs !== void 0 ? decs : []) {
dec.module && modules.add(`import "${dec.module}";`);
dec.namespace && namespaces.add(`using ${dec.namespace};`);
}
};
for (const model of program.models.objects) {
addImports(model.clientDecorators);
for (const property of (_a = model.properties) !== null && _a !== void 0 ? _a : []) {
addImports(property.clientDecorators);
}
}
for (const model of program.models.enums) {
addImports(model.clientDecorators);
for (const choice of model.members) {
addImports(choice.clientDecorators);
}
}
for (const resource of program.models.armResources) {
addImports(resource.clientDecorators);
for (const property of resource.properties) {
addImports(property.clientDecorators);
}
for (const op of resource.resourceOperationGroups.flatMap((g) => g.resourceOperations)) {
addImports(op.clientDecorators);
}
addImports(resource.propertiesPropertyClientDecorator);
}
return {
modules: [...modules],
namespaces: [...namespaces],
};
}
exports.getClientImports = getClientImports;
function getRoutesImports(_program) {
var _a, _b;
const modules = new Set();
const namespaces = new Set();
modules.add(`import "@azure-tools/typespec-azure-core";`);
modules.add(`import "@typespec/rest";`);
modules.add(`import "./models.tsp";`);
namespaces.add(`using TypeSpec.Rest;`);
namespaces.add(`using TypeSpec.Http;`);
const { isArm } = (0, options_1.getOptions)();
if (isArm) {
modules.add(`import "@azure-tools/typespec-azure-resource-manager";`);
namespaces.add("using Azure.ResourceManager;");
}
for (const og of _program.operationGroups) {
for (const operation of og.operations) {
for (const decorator of (_a = operation.decorators) !== null && _a !== void 0 ? _a : []) {
decorator.module && modules.add(`import "${decorator.module}";`);
decorator.namespace && namespaces.add(`using ${decorator.namespace};`);
}
for (const param of operation.parameters) {
for (const decorator of (_b = param.decorators) !== null && _b !== void 0 ? _b : []) {
decorator.module && modules.add(`import "${decorator.module}";`);
decorator.namespace && namespaces.add(`using ${decorator.namespace};`);
}
}
}
}
return {
modules: [...modules],
namespaces: [...namespaces],
};
}
exports.getRoutesImports = getRoutesImports;
//# sourceMappingURL=imports.js.map
;