forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostProcessServiceOperations.ts
31 lines (25 loc) · 1.22 KB
/
postProcessServiceOperations.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
import type { Operation } from '../client/interfaces/Operation';
import type { Service } from '../client/interfaces/Service';
import { flatMap } from './flatMap';
import { postProcessServiceOperationsJsonld } from './postProcessServiceOperationsJsonld';
export const postProcessServiceOperations = (service: Service, options?: { useJsonld?: boolean }): Operation[] => {
const names = new Map<string, number>();
return service.operations.map(operation => {
const clone = { ...operation };
// Parse the service parameters and results, very similar to how we parse
// properties of models. These methods will extend the type if needed.
clone.imports.push(...flatMap(clone.parameters, parameter => parameter.imports));
clone.imports.push(...flatMap(clone.results, result => result.imports));
// Check if the operation name is unique, if not then prefix this with a number
const name = clone.name;
const index = names.get(name) || 0;
if (index > 0) {
clone.name = `${name}${index}`;
}
names.set(name, index + 1);
if (options?.useJsonld) {
postProcessServiceOperationsJsonld(clone);
}
return clone;
});
};