-
-
Notifications
You must be signed in to change notification settings - Fork 513
/
getOperation.ts
85 lines (78 loc) · 4.08 KB
/
getOperation.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
import type { Operation } from '../../../client/interfaces/Operation';
import type { OperationParameters } from '../../../client/interfaces/OperationParameters';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiOperation } from '../interfaces/OpenApiOperation';
import type { OpenApiRequestBody } from '../interfaces/OpenApiRequestBody';
import { getComment } from './getComment';
import { getOperationErrors } from './getOperationErrors';
import { getOperationName } from './getOperationName';
import { getOperationParameters } from './getOperationParameters';
import { getOperationPath } from './getOperationPath';
import { getOperationRequestBody } from './getOperationRequestBody';
import { getOperationResponseHeader } from './getOperationResponseHeader';
import { getOperationResponses } from './getOperationResponses';
import { getOperationResults } from './getOperationResults';
import { getRef } from './getRef';
import { getServiceClassName } from './getServiceClassName';
import { sortByRequired } from './sortByRequired';
export function getOperation(openApi: OpenApi, url: string, method: string, op: OpenApiOperation, pathParams: OperationParameters): Operation {
const serviceName = op.tags?.[0] || 'Service';
const serviceClassName = getServiceClassName(serviceName);
const operationNameFallback = `${method}${serviceClassName}`;
const operationName = getOperationName(op.operationId || operationNameFallback);
const operationPath = getOperationPath(url);
// Create a new operation object for this method.
const operation: Operation = {
service: serviceClassName,
name: operationName,
summary: getComment(op.summary),
description: getComment(op.description),
deprecated: op.deprecated === true,
method: method.toUpperCase(),
path: operationPath,
parameters: [...pathParams.parameters],
parametersPath: [...pathParams.parametersPath],
parametersQuery: [...pathParams.parametersQuery],
parametersForm: [...pathParams.parametersForm],
parametersHeader: [...pathParams.parametersHeader],
parametersCookie: [...pathParams.parametersCookie],
parametersBody: pathParams.parametersBody,
imports: [],
errors: [],
results: [],
responseHeader: null,
};
// Parse the operation parameters (path, query, body, etc).
if (op.parameters) {
const parameters = getOperationParameters(openApi, op.parameters);
operation.imports.push(...parameters.imports);
operation.parameters.push(...parameters.parameters);
operation.parametersPath.push(...parameters.parametersPath);
operation.parametersQuery.push(...parameters.parametersQuery);
operation.parametersForm.push(...parameters.parametersForm);
operation.parametersHeader.push(...parameters.parametersHeader);
operation.parametersCookie.push(...parameters.parametersCookie);
operation.parametersBody = parameters.parametersBody;
}
// TODO: form data goes wrong here: https://github.com/ferdikoomen/openapi-typescript-codegen/issues/257§
if (op.requestBody) {
const requestBodyDef = getRef<OpenApiRequestBody>(openApi, op.requestBody);
const requestBody = getOperationRequestBody(openApi, requestBodyDef);
operation.imports.push(...requestBody.imports);
operation.parameters.push(requestBody);
operation.parameters = operation.parameters.sort(sortByRequired);
operation.parametersBody = requestBody;
}
// Parse the operation responses.
if (op.responses) {
const operationResponses = getOperationResponses(openApi, op.responses);
const operationResults = getOperationResults(operationResponses);
operation.errors = getOperationErrors(operationResponses);
operation.responseHeader = getOperationResponseHeader(operationResults);
operationResults.forEach(operationResult => {
operation.results.push(operationResult);
operation.imports.push(...operationResult.imports);
});
}
return operation;
}