Skip to content

Commit dd33450

Browse files
committed
fix: generate null type and return null data for the empty responses instead of an empty object {}
1 parent ce0b2b7 commit dd33450

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

packages/react-client/src/lib/responseUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function processResponse<TData, TError>(
99
): Promise<RequestFnResponse<TData, TError>> {
1010
if (response.status === 204 || response.headers.get('Content-Length') === '0')
1111
return (
12-
response.ok ? { data: {}, response } : { error: {}, response }
12+
response.ok ? { data: null, response } : { error: null, response }
1313
) as RequestFnResponse<TData, TError>;
1414

1515
const contentType = response.headers.get('Content-Type')?.toLowerCase();

packages/react-client/src/tests/msw/handlers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ export const handlers = [
143143
const query = getQueryParameters<Services['files']['deleteFiles']>(
144144
request.url
145145
);
146+
147+
if (query?.pendingOnly) {
148+
return new HttpResponse(null, {
149+
status: 204,
150+
statusText: 'No pending files deleted',
151+
});
152+
}
153+
146154
return HttpResponse.json({ query });
147155
}
148156
),

packages/react-client/src/tests/qraftAPIClient.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,34 @@ describe('Qraft uses Mutations', () => {
12021202
});
12031203
});
12041204

1205+
it('handles useMutations for 204 responses and returns `null` data', async () => {
1206+
const { qraft, queryClient } = createClient();
1207+
1208+
const { result } = renderHook(() => qraft.files.deleteFiles.useMutation(), {
1209+
wrapper: (props) => <Providers {...props} queryClient={queryClient} />,
1210+
});
1211+
1212+
act(() => {
1213+
result.current.mutate({ query: { pendingOnly: true } });
1214+
});
1215+
1216+
await waitFor(() => {
1217+
expect(result.current.data).toEqual(null);
1218+
});
1219+
1220+
result.current.data satisfies
1221+
| {
1222+
query?: {
1223+
all?: boolean;
1224+
};
1225+
}
1226+
| null
1227+
| undefined;
1228+
1229+
// @ts-expect-error - never satisfies never
1230+
result.current.data satisfies never;
1231+
});
1232+
12051233
it('supports useMutation with predefined parameters', async () => {
12061234
const { qraft, queryClient } = createClient();
12071235

packages/tanstack-query-react-plugin/src/__snapshots__/explicit-import-extensions/services/FilesService.ts.snapshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,15 +1332,15 @@ type DeleteFilesSchema = {
13321332
url: "/files";
13331333
};
13341334
type DeleteFilesParameters = paths["/files"]["delete"]["parameters"];
1335-
type DeleteFilesData = paths["/files"]["delete"]["responses"]["200"]["content"]["application/json"] | undefined;
1335+
type DeleteFilesData = paths["/files"]["delete"]["responses"]["200"]["content"]["application/json"] | null;
13361336
type DeleteFilesError = paths["/files"]["delete"]["responses"]["default"]["content"]["application/json"];
13371337
type DeleteFilesBody = undefined;
13381338
type TrashFilesSchema = {
13391339
method: "delete";
13401340
url: "/files/trash";
13411341
};
13421342
type TrashFilesParameters = paths["/files/trash"]["delete"]["parameters"];
1343-
type TrashFilesData = paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/json"] | paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/octet-stream"] | undefined;
1343+
type TrashFilesData = paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/json"] | paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/octet-stream"] | null;
13441344
type TrashFilesError = paths["/files/trash"]["delete"]["responses"]["default"]["content"]["application/json"];
13451345
type TrashFilesBody = undefined;
13461346
type GetFileListSchema = {

packages/tanstack-query-react-plugin/src/__snapshots__/queryable-write-operations/services/FilesService.ts.snapshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,15 +2316,15 @@ type DeleteFilesSchema = {
23162316
url: "/files";
23172317
};
23182318
type DeleteFilesParameters = paths["/files"]["delete"]["parameters"];
2319-
type DeleteFilesData = paths["/files"]["delete"]["responses"]["200"]["content"]["application/json"] | undefined;
2319+
type DeleteFilesData = paths["/files"]["delete"]["responses"]["200"]["content"]["application/json"] | null;
23202320
type DeleteFilesError = paths["/files"]["delete"]["responses"]["default"]["content"]["application/json"];
23212321
type DeleteFilesBody = undefined;
23222322
type TrashFilesSchema = {
23232323
method: "delete";
23242324
url: "/files/trash";
23252325
};
23262326
type TrashFilesParameters = paths["/files/trash"]["delete"]["parameters"];
2327-
type TrashFilesData = paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/json"] | paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/octet-stream"] | undefined;
2327+
type TrashFilesData = paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/json"] | paths["/files/trash"]["delete"]["responses"]["200"]["content"]["application/octet-stream"] | null;
23282328
type TrashFilesError = paths["/files/trash"]["delete"]["responses"]["default"]["content"]["application/json"];
23292329
type TrashFilesBody = undefined;
23302330
type GetFileListSchema = {

packages/tanstack-query-react-plugin/src/ts-factory/getServiceFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ const getOperationResponseFactory = (
338338
responses
339339
.map(([statusCode, mediaType]) => {
340340
if (mediaType === null)
341-
return factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
341+
return factory.createLiteralTypeNode(factory.createNull());
342342

343343
if (!mediaType)
344344
return factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);

0 commit comments

Comments
 (0)