Skip to content

Commit

Permalink
fetchBaseQuery: return nullon empty body for JSON. Add DevWarnings.
Browse files Browse the repository at this point in the history
fetchBaseQuery: return `null` instead of `undefined` on empty body for JSON. 
Also add dev errors if `baseQuery`/`queryFn` returns a wrong shape (#1699)
  • Loading branch information
phryneas authored Nov 12, 2021
1 parent 1b4615f commit b208e63
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
33 changes: 33 additions & 0 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,39 @@ export function buildThunks<
baseQuery(arg, baseQueryApi, endpointDefinition.extraOptions as any)
)
}
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development'
) {
const what = endpointDefinition.query ? '`baseQuery`' : '`queryFn`'
let err: undefined | string
if (!result) {
err = `${what} did not return anything.`
} else if (typeof result !== 'object') {
err = `${what} did not return an object.`
} else if (result.error && result.data) {
err = `${what} returned an object containing both \`error\` and \`result\`.`
} else if (result.error === undefined && result.data === undefined) {
err = `${what} returned an object containing neither a valid \`error\` and \`result\`. At least one of them should not be \`undefined\``
} else {
for (const key of Object.keys(result)) {
if (key !== 'error' && key !== 'data' && key !== 'meta') {
err = `The object returned by ${what} has the unknown property ${key}.`
break
}
}
}
if (err) {
console.error(
`Error encountered handling the endpoint ${arg.endpointName}.
${err}
It needs to return an object with either the shape \`{ data: <value> }\` or \`{ error: <value> }\` that may contain an optional \`meta\` property.
Object returned was:`,
result
)
}
}

if (result.error) throw new HandledError(result.error, result.meta)

return fulfillWithValue(
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const handleResponse = async (

if (responseHandler === 'json') {
const text = await response.text()
return text.length ? JSON.parse(text) : undefined
return text.length ? JSON.parse(text) : null
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/apiProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { waitMs } from './helpers'
const api = createApi({
baseQuery: async (arg: any) => {
await waitMs()
return { data: arg?.body ? arg.body : undefined }
return { data: arg?.body ? arg.body : null }
},
endpoints: (build) => ({
getUser: build.query<any, number>({
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/buildThunks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test('passes the extraArgument property to the baseQueryApi', async () => {

describe('re-triggering behavior on arg change', () => {
const api = createApi({
baseQuery: () => ({ data: undefined }),
baseQuery: () => ({ data: null }),
endpoints: (build) => ({
getUser: build.query<any, any>({
query: (obj) => obj,
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/cleanup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { render, waitFor } from '@testing-library/react'
import { setupApiStore } from './helpers'

const api = createApi({
baseQuery: () => ({ data: undefined }),
baseQuery: () => ({ data: null }),
endpoints: (build) => ({
a: build.query<unknown, void>({ query: () => '' }),
b: build.query<unknown, void>({ query: () => '' }),
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('wrong tagTypes log errors', () => {
})

beforeEach(() => {
baseQuery.mockResolvedValue({})
baseQuery.mockResolvedValue({ data: 'foo' })
})

test.each<[keyof typeof api.endpoints, boolean?]>([
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('fetchBaseQuery', () => {
expect(res).toBeInstanceOf(Object)
expect(res.meta?.request).toBeInstanceOf(Request)
expect(res.meta?.response).toBeInstanceOf(Object)
expect(res.data).toBeUndefined()
expect(res.data).toBeNull()
})

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

0 comments on commit b208e63

Please sign in to comment.