Skip to content

Commit b208e63

Browse files
authored
fetchBaseQuery: return nullon empty body for JSON. Add DevWarnings.
fetchBaseQuery: return `null` instead of `undefined` on empty body for JSON. Also add dev errors if `baseQuery`/`queryFn` returns a wrong shape (#1699)
1 parent 1b4615f commit b208e63

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,39 @@ export function buildThunks<
302302
baseQuery(arg, baseQueryApi, endpointDefinition.extraOptions as any)
303303
)
304304
}
305+
if (
306+
typeof process !== 'undefined' &&
307+
process.env.NODE_ENV === 'development'
308+
) {
309+
const what = endpointDefinition.query ? '`baseQuery`' : '`queryFn`'
310+
let err: undefined | string
311+
if (!result) {
312+
err = `${what} did not return anything.`
313+
} else if (typeof result !== 'object') {
314+
err = `${what} did not return an object.`
315+
} else if (result.error && result.data) {
316+
err = `${what} returned an object containing both \`error\` and \`result\`.`
317+
} else if (result.error === undefined && result.data === undefined) {
318+
err = `${what} returned an object containing neither a valid \`error\` and \`result\`. At least one of them should not be \`undefined\``
319+
} else {
320+
for (const key of Object.keys(result)) {
321+
if (key !== 'error' && key !== 'data' && key !== 'meta') {
322+
err = `The object returned by ${what} has the unknown property ${key}.`
323+
break
324+
}
325+
}
326+
}
327+
if (err) {
328+
console.error(
329+
`Error encountered handling the endpoint ${arg.endpointName}.
330+
${err}
331+
It needs to return an object with either the shape \`{ data: <value> }\` or \`{ error: <value> }\` that may contain an optional \`meta\` property.
332+
Object returned was:`,
333+
result
334+
)
335+
}
336+
}
337+
305338
if (result.error) throw new HandledError(result.error, result.meta)
306339

307340
return fulfillWithValue(

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const handleResponse = async (
5454

5555
if (responseHandler === 'json') {
5656
const text = await response.text()
57-
return text.length ? JSON.parse(text) : undefined
57+
return text.length ? JSON.parse(text) : null
5858
}
5959
}
6060

packages/toolkit/src/query/tests/apiProvider.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { waitMs } from './helpers'
66
const api = createApi({
77
baseQuery: async (arg: any) => {
88
await waitMs()
9-
return { data: arg?.body ? arg.body : undefined }
9+
return { data: arg?.body ? arg.body : null }
1010
},
1111
endpoints: (build) => ({
1212
getUser: build.query<any, number>({

packages/toolkit/src/query/tests/buildThunks.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test('passes the extraArgument property to the baseQueryApi', async () => {
7474

7575
describe('re-triggering behavior on arg change', () => {
7676
const api = createApi({
77-
baseQuery: () => ({ data: undefined }),
77+
baseQuery: () => ({ data: null }),
7878
endpoints: (build) => ({
7979
getUser: build.query<any, any>({
8080
query: (obj) => obj,

packages/toolkit/src/query/tests/cleanup.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { render, waitFor } from '@testing-library/react'
77
import { setupApiStore } from './helpers'
88

99
const api = createApi({
10-
baseQuery: () => ({ data: undefined }),
10+
baseQuery: () => ({ data: null }),
1111
endpoints: (build) => ({
1212
a: build.query<unknown, void>({ query: () => '' }),
1313
b: build.query<unknown, void>({ query: () => '' }),

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('wrong tagTypes log errors', () => {
136136
})
137137

138138
beforeEach(() => {
139-
baseQuery.mockResolvedValue({})
139+
baseQuery.mockResolvedValue({ data: 'foo' })
140140
})
141141

142142
test.each<[keyof typeof api.endpoints, boolean?]>([

packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('fetchBaseQuery', () => {
101101
expect(res).toBeInstanceOf(Object)
102102
expect(res.meta?.request).toBeInstanceOf(Request)
103103
expect(res.meta?.response).toBeInstanceOf(Object)
104-
expect(res.data).toBeUndefined()
104+
expect(res.data).toBeNull()
105105
})
106106

107107
it('should return an error and status for error responses', async () => {

0 commit comments

Comments
 (0)