Skip to content

Commit 04f8bd3

Browse files
authored
fix: ensure useSuspenseQuery data type does not include undefined (#189)
1 parent b884ade commit 04f8bd3

File tree

5 files changed

+75
-33
lines changed

5 files changed

+75
-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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ const createApiResponseType = ({
6060
ts.factory.createTypeReferenceNode(BuildCommonTypeName(apiResponse.name)),
6161
);
6262

63+
// Response data type for suspense - wrap with NonNullable to exclude undefined
64+
const suspenseResponseDataType = ts.factory.createTypeParameterDeclaration(
65+
undefined,
66+
TData.text,
67+
undefined,
68+
ts.factory.createTypeReferenceNode(
69+
ts.factory.createIdentifier("NonNullable"),
70+
[
71+
ts.factory.createTypeReferenceNode(
72+
BuildCommonTypeName(apiResponse.name),
73+
),
74+
],
75+
),
76+
);
77+
6378
const responseErrorType = ts.factory.createTypeParameterDeclaration(
6479
undefined,
6580
TError.text,
@@ -93,6 +108,12 @@ const createApiResponseType = ({
93108
* MyClassMethodDefaultResponse
94109
*/
95110
responseDataType,
111+
/**
112+
* ResponseDataType for suspense - wrap with NonNullable to exclude undefined
113+
*
114+
* NonNullable<MyClassMethodDefaultResponse>
115+
*/
116+
suspenseResponseDataType,
96117
/**
97118
* ErrorDataType
98119
*
@@ -202,6 +223,7 @@ function createQueryHook({
202223
}
203224

204225
const isInfiniteQuery = queryString === "useInfiniteQuery";
226+
const isSuspenseQuery = queryString === "useSuspenseQuery";
205227

206228
const responseDataTypeRef = responseDataType.default as ts.TypeReferenceNode;
207229
const responseDataTypeIdentifier =
@@ -266,7 +288,9 @@ function createQueryHook({
266288
ts.factory.createIdentifier(
267289
isInfiniteQuery
268290
? "UseInfiniteQueryOptions"
269-
: "UseQueryOptions",
291+
: isSuspenseQuery
292+
? "UseSuspenseQueryOptions"
293+
: "UseQueryOptions",
270294
),
271295
[
272296
ts.factory.createTypeReferenceNode(TData),
@@ -469,6 +493,7 @@ export const createUseQuery = ({
469493
const {
470494
apiResponse: defaultApiResponse,
471495
responseDataType,
496+
suspenseResponseDataType,
472497
responseErrorType,
473498
} = createApiResponseType({
474499
methodName,
@@ -496,7 +521,7 @@ export const createUseQuery = ({
496521
const suspenseQueryHook = createQueryHook({
497522
queryString: "useSuspenseQuery",
498523
suffix: "Suspense",
499-
responseDataType,
524+
responseDataType: suspenseResponseDataType,
500525
responseErrorType,
501526
requestParams,
502527
method,

0 commit comments

Comments
 (0)