@autorest/openapi-to-typespec
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
85 lines • 3.55 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.markPagination = exports.isPageValue = exports.getPageableResponse = exports.isPageableOperation = void 0;
const codemodel_1 = require("@autorest/codemodel");
const metadata_1 = require("./metadata");
const schemas_1 = require("./schemas");
function isPageableOperation(operation) {
const languageMetadata = (0, metadata_1.getLanguageMetadata)(operation.language);
const paginationExtension = languageMetadata.paging;
return Boolean(paginationExtension);
}
exports.isPageableOperation = isPageableOperation;
function getPageableResponse(operation) {
var _a;
if (!isPageableOperation(operation)) {
return undefined;
}
for (const response of (_a = operation.responses) !== null && _a !== void 0 ? _a : []) {
if (!(0, schemas_1.isResponseSchema)(response)) {
continue;
}
if (!(0, codemodel_1.isObjectSchema)(response.schema)) {
continue;
}
if (!response.schema.language.default.paging.isPageable) {
continue;
}
return response;
}
return undefined;
}
exports.getPageableResponse = getPageableResponse;
function isPageValue(property) {
var _a;
return Boolean((_a = property.language.default.paging) === null || _a === void 0 ? void 0 : _a.isValue);
}
exports.isPageValue = isPageValue;
function markPagination(codeModel) {
var _a, _b;
for (const operationGroup of codeModel.operationGroups) {
for (const operation of operationGroup.operations) {
const languageMetadata = (0, metadata_1.getLanguageMetadata)(operation.language);
const paginationExtension = languageMetadata.paging;
if (!isPageableOperation(operation)) {
continue;
}
const itemName = paginationExtension.itemName || "value";
let nextLinkName = "nextLink";
if (typeof paginationExtension.nextLinkName === "string") {
nextLinkName = paginationExtension.nextLinkName;
}
else if (typeof paginationExtension.nextLinkName === "object") {
nextLinkName = null;
}
for (const response of (_a = operation.responses) !== null && _a !== void 0 ? _a : []) {
if (!(0, schemas_1.isResponseSchema)(response)) {
continue;
}
if (!(0, codemodel_1.isObjectSchema)(response.schema)) {
continue;
}
response.schema.language.default.paging = {
...response.schema.language.default.paging,
isPageable: true,
};
for (const property of (_b = response.schema.properties) !== null && _b !== void 0 ? _b : []) {
if (property.serializedName === nextLinkName) {
property.language.default.paging = {
...property.language.default.paging,
isNextLink: true,
};
}
if (property.serializedName === itemName) {
property.language.default.paging = {
...property.language.default.paging,
isValue: true,
};
}
}
}
}
}
}
exports.markPagination = markPagination;
//# sourceMappingURL=paging.js.map
;