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

Merge pull request #3104 from hey-api/refactor/sdk-class-instance

refactor(sdk): clean up instance type so there's no need to cast

authored by

Lubos and committed by
GitHub
d2259087 f46c47ca

+13 -18
+2 -2
dev/openapi-ts.config.ts
··· 43 43 // 'circular.yaml', 44 44 // 'dutchie.json', 45 45 // 'enum-names-values.yaml', 46 - 'full.yaml', 46 + // 'full.yaml', 47 47 // 'integer-formats.yaml', 48 48 // 'invalid', 49 49 // 'object-property-names.yaml', ··· 52 52 // 'pagination-ref.yaml', 53 53 // 'schema-const.yaml', 54 54 // 'sdk-instance.yaml', 55 - // 'sdk-method-class-conflict.yaml', 55 + 'sdk-method-class-conflict.yaml', 56 56 // 'sdk-nested-classes.yaml', 57 57 // 'sdk-nested-conflict.yaml', 58 58 // 'string-with-format.yaml',
+2 -2
packages/openapi-ts/src/plugins/@hey-api/sdk/config.ts
··· 11 11 classStructure: 'auto', 12 12 client: true, 13 13 exportFromIndex: true, 14 - instance: false, 14 + instance: '', 15 15 operationId: true, 16 16 paramsStructure: 'grouped', 17 17 response: 'body', ··· 79 79 80 80 plugin.config.asClass = true; 81 81 } else { 82 - plugin.config.instance = false; 82 + plugin.config.instance = ''; 83 83 } 84 84 85 85 // Set default classNameBuilder based on client type
+8 -11
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts
··· 38 38 value: string; 39 39 }) => { 40 40 const name = stringCase({ case: 'PascalCase', value }); 41 - return typeof plugin.config.classNameBuilder === 'string' 42 - ? plugin.config.classNameBuilder.replace('{{name}}', name) 43 - : plugin.config.classNameBuilder(name); 41 + return ( 42 + (typeof plugin.config.classNameBuilder === 'string' 43 + ? plugin.config.classNameBuilder.replace('{{name}}', name) 44 + : plugin.config.classNameBuilder(name)) || name 45 + ); 44 46 }; 45 47 46 48 export const operationMethodName = ({ ··· 69 71 70 72 if (plugin.config.classStructure === 'auto' && operation.operationId) { 71 73 classCandidates = operation.operationId.split(/[./]/).filter(Boolean); 72 - if (classCandidates.length > 1) { 74 + if (classCandidates.length >= 2) { 73 75 const methodCandidate = classCandidates.pop()!; 74 76 methodName = stringCase({ 75 77 case: 'camelCase', ··· 80 82 } 81 83 82 84 const rootClasses = plugin.config.instance 83 - ? [plugin.config.instance as string] 85 + ? [plugin.config.instance] 84 86 : (operation.tags ?? ['default']); 85 87 86 88 for (const rootClass of rootClasses) { 87 - const finalClassName = operationClassName({ 88 - plugin, 89 - value: className || rootClass, 90 - }); 91 - 92 89 // Default path 93 90 let path = [rootClass]; 94 91 if (className) { ··· 102 99 } 103 100 104 101 classNames.set(rootClass, { 105 - className: finalClassName, 102 + className: operationClassName({ plugin, value: className || rootClass }), 106 103 methodName: methodName || operationMethodName({ operation, plugin }), 107 104 path: path.map((value) => operationClassName({ plugin, value })), 108 105 });
+1 -3
packages/openapi-ts/src/plugins/@hey-api/sdk/types.d.ts
··· 248 248 * Set `instance` to create an instantiable SDK. Using `true` will use the 249 249 * default instance name; in practice, you want to define your own by passing 250 250 * a string value. 251 - * 252 - * @default false 253 251 */ 254 - instance: string | boolean; 252 + instance: string; 255 253 /** 256 254 * Customise the name of methods within the service. By default, 257 255 * {@link IR.OperationObject.id} is used.