Skip to content

Commit 0d78ec2

Browse files
committed
add aliases
1 parent 55775a4 commit 0d78ec2

File tree

5 files changed

+169
-87
lines changed

5 files changed

+169
-87
lines changed

__fixtures__/output/swagger-client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12327,7 +12327,11 @@ export class KubernetesClient extends APIClient {
1232712327
const path = `/api/v1/persistentvolumes/${params.path.name}/status`;
1232812328
return await this.patch<PersistentVolume>(path, params.body);
1232912329
}
12330-
async listCoreV1PodForAllNamespaces(params: ListCoreV1PodForAllNamespacesRequest): Promise<PodList> {
12330+
async getPods(params: ListCoreV1PodForAllNamespacesRequest): Promise<PodList> {
12331+
const path = `/api/v1/pods`;
12332+
return await this.get<PodList>(path);
12333+
}
12334+
async listPods(params: ListCoreV1PodForAllNamespacesRequest): Promise<PodList> {
1233112335
const path = `/api/v1/pods`;
1233212336
return await this.get<PodList>(path);
1233312337
}

packages/schema-sdk/__tests__/__snapshots__/openapi.generate.test.ts.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25674,7 +25674,11 @@ export class KubernetesClient extends APIClient {
2567425674
const path = \`/api/v1/persistentvolumes/\${params.path.name}/status\`;
2567525675
return await this.patch<PersistentVolume>(path, params.body);
2567625676
}
25677-
async listCoreV1PodForAllNamespaces(params: ListCoreV1PodForAllNamespacesRequest): Promise<PodList> {
25677+
async getPods(params: ListCoreV1PodForAllNamespacesRequest): Promise<PodList> {
25678+
const path = \`/api/v1/pods\`;
25679+
return await this.get<PodList>(path);
25680+
}
25681+
async listPods(params: ListCoreV1PodForAllNamespacesRequest): Promise<PodList> {
2567825682
const path = \`/api/v1/pods\`;
2567925683
return await this.get<PodList>(path);
2568025684
}

packages/schema-sdk/__tests__/openapi.generate.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getDefaultSchemaSDKOptions } from '../src/types';
66

77
it('swagger', () => {
88
const options = getDefaultSchemaSDKOptions({
9+
clientName: 'KubernetesClient',
910
includeSwaggerUrl: true,
1011
exclude: [
1112
'*.v1beta1.*',
@@ -17,7 +18,14 @@ it('swagger', () => {
1718
});
1819
const code = generateOpenApiClient({
1920
...options,
20-
// version: 'v1',
21+
operationNamingStrategy: {
22+
aliases: {
23+
listCoreV1PodForAllNamespaces: 'getPods'
24+
},
25+
renameMap: {
26+
listCoreV1PodForAllNamespaces: 'listPods'
27+
}
28+
},
2129
paths: {
2230
exclude: [
2331
'*flowschema*',
@@ -58,6 +66,7 @@ it('merged', () => {
5866
// include: [
5967
// '*.v1.*'
6068
// ],
69+
clientName: 'KubernetesClient',
6170
includeSwaggerUrl: true,
6271
includeMethodComments: true,
6372
namingStrategy: {

packages/schema-sdk/src/openapi.ts

Lines changed: 121 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export function generateOpenApiParams(options: OpenAPIOptions, path: string, pat
265265
}
266266
});
267267

268-
const typeName = toPascalCase(getOperationMethodName(operation, method, path)) + 'Request';
268+
const typeName = getOperationTypeName(options, operation, method, path) + 'Request';
269269
const paramsInterface = t.tsInterfaceDeclaration(
270270
t.identifier(typeName),
271271
null,
@@ -361,12 +361,115 @@ export function generateOpenApiTypes(options: OpenAPIOptions, schema: OpenAPISpe
361361
return interfaces.map(i => t.exportNamedDeclaration(i));
362362
}
363363

364-
const getOperationMethodName = (operation: Operation, method: string, path: string) => {
365-
const methodName = operation.operationId || toCamelCase(method + path.replace(/\W/g, '_'));
364+
const getOperationMethodName = (
365+
options: OpenAPIOptions,
366+
operation: Operation,
367+
method: string,
368+
path: string
369+
) => {
370+
const defaultMethodName = toCamelCase(method + path.replace(/\W/g, '_'));
371+
const methodName = operation.operationId || defaultMethodName;
372+
373+
if (options?.operationNamingStrategy?.renameMap) {
374+
return options.operationNamingStrategy.renameMap[methodName] || methodName;
375+
}
376+
366377
return methodName;
367378
};
368379

369-
export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t.ClassMethod[] {
380+
const getOperationTypeName = (
381+
options: OpenAPIOptions,
382+
operation: Operation,
383+
method: string,
384+
path: string
385+
) => {
386+
const defaultMethodName = toCamelCase(method + path.replace(/\W/g, '_'));
387+
const methodName = operation.operationId || defaultMethodName;
388+
389+
if (!options?.operationNamingStrategy?.renameTypes) {
390+
return toPascalCase(methodName);
391+
}
392+
393+
if (options?.operationNamingStrategy?.renameMap) {
394+
return toPascalCase(options.operationNamingStrategy.renameMap[methodName] || methodName);
395+
}
396+
397+
return methodName;
398+
};
399+
400+
export const createOperation = (
401+
options: OpenAPIOptions,
402+
operation: Operation,
403+
path: string,
404+
method: string,
405+
alias?: string
406+
): t.ClassMethod => {
407+
const typeName = getOperationTypeName(options, operation, method, path) + 'Request';
408+
const id = t.identifier('params');
409+
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(typeName)));
410+
const params = [id];
411+
412+
const returnType = getOperationReturnType(options, operation, method);
413+
const methodName = getOperationMethodName(options, operation, method, path);
414+
415+
const callMethod = t.callExpression(
416+
t.memberExpression(
417+
t.thisExpression(),
418+
t.identifier(method)
419+
),
420+
['post', 'put', 'patch', 'formData'].includes(method) ?
421+
[
422+
t.identifier('path'),
423+
t.memberExpression(
424+
t.identifier('params'),
425+
t.identifier('body')
426+
)
427+
] : [
428+
t.identifier('path')
429+
]
430+
);
431+
callMethod.typeParameters = t.tsTypeParameterInstantiation([
432+
returnType
433+
]);
434+
435+
const methodFunction = t.classMethod(
436+
'method',
437+
t.identifier(alias ? toCamelCase(alias) : methodName),
438+
params,
439+
t.blockStatement([
440+
t.variableDeclaration('const', [
441+
t.variableDeclarator(
442+
t.identifier('path'),
443+
createPathTemplateLiteral(options, path)
444+
)
445+
]),
446+
t.returnStatement(
447+
t.awaitExpression(
448+
callMethod
449+
)
450+
)
451+
]),
452+
false,
453+
false,
454+
false,
455+
true
456+
);
457+
methodFunction.returnType = t.tsTypeAnnotation(
458+
t.tsTypeReference(
459+
t.identifier('Promise'),
460+
t.tsTypeParameterInstantiation([
461+
returnType
462+
])
463+
)
464+
);
465+
466+
return methodFunction;
467+
};
468+
469+
export function generateMethods(
470+
options: OpenAPIOptions,
471+
schema: OpenAPISpec
472+
): t.ClassMethod[] {
370473
const methods: t.ClassMethod[] = [];
371474

372475
// Iterate through each path and each method in the path
@@ -380,67 +483,11 @@ export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t
380483
const operation: Operation = pathItem[method];
381484
if (!shouldIncludeOperation(options, pathItem, path, method as any)) return;
382485

383-
384-
const typeName = toPascalCase(getOperationMethodName(operation, method, path)) + 'Request';
385-
const id = t.identifier('params');
386-
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(typeName)));
387-
const params = [id];
388-
389-
const returnType = getOperationReturnType(options, operation, method);
390-
const methodName = getOperationMethodName(operation, method, path);
391-
392-
393-
394-
const callMethod = t.callExpression(
395-
t.memberExpression(
396-
t.thisExpression(),
397-
t.identifier(method)
398-
),
399-
['post', 'put', 'patch', 'formData'].includes(method) ?
400-
[
401-
t.identifier('path'),
402-
t.memberExpression(
403-
t.identifier('params'),
404-
t.identifier('body')
405-
)
406-
] : [
407-
t.identifier('path')
408-
]
409-
);
410-
callMethod.typeParameters = t.tsTypeParameterInstantiation([
411-
returnType
412-
]);
413-
const methodFunction = t.classMethod(
414-
'method',
415-
t.identifier(methodName),
416-
params,
417-
t.blockStatement([
418-
t.variableDeclaration('const', [
419-
t.variableDeclarator(
420-
t.identifier('path'),
421-
createPathTemplateLiteral(options, path)
422-
)
423-
]),
424-
t.returnStatement(
425-
t.awaitExpression(
426-
callMethod
427-
)
428-
)
429-
]),
430-
false,
431-
false,
432-
false,
433-
true
434-
);
435-
methodFunction.returnType = t.tsTypeAnnotation(
436-
t.tsTypeReference(
437-
t.identifier('Promise'),
438-
t.tsTypeParameterInstantiation([
439-
returnType
440-
])
441-
)
442-
);
443-
methods.push(methodFunction);
486+
const alias = options.operationNamingStrategy?.aliases?.[operation.operationId];
487+
if (alias) {
488+
methods.push(createOperation(options, operation, path, method, alias));
489+
}
490+
methods.push(createOperation(options, operation, path, method));
444491
}
445492

446493
});
@@ -476,7 +523,10 @@ export const getSwaggerJSONMethod = (): t.ClassMethod => {
476523
);
477524
};
478525

479-
export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISpec): string {
526+
export function generateOpenApiClient(
527+
options: OpenAPIOptions,
528+
schema: OpenAPISpec
529+
): string {
480530
const methods = [];
481531
if (options.includeSwaggerUrl) {
482532
methods.push(getSwaggerJSONMethod());
@@ -498,21 +548,21 @@ export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISp
498548
]);
499549

500550
const clientClass = t.exportNamedDeclaration(t.classDeclaration(
501-
t.identifier('KubernetesClient'),
551+
t.identifier(options.clientName),
502552
t.identifier('APIClient'),
503553
classBody,
504554
[]
505555
));
506556

507557
//// INTERFACES
508-
const kubeSchema = {
509-
title: 'Kubernetes',
558+
const apiSchema = {
559+
title: options.clientName,
510560
definitions: schema.definitions
511561
};
512562

513-
const types = generateTypeScriptTypes(kubeSchema, {
563+
const types = generateTypeScriptTypes(apiSchema, {
514564
...(options as any),
515-
exclude: ['Kubernetes', ...(options.exclude ?? [])]
565+
exclude: [options.clientName, ...(options.exclude ?? [])]
516566
});
517567
const openApiTypes = generateOpenApiTypes(options, schema);
518568

packages/schema-sdk/src/types.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,41 @@ import type { DeepPartial } from 'schema-typescript';
33
import { defaultSchemaTSOptions, SchemaTSOptions } from 'schema-typescript';
44

55
export interface OpenAPIOptions extends SchemaTSOptions {
6-
version?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2';
7-
mergedParams?: boolean;
8-
includeSwaggerUrl?: boolean;
9-
paths?: {
10-
// Include/Exclude types
11-
include?: string[];
12-
exclude?: string[];
6+
clientName: string;
7+
version?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2';
8+
mergedParams?: boolean;
9+
includeSwaggerUrl?: boolean;
10+
operationNamingStrategy?: {
11+
renameTypes?: boolean;
12+
renameMap?: {
13+
[originalName: string]: string;
14+
};
15+
aliases?: {
16+
[originalName: string]: string;
17+
};
18+
}
19+
paths?: {
20+
// Include/Exclude types
21+
include?: string[];
22+
exclude?: string[];
1323

14-
includeTags?: string[];
15-
excludeTags?: string[];
24+
includeTags?: string[];
25+
excludeTags?: string[];
1626

17-
includeRequests?: string[];
18-
excludeRequests?: string[];
19-
}
27+
includeRequests?: string[];
28+
excludeRequests?: string[];
29+
}
2030
}
2131

22-
export const defaultSchemaSDKOptions: OpenAPIOptions = {
32+
export const defaultSchemaSDKOptions: Partial<OpenAPIOptions> = {
2333
...defaultSchemaTSOptions,
34+
clientName: 'Client',
2435
mergedParams: false,
2536
includeSwaggerUrl: false,
37+
operationNamingStrategy: {
38+
renameMap: {},
39+
aliases: {}
40+
},
2641
paths: {
2742
include: [],
2843
exclude: [],

0 commit comments

Comments
 (0)