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 #3102 from hey-api/refactor/sdk-operation-name

fix(output): sanitize reserved names with underscore suffix instead o…

authored by

Lubos and committed by
GitHub
61890ef8 dfad1ca2

+106 -150
+5
.changeset/olive-nails-strive.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + **output**: sanitize reserved names with underscore suffix instead of prefix
+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',
+6 -29
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/functions.ts
··· 1 - import type { IR } from '~/ir/types'; 2 1 import { getClientPlugin } from '~/plugins/@hey-api/client-core/utils'; 3 2 import { 4 3 createOperationComment, ··· 8 7 9 8 import type { HeyApiSdkPlugin } from '../types'; 10 9 import { nuxtTypeComposable, nuxtTypeDefault } from './constants'; 11 - import { operationParameters, operationStatements } from './operation'; 12 - import { reservedJavaScriptKeywordsRegExp } from './regexp'; 13 - 14 - const serviceFunctionIdentifier = ({ 15 - id, 16 - operation, 17 - plugin, 18 - }: { 19 - id: string; 20 - operation: IR.OperationObject; 21 - plugin: HeyApiSdkPlugin['Instance']; 22 - }) => { 23 - if (plugin.config.methodNameBuilder) { 24 - return plugin.config.methodNameBuilder(operation); 25 - } 26 - 27 - if (id.match(reservedJavaScriptKeywordsRegExp)) { 28 - return `${id}_`; 29 - } 30 - 31 - return id; 32 - }; 10 + import { 11 + operationMethodName, 12 + operationParameters, 13 + operationStatements, 14 + } from './operation'; 33 15 34 16 export const generateFlatSdk = ({ 35 17 plugin, ··· 66 48 operation, 67 49 plugin, 68 50 }); 69 - const symbol = plugin.registerSymbol({ 51 + const symbol = plugin.symbol(operationMethodName({ operation, plugin }), { 70 52 meta: { 71 53 category: 'sdk', 72 54 path: event._path, ··· 75 57 tags: event.tags, 76 58 tool: 'sdk', 77 59 }, 78 - name: serviceFunctionIdentifier({ 79 - id: operation.id, 80 - operation, 81 - plugin, 82 - }), 83 60 }); 84 61 const node = $.const(symbol) 85 62 .export()
+6 -30
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts
··· 12 12 import type { HeyApiSdkPlugin } from '../types'; 13 13 import { operationAuth } from './auth'; 14 14 import { nuxtTypeComposable, nuxtTypeDefault } from './constants'; 15 - import { reservedJavaScriptKeywordsRegExp } from './regexp'; 16 15 import { getSignatureParameters } from './signature'; 17 16 import { createRequestValidator, createResponseValidator } from './validator'; 18 - 19 - type Plugin = { 20 - config: Pick< 21 - HeyApiSdkPlugin['Instance']['config'], 22 - 'asClass' | 'classNameBuilder' | 'classStructure' | 'instance' 23 - >; 24 - }; 25 17 26 18 interface ClassNameEntry { 27 19 /** ··· 42 34 plugin, 43 35 value, 44 36 }: { 45 - plugin: Plugin; 37 + plugin: HeyApiSdkPlugin['Instance']; 46 38 value: string; 47 39 }) => { 48 40 const name = stringCase({ case: 'PascalCase', value }); ··· 51 43 : plugin.config.classNameBuilder(name); 52 44 }; 53 45 54 - const getOperationMethodName = ({ 46 + export const operationMethodName = ({ 55 47 operation, 56 48 plugin, 57 49 }: { 58 50 operation: IR.OperationObject; 59 - plugin: { 60 - config: Pick< 61 - HeyApiSdkPlugin['Instance']['config'], 62 - 'asClass' | 'methodNameBuilder' 63 - >; 64 - }; 65 - }) => { 66 - if (plugin.config.methodNameBuilder) { 67 - return plugin.config.methodNameBuilder(operation); 68 - } 69 - 70 - const handleIllegal = !plugin.config.asClass; 71 - if (handleIllegal && operation.id.match(reservedJavaScriptKeywordsRegExp)) { 72 - return `${operation.id}_`; 73 - } 74 - 75 - return operation.id; 76 - }; 51 + plugin: HeyApiSdkPlugin['Instance']; 52 + }) => plugin.config.methodNameBuilder?.(operation) || operation.id; 77 53 78 54 /** 79 55 * Returns a list of classes where this operation appears in the generated SDK. ··· 83 59 plugin, 84 60 }: { 85 61 operation: IR.OperationObject; 86 - plugin: Plugin; 62 + plugin: HeyApiSdkPlugin['Instance']; 87 63 }): Map<string, ClassNameEntry> => { 88 64 const classNames = new Map<string, ClassNameEntry>(); 89 65 ··· 127 103 128 104 classNames.set(rootClass, { 129 105 className: finalClassName, 130 - methodName: methodName || getOperationMethodName({ operation, plugin }), 106 + methodName: methodName || operationMethodName({ operation, plugin }), 131 107 path: path.map((value) => operationClassName({ plugin, value })), 132 108 }); 133 109 }
-2
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/regexp.ts
··· 1 - export const reservedJavaScriptKeywordsRegExp = 2 - /^(arguments|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|from|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
+86 -86
packages/openapi-ts/src/ts-dsl/utils/__tests__/name.test.ts
··· 9 9 }> = [ 10 10 { 11 11 name: 'document', 12 - output: '_document', 12 + output: 'document_', 13 13 }, 14 14 { 15 15 name: 'history', 16 - output: '_history', 16 + output: 'history_', 17 17 }, 18 18 { 19 19 name: 'location', 20 - output: '_location', 20 + output: 'location_', 21 21 }, 22 22 { 23 23 name: 'navigator', 24 - output: '_navigator', 24 + output: 'navigator_', 25 25 }, 26 26 { 27 27 name: 'window', 28 - output: '_window', 28 + output: 'window_', 29 29 }, 30 30 { 31 31 name: 'console', 32 - output: '_console', 32 + output: 'console_', 33 33 }, 34 34 { 35 35 name: 'Array', 36 - output: '_Array', 36 + output: 'Array_', 37 37 }, 38 38 { 39 39 name: 'Date', 40 - output: '_Date', 40 + output: 'Date_', 41 41 }, 42 42 { 43 43 name: 'Error', 44 - output: '_Error', 44 + output: 'Error_', 45 45 }, 46 46 { 47 47 name: 'Function', 48 - output: '_Function', 48 + output: 'Function_', 49 49 }, 50 50 { 51 51 name: 'JSON', 52 - output: '_JSON', 52 + output: 'JSON_', 53 53 }, 54 54 { 55 55 name: 'Map', 56 - output: '_Map', 56 + output: 'Map_', 57 57 }, 58 58 { 59 59 name: 'Math', 60 - output: '_Math', 60 + output: 'Math_', 61 61 }, 62 62 { 63 63 name: 'Object', 64 - output: '_Object', 64 + output: 'Object_', 65 65 }, 66 66 { 67 67 name: 'Promise', 68 - output: '_Promise', 68 + output: 'Promise_', 69 69 }, 70 70 { 71 71 name: 'RegExp', 72 - output: '_RegExp', 72 + output: 'RegExp_', 73 73 }, 74 74 { 75 75 name: 'Set', 76 - output: '_Set', 76 + output: 'Set_', 77 77 }, 78 78 { 79 79 name: 'WeakMap', 80 - output: '_WeakMap', 80 + output: 'WeakMap_', 81 81 }, 82 82 { 83 83 name: 'WeakSet', 84 - output: '_WeakSet', 84 + output: 'WeakSet_', 85 85 }, 86 86 { 87 87 name: 'arguments', 88 - output: '_arguments', 88 + output: 'arguments_', 89 89 }, 90 90 { 91 91 name: 'async', 92 - output: '_async', 92 + output: 'async_', 93 93 }, 94 94 { 95 95 name: 'await', 96 - output: '_await', 96 + output: 'await_', 97 97 }, 98 98 { 99 99 name: 'break', 100 - output: '_break', 100 + output: 'break_', 101 101 }, 102 102 { 103 103 name: 'case', 104 - output: '_case', 104 + output: 'case_', 105 105 }, 106 106 { 107 107 name: 'catch', 108 - output: '_catch', 108 + output: 'catch_', 109 109 }, 110 110 { 111 111 name: 'class', 112 - output: '_class', 112 + output: 'class_', 113 113 }, 114 114 { 115 115 name: 'const', 116 - output: '_const', 116 + output: 'const_', 117 117 }, 118 118 { 119 119 name: 'continue', 120 - output: '_continue', 120 + output: 'continue_', 121 121 }, 122 122 { 123 123 name: 'debugger', 124 - output: '_debugger', 124 + output: 'debugger_', 125 125 }, 126 126 { 127 127 name: 'default', 128 - output: '_default', 128 + output: 'default_', 129 129 }, 130 130 { 131 131 name: 'delete', 132 - output: '_delete', 132 + output: 'delete_', 133 133 }, 134 134 { 135 135 name: 'do', 136 - output: '_do', 136 + output: 'do_', 137 137 }, 138 138 { 139 139 name: 'else', 140 - output: '_else', 140 + output: 'else_', 141 141 }, 142 142 { 143 143 name: 'enum', 144 - output: '_enum', 144 + output: 'enum_', 145 145 }, 146 146 { 147 147 name: 'eval', 148 - output: '_eval', 148 + output: 'eval_', 149 149 }, 150 150 { 151 151 name: 'export', 152 - output: '_export', 152 + output: 'export_', 153 153 }, 154 154 { 155 155 name: 'extends', 156 - output: '_extends', 156 + output: 'extends_', 157 157 }, 158 158 { 159 159 name: 'false', 160 - output: '_false', 160 + output: 'false_', 161 161 }, 162 162 { 163 163 name: 'finally', 164 - output: '_finally', 164 + output: 'finally_', 165 165 }, 166 166 { 167 167 name: 'for', 168 - output: '_for', 168 + output: 'for_', 169 169 }, 170 170 { 171 171 name: 'from', 172 - output: '_from', 172 + output: 'from_', 173 173 }, 174 174 { 175 175 name: 'function', 176 - output: '_function', 176 + output: 'function_', 177 177 }, 178 178 { 179 179 name: 'if', 180 - output: '_if', 180 + output: 'if_', 181 181 }, 182 182 { 183 183 name: 'implements', 184 - output: '_implements', 184 + output: 'implements_', 185 185 }, 186 186 { 187 187 name: 'import', 188 - output: '_import', 188 + output: 'import_', 189 189 }, 190 190 { 191 191 name: 'in', 192 - output: '_in', 192 + output: 'in_', 193 193 }, 194 194 { 195 195 name: 'instanceof', 196 - output: '_instanceof', 196 + output: 'instanceof_', 197 197 }, 198 198 { 199 199 name: 'interface', 200 - output: '_interface', 200 + output: 'interface_', 201 201 }, 202 202 { 203 203 name: 'let', 204 - output: '_let', 204 + output: 'let_', 205 205 }, 206 206 { 207 207 name: 'new', 208 - output: '_new', 208 + output: 'new_', 209 209 }, 210 210 { 211 211 name: 'null', 212 - output: '_null', 212 + output: 'null_', 213 213 }, 214 214 { 215 215 name: 'package', 216 - output: '_package', 216 + output: 'package_', 217 217 }, 218 218 { 219 219 name: 'private', 220 - output: '_private', 220 + output: 'private_', 221 221 }, 222 222 { 223 223 name: 'protected', 224 - output: '_protected', 224 + output: 'protected_', 225 225 }, 226 226 { 227 227 name: 'public', 228 - output: '_public', 228 + output: 'public_', 229 229 }, 230 230 { 231 231 name: 'return', 232 - output: '_return', 232 + output: 'return_', 233 233 }, 234 234 { 235 235 name: 'static', 236 - output: '_static', 236 + output: 'static_', 237 237 }, 238 238 { 239 239 name: 'super', 240 - output: '_super', 240 + output: 'super_', 241 241 }, 242 242 { 243 243 name: 'switch', 244 - output: '_switch', 244 + output: 'switch_', 245 245 }, 246 246 { 247 247 name: 'this', 248 - output: '_this', 248 + output: 'this_', 249 249 }, 250 250 { 251 251 name: 'throw', 252 - output: '_throw', 252 + output: 'throw_', 253 253 }, 254 254 { 255 255 name: 'true', 256 - output: '_true', 256 + output: 'true_', 257 257 }, 258 258 { 259 259 name: 'try', 260 - output: '_try', 260 + output: 'try_', 261 261 }, 262 262 { 263 263 name: 'typeof', 264 - output: '_typeof', 264 + output: 'typeof_', 265 265 }, 266 266 { 267 267 name: 'var', 268 - output: '_var', 268 + output: 'var_', 269 269 }, 270 270 { 271 271 name: 'void', 272 - output: '_void', 272 + output: 'void_', 273 273 }, 274 274 { 275 275 name: 'while', 276 - output: '_while', 276 + output: 'while_', 277 277 }, 278 278 { 279 279 name: 'with', 280 - output: '_with', 280 + output: 'with_', 281 281 }, 282 282 { 283 283 name: 'yield', 284 - output: '_yield', 284 + output: 'yield_', 285 285 }, 286 286 { 287 287 name: 'global', 288 - output: '_global', 288 + output: 'global_', 289 289 }, 290 290 { 291 291 name: 'process', 292 - output: '_process', 292 + output: 'process_', 293 293 }, 294 294 { 295 295 name: 'Buffer', 296 - output: '_Buffer', 296 + output: 'Buffer_', 297 297 }, 298 298 { 299 299 name: 'any', 300 - output: '_any', 300 + output: 'any_', 301 301 }, 302 302 { 303 303 name: 'as', 304 - output: '_as', 304 + output: 'as_', 305 305 }, 306 306 { 307 307 name: 'bigint', 308 - output: '_bigint', 308 + output: 'bigint_', 309 309 }, 310 310 { 311 311 name: 'boolean', 312 - output: '_boolean', 312 + output: 'boolean_', 313 313 }, 314 314 { 315 315 name: 'namespace', 316 - output: '_namespace', 316 + output: 'namespace_', 317 317 }, 318 318 { 319 319 name: 'never', 320 - output: '_never', 320 + output: 'never_', 321 321 }, 322 322 { 323 323 name: 'null', 324 - output: '_null', 324 + output: 'null_', 325 325 }, 326 326 { 327 327 name: 'number', 328 - output: '_number', 328 + output: 'number_', 329 329 }, 330 330 { 331 331 name: 'string', 332 - output: '_string', 332 + output: 'string_', 333 333 }, 334 334 { 335 335 name: 'symbol', 336 - output: '_symbol', 336 + output: 'symbol_', 337 337 }, 338 338 { 339 339 name: 'type', 340 - output: '_type', 340 + output: 'type_', 341 341 }, 342 342 { 343 343 name: 'undefined', 344 - output: '_undefined', 344 + output: 'undefined_', 345 345 }, 346 346 { 347 347 name: 'unknown', 348 - output: '_unknown', 348 + output: 'unknown_', 349 349 }, 350 350 { 351 351 name: 'void', 352 - output: '_void', 352 + output: 'void_', 353 353 }, 354 354 ]; 355 355
+1 -1
packages/openapi-ts/src/ts-dsl/utils/name.ts
··· 52 52 } 53 53 54 54 if (reserved['~values'].has(sanitized)) { 55 - sanitized = `_${sanitized}`; 55 + sanitized = `${sanitized}_`; 56 56 } 57 57 58 58 return sanitized || '_';