@azure-tools/typespec-powershell
Version:
An experimental TypeSpec emitter for PowerShell codegen
56 lines • 2.22 kB
JavaScript
import { ignoreDiagnostics } from "@typespec/compiler";
import { getHttpOperation } from "@typespec/http";
import { hasPagingOperations, extractPagedMetadataNested, parseNextLinkName, parseItemName } from "../utils/operationUtil.js";
import { listOperations } from "./clientUtils.js";
const pageableOperationsKey = Symbol("pageable");
export function getPageable(program, entity) {
return program.stateMap(pageableOperationsKey).get(entity);
}
export function extractPageDetailFromCore(program, client, dpgContext) {
if (!hasPagingOperations(program, client, dpgContext)) {
return;
}
const nextLinks = new Set();
const itemNames = new Set();
// Add default values
nextLinks.add("nextLink");
itemNames.add("value");
const clientOperations = listOperations(client);
for (const clientOp of clientOperations) {
const route = ignoreDiagnostics(getHttpOperation(program, clientOp));
// ignore overload base operation
if (route.overloads && route.overloads?.length > 0) {
continue;
}
extractPageDetailFromCoreForRoute(route);
}
function extractPageDetailFromCoreForRoute(route) {
for (const response of route.responses) {
const paged = extractPagedMetadataNested(program, response.type);
if (paged) {
const nextLinkName = parseNextLinkName(paged);
if (nextLinkName) {
nextLinks.add(nextLinkName);
}
const itemName = parseItemName(paged);
if (itemName) {
itemNames.add(itemName);
}
// Once we find paged metadata, we don't need to processs any further.
continue;
}
}
}
// If there are more than one options for nextLink and item names we need to generate a
// more complex pagination helper.
const isComplexPaging = nextLinks.size > 1 || itemNames.size > 1;
return {
hasPaging: true,
pageDetails: {
itemNames: [...itemNames],
nextLinkNames: [...nextLinks],
isComplexPaging
}
};
}
//# sourceMappingURL=pageUtils.js.map