这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-readers-cut.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

**@tanstack/query**: prettier mutation options
11 changes: 8 additions & 3 deletions dev/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,14 @@ export default defineConfig(() => {
// infiniteQueryOptions: {
// name: '{{name}}IQO',
// },
// mutationOptions: {
// name: '{{name}}MO',
// },
mutationOptions: {
// name: '{{name}}MO',
meta() {
return {
custom: 'value',
};
},
},
name: '@tanstack/react-query',
// queryKeys: {
// name: '{{name}}QK',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export const generateClassSdk = ({
const functionNode = $.method(entry.methodName, (m) =>
m
.$if(createOperationComment({ operation }), (m, v) =>
m.describe(v as Array<string>),
m.describe(v as ReadonlyArray<string>),
)
.public()
.static(!isAngularClient && !plugin.config.instance)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type ts from 'typescript';

import type { IR } from '~/ir/types';
import { buildName } from '~/openApi/shared/utils/name';
import { createOperationComment } from '~/plugins/shared/utils/operation';
import { tsc } from '~/tsc';
import type { TsDsl } from '~/ts-dsl';
import { $ } from '~/ts-dsl';

import { handleMeta } from './shared/meta';
import { useTypeData, useTypeError, useTypeResponse } from './shared/useType';
Expand Down Expand Up @@ -31,112 +30,61 @@ export const createMutationOptions = ({

const fnOptions = 'fnOptions';

const awaitSdkExpression = tsc.awaitExpression({
expression: tsc.callExpression({
functionName: queryFn,
parameters: [
tsc.objectExpression({
multiLine: true,
obj: [
{
spread: 'options',
},
{
spread: fnOptions,
},
{
key: 'throwOnError',
value: true,
},
],
}),
],
}),
});

const statements: Array<ts.Statement> = [];
const awaitSdkFn = $(queryFn)
.call(
$.object()
.spread('options')
.spread(fnOptions)
.prop('throwOnError', $.literal(true)),
)
.await();

const statements: Array<TsDsl<any>> = [];
if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') {
statements.push(
tsc.returnVariable({
expression: awaitSdkExpression,
}),
);
statements.push($.return(awaitSdkFn));
} else {
statements.push(
tsc.constVariable({
destructure: true,
expression: awaitSdkExpression,
name: 'data',
}),
tsc.returnVariable({
expression: 'data',
}),
$.const().object('data').assign(awaitSdkFn),
$.return('data'),
);
}

const mutationOptionsObj: Array<{ key: string; value: ts.Expression }> = [
{
key: 'mutationFn',
value: tsc.arrowFunction({
async: true,
multiLine: true,
parameters: [
{
name: fnOptions,
},
],
statements,
}),
},
];

const meta = handleMeta(plugin, operation, 'mutationOptions');

if (meta) {
mutationOptionsObj.push({
key: 'meta',
value: meta,
});
}

const mutationOptionsFn = 'mutationOptions';
const expression = tsc.arrowFunction({
parameters: [
{
isRequired: false,
name: 'options',
type: `Partial<${typeData}>`,
},
],
returnType: mutationType,
statements: [
tsc.constVariable({
expression: tsc.objectExpression({
obj: mutationOptionsObj,
}),
name: mutationOptionsFn,
typeName: mutationType,
}),
tsc.returnVariable({
expression: mutationOptionsFn,
}),
],
});
const symbolMutationOptions = plugin.registerSymbol({
exported: true,
name: buildName({
config: plugin.config.mutationOptions,
name: operation.id,
}),
});
const statement = tsc.constVariable({
comment: plugin.config.comments
? createOperationComment({ operation })
: undefined,
exportConst: symbolMutationOptions.exported,
expression,
name: symbolMutationOptions.placeholder,
});
const statement = $.const(symbolMutationOptions.placeholder)
.export(symbolMutationOptions.exported)
.$if(
plugin.config.comments && createOperationComment({ operation }),
(c, v) => c.describe(v as ReadonlyArray<string>),
)
.assign(
$.func()
.param('options', (p) => p.optional().type(`Partial<${typeData}>`))
.returns(mutationType)
.do(
$.const(mutationOptionsFn)
.type(mutationType)
.assign(
$.object()
.pretty()
.prop(
'mutationFn',
$.func()
.async()
.param(fnOptions)
.do(...statements),
)
.$if(meta, (c, v) => c.prop('meta', v)),
),
$(mutationOptionsFn).return(),
),
);
plugin.setSymbolValue(symbolMutationOptions, statement);
};
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const createQueryOptions = ({
.export(symbolQueryOptionsFn.exported)
.$if(
plugin.config.comments && createOperationComment({ operation }),
(c, v) => c.describe(v as Array<string>),
(c, v) => c.describe(v as ReadonlyArray<string>),
)
.assign(
$.func()
Expand Down
48 changes: 18 additions & 30 deletions packages/openapi-ts/src/plugins/@tanstack/query-core/v5/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
hasOperationSse,
isOperationOptionsRequired,
} from '~/plugins/shared/utils/operation';
import { tsc } from '~/tsc';
import { $ } from '~/ts-dsl';

import { useTypeData } from '../shared/useType';
import type { PluginInstance } from '../types';
Expand Down Expand Up @@ -53,34 +53,22 @@ export const createUseQuery = ({
role: 'queryOptions',
tool: plugin.name,
});
const statement = tsc.constVariable({
comment: plugin.config.comments
? createOperationComment({ operation })
: undefined,
exportConst: symbolUseQueryFn.exported,
expression: tsc.arrowFunction({
parameters: [
{
isRequired: isRequiredOptions,
name: optionsParamName,
type: typeData,
},
],
statements: [
tsc.returnStatement({
expression: tsc.callExpression({
functionName: symbolUseQuery.placeholder,
parameters: [
tsc.callExpression({
functionName: symbolQueryOptionsFn.placeholder,
parameters: [optionsParamName],
}),
],
}),
}),
],
}),
name: symbolUseQueryFn.placeholder,
});
const statement = $.const(symbolUseQueryFn.placeholder)
.export(symbolUseQueryFn.exported)
.$if(
plugin.config.comments && createOperationComment({ operation }),
(c, v) => c.describe(v as ReadonlyArray<string>),
)
.assign(
$.func()
.param(optionsParamName, (p) =>
p.optional(!isRequiredOptions).type(typeData),
)
.do(
$(symbolUseQuery.placeholder)
.call($(symbolQueryOptionsFn.placeholder).call(optionsParamName))
.return(),
),
);
plugin.setSymbolValue(symbolUseQueryFn, statement);
};
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/plugins/swr/v2/useSwr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createUseSwr = ({
.export(symbolUseQueryFn.exported)
.$if(
plugin.config.comments && createOperationComment({ operation }),
(c, v) => c.describe(v as Array<string>),
(c, v) => c.describe(v as ReadonlyArray<string>),
)
.assign(
$.func().do(
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/plugins/valibot/shared/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const exportAst = ({
const statement = $.const(symbol.placeholder)
.export(symbol.exported)
.$if(plugin.config.comments && createSchemaComment({ schema }), (c, v) =>
c.describe(v as Array<string>),
c.describe(v as ReadonlyArray<string>),
)
.$if(state.hasLazyExpression.value, (c) =>
c.type(
Expand Down
Loading