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

Merge pull request #3067 from hey-api/chore/dsl-lazy

chore: add lazy dsl node

authored by

Lubos and committed by
GitHub
8d5d901c e2f194d5

+95 -49
+11 -9
packages/openapi-ts/src/plugins/@pinia/colada/mutationOptions.ts
··· 31 31 const options = plugin.symbol('options'); 32 32 const fnOptions = plugin.symbol('vars'); 33 33 34 - const awaitSdkFn = $(queryFn) 35 - .call( 36 - $.object() 37 - .pretty() 38 - .spread(options) 39 - .spread(fnOptions) 40 - .prop('throwOnError', $.literal(true)), 41 - ) 42 - .await(); 34 + const awaitSdkFn = $.lazy(() => 35 + $(queryFn) 36 + .call( 37 + $.object() 38 + .pretty() 39 + .spread(options) 40 + .spread(fnOptions) 41 + .prop('throwOnError', $.literal(true)), 42 + ) 43 + .await(), 44 + ); 43 45 44 46 const statements: Array<ReturnType<typeof $.var | typeof $.return>> = []; 45 47
+10 -8
packages/openapi-ts/src/plugins/@pinia/colada/queryOptions.ts
··· 88 88 const client = getClientPlugin(plugin.context.config); 89 89 const isNuxtClient = client.name === '@hey-api/client-nuxt'; 90 90 const typeData = getPublicTypeData({ isNuxtClient, operation, plugin }); 91 - const awaitSdkFn = $(queryFn) 92 - .call( 93 - $.object() 94 - .spread(optionsParamName) 95 - .spread(fnOptions) 96 - .prop('throwOnError', $.literal(true)), 97 - ) 98 - .await(); 91 + const awaitSdkFn = $.lazy(() => 92 + $(queryFn) 93 + .call( 94 + $.object() 95 + .spread(optionsParamName) 96 + .spread(fnOptions) 97 + .prop('throwOnError', $.literal(true)), 98 + ) 99 + .await(), 100 + ); 99 101 100 102 const statements: Array<ReturnType<typeof $.return | typeof $.const>> = []; 101 103 if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') {
+11 -9
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/infiniteQueryOptions.ts
··· 198 198 }); 199 199 plugin.node(node); 200 200 201 - const awaitSdkFn = $(queryFn) 202 - .call( 203 - $.object() 204 - .spread('options') 205 - .spread('params') 206 - .prop('signal', $('signal')) 207 - .prop('throwOnError', $.literal(true)), 208 - ) 209 - .await(); 201 + const awaitSdkFn = $.lazy(() => 202 + $(queryFn) 203 + .call( 204 + $.object() 205 + .spread('options') 206 + .spread('params') 207 + .prop('signal', $('signal')) 208 + .prop('throwOnError', $.literal(true)), 209 + ) 210 + .await(), 211 + ); 210 212 211 213 const symbolCreateInfiniteParams = plugin.referenceSymbol({ 212 214 category: 'utility',
+10 -8
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/mutationOptions.ts
··· 30 30 31 31 const fnOptions = 'fnOptions'; 32 32 33 - const awaitSdkFn = $(queryFn) 34 - .call( 35 - $.object() 36 - .spread('options') 37 - .spread(fnOptions) 38 - .prop('throwOnError', $.literal(true)), 39 - ) 40 - .await(); 33 + const awaitSdkFn = $.lazy(() => 34 + $(queryFn) 35 + .call( 36 + $.object() 37 + .spread('options') 38 + .spread(fnOptions) 39 + .prop('throwOnError', $.literal(true)), 40 + ) 41 + .await(), 42 + ); 41 43 42 44 const statements: Array<TsDsl<any>> = []; 43 45 if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') {
+11 -9
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/queryOptions.ts
··· 69 69 70 70 const typeResponse = useTypeResponse({ operation, plugin }); 71 71 72 - const awaitSdkFn = $(queryFn) 73 - .call( 74 - $.object() 75 - .spread(optionsParamName) 76 - .spread($('queryKey').attr(0)) 77 - .prop('signal', $('signal')) 78 - .prop('throwOnError', $.literal(true)), 79 - ) 80 - .await(); 72 + const awaitSdkFn = $.lazy(() => 73 + $(queryFn) 74 + .call( 75 + $.object() 76 + .spread(optionsParamName) 77 + .spread($('queryKey').attr(0)) 78 + .prop('signal', $('signal')) 79 + .prop('throwOnError', $.literal(true)), 80 + ) 81 + .await(), 82 + ); 81 83 82 84 const statements: Array<TsDsl<any>> = []; 83 85 if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') {
+5 -3
packages/openapi-ts/src/plugins/swr/v2/useSwr.ts
··· 33 33 }), 34 34 }); 35 35 36 - const awaitSdkFn = $(queryFn) 37 - .call($.object().prop('throwOnError', $.literal(true))) 38 - .await(); 36 + const awaitSdkFn = $.lazy(() => 37 + $(queryFn) 38 + .call($.object().prop('throwOnError', $.literal(true))) 39 + .await(), 40 + ); 39 41 40 42 const statements: Array<TsDsl<any>> = []; 41 43 if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') {
+11 -3
packages/openapi-ts/src/ts-dsl/index.ts
··· 1 + import type ts from 'typescript'; 2 + 1 3 import { ClassTsDsl } from './decl/class'; 2 4 import { DecoratorTsDsl } from './decl/decorator'; 3 5 import { EnumTsDsl } from './decl/enum'; ··· 56 58 import { TypeQueryTsDsl } from './type/query'; 57 59 import { TypeTemplateTsDsl } from './type/template'; 58 60 import { TypeTupleTsDsl } from './type/tuple'; 61 + import { LazyTsDsl } from './utils/lazy'; 59 62 60 - const base = { 63 + const tsDsl = { 61 64 /** Creates an array literal expression (e.g. `[1, 2, 3]`). */ 62 65 array: (...args: ConstructorParameters<typeof ArrayTsDsl>) => 63 66 new ArrayTsDsl(...args), ··· 151 154 init: (...args: ConstructorParameters<typeof InitTsDsl>) => 152 155 new InitTsDsl(...args), 153 156 157 + /** Creates a lazy, context-aware node with deferred evaluation. */ 158 + lazy: <T extends ts.Node>( 159 + ...args: ConstructorParameters<typeof LazyTsDsl<T>> 160 + ) => new LazyTsDsl<T>(...args), 161 + 154 162 /** Creates a let variable declaration (`let`). */ 155 163 let: (...args: ConstructorParameters<typeof VarTsDsl>) => 156 164 new VarTsDsl(...args).let(), ··· 324 332 325 333 export const $ = Object.assign( 326 334 (...args: ConstructorParameters<typeof ExprTsDsl>) => new ExprTsDsl(...args), 327 - base, 335 + tsDsl, 328 336 ); 329 337 330 338 export type DollarTsDsl = { ··· 342 350 * 343 351 * Returns: 344 352 * - A new `ExprTsDsl` instance when called directly. 345 - * - The `base` factory object for constructing more specific nodes. 353 + * - The `tsDsl` object for constructing more specific nodes. 346 354 */ 347 355 $: typeof $; 348 356 };
+26
packages/openapi-ts/src/ts-dsl/utils/lazy.ts
··· 1 + import type { AnalysisContext } from '@hey-api/codegen-core'; 2 + import type ts from 'typescript'; 3 + 4 + import { TsDsl } from '../base'; 5 + 6 + export type LazyThunk<T extends ts.Node> = () => TsDsl<T>; 7 + 8 + export class LazyTsDsl<T extends ts.Node = ts.Node> extends TsDsl<T> { 9 + readonly '~dsl' = 'LazyTsDsl'; 10 + 11 + private _thunk: LazyThunk<T>; 12 + 13 + constructor(thunk: LazyThunk<T>) { 14 + super(); 15 + this._thunk = thunk; 16 + } 17 + 18 + override analyze(ctx: AnalysisContext): void { 19 + super.analyze(ctx); 20 + ctx.analyze(this._thunk()); 21 + } 22 + 23 + override toAst() { 24 + return this._thunk().toAst(); 25 + } 26 + }