import { ImplementationLocation , Operation , Parameter , SchemaContext } from "@autorest/codemodel" ; import { OptionalKind , MethodSignatureStructure , ParameterDeclarationStructure } from "ts-morph" ; import { Methods , PathParameter } from "../interfaces" ; import { getElementType } from "../schemaHelpers" ; /**
 * Given an operation, extract all its parameters
 */ export function getOperationParameters ( operation , requestIndex = 0 ) { const operationParams = [ ... ( operation . parameters ?? [ ] ) , ... ( operation . signatureParameters ?? [ ] ) ] ; const distinctParams = new Set ( operationParams ) ; if ( operation . requests ) { [ ... ( operation . requests [ requestIndex ] . parameters ?? [ ] ) , ... ( operation . requests [ requestIndex ] . signatureParameters ?? [ ] ) ] . forEach ( p => distinctParams . add ( p ) ) ; } return [ ... distinctParams ] . filter ( filterMethodNotSynthetic ) ; } function filterMethodNotSynthetic ( parameter ) { return ( ! parameter . origin && parameter . implementation === ImplementationLocation . Method ) ; } export function buildMethodDefinitions ( methods , pathParams = [ ] ) { const methodDefinitions = [ ] ; for ( const key of Object . keys ( methods ) ) { const method = methods [ key ] ; const description = methods [ key ] [ 0 ] . description ; let areAllOptional = ! method . some ( m => ! m . hasOptionalOptions ) ; methodDefinitions . push ( { name : key , ... ( description && { docs : [ { description } ] } ) , parameters : [ ... getPathParamDefinitions ( pathParams ) , { name : "options" , hasQuestionToken : areAllOptional , type : method . map ( m => m . optionsName ) . join ( " | " ) } ] , returnType : method . map ( m => m . returnType ) . join ( " | " ) } ) ; } return methodDefinitions ; } export function getPathParamDefinitions ( pathParams ) { return pathParams . map ( p => { return { name : p . name , type : getElementType ( p . schema , [ SchemaContext . Input , SchemaContext . Exception ] ) , description : p . description } ; } ) ; }