-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathapi-file.ts
139 lines (127 loc) · 3.8 KB
/
api-file.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {
codeBlock,
createLogger,
partition,
unique
} from '@sap-cloud-sdk/util';
import {
OpenApiDocument,
OpenApiOperation,
SchemaMetadata
} from '../openapi-types';
const logger = createLogger('openapi-generator');
/**
* @experimental This API is experimental and might change in newer versions. Use with caution.
* Get the content of the SAP Cloud SDK API wrapper.
* @param openApiDocument Parsed service.
* @returns The generated code for the SDK API wrapper.
*/
export function apiFile(openApiDocument: OpenApiDocument): string {
const requestBodyTypes = getRequestBodyReferenceTypes(openApiDocument);
return codeBlock`
import { OpenApiRequestBuilder } from '@sap-cloud-sdk/core';
import { DefaultApi } from './openapi/api';
${
requestBodyTypes
? `import { ${requestBodyTypes} } from './openapi/model';`
: ''
}
export const ${openApiDocument.apiName} = {
${getOperations(openApiDocument)}
};
`;
}
/**
* Get the reference types for all request body types in the given service.
* @param openApiDocument Parsed service metadata.
* @returns The list of body types as a string.
*/
function getRequestBodyReferenceTypes(
openApiDocument: OpenApiDocument
): string {
const bodyTypes = openApiDocument.operations
.map(operation => operation.requestBody?.parameterType)
.filter(requestBody => typeof requestBody !== 'undefined')
.filter(requestBody => requestBody?.isInnerTypeReferenceType)
.map(requestBody => requestBody?.innerType) as string[];
return unique(bodyTypes).join(', ');
}
/**
* Get all operation representations for the given service.
* @param openApiDocument Parsed service.
* @returns All operations as a string.
*/
function getOperations(openApiDocument: OpenApiDocument): string {
return openApiDocument.operations
.map(operation => getOperation(operation))
.join(',\n');
}
/**
* Get the string representation of one operation.
* @param operation Operation to serialize.
* @returns The operation as a string.
*/
function getOperation(operation: OpenApiOperation): string {
const params = getParams(operation);
const argsQuestionMark = params.every(param => !param.required) ? '?' : '';
const paramsArg = params.length
? codeBlock`args${argsQuestionMark}: {
${params
.map(param => `${param.name}${param.required ? '' : '?'}: ${param.type}`)
.join(',\n')}
}`
: '';
const requestBuilderParams = [
'DefaultApi',
`'${operation.operationName}'`,
...params.map(param => `args${argsQuestionMark}.${param.name}`)
];
return codeBlock`
${
operation.operationName
}: (${paramsArg}) => new OpenApiRequestBuilder<DefaultApi, '${
operation.operationName
}'>(
${requestBuilderParams.join(',\n')}
)`;
}
interface Parameter {
type: string;
name: string;
required?: boolean;
}
function getParams(operation: OpenApiOperation): Parameter[] {
const parameters = [
...operation.parameters,
...getRequestBodyParams(operation)
];
const [required, optional] = partition(parameters, param => !!param.required);
return [...required, ...optional];
}
function getRequestBodyParams(operation: OpenApiOperation): Parameter[] {
return operation.requestBody
? [
{
name: operation.requestBody.parameterName,
type: getParameterTypeString(operation.requestBody.parameterType),
required: operation.requestBody.required
}
]
: [];
}
function getParameterTypeString(schemaMetadata: SchemaMetadata): string {
return schemaMetadata.isArrayType
? toArrayTypeWithArrayFormat(
schemaMetadata.innerType,
schemaMetadata.arrayLevel!
)
: schemaMetadata.innerType;
}
function toArrayTypeWithArrayFormat(
innerType: string,
arrayLevel: number
): string {
return arrayLevel === 0
? innerType
: `${toArrayTypeWithArrayFormat(innerType, arrayLevel - 1)}[]`;
}