-
Notifications
You must be signed in to change notification settings - Fork 45
/
function.ts
63 lines (56 loc) · 2.19 KB
/
function.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { FunctionDeclarationStructure, StructureKind } from 'ts-morph';
import { caps } from '@sap-cloud-sdk/util';
import { VdmFunctionImport, VdmServiceMetadata } from '../vdm-types';
import { getRequestBuilderArgumentsBase } from './request-builder-arguments';
const parameterName = 'parameters';
export function functionImportFunction(
functionImport: VdmFunctionImport,
service: VdmServiceMetadata
): FunctionDeclarationStructure {
return {
kind: StructureKind.Function,
name: functionImport.name,
isExported: true,
parameters: [
{
name: parameterName,
type: functionImport.parametersTypeName
}
],
returnType: `FunctionImportRequestBuilder${caps(service.oDataVersion)}<${
functionImport.parametersTypeName
}, ${functionImport.returnType.returnType}${
functionImport.returnType.isCollection ? '[]' : ''
}>`,
statements: getFunctionImportStatements(functionImport, service),
docs: [
[
`${functionImport.description}\n`,
'@param parameters - Object containing all parameters for the function import.',
'@returns A request builder that allows to overwrite some of the values and execute the resultng request.'
].join('\n')
]
};
}
function getFunctionImportStatements(
functionImport: VdmFunctionImport,
service: VdmServiceMetadata
): string {
const context = functionImport.parameters
? functionImport.parameters.reduce((cumulator, currentParameters) => {
if (cumulator !== 'const params = {\n') {
cumulator += ',\n';
}
cumulator += `${currentParameters.parameterName}: new FunctionImportParameter('${currentParameters.originalName}', '${currentParameters.edmType}', ${parameterName}.${currentParameters.parameterName})`;
return cumulator;
}, 'const params = {\n') + '\n}'
: '{}';
let parameters = getRequestBuilderArgumentsBase(functionImport, service);
if (service.oDataVersion === 'v2') {
parameters = [`'${functionImport.httpMethod}'`, ...parameters];
}
const returnStatement = `return new FunctionImportRequestBuilder${caps(
service.oDataVersion
)}(${parameters.join(', ')});`;
return context + '\n\n' + returnStatement;
}