@autorest/openapi-to-typespec
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
98 lines • 4.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getResourceDataSchema = getResourceDataSchema;
exports.setResourceDataSchema = setResourceDataSchema;
exports.populateSingletonRequestPath = populateSingletonRequestPath;
exports.findOperation = findOperation;
const codemodel_1 = require("@autorest/codemodel");
const schemas_1 = require("../utils/schemas");
const constants_1 = require("./constants");
const resource_equivalent_1 = require("./resource-equivalent");
const resourceDataSchemaCache = new WeakMap();
function getResourceDataSchema(set) {
if (resourceDataSchemaCache.has(set))
return resourceDataSchemaCache.get(set);
// Check if the request path has even number of segments after the providers segment
if (!checkEvenSegments(set.RequestPath)) {
resourceDataSchemaCache.set(set, undefined);
return undefined;
}
// before we are finding any operations, we need to ensure this operation set has a GET request.
if (findOperation(set, codemodel_1.HttpMethod.Get) === undefined) {
resourceDataSchemaCache.set(set, undefined);
return undefined;
}
// try put operation to get the resource name
let resourceSchemaName = getResourceSchemaName(set, codemodel_1.HttpMethod.Put);
if (resourceSchemaName !== undefined) {
resourceDataSchemaCache.set(set, resourceSchemaName);
return resourceSchemaName;
}
// try get operation to get the resource name
resourceSchemaName = getResourceSchemaName(set, codemodel_1.HttpMethod.Get);
if (resourceSchemaName !== undefined) {
resourceDataSchemaCache.set(set, resourceSchemaName);
return resourceSchemaName;
}
// We tried everything, this is not a resource
resourceDataSchemaCache.set(set, undefined);
return undefined;
}
function setResourceDataSchema(set, resourceDataName) {
resourceDataSchemaCache.set(set, resourceDataName);
}
function checkEvenSegments(path) {
const index = path.lastIndexOf(constants_1.ProvidersSegment);
// this request path does not have providers segment - it can be a "ById" request, skip to next criteria
if (index < 0)
return true;
// get whatever following the providers
const following = path.substring(index);
const segments = following.split("/").filter((s) => s.length > 0);
return segments.length % 2 === 0;
}
function populateSingletonRequestPath(set) {
var _a;
const updatedSegments = [];
const segments = set.RequestPath.split("/");
for (const segment of segments) {
if (segment.match(/^\{\w+\}$/) === null) {
updatedSegments.push(segment);
}
else {
const keyName = segment.replace(/^\{(\w+)\}$/, "$1");
const resourceKeyParameter = (_a = set.Operations[0].parameters) === null || _a === void 0 ? void 0 : _a.find((p) => p.language.default.name === keyName || p.language.default.serializedName === keyName);
if (resourceKeyParameter === undefined)
throw `Cannot find parameter ${keyName} in operation ${set.Operations[0].operationId}`;
if (!(0, schemas_1.isConstantSchema)(resourceKeyParameter.schema)) {
updatedSegments.push(segment);
}
else {
const value = resourceKeyParameter.schema.value.value;
updatedSegments.push(value);
}
}
}
set.SingletonRequestPath = updatedSegments.join("/");
}
function findOperation(set, method) {
return set.Operations.find((o) => { var _a; return ((_a = o.requests[0].protocol.http) === null || _a === void 0 ? void 0 : _a.method) === method; });
}
function getResourceSchemaName(set, method) {
var _a;
const operation = findOperation(set, method);
if (operation === undefined)
return undefined;
// find the response with code 200
const response = (_a = operation.responses) === null || _a === void 0 ? void 0 : _a.find((r) => { var _a; return (_a = r.protocol.http) === null || _a === void 0 ? void 0 : _a.statusCodes.includes("200"); });
if (response === undefined)
return undefined;
// find the response schema
if (!(0, schemas_1.isResponseSchema)(response) || !(0, codemodel_1.isObjectSchema)(response.schema))
return undefined;
// we need to verify this schema has ID, type and name so that this is a resource model
if (!(0, resource_equivalent_1.isResource)(response.schema))
return undefined;
return response.schema.language.default.name;
}
//# sourceMappingURL=operation-set.js.map