fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #696 from anchan828/add-operationId-to-methodNameBuilder

feat: pass the Operation object to methodNameBuilder

authored by

Lubos and committed by
GitHub
41cca181 9a1e8c98

+136 -3
+5
.changeset/weak-ghosts-poke.md
··· 1 + --- 2 + '@hey-api/openapi-ts': minor 3 + --- 4 + 5 + feat: pass the Operation object to methodNameBuilder
+4
packages/openapi-ts/src/openApi/common/interfaces/client.ts
··· 48 48 deprecated: boolean; 49 49 description: string | null; 50 50 errors: OperationResponse[]; 51 + /** 52 + * The operationId from OpenAPI specification. 53 + */ 54 + id: string | null; 51 55 method: Method; 52 56 /** 53 57 * Method name. Methods contain the request logic.
+1
packages/openapi-ts/src/openApi/v2/parser/getOperation.ts
··· 42 42 deprecated: op.deprecated === true, 43 43 description: op.description || null, 44 44 errors: [], 45 + id: op.operationId || null, 45 46 imports: [], 46 47 method: method.toUpperCase() as Operation['method'], 47 48 name,
+1
packages/openapi-ts/src/openApi/v3/parser/operation.ts
··· 66 66 deprecated: Boolean(op.deprecated), 67 67 description: op.description || null, 68 68 errors: [], 69 + id: op.operationId || null, 69 70 imports: [], 70 71 method: method.toUpperCase() as Operation['method'], 71 72 name,
+4 -2
packages/openapi-ts/src/types/config.ts
··· 1 + import type { Operation } from '../openApi'; 2 + 1 3 export interface ClientConfig { 2 4 /** 3 5 * Manually set base in OpenAPI config instead of inferring from server value ··· 125 127 */ 126 128 name?: string; 127 129 /** 128 - * Customise the method name of methods within the service 130 + * Customise the method name of methods within the service. By default, {@link Operation.name} is used. 129 131 */ 130 - methodNameBuilder?: (service: string, operationId: string) => string; 132 + methodNameBuilder?: (operation: Operation) => string; 131 133 /** 132 134 * Use operation ID to generate operation names? 133 135 * @default true
+120
packages/openapi-ts/src/utils/write/__tests__/services.spec.ts
··· 4 4 import { describe, expect, it, vi } from 'vitest'; 5 5 6 6 import { TypeScriptFile } from '../../../compiler'; 7 + import type { Operation } from '../../../openApi'; 7 8 import { setConfig } from '../../config'; 8 9 import { processServices } from '../services'; 9 10 ··· 61 62 ); 62 63 }); 63 64 }); 65 + 66 + describe('methodNameBuilder', () => { 67 + // If the generated text has the expected method, tests are considered pass. 68 + 69 + const operation: Operation = { 70 + $refs: [], 71 + deprecated: false, 72 + description: null, 73 + errors: [], 74 + id: 'User_get', 75 + imports: [], 76 + method: 'GET', 77 + name: 'userGet', 78 + parameters: [], 79 + parametersBody: null, 80 + parametersCookie: [], 81 + parametersForm: [], 82 + parametersHeader: [], 83 + parametersPath: [], 84 + parametersQuery: [], 85 + path: '/users', 86 + responseHeader: null, 87 + results: [], 88 + service: 'User', 89 + summary: null, 90 + }; 91 + 92 + const client: Parameters<typeof processServices>[0]['client'] = { 93 + models: [], 94 + server: 'http://localhost:8080', 95 + services: [ 96 + { 97 + $refs: [], 98 + imports: [], 99 + name: 'User', 100 + operations: [operation], 101 + }, 102 + ], 103 + types: {}, 104 + version: 'v1', 105 + }; 106 + 107 + it('use default name', async () => { 108 + setConfig({ 109 + client: 'fetch', 110 + debug: false, 111 + dryRun: false, 112 + exportCore: true, 113 + input: '', 114 + output: { 115 + path: '', 116 + }, 117 + schemas: {}, 118 + services: { 119 + asClass: true, 120 + }, 121 + types: {}, 122 + useOptions: false, 123 + }); 124 + 125 + const file = new TypeScriptFile({ 126 + dir: '/', 127 + name: 'services.ts', 128 + }); 129 + const files = { 130 + services: file, 131 + }; 132 + 133 + await processServices({ client, files }); 134 + 135 + file.write(); 136 + 137 + expect(writeFileSync).toHaveBeenCalledWith( 138 + path.resolve('/services.gen.ts'), 139 + expect.stringContaining('public static userGet()'), 140 + ); 141 + }); 142 + 143 + it('call methodNameBuilder', async () => { 144 + const methodNameBuilderMock = vi.fn().mockReturnValue('customName'); 145 + 146 + setConfig({ 147 + client: 'fetch', 148 + debug: false, 149 + dryRun: false, 150 + exportCore: true, 151 + input: '', 152 + output: { 153 + path: '', 154 + }, 155 + schemas: {}, 156 + services: { 157 + asClass: true, 158 + methodNameBuilder: methodNameBuilderMock, 159 + }, 160 + types: {}, 161 + useOptions: false, 162 + }); 163 + 164 + const file = new TypeScriptFile({ 165 + dir: '/', 166 + name: 'services.ts', 167 + }); 168 + const files = { 169 + services: file, 170 + }; 171 + 172 + await processServices({ client, files }); 173 + 174 + file.write(); 175 + 176 + expect(writeFileSync).toHaveBeenCalledWith( 177 + path.resolve('/services.gen.ts'), 178 + expect.stringContaining('public static customName()'), 179 + ); 180 + 181 + expect(methodNameBuilderMock).toHaveBeenCalledWith(operation); 182 + }); 183 + });
+1 -1
packages/openapi-ts/src/utils/write/services.ts
··· 536 536 comment: toOperationComment(operation), 537 537 isStatic: config.name === undefined && config.client !== 'angular', 538 538 name: config.services.methodNameBuilder 539 - ? config.services.methodNameBuilder(operation.service, operation.name) 539 + ? config.services.methodNameBuilder(operation) 540 540 : operation.name, 541 541 parameters: toOperationParamType(client, operation), 542 542 returnType: isStandalone