import { CodeModel , Operation } from "@autorest/codemodel" ; import { OptionalKind , MethodSignatureStructure } from "ts-morph" ; import { NameType , normalizeName , ReservedName } from "../utils/nameUtils" ; import { buildMethodDefinitions } from "./helpers/operationHelpers" ; import { PathParameter , Paths } from "./interfaces" ; const REST_CLIENT_RESERVED = [ { name : "path" , reservedFor : [ NameType . Property ] } , { name : "pathUnchecked" , reservedFor : [ NameType . Property ] } , { name : "pipeline" , reservedFor : [ NameType . Property ] } ] ; export function generateMethodShortcuts ( model , paths ) { const operationGroups = model . operationGroups ; let keys = { } ; for ( const group of operationGroups ) { const groupName = normalizeName ( group . language . default . name , NameType . Property , true , REST_CLIENT_RESERVED ) ; keys [ groupName ] = buildOperationDefinitions ( group . operations , paths ) ; } return keys ; } export function generateMethodShortcutImplementation ( model , paths ) { const operationGroups = model . operationGroups ; let keys = { } ; for ( const group of operationGroups ) { const groupName = normalizeName ( group . language . default . name , NameType . Property , true , REST_CLIENT_RESERVED ) ; keys [ groupName ] = buildOperationDeclarations ( group . operations , paths ) ; } return keys ; } function buildOperationDeclarations ( operations , paths ) { let ops = [ ] ; for ( const operation of operations ) { const name = normalizeName ( operation . language . default . name , NameType . Property , true ) ; const path = operation . requests ! [ 0 ] . protocol . http ?. path ; const method = operation . requests ! [ 0 ] . protocol . http ?. method ; const pathParams = paths [ path ] ?. pathParameters ; const methodDefinitions = generateOperationDeclaration ( path , name , method , pathParams ) ; ops = [ ... ops , methodDefinitions ] ; } return ops ; } function generateOperationDeclaration ( path , operationName , method , pathParams = [ ] ) { const pathParamNames = `${ pathParams . length > 0 ? `${ pathParams . map ( p => p . name ) },` : "" }` ; return `"${ operationName }": (${ pathParamNames } options) => {
    return client.path("${ path }", ${ pathParamNames }).${ method }(options);
  }` ; } function buildOperationDefinitions ( operations , paths ) { let ops = [ ] ; for ( const operation of operations ) { const name = normalizeName ( operation . language . default . name , NameType . Property , true ) ; const path = operation . requests ! [ 0 ] . protocol . http ?. path ; const method = operation . requests ! [ 0 ] . protocol . http ?. method ; const methods = paths [ path ] ?. methods [ method ] ; const pathParams = paths [ path ] ?. pathParameters ; const methodDefinitions = buildMethodDefinitions ( { [ name ] : methods } , pathParams ) ; ops = [ ... ops , ... methodDefinitions ] ; } return ops ; }