import type { Api , ApiContext , Module , ModuleName } from './apiTypes' ; import type { CombinedState } from './core/apiState' ; import type { BaseQueryArg , BaseQueryFn } from './baseQueryTypes' ; import type { SerializeQueryArgs } from './defaultSerializeQueryArgs' ; import { defaultSerializeQueryArgs } from './defaultSerializeQueryArgs' ; import type { EndpointBuilder , EndpointDefinitions , } from './endpointDefinitions' ; import { DefinitionType } from './endpointDefinitions' ; import { nanoid } from '@reduxjs/toolkit' ; import type { AnyAction } from '@reduxjs/toolkit' ; import type { NoInfer } from './tsHelpers' ; import { defaultMemoize } from 'reselect' ; export interface CreateApiOptions < BaseQuery extends BaseQueryFn , Definitions extends EndpointDefinitions , ReducerPath extends string = 'api' , TagTypes extends string = never > { example baseQuery : BaseQuery ; example tagTypes ? : readonly TagTypes [ ] ; example reducerPath ? : ReducerPath ; /**
     * Accepts a custom function if you have a need to change the creation of cache keys for any reason.
     */ serializeQueryArgs ? : SerializeQueryArgs < BaseQueryArg < BaseQuery > > ; /**
     * Endpoints are just a set of operations that you want to perform against your server. You define them as an object using the builder syntax. There are two basic endpoint types: [`query`](../../rtk-query/usage/queries) and [`mutation`](../../rtk-query/usage/mutations).
     */ endpoints ( build ) : Definitions ; /**
     * Defaults to `60` _(this value is in seconds)_. This is how long RTK Query will keep your data cached for **after** the last component unsubscribes. For example, if you query an endpoint, then unmount the component, then mount another component that makes the same request within the given time frame, the most recent value will be served from the cache.
     *
     * ```ts
     * // codeblock-meta title="keepUnusedDataFor example"
     *
     * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
     * interface Post {
     *   id: number
     *   name: string
     * }
     * type PostsResponse = Post[]
     *
     * const api = createApi({
     *   baseQuery: fetchBaseQuery({ baseUrl: '/' }),
     *   endpoints: (build) => ({
     *     getPosts: build.query<PostsResponse, void>({
     *       query: () => 'posts',
     *       // highlight-start
     *       keepUnusedDataFor: 5
     *       // highlight-end
     *     })
     *   })
     * })
     * ```
     */ keepUnusedDataFor ? : number ; /**
     * Defaults to `false`. This setting allows you to control whether if a cached result is already available RTK Query will only serve a cached result, or if it should `refetch` when set to `true` or if an adequate amount of time has passed since the last successful query result.
     * - `false` - Will not cause a query to be performed _unless_ it does not exist yet.
     * - `true` - Will always refetch when a new subscriber to a query is added. Behaves the same as calling the `refetch` callback or passing `forceRefetch: true` in the action creator.
     * - `number` - **Value is in seconds**. If a number is provided and there is an existing query in the cache, it will compare the current time vs the last fulfilled timestamp, and only refetch if enough time has elapsed.
     *
     * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
     */ refetchOnMountOrArgChange ? : boolean | number ; /**
     * Defaults to `false`. This setting allows you to control whether RTK Query will try to refetch all subscribed queries after the application window regains focus.
     *
     * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
     *
     * Note: requires [`setupListeners`](./setupListeners) to have been called.
     */ refetchOnFocus ? : boolean ; /**
     * Defaults to `false`. This setting allows you to control whether RTK Query will try to refetch all subscribed queries after regaining a network connection.
     *
     * If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
     *
     * Note: requires [`setupListeners`](./setupListeners) to have been called.
     */ refetchOnReconnect ? : boolean ; example extractRehydrationInfo ? : ( action : AnyAction , { reducerPath , } : { reducerPath : ReducerPath ; } ) => undefined | CombinedState < NoInfer < Definitions > , NoInfer < TagTypes > , NoInfer < ReducerPath > > ; } export type CreateApi < Modules extends ModuleName > = { link < BaseQuery extends BaseQueryFn , Definitions extends EndpointDefinitions , ReducerPath extends string = 'api' , TagTypes extends string = never > ( options ) : Api < BaseQuery , Definitions , ReducerPath , TagTypes , Modules > ; } ; link example param modules returns export function buildCreateApi < Modules extends [ Module < any > , ... Module < any > [ ] ] > ( ... modules ) { return function baseCreateApi ( options ) { const extractRehydrationInfo = defaultMemoize ( ( action ) => options . extractRehydrationInfo ?. ( action , { reducerPath : ( options . reducerPath ?? 'api' ) as any , } ) ) ; const optionsWithDefaults = { reducerPath : 'api' , serializeQueryArgs : defaultSerializeQueryArgs , keepUnusedDataFor : 60 , refetchOnMountOrArgChange : false , refetchOnFocus : false , refetchOnReconnect : false , ... options , extractRehydrationInfo , tagTypes : [ ... ( options . tagTypes || [ ] ) ] , } ; const context = { endpointDefinitions : { } , batch ( fn ) { fn ( ) ; } , apiUid : nanoid ( ) , extractRehydrationInfo , hasRehydrationInfo : defaultMemoize ( ( action ) => extractRehydrationInfo ( action ) != null ) , } ; const api = { injectEndpoints , enhanceEndpoints ( { addTagTypes , endpoints } ) { if ( addTagTypes ) { for ( const eT of addTagTypes ) { if ( ! optionsWithDefaults . tagTypes . includes ( eT as any ) ) { optionsWithDefaults . tagTypes . push ( eT as any ) ; } } } if ( endpoints ) { for ( const [ endpointName , partialDefinition ] of Object . entries ( endpoints ) ) { if ( typeof partialDefinition === 'function' ) { partialDefinition ( context . endpointDefinitions [ endpointName ] ) ; } Object . assign ( context . endpointDefinitions [ endpointName ] || { } , partialDefinition ) ; } } return api ; } , } as Api < BaseQueryFn , { } , string , string , Modules [ number ] [ 'name' ] > ; const initializedModules = modules . map ( ( m ) => m . init ( api as any , optionsWithDefaults , context ) ) ; function injectEndpoints ( inject ) { const evaluatedEndpoints = inject . endpoints ( { query : ( x ) => ( { ... x , type : DefinitionType . query } as any ) , mutation : ( x ) => ( { ... x , type : DefinitionType . mutation } as any ) , } ) ; for ( const [ endpointName , definition ] of Object . entries ( evaluatedEndpoints ) ) { if ( ! inject . overrideExisting && endpointName in context . endpointDefinitions ) { if ( typeof process !== 'undefined' && process . env . NODE_ENV === 'development' ) { console . error ( `called \`injectEndpoints\` to override already-existing endpointName ${ endpointName } without specifying \`overrideExisting: true\`` ) ; } continue ; } context . endpointDefinitions [ endpointName ] = definition ; for ( const m of initializedModules ) { m . injectEndpoint ( endpointName , definition ) ; } } return api as any ; } return api . injectEndpoints ( { endpoints : options . endpoints as any } ) ; } ; }