@azure-tools/typespec-powershell
Version:
An experimental TypeSpec emitter for PowerShell codegen
79 lines (73 loc) • 2.29 kB
text/typescript
import {
SdkClient,
SdkContext,
listOperationGroups,
listOperationsInOperationGroup
} from "@azure-tools/typespec-client-generator-core";
import { ignoreDiagnostics, Model, Program, Type } from "@typespec/compiler";
import { getHttpOperation, HttpOperation } 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: Program,
entity: Type
): string | undefined {
return program.stateMap(pageableOperationsKey).get(entity);
}
export function extractPageDetailFromCore(
program: Program,
client: SdkClient,
dpgContext: SdkContext
) {
if (!hasPagingOperations(program, client, dpgContext)) {
return;
}
const nextLinks = new Set<string>();
const itemNames = new Set<string>();
// 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: HttpOperation) {
for (const response of route.responses) {
const paged = extractPagedMetadataNested(program, response.type as Model);
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
}
};
}