Skip to content

[RED-15] Sharing a set of endpoints between the two Apis? #3598

@eric-crowell

Description

@eric-crowell
Contributor

Hello,
I would like to define a set of endpoints I can use between two Api types. That way, if I need to alter or change those endpoints, I only need to modify it once for both Apis

For instance, I am looking to make some modules like so:

- src/
- - todo/
- - - todoEndpoints.ts # Set of endpoints for both `todoApi.ts` and `todoApiReact.ts`
- - - todoApi.ts # @reduxjs/toolkit/query
- - - todoApiReact.ts # @reduxjs/toolkit/query/react

I'd import and use the endpoints in a way like so:

// todoApiReact.ts
import { createApi } from '@reduxjs/toolkit/query/react';
import { todoEndpoints } from './todoEndpoints.ts';

export const todoApiReact = createApi({
   // ...
   endpoints: todoEndpoints,
});

I'm imagining the endpoints module could look something like this:

import type {
  EndpointBuilder,
  BaseQueryFn,
} from '@reduxjs/toolkit/query';

export const todoEndpoints = <T extends EndpointBuilder<BaseQueryFn, string, string>>(
  builder: T,
) => ({
   createTodo: builder.mutation</** ... */>(/** ... */),
   readTodo: builder.query</** ... */>(/** ... */),
});

For my case, it would be especially useful if I could apply React hooks to my non-react api. Maybe like const myReactApi = reactifyApi(myApi);. Maybe that is already possible?

I understand I can create individual definitions with EndpointDefinition(s), but it doesn't save time if I want to add new endpoints. Might be something I'm missing. How would one best share the same endpoints?

RED-15

Activity

added this to the 2.0 milestone on Aug 16, 2023
changed the title [-]Sharing a set of endpoints between the two `Api`s?[/-] [+][RED-15] Sharing a set of endpoints between the two `Api`s?[/+] on Sep 22, 2023
modified the milestones: 2.0, Post 2.0 on Oct 1, 2023
markerikson

markerikson commented on Feb 6, 2024

@markerikson
Collaborator

This feels like two separate requests:

  • Extract endpoint definitions outside of createApi
  • Add React hooks to an already created API instance

(Remember that endpoint definitions don't know anything about hooks - it's the createApi module definition looking at the endpoints and doing something additional with those definitions.

We've had some requests for the ability to add React hooks from a UI-agnostic API instance, and there's an early POC over at #4128 .
For extracting endpoints, your current snippet looks pretty plausible - is there something about that that doesn't work right now?

added
enhancementNew feature or request
questionFurther information is requested
and removed
linearCreated by Linear-GitHub Sync
on Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @markerikson@eric-crowell

        Issue actions

          [RED-15] Sharing a set of endpoints between the two `Api`s? · Issue #3598 · reduxjs/redux-toolkit