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

Merge pull request #3060 from hey-api/refactor/plugin-node

refactor: clean up plugin node methods

authored by

Lubos and committed by
GitHub
287f8918 e5520867

+80 -77
+2 -2
packages/openapi-ts/src/plugins/@angular/common/httpRequests.ts
··· 156 156 ), 157 157 ) 158 158 .do(...currentClass.nodes); 159 - plugin.addNode(node); 159 + plugin.node(node); 160 160 161 161 generatedClasses.add(currentClass.className); 162 162 }; ··· 195 195 plugin, 196 196 symbol, 197 197 }); 198 - plugin.addNode(node); 198 + plugin.node(node); 199 199 }, 200 200 { 201 201 order: 'declarations',
+2 -2
packages/openapi-ts/src/plugins/@angular/common/httpResources.ts
··· 153 153 ), 154 154 ) 155 155 .do(...currentClass.nodes); 156 - plugin.addNode(node); 156 + plugin.node(node); 157 157 158 158 generatedClasses.add(currentClass.className); 159 159 }; ··· 185 185 plugin, 186 186 symbol, 187 187 }); 188 - plugin.addNode(node); 188 + plugin.node(node); 189 189 }, 190 190 { 191 191 order: 'declarations',
+1 -1
packages/openapi-ts/src/plugins/@hey-api/client-core/client.ts
··· 99 99 (c) => c.call(...createConfigParameters), 100 100 ), 101 101 ); 102 - plugin.addNode(statement); 102 + plugin.node(statement); 103 103 };
+1 -1
packages/openapi-ts/src/plugins/@hey-api/client-core/createClientConfig.ts
··· 61 61 ), 62 62 ), 63 63 ); 64 - plugin.addNode(typeCreateClientConfig); 64 + plugin.node(typeCreateClientConfig); 65 65 };
+3 -3
packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts
··· 390 390 }), 391 391 ).as('const'), 392 392 ); 393 - plugin.addNode(statement); 393 + plugin.node(statement); 394 394 } 395 395 }; 396 396 ··· 430 430 }), 431 431 ).as('const'), 432 432 ); 433 - plugin.addNode(statement); 433 + plugin.node(statement); 434 434 } 435 435 }; 436 436 ··· 470 470 }), 471 471 ).as('const'), 472 472 ); 473 - plugin.addNode(statement); 473 + plugin.node(statement); 474 474 } 475 475 }; 476 476
+5 -5
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/class.ts
··· 306 306 ); 307 307 308 308 const heyApiClientIndex = plugin.config.instance 309 - ? plugin.addNode(null) 309 + ? plugin.node(null) 310 310 : undefined; 311 311 const symbolHeyApiClient = 312 312 heyApiClientIndex !== undefined ··· 320 320 }) 321 321 : undefined; 322 322 const heyApiRegistryIndex = plugin.config.instance 323 - ? plugin.addNode(null) 323 + ? plugin.node(null) 324 324 : undefined; 325 325 326 326 const generateClass = (currentClass: SdkClassEntry) => { ··· 404 404 plugin, 405 405 symbol: symbolHeyApiClient, 406 406 }); 407 - plugin.updateNode(heyApiClientIndex, node); 407 + plugin.node(node, heyApiClientIndex); 408 408 } 409 409 410 410 const symbol = plugin.symbol(resourceId, { ··· 465 465 sdkSymbol: symbol, 466 466 symbol: symbolRegistry, 467 467 }); 468 - plugin.updateNode(heyApiRegistryIndex, node); 468 + plugin.node(node, heyApiRegistryIndex); 469 469 const registryNode = $.field(registryName, (f) => 470 470 f 471 471 .public() ··· 489 489 ), 490 490 ) 491 491 .do(...currentClass.nodes); 492 - plugin.addNode(node); 492 + plugin.node(node); 493 493 }; 494 494 495 495 for (const sdkClass of sdkClasses.values()) {
+1 -1
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/functions.ts
··· 121 121 ) 122 122 .do(...statements), 123 123 ); 124 - plugin.addNode(node); 124 + plugin.node(node); 125 125 }, 126 126 { 127 127 order: 'declarations',
+1 -1
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/typeOptions.ts
··· 109 109 ), 110 110 ), 111 111 ); 112 - plugin.addNode(typeOptions); 112 + plugin.node(typeOptions); 113 113 };
+2 -2
packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts
··· 96 96 .param(dataVariableName, (p) => p.type('any')) 97 97 .do(...nodes), 98 98 ); 99 - plugin.addNode(node); 99 + plugin.node(node); 100 100 } 101 101 } finally { 102 102 buildingSymbols.delete(symbol.id); ··· 336 336 .returns($.type('Promise').generic(symbolResponse)) 337 337 .do(...nodes), 338 338 ); 339 - plugin.addNode(value); 339 + plugin.node(value); 340 340 }, 341 341 { 342 342 order: 'declarations',
+1 -1
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts
··· 76 76 p.type($.type.or(...types)), 77 77 ), 78 78 ); 79 - plugin.updateNode(nodeIndex, node); 79 + plugin.node(node, nodeIndex); 80 80 };
+4 -4
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts
··· 134 134 ), 135 135 ).as('const'), 136 136 ); 137 - plugin.addNode(objectNode); 137 + plugin.node(objectNode); 138 138 139 139 const symbol = plugin.registerSymbol({ 140 140 meta: { ··· 157 157 .type( 158 158 $.type(symbol).idx($.type(symbol).typeofType().keyof()).typeofType(), 159 159 ); 160 - plugin.addNode(node); 160 + plugin.node(node); 161 161 return; 162 162 } else if ( 163 163 plugin.config.enums.mode === 'typescript' || ··· 193 193 .value($.fromValue(item.schema.const)), 194 194 ), 195 195 ); 196 - plugin.addNode(enumNode); 196 + plugin.node(enumNode); 197 197 return; 198 198 } 199 199 } ··· 218 218 .export() 219 219 .$if(createSchemaComment(schema), (t, v) => t.doc(v)) 220 220 .type(type); 221 - plugin.addNode(node); 221 + plugin.node(node); 222 222 };
+5 -5
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts
··· 147 147 state, 148 148 }), 149 149 ); 150 - plugin.addNode(node); 150 + plugin.node(node); 151 151 }; 152 152 153 153 export const operationToType = ({ ··· 188 188 state, 189 189 }), 190 190 ); 191 - plugin.addNode(node); 191 + plugin.node(node); 192 192 193 193 if (error) { 194 194 const symbol = plugin.registerSymbol({ ··· 213 213 .alias(symbol) 214 214 .export() 215 215 .type($.type(symbolErrors).idx($.type(symbolErrors).keyof())); 216 - plugin.addNode(node); 216 + plugin.node(node); 217 217 } 218 218 } 219 219 ··· 243 243 state, 244 244 }), 245 245 ); 246 - plugin.addNode(node); 246 + plugin.node(node); 247 247 248 248 if (response) { 249 249 const symbol = plugin.registerSymbol({ ··· 268 268 .alias(symbol) 269 269 .export() 270 270 .type($.type(symbolResponses).idx($.type(symbolResponses).keyof())); 271 - plugin.addNode(node); 271 + plugin.node(node); 272 272 } 273 273 } 274 274 };
+2 -2
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts
··· 55 55 state, 56 56 }), 57 57 ); 58 - plugin.addNode(node); 58 + plugin.node(node); 59 59 60 60 data.properties.body = { symbolRef: symbolWebhookPayload }; 61 61 dataRequired.push('body'); ··· 99 99 state, 100 100 }), 101 101 ); 102 - plugin.addNode(node); 102 + plugin.node(node); 103 103 104 104 return symbolWebhookRequest; 105 105 };
+3 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts
··· 86 86 87 87 export const handlerV1: HeyApiTypeScriptPlugin['Handler'] = ({ plugin }) => { 88 88 // reserve node for ClientOptions 89 - const nodeClientIndex = plugin.addNode(null); 89 + const nodeClientIndex = plugin.node(null); 90 90 // reserve node for Webhooks 91 - const nodeWebhooksIndex = plugin.addNode(null); 91 + const nodeWebhooksIndex = plugin.node(null); 92 92 93 93 const servers: Array<IR.ServerObject> = []; 94 94 const webhooks: Array<Symbol> = []; ··· 176 176 .alias(symbol) 177 177 .export() 178 178 .type($.type.or(...webhooks)); 179 - plugin.updateNode(nodeWebhooksIndex, node); 179 + plugin.node(node, nodeWebhooksIndex); 180 180 } 181 181 };
+2 -2
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/string.ts
··· 60 60 .type( 61 61 $.type.template().add($.type('T')).add('_').add($.type('string')), 62 62 ); 63 - plugin.addNode(nodeTypeId); 63 + plugin.node(nodeTypeId); 64 64 } 65 65 66 66 const symbolTypeId = plugin.referenceSymbol(queryTypeId); ··· 75 75 .alias(symbolTypeName) 76 76 .export() 77 77 .type($.type(symbolTypeId).generic($.type.literal(type))); 78 - plugin.addNode(node); 78 + plugin.node(node); 79 79 } 80 80 const symbol = plugin.referenceSymbol(query); 81 81 return $.type(symbol);
+1 -1
packages/openapi-ts/src/plugins/@pinia/colada/mutationOptions.ts
··· 92 92 ) 93 93 .do($.return(mutationOpts)), 94 94 ); 95 - plugin.addNode(statement); 95 + plugin.node(statement); 96 96 };
+2 -2
packages/openapi-ts/src/plugins/@pinia/colada/queryKey.ts
··· 123 123 $.return($.array($('params'))), 124 124 ), 125 125 ); 126 - plugin.addNode(fn); 126 + plugin.node(fn); 127 127 }; 128 128 129 129 const createQueryKeyLiteral = ({ ··· 197 197 ), 198 198 ), 199 199 ); 200 - plugin.addNode(queryKeyType); 200 + plugin.node(queryKeyType); 201 201 }; 202 202 203 203 export const queryKeyStatement = ({
+2 -2
packages/openapi-ts/src/plugins/@pinia/colada/queryOptions.ts
··· 61 61 plugin, 62 62 symbol: symbolQueryKey, 63 63 }); 64 - plugin.addNode(node); 64 + plugin.node(node); 65 65 keyExpression = $(symbolQueryKey).call(optionsParamName); 66 66 } else { 67 67 const symbolCreateQueryKey = plugin.referenceSymbol({ ··· 152 152 .do($.return(queryOpts)), 153 153 ), 154 154 ); 155 - plugin.addNode(statement); 155 + plugin.node(statement); 156 156 };
+2 -2
packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts
··· 99 99 $.return($.array().element($('params'))), 100 100 ), 101 101 ); 102 - plugin.addNode(fn); 102 + plugin.node(fn); 103 103 }; 104 104 105 105 const createQueryKeyLiteral = ({ ··· 166 166 ), 167 167 ), 168 168 ); 169 - plugin.addNode(queryKeyType); 169 + plugin.node(queryKeyType); 170 170 }; 171 171 172 172 export const queryKeyStatement = ({
+3 -3
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/infiniteQueryOptions.ts
··· 100 100 $.return($('params').as('unknown').as($('page').typeofType())), 101 101 ), 102 102 ); 103 - plugin.addNode(fn); 103 + plugin.node(fn); 104 104 }; 105 105 106 106 export const createInfiniteQueryOptions = ({ ··· 196 196 symbol: symbolInfiniteQueryKey, 197 197 typeQueryKey, 198 198 }); 199 - plugin.addNode(node); 199 + plugin.node(node); 200 200 201 201 const awaitSdkFn = $(queryFn) 202 202 .call( ··· 288 288 ), 289 289 ), 290 290 ); 291 - plugin.addNode(statement); 291 + plugin.node(statement); 292 292 };
+1 -1
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/mutationOptions.ts
··· 87 87 $(mutationOptionsFn).return(), 88 88 ), 89 89 ); 90 - plugin.addNode(statement); 90 + plugin.node(statement); 91 91 };
+2 -2
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/queryOptions.ts
··· 65 65 plugin, 66 66 symbol: symbolQueryKey, 67 67 }); 68 - plugin.addNode(node); 68 + plugin.node(node); 69 69 70 70 const typeResponse = useTypeResponse({ operation, plugin }); 71 71 ··· 142 142 .return(), 143 143 ), 144 144 ); 145 - plugin.addNode(statement); 145 + plugin.node(statement); 146 146 };
+1 -1
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/useQuery.ts
··· 68 68 .return(), 69 69 ), 70 70 ); 71 - plugin.addNode(statement); 71 + plugin.node(statement); 72 72 };
+2 -2
packages/openapi-ts/src/plugins/arktype/shared/export.ts
··· 40 40 // : undefined, 41 41 // ) 42 42 .assign($(type).call(ast.def ? $.literal(ast.def) : ast.expression)); 43 - plugin.addNode(statement); 43 + plugin.node(statement); 44 44 45 45 if (typeInferSymbol) { 46 46 const inferType = $.type 47 47 .alias(typeInferSymbol) 48 48 .export() 49 49 .type($.type(symbol).attr(identifiers.type.infer).typeofType()); 50 - plugin.addNode(inferType); 50 + plugin.node(inferType); 51 51 } 52 52 };
+1 -1
packages/openapi-ts/src/plugins/fastify/plugin.ts
··· 161 161 ); 162 162 163 163 const node = $.type.alias(symbolRouteHandlers).export().type(type); 164 - plugin.addNode(node); 164 + plugin.node(node); 165 165 };
+24 -21
packages/openapi-ts/src/plugins/shared/utils/instance.ts
··· 106 106 this.package = props.context.package; 107 107 } 108 108 109 - addNode(node: Node | null): number { 110 - for (const hook of this.eventHooks['node:set:before']) { 111 - hook({ node, plugin: this }); 112 - } 113 - const index = this.gen.nodes.add(node); 114 - for (const hook of this.eventHooks['node:set:after']) { 115 - hook({ node, plugin: this }); 116 - } 117 - return index; 118 - } 119 - updateNode(index: number, node: Node | null): void { 120 - for (const hook of this.eventHooks['node:set:before']) { 121 - hook({ node, plugin: this }); 122 - } 123 - const result = this.gen.nodes.update(index, node); 124 - for (const hook of this.eventHooks['node:set:after']) { 125 - hook({ node, plugin: this }); 126 - } 127 - return result; 128 - } 129 - 130 109 /** 131 110 * Iterates over various input elements as specified by the event types, in 132 111 * a specific order: servers, schemas, parameters, request bodies, then ··· 318 297 319 298 isSymbolRegistered(identifier: SymbolIdentifier): boolean { 320 299 return this.gen.symbols.isRegistered(identifier); 300 + } 301 + 302 + /** 303 + * Sets or adds a node to the project graph. 304 + * 305 + * @param node The node to be added or updated in the project graph. 306 + * @param index The index at which to update the node. If undefined, the node will be added. 307 + * @returns The index of the added node or void if updated. 308 + */ 309 + node<T extends number | undefined = undefined>( 310 + node: Node | null, 311 + index?: T, 312 + ): T extends number ? void : number { 313 + for (const hook of this.eventHooks['node:set:before']) { 314 + hook({ node, plugin: this }); 315 + } 316 + const result = 317 + index !== undefined 318 + ? this.gen.nodes.update(index, node) 319 + : this.gen.nodes.add(node); 320 + for (const hook of this.eventHooks['node:set:after']) { 321 + hook({ node, plugin: this }); 322 + } 323 + return result as T extends number ? void : number; 321 324 } 322 325 323 326 querySymbol(filter: SymbolMeta): Symbol | undefined {
+1 -1
packages/openapi-ts/src/plugins/swr/v2/useSwr.ts
··· 64 64 .return(), 65 65 ), 66 66 ); 67 - plugin.addNode(statement); 67 + plugin.node(statement); 68 68 };
+1 -1
packages/openapi-ts/src/plugins/valibot/shared/export.ts
··· 33 33 c.type($.type(v).attr(ast.typeName || identifiers.types.GenericSchema)), 34 34 ) 35 35 .assign(pipesToAst({ pipes: ast.pipes, plugin })); 36 - plugin.addNode(statement); 36 + plugin.node(statement); 37 37 };
+2 -2
packages/openapi-ts/src/plugins/zod/shared/export.ts
··· 33 33 ) 34 34 .$if(ast.typeName, (c, v) => c.type($.type(z).attr(v))) 35 35 .assign(ast.expression); 36 - plugin.addNode(statement); 36 + plugin.node(statement); 37 37 38 38 if (typeInferSymbol) { 39 39 const inferType = $.type 40 40 .alias(typeInferSymbol) 41 41 .export() 42 42 .type($.type(z).attr(identifiers.infer).generic($(symbol).typeofType())); 43 - plugin.addNode(inferType); 43 + plugin.node(inferType); 44 44 } 45 45 };