Skip to content

Commit f083ffb

Browse files
committed
- Added multiple tags support
1 parent 697de58 commit f083ffb

15 files changed

+329
-70
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export const $MyModel = {
257257
format: 'date-time',
258258
},
259259
},
260-
};
260+
} as const;
261261
```
262262

263263
These runtime object are prefixed with a `$` character and expose all the interesting attributes of a model

src/openApi/v2/parser/getOperation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ import { getOperationPath } from './getOperationPath';
1010
import { getOperationResponseHeader } from './getOperationResponseHeader';
1111
import { getOperationResponses } from './getOperationResponses';
1212
import { getOperationResults } from './getOperationResults';
13-
import { getServiceClassName } from './getServiceClassName';
13+
import { getServiceName } from './getServiceName';
1414
import { sortByRequired } from './sortByRequired';
1515

1616
export function getOperation(
1717
openApi: OpenApi,
1818
url: string,
1919
method: string,
20+
tag: string,
2021
op: OpenApiOperation,
2122
pathParams: OperationParameters
2223
): Operation {
23-
const serviceName = op.tags?.[0] || 'Service';
24-
const serviceClassName = getServiceClassName(serviceName);
25-
const operationNameFallback = `${method}${serviceClassName}`;
24+
const serviceName = getServiceName(tag);
25+
const operationNameFallback = `${method}${serviceName}`;
2626
const operationName = getOperationName(op.operationId || operationNameFallback);
2727
const operationPath = getOperationPath(url);
2828

2929
// Create a new operation object for this method.
3030
const operation: Operation = {
31-
service: serviceClassName,
31+
service: serviceName,
3232
name: operationName,
3333
summary: getComment(op.summary),
3434
description: getComment(op.description),

src/openApi/v2/parser/getServiceClassName.spec.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getServiceName } from './getServiceName';
2+
3+
describe('getServiceName', () => {
4+
it('should produce correct result', () => {
5+
expect(getServiceName('')).toEqual('');
6+
expect(getServiceName('FooBar')).toEqual('FooBar');
7+
expect(getServiceName('Foo Bar')).toEqual('FooBar');
8+
expect(getServiceName('foo bar')).toEqual('FooBar');
9+
expect(getServiceName('@fooBar')).toEqual('FooBar');
10+
expect(getServiceName('$fooBar')).toEqual('FooBar');
11+
expect(getServiceName('123fooBar')).toEqual('FooBar');
12+
});
13+
});

src/openApi/v2/parser/getServiceClassName.ts renamed to src/openApi/v2/parser/getServiceName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import camelCase from 'camelcase';
22

33
/**
4-
* Convert the input value to a correct service classname. This converts
4+
* Convert the input value to a correct service name. This converts
55
* the input string to PascalCase.
66
*/
7-
export function getServiceClassName(value: string): string {
7+
export function getServiceName(value: string): string {
88
const clean = value
99
.replace(/^[^a-zA-Z]+/g, '')
1010
.replace(/[^\w\-]+/g, '-')

src/openApi/v2/parser/getServices.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Service } from '../../../client/interfaces/Service';
2+
import { unique } from '../../../utils/unique';
23
import type { OpenApi } from '../interfaces/OpenApi';
34
import { getOperation } from './getOperation';
45
import { getOperationParameters } from './getOperationParameters';
@@ -27,20 +28,23 @@ export function getServices(openApi: OpenApi): Service[] {
2728
case 'patch':
2829
// Each method contains an OpenAPI operation, we parse the operation
2930
const op = path[method]!;
30-
const operation = getOperation(openApi, url, method, op, pathParams);
31+
const tags = op.tags?.filter(unique) || ['Service'];
32+
tags.forEach(tag => {
33+
const operation = getOperation(openApi, url, method, tag, op, pathParams);
3134

32-
// If we have already declared a service, then we should fetch that and
33-
// append the new method to it. Otherwise we should create a new service object.
34-
const service: Service = services.get(operation.service) || {
35-
name: operation.service,
36-
operations: [],
37-
imports: [],
38-
};
35+
// If we have already declared a service, then we should fetch that and
36+
// append the new method to it. Otherwise we should create a new service object.
37+
const service: Service = services.get(operation.service) || {
38+
name: operation.service,
39+
operations: [],
40+
imports: [],
41+
};
3942

40-
// Push the operation in the service
41-
service.operations.push(operation);
42-
service.imports.push(...operation.imports);
43-
services.set(operation.service, service);
43+
// Push the operation in the service
44+
service.operations.push(operation);
45+
service.imports.push(...operation.imports);
46+
services.set(operation.service, service);
47+
});
4448
break;
4549
}
4650
}

src/openApi/v3/parser/getOperation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ import { getOperationResponseHeader } from './getOperationResponseHeader';
1313
import { getOperationResponses } from './getOperationResponses';
1414
import { getOperationResults } from './getOperationResults';
1515
import { getRef } from './getRef';
16-
import { getServiceClassName } from './getServiceClassName';
16+
import { getServiceName } from './getServiceName';
1717
import { sortByRequired } from './sortByRequired';
1818

1919
export function getOperation(
2020
openApi: OpenApi,
2121
url: string,
2222
method: string,
23+
tag: string,
2324
op: OpenApiOperation,
2425
pathParams: OperationParameters
2526
): Operation {
26-
const serviceName = op.tags?.[0] || 'Service';
27-
const serviceClassName = getServiceClassName(serviceName);
28-
const operationNameFallback = `${method}${serviceClassName}`;
27+
const serviceName = getServiceName(tag);
28+
const operationNameFallback = `${method}${serviceName}`;
2929
const operationName = getOperationName(op.operationId || operationNameFallback);
3030
const operationPath = getOperationPath(url);
3131

3232
// Create a new operation object for this method.
3333
const operation: Operation = {
34-
service: serviceClassName,
34+
service: serviceName,
3535
name: operationName,
3636
summary: getComment(op.summary),
3737
description: getComment(op.description),

src/openApi/v3/parser/getServiceClassName.spec.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getServiceName } from './getServiceName';
2+
3+
describe('getServiceName', () => {
4+
it('should produce correct result', () => {
5+
expect(getServiceName('')).toEqual('');
6+
expect(getServiceName('FooBar')).toEqual('FooBar');
7+
expect(getServiceName('Foo Bar')).toEqual('FooBar');
8+
expect(getServiceName('foo bar')).toEqual('FooBar');
9+
expect(getServiceName('@fooBar')).toEqual('FooBar');
10+
expect(getServiceName('$fooBar')).toEqual('FooBar');
11+
expect(getServiceName('123fooBar')).toEqual('FooBar');
12+
});
13+
});

src/openApi/v3/parser/getServiceClassName.ts renamed to src/openApi/v3/parser/getServiceName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import camelCase from 'camelcase';
22

33
/**
4-
* Convert the input value to a correct service classname. This converts
4+
* Convert the input value to a correct service name. This converts
55
* the input string to PascalCase.
66
*/
7-
export function getServiceClassName(value: string): string {
7+
export function getServiceName(value: string): string {
88
const clean = value
99
.replace(/^[^a-zA-Z]+/g, '')
1010
.replace(/[^\w\-]+/g, '-')

0 commit comments

Comments
 (0)