Skip to content

Commit 2080cf7

Browse files
committed
fix: ensure useSuspenseQuery data type does not include undefined
1 parent b884ade commit 2080cf7

File tree

5 files changed

+76
-33
lines changed

5 files changed

+76
-33
lines changed

examples/react-app/src/components/SuspenseChild.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { useFindPetsSuspense } from "../../openapi/queries/suspense";
22

33
export const SuspenseChild = () => {
4-
const { data, error } = useFindPetsSuspense({
4+
// useSuspenseQuery enforces throwOnError: true, so errors are thrown and caught by ErrorBoundary
5+
const { data } = useFindPetsSuspense({
56
query: { tags: [], limit: 10 },
67
});
7-
console.log({ error });
8-
if (!Array.isArray(data)) {
9-
return <div>Error!</div>;
10-
}
118

129
return (
1310
<ul>
14-
{data?.map((pet) => (
11+
{data.map((pet) => (
1512
<li key={pet.id}>{pet.name}</li>
1613
))}
1714
</ul>

src/createImports.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ export const createImports = ({
102102
undefined,
103103
ts.factory.createIdentifier("UseMutationResult"),
104104
),
105+
ts.factory.createImportSpecifier(
106+
false,
107+
undefined,
108+
ts.factory.createIdentifier("UseSuspenseQueryOptions"),
109+
),
105110
]),
106111
),
107112
ts.factory.createStringLiteral("@tanstack/react-query"),

src/createUseQuery.mts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ const createApiResponseType = ({
6060
ts.factory.createTypeReferenceNode(BuildCommonTypeName(apiResponse.name)),
6161
);
6262

63+
// Response data type for suspense - use Response type directly to exclude undefined
64+
const suspenseResponseDataType = ts.factory.createTypeParameterDeclaration(
65+
undefined,
66+
TData.text,
67+
undefined,
68+
ts.factory.createTypeReferenceNode(
69+
ts.factory.createIdentifier(
70+
`${capitalizeFirstLetter(methodName)}Response`,
71+
),
72+
),
73+
);
74+
6375
const responseErrorType = ts.factory.createTypeParameterDeclaration(
6476
undefined,
6577
TError.text,
@@ -93,6 +105,12 @@ const createApiResponseType = ({
93105
* MyClassMethodDefaultResponse
94106
*/
95107
responseDataType,
108+
/**
109+
* ResponseDataType for suspense - use Response type directly to exclude undefined
110+
*
111+
* MyClassMethodResponse
112+
*/
113+
suspenseResponseDataType,
96114
/**
97115
* ErrorDataType
98116
*
@@ -202,6 +220,7 @@ function createQueryHook({
202220
}
203221

204222
const isInfiniteQuery = queryString === "useInfiniteQuery";
223+
const isSuspenseQuery = queryString === "useSuspenseQuery";
205224

206225
const responseDataTypeRef = responseDataType.default as ts.TypeReferenceNode;
207226
const responseDataTypeIdentifier =
@@ -266,7 +285,9 @@ function createQueryHook({
266285
ts.factory.createIdentifier(
267286
isInfiniteQuery
268287
? "UseInfiniteQueryOptions"
269-
: "UseQueryOptions",
288+
: isSuspenseQuery
289+
? "UseSuspenseQueryOptions"
290+
: "UseQueryOptions",
270291
),
271292
[
272293
ts.factory.createTypeReferenceNode(TData),
@@ -469,6 +490,7 @@ export const createUseQuery = ({
469490
const {
470491
apiResponse: defaultApiResponse,
471492
responseDataType,
493+
suspenseResponseDataType,
472494
responseErrorType,
473495
} = createApiResponseType({
474496
methodName,
@@ -496,7 +518,7 @@ export const createUseQuery = ({
496518
const suspenseQueryHook = createQueryHook({
497519
queryString: "useSuspenseQuery",
498520
suffix: "Suspense",
499-
responseDataType,
521+
responseDataType: suspenseResponseDataType,
500522
responseErrorType,
501523
requestParams,
502524
method,

0 commit comments

Comments
 (0)