-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathoperation-request-builder-class.ts
108 lines (102 loc) · 3.12 KB
/
operation-request-builder-class.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
ClassDeclarationStructure,
MethodDeclarationStructure,
OptionalKind,
ParameterDeclarationStructure,
Scope,
StructureKind
} from 'ts-morph';
import { toPascalCase, toPropertyFormat } from '@sap-cloud-sdk/core';
import {
OpenApiOperation,
OpenApiPath,
OpenApiServiceMetadata
} from '../open-api-types';
import {
pathParamToParamStructure,
refNameToParamStructure
} from './api-request-builder-class';
/**
* Used by the generator for generating operation request builder classes.
* @param metadata The service metadata model converted from the open api file.
* @param openApiPath The open api path object. The service metadata contains multiple open api paths.
* @param operation The open api operation object. The open api path contains multiple open api operations.
* @returns class declaration structure of the operation request builder class.
*/
export function operationRequestBuilderClass(
metadata: OpenApiServiceMetadata,
openApiPath: OpenApiPath,
operation: OpenApiOperation
): ClassDeclarationStructure {
return {
kind: StructureKind.Class,
name: `${toPascalCase(operation.operationName)}RequestBuilder`,
methods: method(metadata, openApiPath, operation),
extends: 'RestRequestBuilder',
isExported: true,
ctors: [
{
parameters: [
...addPublicScope(
pathParamToParamStructure(openApiPath.pathParameters)
),
...addPublicScope(
refNameToParamStructure(operation.requestBodySchemaRefName)
)
],
statements: 'super();'
}
]
};
}
function addPublicScope(
objs: OptionalKind<ParameterDeclarationStructure>[]
): OptionalKind<ParameterDeclarationStructure>[] {
return objs.map(o => ({ ...o, scope: Scope.Public }));
}
function method(
metadata: OpenApiServiceMetadata,
openApiPath: OpenApiPath,
operation: OpenApiOperation
): MethodDeclarationStructure[] {
return [
{
kind: StructureKind.Method,
name: 'execute',
isAsync: true,
parameters: [
{
name: 'destination',
type: 'Destination | DestinationNameAndJwt'
}
],
returnType: `Promise<AxiosResponse<${operation.responseSchemaRefName}>>`,
statements: toStatement(metadata, openApiPath, operation)
}
];
}
function toStatement(
metadata: OpenApiServiceMetadata,
openApiPath: OpenApiPath,
operation: OpenApiOperation
) {
const parameters = [
...buildParameterFromPathParameters(openApiPath.pathParameters),
...buildParameterFromRefName(operation.requestBodySchemaRefName),
'requestConfig'
];
return (
'const requestConfig: AxiosRequestConfig = await this.buildRequestConfig(destination);\n' +
`return new ${metadata.apiName}Api({basePath: requestConfig.baseURL})` +
`.${toPropertyFormat(operation.operationName)}(${parameters.join(', ')});`
);
}
function buildParameterFromPathParameters(pathParameters: string[]) {
return pathParameters.map(p => `this.${toPropertyFormat(p)}`);
}
function buildParameterFromRefName(refName?: string): string[] {
if (refName) {
return [`this.${toPropertyFormat(refName)}`];
}
return [];
}