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

Merge pull request #2972 from hey-api/fix/tanstack-query-pretty-2

@tanstack/query: prettier mutation options

authored by

Lubos and committed by
GitHub
c2a58894 cce7e074

+77 -131
+5
.changeset/small-readers-cut.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + **@tanstack/query**: prettier mutation options
+8 -3
dev/openapi-ts.config.ts
··· 310 310 // infiniteQueryOptions: { 311 311 // name: '{{name}}IQO', 312 312 // }, 313 - // mutationOptions: { 314 - // name: '{{name}}MO', 315 - // }, 313 + mutationOptions: { 314 + // name: '{{name}}MO', 315 + meta() { 316 + return { 317 + custom: 'value', 318 + }; 319 + }, 320 + }, 316 321 name: '@tanstack/react-query', 317 322 // queryKeys: { 318 323 // name: '{{name}}QK',
+1 -1
packages/openapi-ts/src/plugins/@hey-api/sdk/shared/class.ts
··· 257 257 const functionNode = $.method(entry.methodName, (m) => 258 258 m 259 259 .$if(createOperationComment({ operation }), (m, v) => 260 - m.describe(v as Array<string>), 260 + m.describe(v as ReadonlyArray<string>), 261 261 ) 262 262 .public() 263 263 .static(!isAngularClient && !plugin.config.instance)
+42 -94
packages/openapi-ts/src/plugins/@tanstack/query-core/mutationOptions.ts
··· 1 - import type ts from 'typescript'; 2 - 3 1 import type { IR } from '~/ir/types'; 4 2 import { buildName } from '~/openApi/shared/utils/name'; 5 3 import { createOperationComment } from '~/plugins/shared/utils/operation'; 6 - import { tsc } from '~/tsc'; 4 + import type { TsDsl } from '~/ts-dsl'; 5 + import { $ } from '~/ts-dsl'; 7 6 8 7 import { handleMeta } from './shared/meta'; 9 8 import { useTypeData, useTypeError, useTypeResponse } from './shared/useType'; ··· 31 30 32 31 const fnOptions = 'fnOptions'; 33 32 34 - const awaitSdkExpression = tsc.awaitExpression({ 35 - expression: tsc.callExpression({ 36 - functionName: queryFn, 37 - parameters: [ 38 - tsc.objectExpression({ 39 - multiLine: true, 40 - obj: [ 41 - { 42 - spread: 'options', 43 - }, 44 - { 45 - spread: fnOptions, 46 - }, 47 - { 48 - key: 'throwOnError', 49 - value: true, 50 - }, 51 - ], 52 - }), 53 - ], 54 - }), 55 - }); 56 - 57 - const statements: Array<ts.Statement> = []; 33 + const awaitSdkFn = $(queryFn) 34 + .call( 35 + $.object() 36 + .spread('options') 37 + .spread(fnOptions) 38 + .prop('throwOnError', $.literal(true)), 39 + ) 40 + .await(); 58 41 42 + const statements: Array<TsDsl<any>> = []; 59 43 if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') { 60 - statements.push( 61 - tsc.returnVariable({ 62 - expression: awaitSdkExpression, 63 - }), 64 - ); 44 + statements.push($.return(awaitSdkFn)); 65 45 } else { 66 46 statements.push( 67 - tsc.constVariable({ 68 - destructure: true, 69 - expression: awaitSdkExpression, 70 - name: 'data', 71 - }), 72 - tsc.returnVariable({ 73 - expression: 'data', 74 - }), 47 + $.const().object('data').assign(awaitSdkFn), 48 + $.return('data'), 75 49 ); 76 50 } 77 51 78 - const mutationOptionsObj: Array<{ key: string; value: ts.Expression }> = [ 79 - { 80 - key: 'mutationFn', 81 - value: tsc.arrowFunction({ 82 - async: true, 83 - multiLine: true, 84 - parameters: [ 85 - { 86 - name: fnOptions, 87 - }, 88 - ], 89 - statements, 90 - }), 91 - }, 92 - ]; 93 - 94 52 const meta = handleMeta(plugin, operation, 'mutationOptions'); 95 - 96 - if (meta) { 97 - mutationOptionsObj.push({ 98 - key: 'meta', 99 - value: meta, 100 - }); 101 - } 102 - 103 53 const mutationOptionsFn = 'mutationOptions'; 104 - const expression = tsc.arrowFunction({ 105 - parameters: [ 106 - { 107 - isRequired: false, 108 - name: 'options', 109 - type: `Partial<${typeData}>`, 110 - }, 111 - ], 112 - returnType: mutationType, 113 - statements: [ 114 - tsc.constVariable({ 115 - expression: tsc.objectExpression({ 116 - obj: mutationOptionsObj, 117 - }), 118 - name: mutationOptionsFn, 119 - typeName: mutationType, 120 - }), 121 - tsc.returnVariable({ 122 - expression: mutationOptionsFn, 123 - }), 124 - ], 125 - }); 126 54 const symbolMutationOptions = plugin.registerSymbol({ 127 55 exported: true, 128 56 name: buildName({ ··· 130 58 name: operation.id, 131 59 }), 132 60 }); 133 - const statement = tsc.constVariable({ 134 - comment: plugin.config.comments 135 - ? createOperationComment({ operation }) 136 - : undefined, 137 - exportConst: symbolMutationOptions.exported, 138 - expression, 139 - name: symbolMutationOptions.placeholder, 140 - }); 61 + const statement = $.const(symbolMutationOptions.placeholder) 62 + .export(symbolMutationOptions.exported) 63 + .$if( 64 + plugin.config.comments && createOperationComment({ operation }), 65 + (c, v) => c.describe(v as ReadonlyArray<string>), 66 + ) 67 + .assign( 68 + $.func() 69 + .param('options', (p) => p.optional().type(`Partial<${typeData}>`)) 70 + .returns(mutationType) 71 + .do( 72 + $.const(mutationOptionsFn) 73 + .type(mutationType) 74 + .assign( 75 + $.object() 76 + .pretty() 77 + .prop( 78 + 'mutationFn', 79 + $.func() 80 + .async() 81 + .param(fnOptions) 82 + .do(...statements), 83 + ) 84 + .$if(meta, (c, v) => c.prop('meta', v)), 85 + ), 86 + $(mutationOptionsFn).return(), 87 + ), 88 + ); 141 89 plugin.setSymbolValue(symbolMutationOptions, statement); 142 90 };
+1 -1
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/queryOptions.ts
··· 126 126 .export(symbolQueryOptionsFn.exported) 127 127 .$if( 128 128 plugin.config.comments && createOperationComment({ operation }), 129 - (c, v) => c.describe(v as Array<string>), 129 + (c, v) => c.describe(v as ReadonlyArray<string>), 130 130 ) 131 131 .assign( 132 132 $.func()
+18 -30
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/useQuery.ts
··· 5 5 hasOperationSse, 6 6 isOperationOptionsRequired, 7 7 } from '~/plugins/shared/utils/operation'; 8 - import { tsc } from '~/tsc'; 8 + import { $ } from '~/ts-dsl'; 9 9 10 10 import { useTypeData } from '../shared/useType'; 11 11 import type { PluginInstance } from '../types'; ··· 53 53 role: 'queryOptions', 54 54 tool: plugin.name, 55 55 }); 56 - const statement = tsc.constVariable({ 57 - comment: plugin.config.comments 58 - ? createOperationComment({ operation }) 59 - : undefined, 60 - exportConst: symbolUseQueryFn.exported, 61 - expression: tsc.arrowFunction({ 62 - parameters: [ 63 - { 64 - isRequired: isRequiredOptions, 65 - name: optionsParamName, 66 - type: typeData, 67 - }, 68 - ], 69 - statements: [ 70 - tsc.returnStatement({ 71 - expression: tsc.callExpression({ 72 - functionName: symbolUseQuery.placeholder, 73 - parameters: [ 74 - tsc.callExpression({ 75 - functionName: symbolQueryOptionsFn.placeholder, 76 - parameters: [optionsParamName], 77 - }), 78 - ], 79 - }), 80 - }), 81 - ], 82 - }), 83 - name: symbolUseQueryFn.placeholder, 84 - }); 56 + const statement = $.const(symbolUseQueryFn.placeholder) 57 + .export(symbolUseQueryFn.exported) 58 + .$if( 59 + plugin.config.comments && createOperationComment({ operation }), 60 + (c, v) => c.describe(v as ReadonlyArray<string>), 61 + ) 62 + .assign( 63 + $.func() 64 + .param(optionsParamName, (p) => 65 + p.optional(!isRequiredOptions).type(typeData), 66 + ) 67 + .do( 68 + $(symbolUseQuery.placeholder) 69 + .call($(symbolQueryOptionsFn.placeholder).call(optionsParamName)) 70 + .return(), 71 + ), 72 + ); 85 73 plugin.setSymbolValue(symbolUseQueryFn, statement); 86 74 };
+1 -1
packages/openapi-ts/src/plugins/swr/v2/useSwr.ts
··· 52 52 .export(symbolUseQueryFn.exported) 53 53 .$if( 54 54 plugin.config.comments && createOperationComment({ operation }), 55 - (c, v) => c.describe(v as Array<string>), 55 + (c, v) => c.describe(v as ReadonlyArray<string>), 56 56 ) 57 57 .assign( 58 58 $.func().do(
+1 -1
packages/openapi-ts/src/plugins/valibot/shared/export.ts
··· 27 27 const statement = $.const(symbol.placeholder) 28 28 .export(symbol.exported) 29 29 .$if(plugin.config.comments && createSchemaComment({ schema }), (c, v) => 30 - c.describe(v as Array<string>), 30 + c.describe(v as ReadonlyArray<string>), 31 31 ) 32 32 .$if(state.hasLazyExpression.value, (c) => 33 33 c.type(