Skip to content

Commit 93e5baa

Browse files
committed
extract common props to separate def and add missing phantom property
1 parent c89fce3 commit 93e5baa

File tree

1 file changed

+153
-24
lines changed

1 file changed

+153
-24
lines changed

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 153 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import type {
4040
import { isNotNullish } from './utils'
4141
import type { NamedSchemaError } from './standardSchema'
4242

43+
const rawResultType = /* @__PURE__ */ Symbol()
4344
const resultType = /* @__PURE__ */ Symbol()
4445
const baseQuery = /* @__PURE__ */ Symbol()
4546

@@ -118,10 +119,52 @@ type EndpointDefinitionWithQuery<
118119
arg: QueryArg,
119120
): unknown
120121

121-
/** A schema for the result *before* it's passed to `transformResponse` */
122+
/**
123+
* A schema for the result *before* it's passed to `transformResponse`
124+
*
125+
* @example
126+
* ```ts
127+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
128+
* import * as v from "valibot"
129+
*
130+
* const postSchema = v.object({ id: v.number(), name: v.string() })
131+
* type Post = v.InferOutput<typeof postSchema>
132+
*
133+
* const api = createApi({
134+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
135+
* endpoints: (build) => ({
136+
* getPostName: build.query<Post, { id: number }>({
137+
* query: ({ id }) => `/post/${id}`,
138+
* rawResponseSchema: postSchema,
139+
* transformResponse: (post) => post.name,
140+
* }),
141+
* })
142+
* })
143+
* ```
144+
*/
122145
rawResponseSchema?: StandardSchemaV1<RawResultType>
123146

124-
/** A schema for the error object returned by the `query` or `queryFn`, *before* it's passed to `transformErrorResponse` */
147+
/**
148+
* A schema for the error object returned by the `query` or `queryFn`, *before* it's passed to `transformErrorResponse`
149+
*
150+
* @example
151+
* ```ts
152+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
153+
* import * as v from "valibot"
154+
* import {customBaseQuery, baseQueryErrorSchema} from "./customBaseQuery"
155+
*
156+
* const api = createApi({
157+
* baseQuery: customBaseQuery,
158+
* endpoints: (build) => ({
159+
* getPost: build.query<Post, { id: number }>({
160+
* query: ({ id }) => `/post/${id}`,
161+
* rawErrorResponseSchema: baseQueryErrorSchema,
162+
* transformErrorResponse: (error) => error.data,
163+
* }),
164+
* })
165+
* })
166+
* ```
167+
*/
125168
rawErrorResponseSchema?: StandardSchemaV1<BaseQueryError<BaseQuery>>
126169
}
127170

@@ -193,32 +236,98 @@ type BaseEndpointTypes<QueryArg, BaseQuery extends BaseQueryFn, ResultType> = {
193236
ResultType: ResultType
194237
}
195238

196-
export type BaseEndpointDefinition<
239+
interface CommonEndpointDefinition<
197240
QueryArg,
198241
BaseQuery extends BaseQueryFn,
199242
ResultType,
200-
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
201-
> = (
202-
| ([CastAny<BaseQueryResult<BaseQuery>, {}>] extends [NEVER]
203-
? never
204-
: EndpointDefinitionWithQuery<
205-
QueryArg,
206-
BaseQuery,
207-
ResultType,
208-
RawResultType
209-
>)
210-
| EndpointDefinitionWithQueryFn<QueryArg, BaseQuery, ResultType>
211-
) & {
212-
/** A schema for the arguments to be passed to the `query` or `queryFn` */
243+
> {
244+
/**
245+
* A schema for the arguments to be passed to the `query` or `queryFn`.
246+
*
247+
* @example
248+
* ```ts
249+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
250+
* import * as v from "valibot"
251+
*
252+
* const api = createApi({
253+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
254+
* endpoints: (build) => ({
255+
* getPost: build.query<Post, { id: number }>({
256+
* query: ({ id }) => `/post/${id}`,
257+
* argSchema: v.object({ id: v.number() }),
258+
* }),
259+
* })
260+
* })
261+
* ```
262+
*/
213263
argSchema?: StandardSchemaV1<QueryArg>
214264

215-
/** A schema for the result (including `transformResponse` if provided) */
265+
/**
266+
* A schema for the result (including `transformResponse` if provided)
267+
*
268+
* @example
269+
* ```ts
270+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
271+
* import * as v from "valibot"
272+
*
273+
* const postSchema = v.object({ id: v.number(), name: v.string() })
274+
* type Post = v.InferOutput<typeof postSchema>
275+
*
276+
* const api = createApi({
277+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
278+
* endpoints: (build) => ({
279+
* getPost: build.query<Post, { id: number }>({
280+
* query: ({ id }) => `/post/${id}`,
281+
* responseSchema: postSchema,
282+
* }),
283+
* })
284+
* })
285+
* ```
286+
*/
216287
responseSchema?: StandardSchemaV1<ResultType>
217288

218-
/** A schema for the error object returned by the `query` or `queryFn` (including `transformErrorResponse` if provided) */
289+
/**
290+
* A schema for the error object returned by the `query` or `queryFn` (including `transformErrorResponse` if provided)
291+
*
292+
* @example
293+
* ```ts
294+
* import { createApi } from '@reduxjs/toolkit/query/react'
295+
* import * as v from "valibot"
296+
* import { customBaseQuery, baseQueryErrorSchema } from "./customBaseQuery"
297+
*
298+
* const api = createApi({
299+
* baseQuery: customBaseQuery,
300+
* endpoints: (build) => ({
301+
* getPost: build.query<Post, { id: number }>({
302+
* query: ({ id }) => `/post/${id}`,
303+
* errorResponseSchema: baseQueryErrorSchema,
304+
* }),
305+
* })
306+
* })
307+
* ```
308+
*/
219309
errorResponseSchema?: StandardSchemaV1<BaseQueryError<BaseQuery>>
220310

221-
/** A schema for the `meta` property returned by the `query` or `queryFn` */
311+
/**
312+
* A schema for the `meta` property returned by the `query` or `queryFn`
313+
*
314+
* @example
315+
* ```ts
316+
* import { createApi } from '@reduxjs/toolkit/query/react'
317+
* import * as v from "valibot"
318+
* import { customBaseQuery, baseQueryMetaSchema } from "./customBaseQuery"
319+
*
320+
* const api = createApi({
321+
* baseQuery: customBaseQuery,
322+
* endpoints: (build) => ({
323+
* getPost: build.query<Post, { id: number }>({
324+
* query: ({ id }) => `/post/${id}`,
325+
* metaSchema: baseQueryMetaSchema,
326+
* }),
327+
* })
328+
* })
329+
* ```
330+
*/
222331
metaSchema?: StandardSchemaV1<BaseQueryMeta<BaseQuery>>
223332

224333
/**
@@ -237,12 +346,32 @@ export type BaseEndpointDefinition<
237346

238347
onSchemaFailure?: SchemaFailureHandler
239348
skipSchemaValidation?: boolean
349+
}
240350

241-
/* phantom type */
242-
[resultType]?: ResultType
243-
/* phantom type */
244-
[baseQuery]?: BaseQuery
245-
} & HasRequiredProps<
351+
export type BaseEndpointDefinition<
352+
QueryArg,
353+
BaseQuery extends BaseQueryFn,
354+
ResultType,
355+
RawResultType extends BaseQueryResult<BaseQuery> = BaseQueryResult<BaseQuery>,
356+
> = (
357+
| ([CastAny<BaseQueryResult<BaseQuery>, {}>] extends [NEVER]
358+
? never
359+
: EndpointDefinitionWithQuery<
360+
QueryArg,
361+
BaseQuery,
362+
ResultType,
363+
RawResultType
364+
>)
365+
| EndpointDefinitionWithQueryFn<QueryArg, BaseQuery, ResultType>
366+
) &
367+
CommonEndpointDefinition<QueryArg, BaseQuery, ResultType> & {
368+
/* phantom type */
369+
[rawResultType]?: RawResultType
370+
/* phantom type */
371+
[resultType]?: ResultType
372+
/* phantom type */
373+
[baseQuery]?: BaseQuery
374+
} & HasRequiredProps<
246375
BaseQueryExtraOptions<BaseQuery>,
247376
{ extraOptions: BaseQueryExtraOptions<BaseQuery> },
248377
{ extraOptions?: BaseQueryExtraOptions<BaseQuery> }

0 commit comments

Comments
 (0)